// JavaScript Document for Aussie SE product pages that is pulled in dynamically.

$(document).ready(function() {
	
	
	
	$('#containerAddHairMustHaves a').after(
		'<div></div>'
	);
		
	// Click function to indicate that a product has been added to 'My Hair Must-Haves'.
	// TODO: Need to write some Ajax to actually add the product to the DB.
	$('#containerAddHairMustHaves').click(function() {
		// Get the URL from the clicked element.																	
		var href = $(this).children('a').attr('href');
		var h = href.split('=');
		href =  h[1];
																								 
		$('#containerMustHaves').animate({backgroundColor: '#ffffff'}, function() {
			$(this).animate({backgroundColor: '#2d2d2d'});																																			
		});
		$('#contentMustHaves').animate({backgroundColor: '#ffffff'}, function() {
			$(this).animate({backgroundColor: '#2d2d2d'});																																			
		});
		
		if ($('body').attr('id') == 'collections') {
			jQuery.post('/my-hair-must-haves/add-collection.php', {
				'collection': href,
				'json': 'true'
			}, function(data) {
								
				if (data != null && data.status == 'logged-in') {
					$('#noMustHaves').text(data.number);
				}
				
			}, "json");
		} else {
			jQuery.post('/my-hair-must-haves/add.php', {
				'product': href,
				'json': 'true'
			}, function(data) {
								
				if (data != null && data.status == 'logged-in') {
					$('#noMustHaves').text(data.number);
				}
				
			}, "json");
		}
		
		return false;
	});	
	
	// Click function to launch an overlay for a tell a friend mechanism.
	$('#linkSendFriend a').click(function() {	
		// Get the URL from the clicked element.																	
		var href = $(this).attr('href');
		
		$('#contentTaf').load(href + ' .overlay', function() {
			$.getScript('../scripts/taf.js');
			$.getScript('../scripts/jquery.mousewheel.min.js');
			$.getScript('../scripts/jScrollPane.js', function() {
				$('#tafScrollPane').jScrollPane({showArrows:true,scrollbarWidth:12});
			});
			$('#tafScrollPane').height($('#calloutsProducts').height());
			$(this).parent().css('display', 'block')
				.animate({'height': $('#calloutsProducts').height(), 'opacity': 1}, {duration: 800, queue: false});
		});
				
		return false;
	});
	
	// Close button function for tell a friend overlay.
	$('#containerTaf span').click(function() {
		$(this).parent().animate({'height': 0, 'opacity': 0}, 800, function() {																																													
			$(this).css('display', 'none');
			if ($(this).width() > 205) {
				$(this).width(205);
			}
			if ($('#containerPreview').css('display') == 'block') {
				$('#containerPreview').css('display', 'none')
			}
		});
	});
	
	// Click function to fade in and out product pages without page reload from the 'you may also like...' links.
	$('#containerAlsoLike a').click(function() {
		
		// Get the URL from the clicked element.
		var href = $(this).attr('href');
		
		if (href.match('/products')) {
			var h = href.split('/products/');
			href =  h[1];
		}
		
		// Iterate over all the products in our list. If the href attribute of the one we clicked on matches any in the list then we'll adjust window.onPos so we can move productHighlight to that position below.
		$('#productIndicator ul li a').each(function() {
			if (('/products/' + href) == $(this).attr('href')) {
				window.onPos = $(this).parent().position().left;
			}
		});
		
		// We'll move productHighlight onMouseOver.
		$('#productIndicator ul li a').hover(
			function() {
				$('#productHighlight').animate({'left': $(this).parent().position().left + 'px'}, {queue: false});
			},
			function() {
				$('#productHighlight').animate({'left': window.onPos}, {queue: false});
			}
		);
		
		$('#productIndicator ul li a').each(function(i) {	
			if (('/products/' + href) == $(this).attr('href')) {
				if (i == 0) {
					$('#linkLeftArrow').attr('href', $('#productIndicator ul li').eq($('#productIndicator ul').children('li').size() - 1).children('a').attr('href'));
					$('#linkRightArrow').attr('href', $('#productIndicator ul li').eq(1).children('a').attr('href'));
					$('#productIndicator span em').text(1);
				} else if (i == ($('#productIndicator ul').children('li').size() - 1)) {
					$('#linkLeftArrow').attr('href', $(this).parent().prev().children('a').attr('href'));
					$('#linkRightArrow').attr('href', $('#productIndicator ul li').eq(0).children('a').attr('href'));
					$('#productIndicator span em').text($('#productIndicator ul').children('li').size());
				} else {
					$('#linkLeftArrow').attr('href', $(this).parent().prev().children('a').attr('href'));
					$('#linkRightArrow').attr('href', $(this).parent().next().children('a').attr('href'));
					$('#productIndicator span em').text((i + 1));
				}
			}
		});
		
		$('#swapContent').fadeOut(600, function() {
			//$('#productsContent').addClass('loading');
			$('#swapContent').load(href + ' #swapContent', function() {
				window.setTimeout (sifrTxt, 1);
				$.getScript('../scripts/products-functions.js');
				//$('#productsContent').removeClass('loading');
				$(this).fadeIn(600);
				$('#productHighlight').animate({'left': window.onPos}, {queue: false});
			})
			
		});
		
		return false;
	});
	
	//OVERLAY FUNCTION
	if($('a.overlayIn').length){
		$("a.overlayIn").overlayIn();
	}
});

/****************
################# FUNCTION CUSTOM OVERLAY WITH PNG IE6 FIX ################# 
****************/

$.fn.overlayIn = function() {
	$(this).click(function(){
		var html = '';
		html += '<div id="overlayContainer">';
		html += '<div id="overlay">&nbsp;</div>';
		html += '<div id="overlayContent">&nbsp;</div>';
		html += '</div>';
		$('body').append(html);
		var overlayUrl = $(this).attr('href');
		//CUSTOM OVERLAY
		if($(this).hasClass('overlayIn')){
			var thisLink = $(this);
			$('#overlayContent').load(overlayUrl,function(){
				$('#overlayContent').show();
				//overlayPngFix();
				
				$("a.overlayOut").overlayOut();
			});
		}
		return false;
	});
};

/****************
################# CLOSE OVERLAY ################# 
****************/

$.fn.overlayOut = function() {
	$(this).click(function(){
		$('#overlayContent').fadeOut('slow');
		$('#overlayContainer').fadeOut('slow',function(){ 
			$(this).remove();										   
		});	
		return false;				   
	});
};

/****************
################# OVERLAY PNG FIX ################# 
****************/

function overlayPngFix(){
	$.getScript('/scripts/jquery.client.js', function(){
	  if($.client.browser == 'Explorer' && $.client.browserversion == '6'){
			var fixPngAjax = '<style type="text/css">';
		  	fixPngAjax += '.pngFix {behavior: url("/scripts/iepngfix.htc"); }';
		  	fixPngAjax += '</style>';
		  	$('#overlayContent').append(fixPngAjax); 
	  }
  });		 
}

































	
	