var boxHeight = 65;
var tabHeight = 36;
var tabZindex = 25;
var mouseOverBooking = false;

//$('html').addClass("jsHide");

jQuery(document).ready(function(){

	 if($.browser.msie && parseInt($.browser.version)==7){
		 $('ul.shortcuts ul.menuLevel2').css({top: 18});
	 }
	
		if(!$('#inPageEditBar').length){
			Cufon.replace('.toCufon');
		}
		
		
		$("a[rel^='lightbox']").fancybox({
			'zoomOpacity'	: true,
			'zoomSpeedIn'	: 500,
			'zoomSpeedOut'	: 300,
			'overlayShow'	: true,
			'overlayOpacity': 0.7
		});
		
		$('a.lightboxIframeLink').colorbox({'width':952,'height':502, iframe:true});

		$('#page_gallery360 .mainContent a').colorbox({'width':952,'height':502, iframe:true});
		
		
		$('#booking-form').customSelect();
			
		$('.toPngFix').pukkaPNGFix();
		
		if(!$('#page_home').length){
			addAutoScroll();
		}
		
		initMenu();
		
		initPageLoadingEffect();
		
		initTabs();
		
		
		initLinksColor();
		
		cycleOffers();
		
		initRooms();
		
});
		
function cycleOffers(){
	$('.cycleOffer').before('<div id="offersNav"></div>').cycle({ 
	    fx:     'fade', 
	    speed:   1000, 
	    timeout: 5000,
		cleartype: false,
		pager:  '#offersNav' 
	});
 }

function initPageLoadingEffect(){
	showMainContent();
	
	window.onbeforeunload = function() {
		if($.browser.msie){
			$('#main-content').css({display:"none"});
			$('#contentShadow').css({display:"none"});
			$('#loaderImg').css({'display':'block'});
		} else {
			$('#main-content').animate({opacity:0},"fast");
			$('#contentShadow').animate({opacity:0},"fast");
			$('#loaderImg').css({'opacity':0,'display':'block'});
			$('#loaderImg').animate({opacity:1},"fast");
		}
		return ;
	};
}
	
		
function showMainContent(){
	if($.browser.msie){
		$('#main-content').css({visibility:'visible'});
	} else {
		$('#main-content').css({visibility:'visible','opacity':0});
		$('#contentShadow').css({visibility:'visible','opacity':0});
		$('#main-content').animate({opacity:1},"slow");
		$('#contentShadow').animate({opacity:1},"slow");
	}
	

	if($('#page_home').length){
		//setTimeout(function(){$('#flash-container').fadeOut(1000);},9000);
	} else {
		setTimeout(function(){$('#logoBox').fadeOut(1000);},1000);
	}
}




function initMenu(){
	
	$('.subMenuLink').each(function(i,o){
		var par = $(o).parent().parent();
		var parHeight = par.height();
		var menuHeight = $(o).parent().height();
		var childMenu = $(o).children('ul.menuLevel2');
	
		childMenu.css({opacity:0});
		
		$(o).click(function(e){
			//e.preventDefault();
		});
		
		$(o).mouseenter(function(e){
			$('.subMenuLink ul.menuLevel2').each(function(i2,o2){
				if(i!=i2){
					$(o2).animate({height:0,opacity:0});
				} 
			});
			par.stop();
			childMenu.stop();
			par.animate({height: parHeight*2});
			childMenu.animate({height:menuHeight,opacity:1});
		});
		par.mouseleave(function(e){
			par.stop();
			childMenu.stop();
			par.animate({height: parHeight});
			childMenu.animate({height:0,opacity:0});
		});
		childMenu.mouseleave(function(e){
			par.stop();
			childMenu.stop();
			par.animate({height: parHeight});
			childMenu.animate({height:0,opacity:0});
		});
	});
}


function initTabs(){

	$('.sliderTab').each(function(i,o){
		var knob = $(o).children('.tabKnob');
		var box = $(o).children('.tabBox');
		var par = $(o).parent();
		
		openTab(0);
		setTimeout(function(){
			closeTab(0);
		},10000);

		
		knob.mouseenter(function(e){
			openTab(i);
		});
		par.mouseenter(function(e){
			mouseOverBooking = true;
		});
		par.mouseleave(function(e){
			mouseOverBooking = false;
			setTimeout(function(){closeTab(i);},2000);
		});
		

	});
}
	
	
function openTab(activeI){
	$('.sliderTab').each(function(i,o){
		var box = $(o).children('.tabBox');
		if(i==activeI){
			$(o).css('z-index',tabZindex+1);
			box.stop();
			box.animate({height:boxHeight},"slow", function(){
				box.css({overflow: 'visible'});
			});
		} else {
			box.css({overflow: 'hidden'});
			$(o).css('z-index',tabZindex-1);
			$(o).stop();
			$(o).animate({bottom:-tabHeight},"slow");
		}
	});
}

function closeTab(activeI){
	if(!mouseOverBooking && $('div.calendar').css('display')!='block'){
		$('.sliderTab').each(function(i,o){
			var box = $(o).children('.tabBox');
			box.css({overflow: 'hidden'});
			if(i==activeI){
				box.stop();
				box.animate({height:0},"slow",function(){
					$(o).css('z-index',tabZindex);
				});
			} else {
				$(o).stop();
				$(o).animate({bottom:0},"slow",function(){
					$(o).css('z-index',tabZindex);
				});
			}
		});
	}
}



function initLinksColor(){
	addFadeEffect('ul.shortcuts li a','#f6f6f6');
}


function addFadeEffect(name,finalColor){
	$(name).each(function(i,o){
		var startColor = $(o).css('color');
		$(o).mouseenter(function(){
			$(o).stop();
			$(o).animate({color: finalColor},"slow");
		});
		$(o).mouseleave(function(){
			$(o).stop();
			$(o).animate({color: startColor},"slow");
		});
	});
}


function flashIntroCallback(){
	$('#content').fadeIn(1000);
	$('#bookingOffersTabs').fadeIn(1000);
	$('#groupHomepageHotelLinks').fadeIn(1000);
	setTimeout(function(){$('#main-visual').fadeIn(1000);},3500);
	
	addAutoScroll();
	
}


function addAutoScroll(){
	$('.contentScroller').jScrollPane({dragMaxHeight:40,scrollbarWidth:9});
}



function initRooms(){
	
	if($('#page_accommodation').length){
		$('a.lightboxlink').fancybox({
			'zoomOpacity'	: true,
			'zoomSpeedIn'	: 500,
			'zoomSpeedOut'	: 300,
			'overlayShow'	: true,
			'overlayOpacity': 0.7,
			'centerOnScroll' : false
		});
		
		$('.itemPreviewViewLink a').each(function(i,o){
			$(o).click(function(e){
				e.preventDefault();
				var parent = $(o).parent().parent().parent().parent();
				parent.children('.contentExtra').slideToggle("slow", function(){
					addAutoScroll();
				});
				parent.children('.smallThumbs').slideToggle("slow");
			});
		});
	} 
}

