var body_ = null;
var imagesToPreload = [];
var animationDuration = 1000;

var actionBlocked = false;
var blockDelay = 750;
var blockTimeout;

var wHeight = 0;
var wWidth = 0;
var dHeight = 0;
var dWidth = 0;

var mainSliderDelay = 6000;
var mainSliderTimeout;
var mainSliderRunning = false;

/*Jquery*/


$(document).ready(function(){
    body_ = $(this).find("body");
    body_.find("div.run-js").remove();
    Cufon.replace('.myriad-pro, h1, h2, h3, h4, h5, h6, .menu, .showcase .navigation');
    $.datepicker.setDefaults($.datepicker.regional['pl']);
	$(".form .date").datepicker({ dateFormat: 'yy-mm-dd' });
	
	$('#content .static ul, #content .news .news-content ul, #content .info ul').addClass('stylized');
    
    $("a[rel='external']").live("click",function(){
      var href = $(this).attr("href");
      window.open(href);
      return false;
    });
    
    $("a[rel='colorbox']").colorbox({speed:animationDuration});

	  wHeight = $(window).height();
	  wWidth = $(window).width();
	
	  dHeight = $(document).height();
	  dWidth = $(document).width();

    $(window).resize(function() {
      //window & page resolution
      wHeight = $(window).height();
      wWidth = $(window).width();

      dHeight = $(document).height();
      dWidth = $(document).width();
    });
    
    
    //centering cover navigation
    cWidth = $(".cover").width();
    cWidth -= $(".cover .navigation").width();
    cWidth = Math.floor(cWidth/2);
    $(".cover .navigation").css("left",cWidth + "px")
    

    //hovering
    $("a").live("mouseover mouseout", function(){Cufon.refresh();});
    
    $(".hoverable").mouseover(function(){
      var img = $(this).css("background-image");
      img = img.replace("off","hover");
      $(this).css("background-image",img);
    });
    
    $(".hoverable").mouseout(function(){
      var img = $(this).css("background-image");
      img = img.replace("hover","off");
      $(this).css("background-image",img);
    });
    
    $(".hover img").mouseover(function(){
      var img = $(this).attr("src");
      img = img.replace("off","hover");
      $(this).attr("src",img);
    });
    
    $(".hover img").mouseout(function(){
      var img = $(this).attr("src");
      img = img.replace("hover","off");
      $(this).attr("src",img);
    });
    
    //Menu
    $(".menu>ul>li").live("mouseover", function() {
    	$(".menu>ul>li>ul").css("display", "none");
    	//$(this).find("ul").width($(this).outerWidth());
    	if($(this).find("ul").width() < $(this).outerWidth()) $(this).find("ul").width($(this).outerWidth() + 25);
    	$(this).find("ul").css("display", "block");
    });
    $(".menu>ul>li>ul").live("mouseout", function() {
    	$(this).css("display", "none");
    });
    
    $(".menu").live("mouseout", function() {
    	$(this).find("ul>li>ul").css("display", "none");
    	Cufon.refresh();
    });
    
    //Main slider actions
    $(".cover").ready(function() {
      var that = $(".cover .slides");
      if((that[0] != null) && (that[0] != undefined)) {
        clearTimeout(mainSliderTimeout);
        
        $(this).find(".navigation li").first().addClass("active");
        $(this).find(".slides li").first().addClass("active");   
        
        mainSliderTimeout = window.setTimeout("mainSliderNext()", mainSliderDelay);

      }
    });
    
    
    $(".cover .navigation ul li").live("click", function() {
   	  var that = $(this);
      var parent = $(this).parents(".cover");
      var current = parent.find(".navigation ul li").index(parent.find(".navigation ul li.active"));
      var next = parent.find(".navigation ul li").index(that);
      
      if(!actionBlocked) {
      	if(current != next) {
      		clearTimeout(mainSliderTimeout);
      		actionBlocked = true;
      		
      		
  			parent.find(".bgholder").css("background-image", "url('" + parent.find(".slides ul li.active img").attr("src") + "')");
  			parent.find(".slides ul li.active").css("display","none");
  			parent.find(".slides ul li.active").removeClass("active");
  			parent.find(".navigation ul li.active").removeClass("active");
  			that.addClass("active");
      		
  			parent.find(".slides ul li").eq(next).fadeIn(animationDuration, function() {
  				actionBlocked = false;
  				$(this).addClass("active");
  				parent.find(".bgholder").css("background-image","none");
  				mainSliderTimeout = window.setTimeout("mainSliderNext()", mainSliderDelay);
  			});
      	}
      }
    });
    
	$(".viewer .close").live("click", function() {
		$(this).parents(".viewer").fadeOut(animationDuration);
	});
	
	$("a.book-it").live("click", function() {
		var h = 584;
		$(".viewer.book-it .box").css("margin-top", Math.floor((wHeight-h)/2) + "px");
		$(".viewer.book-it").fadeIn(animationDuration);
		
		return false;
	});
});

/*Other functions*/
function iframeCentering(wh) {
  var clone = $("#iframe .box").clone();
  var iHeight = 0;
  
  clone.css("display","block");
  body_.append(clone);
  iHeight = clone.height();
  clone.remove();
      
  iHeight = wh - iHeight;
  iHeight = iHeight / 2;
  iHeight = Math.floor(iHeight);
  $("#iframe .box").css("margin-top", iHeight + "px");  
}

//Images preloading
function preloadImages(images) {
  var cache = [];
  var args_len = images.length;
  for (var i = args_len; i--;) {
    var cacheImage = document.createElement('img');
    cacheImage.src = images[i];
    cache.push(cacheImage);
  }
}

//Block action
function lockAction() {actionBlocked = true;}
function unlockAction() {actionBlocked = false;}

//Next slide in main slider
function mainSliderNext() {
      var parent = $(".cover");
      var that = parent.find(".navigation ul li.active");
      
      if(!actionBlocked) {
  		clearTimeout(mainSliderTimeout);
  		actionBlocked = true;

  		var next = that.next();
  		if((next[0] == undefined) && (next[0] == null)) next = parent.find(".navigation ul li").first();
  		
  		that.removeClass("active");
  		next.addClass("active");
  		
  		parent.find(".bgholder").css("background-image", "url('" + parent.find(".slides ul li.active img").attr("src") + "')");
  		parent.find(".slides ul li.active").css("display","none");
  		parent.find(".slides ul li.active").removeClass("active");

		parent.find(".slides ul li").eq(parent.find(".navigation ul li").index(next)).fadeIn(animationDuration, function() {
			actionBlocked = false;
			$(this).addClass("active");
			parent.find(".bgholder").css("background-image","none");
			mainSliderTimeout = window.setTimeout("mainSliderNext()", mainSliderDelay);
		});
      }
}
