/****************************************************
*	Product Display
*	Used to populate, sort, filter and display all 
*	content without page reloads in
*	the products section
*
*	Reads posts from WP
*****************************************************/


// globar variables
var siteRoot = document.location.protocol + '//' + document.location.host; // DO NOT HARD CODE DOMAINS!!!!!! 'http://www.snapple.com';  // no trailing slash;
var subStringCount = siteRoot.length; //is this global even necessary?
var defaultSlug = 'products';

$(document).ready(function(){	
						   
// Initialize history plugin.
// The callback is called at once by present location.hash. 
  $('.menuScrollPane a').attr({rel:'history'});
		//$('#galleryScrollPane').jScrollPane({scrollbarWidth: 15, dragMinHeight: 35, dragMaxHeight: 35});
		
		function pageload(hash) {
			// hash doesn't contain the first # character.
			if(hash) {
				var firstHash = hash.substr(0,6);
				if(firstHash == '/flavo' || firstHash == '/drink' )
				{	
						 
				 // drink or flavor page
				  $('#main').css('background-image','url(/wp-content/themes/snapple/images/productsgallerybg.jpg)');
				  // restore ajax loaded state				
				  var toLoad = siteRoot+'/category/products'+hash+' #galleryContainer';				 
				  $("#center-content-container").fadeOut();
				  $('#products-main-bg').fadeOut();
				  $('#products-gallery-bg').fadeOut();
				  $('#products-gallery-bg').css('background-image','none'); 
				  $('.loading').css('display', 'block');
				  $("#center-content-container").load(toLoad, '', showGalleryPage);				 
					
				}else if(hash == '/all/')
				{						 
					 //$('#products-main-bg').css('background-image','url(/wp-content/themes/snapple/images/productsgallerybg.jpg)');
					// $('#products-gallery-bg').css('background-image','url(/wp-content/themes/snapple/images/productsgallerybg.jpg)');	
					 $('#main').css('background-image','url(/wp-content/themes/snapple/images/productsgallerybg.jpg)');					 
					 var toLoad = siteRoot+'/category/products/ #galleryContainer';
					 $("#center-content-container").fadeOut();
				  	 $('#products-main-bg').fadeOut();
				  	 $('#products-gallery-bg').fadeOut();
				  	 $('.loading').css('display', 'block');					 
					 $('#center-content-container').load(toLoad, '', showGalleryPage);
				}
				else{					
					// product detail page
					$('#main').css('background-image','url(/wp-content/themes/snapple/images/productsgallerybg.jpg)');
					var productToLoad = siteRoot+'/'+hash+' #productInfo';
					//alert(productToLoad);
					$('#center-content-container').fadeOut();					
				    $('#products-main-bg').fadeOut();
				    $('#products-gallery-bg').fadeOut();
					$('#products-gallery-bg').css('background-image','none');
					$('.loading').css('display', 'block');
					$('#center-content-container').load(productToLoad,'',showProductPage);				
				}
				
			} else {
				// 'Products' main page		
				$('#products-main-bg').css('background-image','url(/wp-content/themes/snapple/images/productsbg.jpg)');
				$('#center-content-container').fadeOut('normal', showProductHome);			
			}
		}
	
		
// FADE IN A PRODCUT'S BG AND WIRE UP NAVIGATION
		function showProductPage()
		{			
			$('.productBackground').css('display', 'none');
			var productBG = $('.productBackground').attr('src');			
			$('#products-gallery-bg').css('background-image','url('+productBG+')'); 
			$('#products-gallery-bg').fadeIn('normal', showProductDetail);					
			
		}
// SHOW LIST OF PRODUCTS and SET HEADER IMG
		function showGalleryPage()
		{				
			$('#products-gallery-bg').css('background-image','none');
			$('#products-main-bg').css('background-image','none');
			
			var headerImgStr = window.location.hash;			
			var parentStr = headerImgStr.substr(2,5);			
			
			if(parentStr == 'drink')
			{
				var headerImg = headerImgStr.substr(13);
				headerImg = headerImg.replace(/\/.*/, "");			
			}
			
			if(parentStr == 'flavo')
			{
				var headerImg = headerImgStr.substr(10);				
				headerImg = headerImg.replace(/\/.*/, "");
			}
			if(parentStr == 'all/')
			{
				var headerImg = headerImgStr.substr(2);				
				headerImg = headerImg.replace(/\/.*/, "");
			}	
			
			$("#center-content-container").fadeIn('fast', hideLoader);
			$('#galleryContainer-header').empty();
			$('#galleryContainer-header').html('<img src="/wp-content/themes/snapple/images/categories/'+headerImg+'.gif" />');
			$('#galleryScrollPane').jScrollPane({scrollbarWidth: 15, dragMinHeight: 35, dragMaxHeight: 35});
			
			$('#products-gallery-bg').fadeIn('fast', populateGallery);	
		}		
// DEFAULT PRODUCT PAGE
		function showProductHome()
		{	
			$('#products-main-bg').fadeIn();
			$('#center-content-container').empty();
			$('#center-content-container').html('<div id="mainProductPageLinks"><a href="/pomegranate-raspberry-red-tea/?id=tea" id="redTeaLink">Red Tea</a><a href="/lemon-black-tea/?id=tea" id="lemonTeaLink">Lemon Tea</a><a href="/green-tea/?id=tea" id="greenTeaLink">Green Tea</a><a href="/pomegranate-raspberry-red-tea/?id=tea"><img src="/wp-content/themes/snapple/images/redTeaHover.jpg" width="213" height="114" alt="Red Tea" id="redTeaHover"  /></a><a href="/lemon-black-tea/?id=tea"><img src="/wp-content/themes/snapple/images/lemonTeaHover.jpg" width="183" height="100" alt="Premium Lemon Tea" id="lemonTeaHover" /></a><a href="/green-tea/?id=tea"><img src="/wp-content/themes/snapple/images/greenTeaHover.jpg" width="235" height="105" alt="Super Premium Green Tea" id="greenTeaHover" /></a></div>');
			$('#center-content-container').fadeIn();
			
			// BOTTLE NAVIGATION
			enableBottleNav();			
		}		


///// LEFT NAV
		$('.menuScrollPane a').click(function(){ 
			
			var selected = $(this).attr('id', 'leftNavSelected');			
			setActiveNav(selected);	
		    var hash = this.href;
			hash = hash.substr((subStringCount + 18), hash.length);
			hash = hash.replace(/^.*#/, '');
			catOmni(hash);
			// moves to a new page. 
			// pageload is called at once. 
			$.history.load(hash);				 
		 	return false;
		 
		  });
		
		//VIEW ALL BUTTON
		$('#productMenuBottom a').click(function(){
			setActiveNav(null);									 
			 var hash = this.href;
			 hash = hash.substr((subStringCount + 9), hash.length);
			 hash = hash.replace(/^.*#/, '');
			 $.history.load(hash);	
			 allOmni();		 
			 return false;										 
		});
		
		$('.menuScrollPane').jScrollPane({scrollbarWidth: 15, dragMinHeight: 35, dragMaxHeight: 35});

		$("#menuAccordion").accordion({
			fillSpace: true
		});
		
		$("#menuAccordion h3").click(function(){
			$("#menuAccordion h3").removeClass("active");
			$(this).addClass("active");
		});			
		
		$('.menuScrollPane a').addClass('listItem');
		$('.menuScrollPane a').hover(function(){
			// mouseover
			$(this).addClass('listItemActive');
		}, function(){
			// mouseout
			$(this).removeClass('listItemActive');
		});
		
	
		
	  $.history.init(pageload);
});

function hideLoader() 
{
	$('.loading').fadeOut('fast');
}


	
function setActiveNav(selected)
{
	// 1. remove active state		
	 $('.menuScrollPane a').each(function(){
		$(this).attr('id', '');	
		$(this).removeClass('listItemClicked');
	});	 
	 // 2. set active state on selected
	$(selected).addClass('listItemClicked');
}



function populateGallery(){
		var headerImgStr = window.location.hash;			
		var parentStr = headerImgStr.substr(2,5);			
			
		if(parentStr == 'drink')
		{
			var moreChoicesID = headerImgStr.substr(13);
			moreChoicesID = moreChoicesID.replace(/\/.*/, "");			
		}
		
		if(parentStr == 'flavo')
		{
			var moreChoicesID = headerImgStr.substr(10);				
			moreChoicesID = moreChoicesID.replace(/\/.*/, "");			
		}
		if(parentStr == 'all/')
		{
			var moreChoicesID = 'products';		
					
		}
		if(parentStr == '')
		{
			var moreChoicesID = 'products';
		}
			
	 $('.galleryCategory a').each(function(){	
		 $(this).attr({rel:'history'});
		 var itemURL = $(this).attr('href');
		 $(this).attr('href', itemURL+'?id='+moreChoicesID); 		
		// alert($(this).attr('href'));
		 
		 //products hover function
		 $(this).hover(function(){			
			$(this).children('img.bghover').css("z-index","0");
			$(this).children('img.bghover').fadeIn('fast');
		}, function(){
			$(this).children('img.bghover').fadeOut('fast');
		});			
	 });	 
	 
	 $('.galleryCategory a').click(function(){				
		//	alert('gallery item click');
			var hash = this.href;				
			hash = hash.substr(subStringCount, hash.length);				
			hash = hash.replace(/^.*#/, '');				
			$.history.load(hash);			
			return false;
	});	
	 
   $('#caffeineFreeButton').click(function(event){
	  if($(this).attr('src').indexOf('_un') > -1){
		  $(this).attr('src', '/wp-content/themes/snapple/images/caffeine-free_checked.gif');
		  $('.caffinated').fadeOut('fast');
		  s=s_gi(s_account);
		clearVars();
		   s.linkTrackVars = "eVar17,events";
		   s.linkTrackEvents = "event17";
		   s.eVar17 = "Caffeine Filter On";
		   s.events = s.linkTrackEvents;
		   linkName = s.prop28;
		   s.tl(this,'o',linkName);
	  } else {
		  $(this).attr('src', '/wp-content/themes/snapple/images/caffeine-free_unchecked.gif');
		  $('.caffinated').fadeIn('fast');
			s=s_gi(s_account);
			clearVars();
			s.linkTrackVars = "eVar17,events";
			s.linkTrackEvents = "event17";
			s.eVar17 = "Caffeine Filter Off";
			s.events = s.linkTrackEvents;
			linkName = s.eVar17;
			s.tl(this,'o',linkName);
	  }
	  });	
}


function showProductDetail(){	
	$('#center-content-container').fadeIn('fast', hideLoader);		
	
	$("#infoAccordion h3").click(function(){
		$("#infoAccordion h3").removeClass("active");
		$(this).addClass("active");
	});
	
	$('.infoScrollPane a').addClass('listItem');
	$('.infoScrollPane a').hover(function(){						
		$(this).addClass('listItemActive');
	}, function(){
		$(this).removeClass('listItemActive');
	});

	var locationString = window.location.hash;	
	var idName = locationString.split('?');	
	
	if (idName[1] == undefined)
	{		
		toSub = 'products';		
	}
	if (idName[1] != undefined)
	{
		var toSub = idName[1];
		toSub = toSub.substr(3);
	}
	

	productOmni(idName[0],toSub);
	
	$('#infoPane2 a').each(function(){		
		 var itemURL = $(this).attr('href');
		 $(this).attr('href', itemURL+'?id='+toSub); 
	});
	
	// RIGHT ACCORDION LINKS
	$('.infoScrollPane a').click(function(){			
		 var hash = this.href;
		
		 hash = hash.substr(subStringCount, hash.length);			
		 hash = hash.replace(/^.*#/, '');
		// moves to a new page. 
		// pageload is called at once. 
		$.history.load(hash);
		
		return false;
	});
	
	$('.infoScrollPane').jScrollPane({scrollbarWidth: 15, dragMinHeight: 35, dragMaxHeight: 35});					
	
	$("#infoAccordion").accordion({
		fillSpace: true
	});
}

// BOTTLE NAVIGATION HOVERS
function enableBottleNav()
{	
	$('#mainProductPageLinks a').click(function(){			
		var hash = this.href;				
		hash = hash.substr(subStringCount, hash.length);				
		hash = hash.replace(/^.*#/, '');			
		$.history.load(hash);				
		
		return false;											  
	});
	
	
	$('#redTeaLink').hover(
			function(event){
				$('#redTeaHover').fadeIn("slow");
			},
			function(event){
				if(event.relatedTarget.id != 'redTeaHover'){
					$('#redTeaHover').fadeOut();
				}
			}
			);
			$('#redTeaHover').hover(
			function(event){
				$('#redTeaHover').show();
			},
			function(event){
				if(event.relatedTarget.id != 'redTeaLink'){
					$('#redTeaHover').fadeOut();
				}
			}
			);
			$('#lemonTeaLink').hover(
			  function(event){
				  //Over
				  $('#lemonTeaHover').fadeIn("slow");
			  },
			  function(event){
				  //Out
				  if(event.relatedTarget.id != 'lemonTeaHover'){
					  $('#lemonTeaHover').fadeOut();
				  }
			  }
		  );
		  $('#lemonTeaHover').hover(
			  function(event){
				  //Over
				  $('#lemonTeaHover').show();
			  },
			  function(event){
				  //Out
				  if(event.relatedTarget.id != 'lemonTeaLink'){
					  $('#lemonTeaHover').fadeOut();
				  }
			  }
		  );
		  
		  $('#greenTeaLink').hover(
			  function(event){
				  //Over
				  $('#greenTeaHover').fadeIn("slow");
			  },
			  function(event){
				  //Out
				  if(event.relatedTarget.id != 'greenTeaHover'){
					  $('#greenTeaHover').fadeOut();
				  }
			  }
		  );
		  $('#greenTeaHover').hover(
			  function(event){
				  //Over
				  $('#greenTeaHover').show();
			  },
			  function(event){
				  //Out
				  if(event.relatedTarget.id != 'greenTeaLink'){
					  $('#greenTeaHover').fadeOut();
				  }
			  }
		  );
}



function clearVars() {
		s.linkTrackVars = "";
		s.linkTrackEvents = "";
		s.channel = s.eVar13 = "";
		s.prop28 = "";
		s.prop24 = s.prop41 = s.eVar44 = "";
		s.pageName = s.eVar12 = s.channel = "";
		s.prop5 = "";
		s.prop8 = s.eVar8 = "";
		s.prop9 = s.eVar9 = "";
		s.prop10 = s.eVar10 = "";
		s.prop11 = s.eVar14 = "";
		s.events = s.linkTrackEvents;
		linkName = s.prop28;
	}

function catOmni(id) {
		clearVars();
		var splitID = id.split('/', 3);	
		var theCategory = splitID[2];
		
		s.pageName = 'products: '+splitID[1]+':' + theCategory + ' - Snapple';
		s.eVar12 = 'products: '+splitID[1]+': ' + theCategory + ' - Snapple';
		s.channel='products';
		s.eVar13='products';
		s.prop5='Snapple';
		s.eVar8='North East';
		s.prop8='North East';
		s.eVar9='Snapple';
		s.prop9='Snapple';
		s.eVar10='Tea/Juices/Snacks';
		s.prop10='Tea/Juices/Snacks';
		s.eVar14='NonFlash';
		s.prop11='NonFlash';
		s.prop12='products';
		s.prop28='flavors';
		s.prop24=theCategory;
		s.events='event12';
		
		/************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/
		var s_code=s.t();if(s_code)document.write(s_code); 


}

	


function productOmni(id,category) {		
		clearVars();
		var substrID = id.substr(2, id.length-3);
		s.prop28= category;
		s.prop24 = s.prop7 = s.eVar7= substrID;
		s.products = ';'+substrID;
		s.channel='products';
		s.eVar13='products';
		s.prop5='Snapple';
		s.eVar8='North East';
		s.prop8='North East';
		s.eVar9='Snapple';
		s.prop9='Snapple';
		s.eVar10='Tea/Juices/Snacks';
		s.prop10='Tea/Juices/Snacks';
		s.eVar14='NonFlash';
		s.prop11='NonFlash';
		s.prop12='products';
		s.prop36 = s.eVar36 = category;
		s.pageName = s.eVar12 = s.channel + ': ' + s.prop28 + ': ' + s.prop24 + ' - ' + 'Snapple';
		s.events='prodView,event12';
		
		/************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/
		var s_code=s.t();if(s_code)document.write(s_code); 		
}



function allOmni() {
		s.pageName = 'products: view all products - Snapple';
		s.eVar12 = 'products: view all products - Snapple';
		s.channel='products';
		s.eVar13='products';
		s.prop5='Snapple';
		s.eVar8='North East';
		s.prop8='North East';
		s.eVar9='Snapple';
		s.prop9='Snapple';
		s.eVar10='Tea/Juices/Snacks';
		s.prop10='Tea/Juices/Snacks';
		s.eVar14='NonFlash';
		s.prop11='NonFlash';
		s.prop12='products';
		s.prop28='view all products';
		s.events='event12';
		
		/************* DO NOT ALTER ANYTHING BELOW THIS LINE ! **************/
		var s_code=s.t();if(s_code)document.write(s_code); 
}