$(document).ready(function() {

	$("ul#topnav li:first-child").addClass("first");
	$("ul#topnav li:last-child").addClass("last");

	function megaHoverOver(){
		$(this).find(".sub").stop().fadeTo('fast', 1).show();
			
		//Calculate width of all ul's
		(function($) { 
			jQuery.fn.calcSubWidth = function() {
				rowWidth = 0;
				//Calculate row
				$(this).find("ul").each(function() {					
					rowWidth += $(this).width(); 
				});	
			};
		})(jQuery); 
		
		if ( $(this).find(".row").length > 0 ) { //If row exists...
			var biggestRow = 0;	
			//Calculate each row
			$(this).find(".row").each(function() {							   
				$(this).calcSubWidth();
				//Find biggest row
				if(rowWidth > biggestRow) {
					biggestRow = rowWidth;
				}
			});
			//Set width
			$(this).find(".sub").css({'width' :biggestRow});
			$(this).find(".row:last").css({'margin':'0'});
				
		} else { //If row does not exist...
			
			$(this).calcSubWidth();
			//Set Width
			$(this).find(".sub").css({'width' : rowWidth});
			
		}
	}
	function megaHoverOut(){ 
	  $(this).find(".sub").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}

	var config = {    
		 sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 50, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 50, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$("ul#topnav li .sub").css({'opacity':'0'});
	$("ul#topnav li").hoverIntent(config);
	
	//Assign class to last .sub div
	$(this).find("div.sub:last").addClass('last')
	
   $('#coda-slider-1').codaSlider({
	   crossLinking: true,
	   firstPanelToLoad: 1
   });
   
   function smartColumns() { //Create a function that calculates the smart columns

    //Reset column size to a 100% once view port has been adjusted
	$("ul.column").css({ 'width' : "20%"});
	
	//Assign classes to first and last columns
	$("#cat-links").find("ul.column:first").addClass('first');
	$("#cat-links").find("ul.column:last").addClass('last');

	var colWrap = $("ul.column").width(); //Get the width of row
	var colNum = Math.floor(colWrap / 200); //Find how many columns of 200px can fit per row / then round it down to a whole number
	var colFixed = Math.floor(colWrap / colNum); //Get the width of the row and divide it by the number of columns it can fit / then round it down to a whole number. This value will be the exact width of the re-adjusted column

	$("ul.column").css({ 'width' : colWrap}); //Set exact width of row in pixels instead of using % - Prevents cross-browser bugs that appear in certain view port resolutions.
	$("ul.column li").css({ 'width' : colFixed}); //Set exact width of the re-adjusted column	

	}	
	
	smartColumns();//Execute the function when page loads
	
	$(window).resize(function () { //Each time the viewport is adjusted/resized, execute the function
		smartColumns();
	});
	
	//Login function
	$(".slide").click(function(){
	$("#panel").slideToggle("slow");
	$(this).toggleClass("active"); return false;
	});
	
	// Build the ToolTips
	function simple_tooltip(target_items, name){
		$(target_items).each(function(i){
			$("body").append("<div class='"+name+"' id='"+name+i+"'><p>"+$(this).attr('title')+"</p></div>");
			var my_tooltip = $("#"+name+i);
		
			if($(this).attr("title") != "" && $(this).attr("title") != "undefined" ){
		
			$(this).removeAttr("title").mouseover(function(){
						my_tooltip.css({opacity:0.8, display:"none"}).fadeIn(400);
			}).mousemove(function(kmouse){
					var border_top = $(window).scrollTop();
					var border_right = $(window).width();
					var left_pos;
					var top_pos;
					var offset = 65;
					if(border_right - (offset *1) >= my_tooltip.width() + kmouse.pageX){
						left_pos = kmouse.pageX+offset;
						} else{
						left_pos = border_right-my_tooltip.width()-offset;
						}
		
					if(border_top + (offset *3)>= kmouse.pageY - my_tooltip.height()){
						top_pos = border_top +offset;
						} else{
						top_pos = kmouse.pageY-my_tooltip.height()-offset;
						}
		
					my_tooltip.css({left:left_pos, top:top_pos});
			}).mouseout(function(){
					my_tooltip.css({left:"-9999px"});
			});
		
			}
		
		});
		}
		
		// Add more 'simple_tooltip' lines below to create a new one
		$(document).ready(function(){
		 simple_tooltip("span.freeshippingCart","tooltip");
		});

});

			


