/**
 * 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();
			
			// 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'}
		};
		/* Product tags */
		if(window.currentProductId){
			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 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 *****/

	} catch (err) {
		$.log(err);
	}

});