var theLink;

function showHoverMenu() {
		var x = $(this).find('a');
		$('.show').removeClass('show');
		$(x.attr('rel')).addClass('show');
		//position under the link
	}
	
	function hoverMenuOut() { 
		;
	}
	
	function showSelectedLink(){  
		var p=location.pathname.split("/");
		var l = p.length;
		var locStr = p[(l-1)];
		if (locStr != '') {
			theLink = $("#cp-subnav a[rel*="+locStr+"]");
		} else { //there is no location string - for instance they are at the "root" of a directory 
			//var theLink = 'index.html'; //assume we are on the home page
			$("#online-store").addClass("show");
			;
		}
		if (theLink != undefined) {
			theLink.addClass("nav_selected_item");
			//theLink.closest('ul').addClass("show");
			$("#cp-topnav a[rel*=#"+theLink.closest('ul').attr('id')+"]").addClass('current');
		}
		return;
	}
	
$(document).ready(function(){
	showSelectedLink();	
   $("#cp-topnav h2").hoverIntent({
	 sensitivity: 1, 			 // number = sensitivity threshold (must be 1 or higher)
	 interval: 30,   			 // number = milliseconds for onMouseOver polling interval
	 over: showHoverMenu,     	 // function = onMouseOver callback (required)
	 timeout: 1,   			 // number = milliseconds delay before onMouseOut
	 out: hoverMenuOut     	 // function = onMouseOut callback (required)
   });
   
   function megaHoverOver() {
	    $(this).find(".sub").stop().fadeTo('fast', 100).show(); //Find sub and fade it in
	    (function($) {
	        //Function to calculate total width of all ul's
	        jQuery.fn.calcSubWidth = function() {
	            rowWidth = 0;
	            //Calculate row
	            $(this).find("ul").each(function() { //for each ul...
	                rowWidth += $(this).width(); //Add each ul's width together
	            });
	        };
	    })(jQuery); 
	
	    if ( $(this).find(".row").length > 0 ) { //If row exists...
	        var biggestRow = 0;	
	        $(this).find(".row").each(function() {	//for each row...
	            $(this).calcSubWidth(); //Call function to calculate width of all ul's
	            //Find biggest row
	            if(rowWidth > biggestRow) {
	                biggestRow = rowWidth;
	            }
	        });
	        $(this).find(".sub").css({'width' :biggestRow}); //Set width
	        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin
	    } else { //If row does not exist...
	        $(this).calcSubWidth();  //Call function to calculate width of all ul's
	        $(this).find(".sub").css({'width' : '888px'});
	        //$(this).find(".sub").css({'width' : rowWidth}); //Set Width
	    }
	}

	function megaHoverOut(){
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
	      $(this).hide();  //after fading, hide it
	  });
	}
	
	var config = {
	     sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
	     interval: 10, // number = milliseconds for onMouseOver polling interval
	     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
	     timeout: 250, // number = milliseconds delay before onMouseOut
	     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
	};
	
	$("#cp-topnav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
	$("#cp-topnav li").hoverIntent(config); //Trigger Hover intent with custom configurations
	$("#cp-subnav li").hoverIntent(config);
   
	 $("#addKitToCart").bind('click', function(a) {
		a.preventDefault();
		var type = $("#type-of-kit").val(); // get the value of the selected option
		$("#hiddenbuttonarea").find('#hidden_'+type+' img').click(); //force a click to the button in a hidden area
		return false;
	});
	
	/* Medications page. Calls pajination function. */
	var numberOfProductsOnPage;
	$('.all-meds').addClass('active-med');
	
	/* Medications page. Filters meds. */
	$(".filter").bind('change', function() {
		$('.active-med').removeClass('active-med');
		var filter_class = $(this).attr('value'); // if this doesn't capture, try $(this).find('option:selected').val();
		$(".all-meds").hide();
		$("."+filter_class).addClass('active-med');
		$("."+filter_class).show();
		if (numberOfProductsOnPage == 'All') {
			//
		} else if (numberOfProductsOnPage == '10') {
			$('#pajination_container').pajinate({
				nav_panel_id : '.pajination_container_nav',
				item_container_id : '.pajination_content',
				items_per_page : 10
			});
		} else if (numberOfProductsOnPage == '20') {
			$('#pajination_container').pajinate({
				nav_panel_id : '.pajination_container_nav',
				item_container_id : '.pajination_content',
				items_per_page : 20
			});
		} else if (numberOfProductsOnPage == '50') {
			$('#pajination_container').pajinate({
				nav_panel_id : '.pajination_container_nav',
				item_container_id : '.pajination_content',
				items_per_page : 50
			});
		}
	});
	$(".products_per_page").bind('change', function() {
		var products_per_page_class = $(this).attr('value'); // if this doesn't capture, try $(this).find('option:selected').val();
		numberOfProductsOnPage = products_per_page_class;
		if (products_per_page_class == 'All') {
			$('.pajination_container_nav').hide();
			$('.active-med').show();
		} else if (products_per_page_class == '10') {
			$('.pajination_container_nav').show();
			$('#pajination_container').pajinate({
				nav_panel_id : '.pajination_container_nav',
				item_container_id : '.pajination_content',
				items_per_page : 10
			});
		} else if (products_per_page_class == '20') {
			$('.pajination_container_nav').show();
			$('#pajination_container').pajinate({
				nav_panel_id : '.pajination_container_nav',
				item_container_id : '.pajination_content',
				items_per_page : 20
			});
		} else if (products_per_page_class == '50') {
			$('.pajination_container_nav').show();
			$('#pajination_container').pajinate({
				nav_panel_id : '.pajination_container_nav',
				item_container_id : '.pajination_content',
				items_per_page : 50
			});
		}
	});
	
	/*$("form#addToCart .formButton").bind('click', function(a) { // used for hidden button area in certain product pages
		javascript:history.go(-1);
      	var event = jQuery.Event("click");
		$("#catshopbuy").trigger(event);
		if ( event.isDefaultPrevented() ) {
			//
		} else {
			window.location.href = document.getElementById('catshopbuy').href;
		}
		return false;
	});*/
	
	$('#thumbs ul li a').hover(
		function() {
			var currentBigImage = $('#bigpic img').attr('src');
			var newBigImage = $(this).attr('rel');
			var currentThumbSrc = $(this).attr('rel');
			switchImage(newBigImage, currentBigImage, currentThumbSrc);
		},
		function() {}
	);
	function switchImage(imageHref, currentBigImage, currentThumbSrc) {
		var theBigImage = $('#bigpic img');
		if (imageHref != currentBigImage) {
			theBigImage.fadeOut(1, function(){
				theBigImage.attr('src', imageHref).fadeIn(1);
				var newImageDesc = $("#thumbs ul li a img[src='"+currentThumbSrc+"']").attr('alt');
				$('p#desc').empty().html(newImageDesc);
			});
		}
	}
	
});

$(function () {
	var tabContainers = $('div.tabs > div');
    tabContainers.hide().filter(':first').show();
                        
    $('div.tabs ul.tabNavigation a').click(function () {
    	tabContainers.hide();
        tabContainers.filter(this.hash).show();
        $('div.tabs ul.tabNavigation a').removeClass('selected');
        $(this).addClass('selected');
        return false;
    }).filter(':first').click();
});

