$(window).load(function() {
	// Make sure item-bg height is the same as the image it contains. 
	$('.item-bg').each(function() {       
		var realheight = $(this).children('img').height();
		$(this).css('height', realheight);
	});
});

$(document).ready(function() {

	/* 
	 * Cufon
	 */
	Cufon.replace('#nav .menu, .welcome, #footer, .gbook', { hover: true, fontFamily: 'gotham-book' });
	Cufon.replace('#sliding_cart, .gbold', { hover: true, fontFamily: 'gotham-bold' });

	/*
	 * Checkout Showing/Hiding
	 */
	$('#cart #cart-next-1').click(function() {
		$('#cart #shopping-cart-step-1').slideUp(700, function() { 
			$('#cart #shopping-cart-step-2').slideDown();
		});
		return false;
	});

	$('#cart #cart-back-1').click(function() {
		$('#cart #shopping-cart-step-2').slideUp(700, function() { 
			$('#cart #shopping-cart-step-1').slideDown();
		});
		return false;
	});
	
	// Show the billing/shipping info if there are validation problems
	if ($('p').hasClass('validation-error') || $('div').hasClass('login_error')) {
		$('#cart #shopping-cart-step-1').hide();
		$('#cart #shopping-cart-step-2').show();
	}

	/*
	 * All Jewelry drop down.
	 */
	$("#category-list h4.all, #category-list ul").hover(
	  function () { 
			$('#category-list ul').show(); 
			$('#category-list .cat-arrow').addClass('hover');
		}, function () { 
			$('#category-list ul').hide(); 
			$('#category-list .cat-arrow').removeClass('hover');

		}
	);



	/*
	 * Lookbook pager
	 */
	delay = 300;
	opacity = 0.25;

	item_cur = $('.item:last');
	items_count = $('.item').length;
	// compute width
	item_width = $('.item').outerWidth(true);
	desc_width = $('.item .desc').outerWidth(true);

	total_width = item_width * items_count;

	if(items_count == '1') {
		offset = '20px';
	} else {
		offset = '-' + (total_width - 595) + 'px';
	}


	$('.item').css('left', offset);

	// Show description of first item
	item_cur.children('.desc').show().animate({ opacity: 1 }, delay);

	// Make images opaque
	item_cur_img = $(item_cur).children('.item-bg').children('img');
	$('.item img').not(item_cur_img).animate({ opacity: opacity }, 0, function() { $(this).css('filter',''); } );

//	$('#left').css('visibility', 'hidden');

	// If we're on a single product page, always show the same item info.
	single_product = $('.desc-common').length;
	if(single_product) {
		desc_common = $('.desc-common').html();
		$('.item').children('.desc').html(desc_common);
	}
	

	// Right click functions
	$('#right').click(function() {
		if(item_cur.index() <= 0) return false;

		item_orig = item_cur;
		item_cur = item_cur.prev();

		// Move all items (except original) right.
		$('.item').not(item_orig).stop(true, true).animate({ left: '+='+item_width }, delay, function() {
				item_cur.children('.desc').show().animate({ opacity: 1 }, delay).css('position','absolute').css('left',item_width-20);
			}
		);
		
		// Fade out original and fade in new image.
		$(item_orig).children('.item-bg').children('img').stop(true, true).animate({ opacity: opacity }, delay );
		$(item_cur).children('.item-bg').children('img').stop(true, true).animate({ opacity: 1 }, delay );

		// Now slide the original and hide the description.
		gap_width = item_width + desc_width;
		item_orig.children('.desc').hide().css('opacity','0');
		$(item_orig).stop(true, true).animate({ left: '+='+gap_width }, delay );

		// Update the URL with the current product ID
		var prod_id = $(item_cur).attr('class').split('-');
		//window.location.replace('#' + prod_id[1])

	});

	// Left click functions
	$('#left').click(function() {
		if(item_cur.index() >= items_count -1) return false;

		item_orig = item_cur;
		item_cur = item_cur.next();

		// Fade out original and fade in new image.
		$(item_orig).children('.item-bg').children('img').stop(true, true).animate({ opacity: opacity }, delay );
		$(item_cur).children('.item-bg').children('img').stop(true, true).animate({ opacity: 1 }, delay );

		gap_width = item_width + desc_width;
		item_orig.children('.desc').css('opacity','0').hide();
		$(item_cur).stop(true, true).animate({ left: '-='+gap_width }, delay );

		// And slide everything else left.
		$('.item').not(item_cur).stop(true, true).animate({ left: '-='+item_width }, delay, function() {
				item_cur.children('.desc').show().animate({ opacity: 1 }, delay).css('position','absolute').css('left',item_width-20);
			}
		);

		// Update the URL with the current product ID
		var prod_id = $(item_cur).attr('class').split('-');
		//window.location.replace('#' + prod_id[1])

	}); 

	// Scroll to the item clicked.
	$('.item .item-bg').click(function() {

		if(item_cur.index() == $(this).parent().index()) {
			return false;
		} else {
			diff = parseInt(item_cur.index()) - parseInt($(this).parent().index());
			i = 0;
			if(diff > 0 ) { 
				while( i < diff ){ $('#right').click(); i++; }
			} else {
				while( i > diff ){ $('#left').click(); i--; }
			}
		}
	});


	// Bind an event to window.onhashchange that, when the hash changes, gets the
	// hash and adds the class "selected" to any matching nav link.
	$(window).hashchange( function(){
		var hash = location.hash;
		hash = hash.replace( /^#/, '' );

		if(hash != '' ) {  
			item_cur2 = $('.item:last');
			var prod_id_current = $(item_cur2).attr('class').split('-')[1];
			var prod_id_requested = hash;

			while( prod_id_current != prod_id_requested ){ 
				$('#right').click();
				item_cur2 = item_cur2.prev();
				prod_id_current = $(item_cur2).attr('class').split('-')[1];
			}
		}
	})
	// Since the event is only triggered when the hash changes, we need to trigger
	// the event now, to handle the hash the page may have loaded with.
	$(window).hashchange();


	// Show everything when we're done loading. 
	$('#container').css('visibility', 'visible');

	// Keyboard Controls
	$(document).bind('keydown', 'left',function (){ $('#left').click(); return false; });
	$(document).bind('keydown', 'right',function (){ $('#right').click(); return false; });

	// Swipe Controls
	if(!$.browser.msie){
		$("#container").touchwipe({
		     wipeLeft: function() { $('#left').click(); return false; },
		     wipeRight: function() { $('#right').click(); return false; },
		     min_move_x: 20,
		     preventDefaultEvents: true
		});
	}

	// Add hover class to current descriptions

	$("body.wpsc .desc .inner").hover(
	  function () { $('.desc').addClass("hover"); },
	  function () { $('.desc').removeClass("hover"); }
	);

	// Add hover class to button
	$(".wpsc_buy_button").hover(
	  function () { $(this).addClass("hover"); },
	  function () { $(this).removeClass("hover"); }
	);
	
	/*
	 * Gallery Lightboxes
	 */
	$('.gallery a').lightBox({
		overlayBgColor: '#000',
		overlayOpacity: 0.6,
		imageLoading: '/wp/wp-content/themes/fantaci/images/lightbox-ico-loading.gif',
		imageBtnClose: '/wp/wp-content/themes/fantaci/images/lightbox-btn-close.gif',
		imageBtnPrev: '/wp/wp-content/themes/fantaci/images/lightbox-btn-prev.gif',
		imageBtnNext: '/wp/wp-content/themes/fantaci/images/lightbox-btn-next.gif',
//		imageBtnNext: '/wp/wp-content/themes/fantaci/images/arrow2-r.gif',
		containerResizeSpeed: 350,
		txtImage: 'Image',
		txtOf: 'of'
	});

	/*
	 * Misc minor functions
	 */ 
	$('#buyer-submit').hover(function() { $(this).addClass('hover'); }, function() { $(this).removeClass('hover'); } );

	/* 
	 * Prevent arrows from being highlighted due to fast clicking
	 */
	$.extend($.fn.disableTextSelect = function() {
		return this.each(function(){
			if($.browser.mozilla){//Firefox
				$(this).css('MozUserSelect','none');
			}else if($.browser.msie){//IE
				$(this).bind('selectstart',function(){return false;});
			}else{//Opera, etc.
				$(this).mousedown(function(){return false;});
			}
		});
	});
	$('.controls a').disableTextSelect();//No text selection on elements with a class of 'noSelect'

});


