$(document).ready(function(){
	$('.confirm').click(function(){
		return confirm('Säker?');
	});
	
	$("#generate_url_slug").click(function(){
		var from	= $(this).attr('from_field');
		var to		= $(this).attr('to_field');
		from 		= $('#'+from+'');
		to			= $('#'+to+'');
		var Text 	= from.val();
		
		Text = Text.toLowerCase();
		Text = Text.replace(/[^a-zA-Z0-9]+/g,'-');
		
		to.val(Text);        
	});
	
	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: 20, // number = milliseconds for onMouseOver polling interval    
		 over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		 timeout: 100, // number = milliseconds delay before onMouseOut    
		 out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};

	$("#nav li .sub").css({'opacity':'0'});
	$("#nav li").hoverIntent(config);
	
	
   	/* -- clickableArea() - extracts a link from inside a parent DOM element, makes entire element hoverable and clickable --*/
	
	(function( $ ){
		$.fn.clickableArea = function() {

		return this.click(function() {
			var $this = $(this);
			var linkloc = $this.find("a").attr("href");
			var blank = $this.find("a").attr("target"); /* for external links */
			if ( blank ) {
				window.open(linkloc);
			    return false;
			} else if ( !linkloc ) {
				return false;
			} else {
				window.location=linkloc;
			    return false;
			}
			
		});
		
	};
	  
	})( jQuery );
	
	
	/*---------------	 Equal height row grids   -------------*/
	
	(function( $ ){ 
	   $.fn.equalHeights = function() {
		
		var currentTallest = 0,
		     currentRowStart = 0,
		     rowDivs = new Array(),
		     $this,
		     topPosition = 0;

		 this.each(function() {

		   $this = $(this);
		   topPostion = $this.position().top;

		   if (currentRowStart != topPostion) {

		     // we just came to a new row.  Set all the heights on the completed row
		     for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
		       rowDivs[currentDiv].height(currentTallest);
		     }

		     // set the variables for the new row
		     rowDivs.length = 0; // empty the array
		     currentRowStart = topPostion;
		     currentTallest = $this.height();
		     rowDivs.push($this);

		   } else {

		     // another div on the current row.  Add it to the list and check if it's taller
		     rowDivs.push($this);
		     currentTallest = (currentTallest < $this.height()) ? ($this.height()) : (currentTallest);

		  }

		  // do the last row
		   for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
		     rowDivs[currentDiv].height(currentTallest);
		   }

		 });
		
		};
			  
	})( jQuery );                                  
	        
	// ==================
	// = Function calls =
	// ==================
	 
	
	// equal height puffs and hotels
	$(".hotel").equalHeights();
	$(".puff").equalHeights();
	
	// rollover area
	$(".puff .clickable").clickableArea();
	$(".presentkort-item").clickableArea();
	$(".hotel").clickableArea();
	$(".hotel").mouseover(function(){
		$(this).css({
			background: "#f9f9f9",
			cursor:"pointer"
		});
		$(this).find("a").css("text-decoration", "underline");
		}).mouseout(function(){
			$(this).css({
				background: "transparent",
				cursor:"default"
			});
			$(this).find("a").css("text-decoration", "none");
		});
});
