/**
 * hoverScroll Plugin
 * jQuery Tools Scrollable Plugin, that enables scroll when user hovers scrollable controls
 * 
 */
$.fn.hoverScroll = function() {
	
	var timer = null; 
     
    return this.each(function() { 
 
        var api = $(this).scrollable({api:true}); 
 
        $prev = $(this).siblings(api.getConf().prev);
        $prev.hover(function() {
        	clearInterval(timer);
        	api.prev();
        	timer = setInterval(function name() {
        		if(api.getIndex()==0){
        			clearInterval(timer);
        		}else{
        			api.prev();
        		}
			},api.getConf().speed);
		},function() {
			clearInterval(timer);
		});
        $next = $(this).siblings(api.getConf().next);
        $next.hover(function() {
        	clearInterval(timer);
        	api.next();
        	timer = setInterval(function name() {
        		if(api.getIndex()+1==api.getSize()){
        			clearInterval(timer);
        		}else{
        			api.next();
        		}
			},api.getConf().speed);
		},function() {
			clearInterval(timer);
		});
 
    }); 
};


function showDropDown() {
	$('.dropdown-link').mouseover( function() {
		$(this).next().removeClass('hidden');
	});
	$('.dropdown-link').click( function() {
		$(this).next().removeClass('hidden');
	});

	$('.dropdown-products').mouseover( function() {
		$(this).removeClass('hidden');
	});

	$('.dropdown-products').mouseout( function() {
		$(this).addClass('hidden');
	});
	$('.dropdown-products').click( function() {
		$(this).addClass('hidden');
	});
}

