// JavaScript Document
$(document).ready(function(){
	
	showScienceArticle();
	
	$('#adLink').click(function(){
		showAd();
		return false;
	});
	
	$('#popupClose').click(function(){
		hideAd();
		return false;
	});
	
	/*$('#adLink').fancybox({
						'href' : baseURL + 'home/ad',
						'padding' : 0,
						'width' : 612,
						'height' : 609,
						'isAd' : 'true',
						'scrolling' : 'no',
						onClosed : function(){
							$.ajax({
								type : "POST",
								cache : false,
								url : baseURL + "/home/closeAd",
								success: function(data) {}
							}); 
						},
						onStart : function(){
							$('#fancybox-inner').css({'height' : '609px'})
						}
					});*/
	
	$(".retailViewMore").click(function() {
		
		var address = $(this).attr('address');
		var id = $(this).attr('rel');
		
		$.fancybox({
			'href' : this.href,
			'onComplete' : function(){ showMap(address,id); }
		});

		return false;
	});
	
	$('.press').fancybox({'titleShow' : false, 'showNavArrows' : false});
	
	//Hover effect for the category and press icons
	$('.fadeItem').hover(
			function(){
				$(this).addClass('fadeProduct');
				$(this).css({'opacity': 1});
				$('.fadeItem').not('.fadeProduct').css({'opacity': .8});
			},
			function(){
				$(this).removeClass('fadeProduct');
			}
	);
	
	$('#categoryProducts .catProduct').tooltip({
		track: true,
		bgiframe: true,
		delay: 0,
		bodyHandler: function(){ return $(this).find('.productMsg').html(); },
		showURL: false,
		extraClass: "hoverMsg"
	});
	
	$('.subContent').hover(
		function(){},
		function(){  $('.fadeItem').css({'opacity': 1}); }
	);
	
	
	
	$('.videoThumb').tooltip({
		track: true,
		delay: 0,
		bodyHandler: function(){ return $(this).attr("id"); },
		showURL: false,
		extraClass: "vidHoverMsg",
		fixPNG: true
	});

	
	$('.scienceBtn').click(function(e){
		
		if($(this).hasClass('scienceBtnActive'))
		{
			return false;
		}
		
		var id = $(this).attr('href');
		//Remove the active state from all science buttons
		$('.scienceBtn').removeClass('scienceBtnActive');
		//Add the active state to this science button
		$(this).addClass('scienceBtnActive');
		$('.scArticleActive').hide();
		$('.scArticle').removeClass('scArticleActive');
		$(id).fadeIn();
		$(id).addClass('scArticleActive');

		window.location.hash = id + ' ';
		return false;
		
	}); //End Science Btn Click
	
	$('#scrollContent').jScrollPane({'dragMaxHeight' : 13, 'scrollDragWidth' : 4, 'scrollbarWidth' : 6, 'scrollbarMargin' : 20, 'topCapHeight' : 50, 'bottomCapHeight' : 30});
	
	
	
	//Hover effect for the shop now links
	$('#shopNowBtn').hover(
		function(){
			$('#productShopLinks').fadeIn();
		},
		function(){
			$('#productShopLinks').fadeOut();
		}
	);
	
	//Hover effect for the share links
	$('#shareBtn').hover(
		function(){
			$('#shareLinks').fadeIn();
		},
		function(){
			$('#shareLinks').fadeOut();
		}
	);

	
	$('#productSearchInput').focus(function(){
		
		if($('#productSearchInput').val() == 'PRODUCT SEARCH')
		{
			$('#productSearchInput').val('');
		}
	});
	
	$('.pageNo').click(function(){
		$('.pageNo').removeClass('pageNo_active');
		$(this).addClass('pageNo_active');
	});
});


/*function scrollBar()
{
	//Distance to scroll
	var distance = 60;
		
	//Get the current margin
	var pos = $('#scrollContent').css('marginTop');
	var contentHeight = $('#scrollContent').height();
	var holderHeight = 376;
	var difference = parseInt(Math.round(contentHeight)) - holderHeight;
	pos = pos.replace('px',''); //Remove the px
		
	//Scroll Down
	$('#scrollDown').click(function(){
		//alert(contentHeight);							
		if(difference > 0)
		{
			//Recalculate Position
			pos = parseInt(pos) - distance;
			$('#scrollContent').animate({'marginTop' : pos + 'px'},'normal','swing');
			//Recalculate difference
			difference = difference - distance;
		}
		return false;
	});
		
	//Scroll Down
	$('#scrollUp').click(function(){
		//alert(pos);
		//alert(difference);

		if(difference < (parseInt(contentHeight) - holderHeight))
		{
			//Recalculate Position
			pos = parseInt(pos) + distance;
			$('#scrollContent').animate({'marginTop' : pos + 'px'},'normal','swing');
			//Recalculate difference
			difference = difference + distance;
		}
			
		return false;
	});	
} //End Scrollbar*/


function slideshow()
{
	$('#slideshow').cycle({ 
		speed:'slow',
		timeout: 10000,
		pager:'#slideshowNavBtns',
		next: '#slideshowNext',
		prev: '#slideshowPrev'
	});
	
	$('#slideshowPause').click(function() { 
    	if($(this).hasClass('slideshowPauseActive'))
		{
			$(this).removeClass('slideshowPauseActive');
			$('#slideshow').cycle('resume');
		}
		else
		{
			$(this).addClass('slideshowPauseActive');
			$('#slideshow').cycle('pause'); 
		}
	});
}


function productTabs()
{
	$('.tab').click(function(){
		
		var contentID = $(this).attr('href');
		
		$('.tab').removeClass('tabActive');
		$(this).addClass('tabActive');
		
		$('.tabContentChild').hide();
		$(contentID).fadeIn();
		
		return false;
	});
}

function showMap(myAddress,id)
{
	$("#map_" + id).gMap({ markers: [{ address : myAddress}], zoom : 15 });
}


//Shows the science article if the url hash is a match
function showScienceArticle()
{
	if(window.location.hash != '' && window.location.hash != '#')
	{
		//Fetch the hash
		var id = window.location.hash;
		
		//Loop through the science nav and set the selected article btn to active.
		$('#scienceNav a').each(function(i,n){
			
			$(n).removeClass('scienceBtnActive');
			
			if($(n).attr('href') == $.trim(id))
			{
				$(n).addClass('scienceBtnActive');
			}
			
		});
		
		//Show the selected article
		$('.scArticleActive').hide();
		$('.scArticle').removeClass('scArticleActive');
		$(id).fadeIn();
		$(id).addClass('scArticleActive');
		
	}
}


function showAd()
{
	$('#adOverlay').fadeTo("normal",0.30);
	$('#popupAd').fadeIn();
	
	$('#adOverlay').bind("click",hideAd);
}

function hideAd()
{
	$('#adOverlay').unbind("click");
	$('#adOverlay').fadeOut();
	$('#popupAd').fadeOut();
	
	//Set cookie
	$.ajax({
		type : "POST",
		cache : false,
		url : baseURL + "/home/closeAd",
		success: function(data) {}
	}); 
}