jQuery.noConflict();

jQuery(document).ready( function ($) {
	
	$("#carousel").css({'display':'block'});
	
	//calculate page size
	var scrollSize = 9;
	var carouselSize = $("#carousel ul").children('li').length;
	var pages = Math.ceil(carouselSize/scrollSize);
	$('#jcarousel-pages', $("#pagination")).html(pages);
	
	function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
		$('.jcarousel-item').removeClass('leftmost'); //remove all previous leftmost
		$(item).prev().addClass('leftmost');
	};
	
	function mycarousel_itemLastInCallback(carousel, item, idx, state) {
		$('.jcarousel-item').removeClass('rightmost'); //remove all previous rightmost
		$(item).next().addClass('rightmost');
	};

	//initialize carousel
	 $("#carousel").jcarousel({
        scroll: scrollSize,
		itemFirstInCallback:  mycarousel_itemFirstInCallback,
		itemLastInCallback:   mycarousel_itemLastInCallback
    });
	
	//update index
	var index = 1;
	$(".jcarousel-next", $("#carousel")).click(function () {
		var disabled = $(".jcarousel-prev-disabled", $("#carousel")); //check if disabled
		if ((disabled.length != 0) || (index == (pages-1))) {
			index ++;
			$('#jcarousel-index', $("#pagination")).html(index);
		}
		return false;
	});
	$(".jcarousel-prev", $("#carousel")).click(function () {
		var disabled = $(".jcarousel-next-disabled", $("#carousel")); //check if disabled
		if ((disabled.length != 0) || (index == 2)) {
			index --;
			$('#jcarousel-index', $("#pagination")).html(index);
		}
	});

	$(".carousel_btn", $("#carousel")).click(function () {
		//remove active state from first thumbnail
		$(".carousel_btn:first .carousel_hover").remove();
		
		var title = $(this).next('.title').html();
		$('#title .nr', $("#copy")).html(title);
		
		var prod_title = $(this).next().next('.prod_title').html();
		$('#prod_title h3', $("#copy")).html(prod_title);
		
		var prod_desc = $(this).next().next().next('.prod_desc').html();
		$('#prod_description p', $("#copy")).html(prod_desc);

		var buy_now = $(this).siblings('.buy_now').html();
		
		var catwalk_exclusive = $(this).siblings('.catwalk_exclusive').html();
				
		if(buy_now) {
			$('#buy-now').attr('href', buy_now).show();
			$('#contact-info').hide();
			$('#contact-us-info').hide();
			$('#prod_description').show();
		}
		else if(catwalk_exclusive) {
			$('#buy-now').hide();
			$('#catwalk_exclusive').attr('span', catwalk_exclusive).show();
			$('#contact-info').hide();
			$('#contact-us-info').show();
			$('#prod_description').hide();
		}
		else {
			$('#buy-now').hide();
			$('#contact-info').show();
			$('#contact-us-info').hide();
			$('#prod_description').show();
		}

		var prod_title2 = $(this).siblings('.prod_title2').get(0);
		if (prod_title2) { 
			$('#prod_title2 h3', $("#copy")).html($(prod_title2).html()).show();
		}
		else {
			$('#prod_title2 h3', $("#copy")).hide();
		}

		var prod_desc2 = $(this).siblings('.prod_desc2').get(0);
		if (prod_desc2) { 
			$('#prod_desc2 p', $("#copy")).html($(prod_desc2).html()).show();
		}
		else {
			$('#prod_desc2 p', $("#copy")).hide();
		}
		
		var buy_now2 = $(this).siblings('.buy_now2').get(0);
		if (buy_now2) { 
		    $('#buy-now2').attr('href', $(buy_now2).html()).show();
		}
		else {
			$('#buy-now2').hide();
		}

		var prod_title3 = $(this).siblings('.prod_title3').get(0);
		if (prod_title3) { 
			$('#prod_title3 h3', $("#copy")).html($(prod_title3).html()).show();
		}
		else {
			$('#prod_title3 h3', $("#copy")).hide();
		}
		
		var prod_title4 = $(this).siblings('.prod_title4').get(0);
		if (prod_title4) { 
			$('#prod_title4 h3', $("#copy")).html($(prod_title4).html()).show();
		}
		else {
			$('#prod_title4 h3', $("#copy")).hide();
		}
		
		var prod_title5 = $(this).siblings('.prod_title5').get(0);
		if (prod_title5) { 
			$('#prod_title5 h3', $("#copy")).html($(prod_title5).html()).show();
		}
		else {
			$('#prod_title5 h3', $("#copy")).hide();
		}

		var prod_desc3 = $(this).siblings('.prod_desc3').get(0);
		if (prod_desc3) { 
			$('#prod_desc3 p', $("#copy")).html($(prod_desc3).html()).show();
		}
		else {
			$('#prod_desc3 p', $("#copy")).hide();
		}
		
		var prod_desc4 = $(this).siblings('.prod_desc4').get(0);
		if (prod_desc4) { 
			$('#prod_desc4 p', $("#copy")).html($(prod_desc4).html()).show();
		}
		else {
			$('#prod_desc4 p', $("#copy")).hide();
		}
		
		var prod_desc5 = $(this).siblings('.prod_desc5').get(0);
		if (prod_desc5) { 
			$('#prod_desc5 p', $("#copy")).html($(prod_desc5).html()).show();
		}
		else {
			$('#prod_desc5 p', $("#copy")).hide();
		}
		
		var buy_now3 = $(this).siblings('.buy_now3').get(0);
		if (buy_now3) { 
		    $('#buy-now3').attr('href', $(buy_now3).html()).show();
		}
		else {
			$('#buy-now3').hide();
		}

		var contact_info1 = $(this).siblings('.contact_info1').get(0);
		if (contact_info1) { 
			$('#contact-info1').show();
		}
		else {
			$('#contact-info1').hide();
		}
		
		var exclusive1 = $(this).siblings('.exclusive1').get(0);
		if (exclusive1) { 
			$('#exclusive1').show();
		}
		else {
			$('#exclusive1').hide();
		}

		var contact_info2 = $(this).siblings('.contact_info2').get(0);
		if (contact_info2) { 
			$('#contact-info2').show();
		}
		else {
			$('#contact-info2').hide();
		}

		var contact_info3 = $(this).siblings('.contact_info3').get(0);
		if (contact_info3) { 
			$('#contact-info3').show();
		}
		else {
			$('#contact-info3').hide();
		}
		
		var contact_info4 = $(this).siblings('.contact_info4').get(0);
		if (contact_info4) { 
			$('#contact-info4').show();
		}
		else {
			$('#contact-info4').hide();
		}
		

		var prod_img = $(this).siblings('.prod_img').html();
		var image = '<img src='+prod_img+' alt="Women\'s Designer Clothing - Amanda Wakeley Collection" height="1237" width="700" style="display:none" />'
		$('#prod_image', $("#lookbook_wrapper")).html(image);
		$('#prod_image img', $("#lookbook_wrapper")).fadeIn('slow');
		
		Cufon.replace('#content .gillsanslight', { fontFamily: 'gillsanslight' });
		
		//remove all active states
		$(".carousel_btn .thumb", $("#carousel")).removeClass('carousel_hover_active');
		$(".carousel_btn .active_hover", $("#carousel")).removeClass('active_hover');
		$(".carousel_btn .carousel_hover_large", $("#carousel")).removeClass('carousel_hover_large');
		
		//add active state/styles
		var img = $(this).find('.thumb');
		$(img).removeClass('carousel_hover_normal');
		$(img).addClass('carousel_hover_active');
		$(this).prepend('<img class="carousel_hover active_hover png_fix" src="/wp-content/themes/amanda-wakeley-ecomm/images/readytowear/static/carousel_hover.png" alt="" />');
		var IE6 = IsThisBrowserIE6();
		if (IE6) { //only run png fix if IE6
			$('.png_fix').supersleight({shim: '/wp-content/themes/amanda-wakeley-ecomm/images/transparent.gif'});
		}
		return false;
	});
	
	$(".carousel_btn", $("#carousel")).hover(hoverIn,hoverOut);
	$(".carousel_btn:first", $("#carousel")).trigger('click');
		
	function hoverIn() {
		var img = $(this).find('.thumb');
		$(img).removeClass('carousel_hover_normal');
		$(img).addClass('carousel_hover_large');
		$(this).prepend('<img class="carousel_hover png_fix" src="/wp-content/themes/amanda-wakeley-ecomm/images/readytowear/static/carousel_hover.png" alt="" />');
		
		var IE6 = IsThisBrowserIE6();
		if (IE6) { //only run png fix if IE6
			$('.png_fix').supersleight({shim: '/wp-content/themes/amanda-wakeley-ecomm/images/transparent.gif'});
	}
	}
	
	function hoverOut() {
		var hover = $(this).find('.carousel_hover');
		$(hover).css({'display':'none'});
		var img = $(this).find('.thumb');
		$(img).removeClass('carousel_hover_large');
		$(img).addClass('carousel_hover_normal');
	}
	
});

function IsThisBrowserIE6() {
    	return ((window.XMLHttpRequest == undefined) && (ActiveXObject != undefined))
	}