$(document).ready( function() {
	
	var isProductPage = (typeof window.currentCategory!='undefined');
	
	/* CLR-102 */
	var needToHideProductDisply=false; 
	/* CLR-102 */
	
	showDropDown();

	// Add to parents of "vertical" images class "vertical" and reflect them afterwards
	$(".product-list LI IMG.reflect").each(function(){
		function processImage(){
			$.log("Complete: "+img.src+", analizing...."+this.width+"x"+this.height);
			if(this.height>this.width){
				$(this).parents("li").addClass('vertical');
			}
			$(this).reflect( {
				height :.25,
				opacity :.25
			});
		}
		var img = this;
		if(img.complete){
			processImage.call(img);
		}else{
			$.log("Waiting for complete: "+img.src);
			$(img).load(processImage);
		}
	});	
	
	// Reflect big product image
	$('.Column.column-left>IMG.reflect').reflect( {
		height :.25,
		opacity :.25
	});
	
	
	try {
		
		// Add scrollable
		var api = $("DIV.scrollable").scrollable({
			size: 3,
			items:'UL.product-list',
			api:false,
			speed:1000
		});//.hoverScroll();
		
		if(isProductPage){			
			
			$('a.viewBackOfTheBox, #read-the-label a').fancybox({
				hideOnContentClick:false,
				callbackOnStart:function(){
					$('#fancy_outer').addClass('backOfTheBox');
				},
				callbackOnClose:function(){
					$('#fancy_outer').removeClass('backOfTheBox');
				}
			});
			
			// Add accordion
			$("#tabs-faq .product-accordion > li > a").disclosurePanel();
			
			// And subtabs
			$(".sub-tabs > ul").tabs("div.tabPane",{
				history: true, 
				api:false,
				current:'ui-tabs-selected'
			});
			
		}
		
	
	
		var tabsApi = $(".tabs > ul").tabs(".tabs > div.tabPane",{
			history: !(isProductPage), /*enable history only if it's products page*/
			api:true,
			current:'ui-tabs-selected',
			onBeforeClick: function(i) {
				/*if(i==1){
					setChildCallouts();
				}else{
					setAdultCallouts();
				}*/	
				
				/* CLR-102 */
				if(isProductPage&&needToHideProductDisply){
					$('#tabs-'+window.currentCategory+' > div:eq(1)').removeClass('hidden');					
					$('#tabs-'+window.currentCategory+' > div:eq(0)').addClass('hidden');
					needToHideProductDisply=false;
				}
				/* CLR-102 */
			}
		}); 
		
		if(isProductPage){
			if(window.currentCategory=='Child'){
				tabsApi.click(1);
			}			
			
			/* CLR-102 */
			$('#tabs-'+window.currentCategory+' > div:eq(1)').addClass('hidden');
			needToHideProductDisply=true;
			/* CLR-102 */
		}
		
		
		/******** Spotlight tags ********/
		var spotlightTags = {
			'#Adult':{src:'1608566',type:'produ440',cat:'71cla361'},
			'#Child':{src:'1608566',type:'produ440',cat:'71cla586'},
			'#uses':{src:'1608566',type:'produ440',cat:'71cla301'},
			'#warnings':{src:'1608566',type:'produ440',cat:'71cla018'},
			'#faq':{src:'1608566',type:'produ440',cat:'71cla873'}
		};
		var productLabelsSpotlightTags = {
			'Adult':{src:'1608566',type:'produ440',cat:'clari159'},
			'Child':{src:'1608566',type:'produ440',cat:'71cla362'}
		};
		var productsSpotlightTags = {
			'Claritin12HourRediTabs':{src:'1608566',type:'produ440',cat:'clari098'},
//			'ClaritinEye':{src:'1608566',type:'produ440',cat:'clari176'},
			'ClaritinLiquiGels':{src:'1608566',type:'produ440',cat:'clari163'},
			'ClaritinD24Hour':{src:'1608566',type:'produ440',cat:'clari818'},
			'ClaritinRediTabs24Hour':{src:'1608566',type:'produ440',cat:'clari414'},
			'ClaritinTablets':{src:'1608566',type:'produ440',cat:'clari469'},
			'Claritin12HourRediTabsForKids':{src:'1608566',type:'produ440',cat:'71cla013'},
			'ChildrensClaritinGrapeChewables':{src:'1608566',type:'produ440',cat:'child100'},
			'ChildrensClaritinGrapeFlavoredSyrup':{src:'1608566',type:'produ440',cat:'71chi798'},
			'ClaritinRediTabsForKids24Hour':{src:'1608566',type:'produ440',cat:'clari487'}
		};
		
		/******* Atlas Tags *********/
		var atlasTags = {
			'#Adult':'cbnctn_ClaritinProductsforAdults_1',
			'#Child':'cbnctn_ClaritinProductsforChildren_1',
			'#uses':'cbnctn_ClaritinProductsUses_1',
			'#warnings':'cbnctn_ClaritinProductsWarnings_1',
			'#faq':'cbnctn_ClaritinProductsFAQ_1'
		};
        /*var productLabelsAtlasTags = {
			'Adult':'',
			'Child':''
		};*/
		var productsAtlasTags = {
			'Claritin12HourRediTabs':'cbnctn_Claritin12HourReditabs_1',
//			'ClaritinEye':'cbnctn_ClaritinEye_1',
			'ClaritinLiquiGels':'cbnctn_ClaritinEye_1',
			'ClaritinD24Hour':'cbnctn_ClaritinD24Hour_1',
			'ClaritinRediTabs24Hour':'cbnctn_ClaritinRediTabs24Hour_1',
			'ClaritinTablets':'cbnctn_ClaritinTablets_1',
			'Claritin12HourRediTabsForKids':'cbnctn_Claritin12HourRediTabsforKids_1',
			'ChildrensClaritinGrapeChewables':'cbnctn_ChildrensClaritinGrapeChewables_1',
			'ChildrensClaritinGrapeFlavoredSyrup':'cbnctn_ChildrensClaritinGrapeFlavoredSyrup_1',
			'ClaritinRediTabsForKids24Hour':'cbnctn_ClaritinRediTabsforKids24Hour_1'
		};
		
		/* Product tags */
		if(window.currentProductId){
		    var aTag=productsAtlasTags[window.currentProductId];				
			$.attachAtlas(aTag);
			
			var tag=productsSpotlightTags[window.currentProductId];				
			$.attachSpotlightTag(tag.src,tag.type,tag.cat);
		}		
		/* Read the label/back of the box */
		$('a.viewBackOfTheBox, #read-the-label a').click(function(){
			var tag = productLabelsSpotlightTags[window.currentCategory];
			if(tag){ $.attachSpotlightTag(tag.src,tag.type,tag.cat); }
			
		});
		/* User switching (sub)tabs */
		$(window).bind('hash',function(event,hash){
		    var aTag = atlasTags[hash];
			if(aTag){ $.attachAtlas(aTag); }
			
			var tag = spotlightTags[hash];
			if(tag){ $.attachSpotlightTag(tag.src,tag.type,tag.cat); }
		});
		/******** END Spotlight tags ********/
		
		/***** Change title on tabs change *****/
		var tabTitles={
				"#Adult":"Claritin® Products For Adults | Claritin®",
				"#Child":"Children's Claritin® Products | Claritin®",
				"#all-products":"Claritin® Products | Claritin®"				
		};
		$(window).bind('hash',function(event,hash){
			var title = tabTitles[hash];
			if(title){ document.title = title; };
			updateCallouts();
			
		});
		/***** END Change title on tabs change *****/
		
		$('.buyNowBtn').fancybox({
			hideOnContentClick:false,
			callbackOnStart:function(){
				$.attachSpotlightTag('1608566','buyno675','buyno852');
				$('#fancy_outer').addClass('buyNowPopup');
			},
			callbackOnClose:function(){
				$('#fancy_outer').removeClass('buyNowPopup');
			}
		});
		
		var insertFootnotes = function (fnotes, base){
		    var el = null;
		    for(i in fnotes){
			if(i%2==0){
			    el = $(document.createElement('div'));
			    $("DIV.footnote.footnoteLanding", base).append(el);
			    el.append("<sup>"+fnotes[i].index+"</sup>"+fnotes[i].title);
			}else{
			    el.append("<br/><sup>"+fnotes[i].index+"</sup>"+fnotes[i].title);
			}
		    }
		};
		
		if(!isProductPage){
		    $("#tabs-Adult SUP.footnote").footnotes(function(fnotes){
			insertFootnotes(fnotes, $("#tabs-Adult"));
		    });
		    $("#tabs-Child SUP.footnote").footnotes(function(fnotes){
			insertFootnotes(fnotes, $("#tabs-Child"));
		    });
		}
			
	} catch (err) {
		$.log(err);
	}

});