$.fn.equalizeCols = function(){
  var height = 0; 
  return this.css("height","auto").each(function(){ 
    height = Math.max( height, jQuery(this).outerHeight() ); 
  }).css("height", height); 

};

$(document).ready(function(){


	// *** I. SLIDESHOW TRAILER START *** //
	
	$("#infoNav ul li:last").addClass("noPadding");
	$("#infoNav ul").show();
	
	$("#infoNav ul a").click(function(){
	
	  //if ($("#info:has(:animated)").length == 0) {

	  if (!$(this).is(".active")) {

		var div = $(this).attr("rel");
		var img = $(this).attr("href");

		// set active thumbnail
		$("#infoNav ul a").removeClass("active");
		$(this).addClass("active");

		// fade proper trailer picture
		$("#infoPicture").fadeOut("fast",function(){
			$(this).html('<img src="' + img + '" alt="" />').fadeIn("normal");
		});

		// select proper trailer description (div)
		$("#trailer #info div:not(#infoNav)").fadeOut("normal").hide();
		$("#trailer #info div." + div).fadeIn("normal");

		return false;
		  
	  } else {
		return false;
	  }

	  //} else {	return false;	}
	  
	});

	  
	function slideShow() {
	  ($("#infoNav ul a:last").is(".active"))
		? $("#infoNav ul a:first").click()
		: $("#infoNav ul li:has(a.active)").next().children("a").click();
	  return false;
	}
	
	
	$("#infoNav ul a").mousedown(function(){
	  clearInterval(run);
	  run = setInterval(slideShow, 10000);
	});
	
	run = setInterval(slideShow, 10000);
	
	$("#infoNav ul a:first").click();
	
	$("#infoNav ul a").tooltip({
		delay:	0,
		fade:	250,
		showURL:	false,
		track:	true
	});
	
	$("div.eItem div span a").tooltip({
		delay:	0,
		fade:	250,
		showURL:	false,
		track:	true
	});
	
	
	

	// *** II. SORTABLE PANELS *** //
	
	if ($("#sortContainer").length) {
	
	if ($.cookie("ordered_panels1") || $.cookie("ordered_panels2") || $.cookie("ordered_panels3")) {
	
		//create an array to hold the variables from the cookie
		if ($.cookie("ordered_panels1")) var order_array1 = $.cookie("ordered_panels1").split(",");
		if ($.cookie("ordered_panels2")) var order_array2 = $.cookie("ordered_panels2").split(",");
		if ($.cookie("ordered_panels3")) var order_array3 = $.cookie("ordered_panels3").split(",");
		var count = 0;
		//loop thru all the LI in the ul #myList
		$("#sort_1 > div, #sort_2 > div, #sort_3 > div").each(function() {
			//move the elements within #myList
			if ($.cookie("ordered_panels1")) $("#sort_1").append($("#"+order_array1[count]));
			if ($.cookie("ordered_panels2")) $("#sort_2").append($("#"+order_array2[count]));
			if ($.cookie("ordered_panels3")) $("#sort_3").append($("#"+order_array3[count]));
			//add 1 to count
			count++;
		});
	}

	$(".col").sortable({
		connectWith: 	".col",
		cursor: 		"move",
		dragOnEmpty:	true,
		forcePlaceholderSize: true,
		handle:		"div.caption, .pHeader, .pContent h2",
		opacity: 		0.5,
		placeholder: 	"helper",
		revert:		true,
		scroll:		true,
		start:		function(){	$("div.col").equalizeCols();	},
		tolerance:	"intersect",
		update:		function(){
        if (/opera/.test(navigator.userAgent.toLowerCase())) {
          $(".panel", this).css("top", "0");
        }
					  for (x = 1; x <= 3; x++) {
						$.cookie("ordered_panels"+x,$("#sort_"+x).sortable("toArray"),{ path: '/', expires: 730});
						}
					$("div.col").equalizeCols();
					}
	});
	
	}
	
	
	if ($("div.bigCol div.eItem").length > 0) $("div.eItem > a").fancybox();
	if ($("div.bigCol div.pItem").length > 0) $("div.pItem > a").fancybox();
	if ($("div.bigCol div.rItem").length > 0) $("div.rItem > a").fancybox();


});