
// strParentRule - css rule to find parent elements 
// strHoverClass - if not empty add this class on mouseover to parent element
// strChildRule - css rule to find child element inside parent and apply its href (or href of first found <a> inside child )
// dontHide - whether do hide child element
// UseLightBox - apply UseLightBox.start instead of window.location change

function JGNT_LinkBoxes( strParentRule, strHoverClass, strChildRule, dontHide, UseLightBox ) {

	var ParentElementArray = $$(strParentRule);

	for (var k=0; k<ParentElementArray.length; k++){

		if( strHoverClass ){
			ParentElementArray[k].observe( 'mouseover', function( event ){ 
				this.addClassName( strHoverClass ); } )
			ParentElementArray[k].observe( 'mouseout', function( event ){ 
				this.removeClassName( strHoverClass ); } )
		}
		
		if( strChildRule ){
			var targetElement = ParentElementArray[k].select(strChildRule)[0];
			if( targetElement ){
				var strHref = "";
				if( targetElement.readAttribute( "href" ) ){
					strHref = targetElement.readAttribute( "href" );
					ChildHref = targetElement;
				} else {
					ChildHref = targetElement.select('[href!=""]')[0];
					if( ChildHref )
						strHref = ChildHref.readAttribute( "href" );
				}
				
				if( strHref != "" ){
					if( UseLightBox ){
						ParentElementArray[k].observe( 'click', ( function( event ){ UseLightBox.start( this ); } ).bind( ChildHref ) );
						ParentElementArray[k].observe( 'keypress', ( function( event ){ UseLightBox.start( ChildHref ); } ).bind( ChildHref ) );
					} else {
						ParentElementArray[k].observe( 'click', ( function( event ){ window.location = this.href; } ).bind( ChildHref ) );
						ParentElementArray[k].observe( 'keypress', ( function( event ){ window.location = this.href; } ).bind( ChildHref ) );
					};
				}

				if( ! dontHide )
					targetElement.hide();

			}
		}
	}
}


function getMovieName(movieName) {
		if (navigator.appName.indexOf("Microsoft") != -1){
			return window[movieName]
    	} else {
			return document[movieName]
    	}
    }
    
function JGNT_DayTime( ){
	getMovieName("flash").setDayTime(CurrentDayTime);
}

function ChangeBG( strDayPart ){

	if( strDayPart ) 
		CurrentDayTime = strDayPart;
	else 
		strDayPart = CurrentDayTime;  
	
	if( $("daytime1") && strDayPart ){

		if( $("daytime1").visible() ){
			Effect.Fade("daytime1", { duration: 1, queue: 'end', afterFinish: function(){ $("daytime1").hide(); $("daytime2").hide(); $("daytime3").hide(); $("daytime4").hide(); Effect.Appear( "daytime"+strDayPart, { duration: 1, queue: 'end' }) } });
			};

		if( $("daytime2").visible() ){
			Effect.Fade("daytime2", { duration: 1, queue: 'end', afterFinish: function(){ $("daytime1").hide(); $("daytime2").hide(); $("daytime3").hide(); $("daytime4").hide(); Effect.Appear( "daytime"+strDayPart, { duration: 1, queue: 'end' }) } });
			};

		if( $("daytime3").visible() ){
			Effect.Fade("daytime3", { duration: 1, queue: 'end', afterFinish: function(){ $("daytime1").hide(); $("daytime2").hide(); $("daytime3").hide(); $("daytime4").hide(); Effect.Appear( "daytime"+strDayPart, { duration: 1, queue: 'end' }) } });
			};

		if( $("daytime4").visible() ){
			Effect.Fade("daytime4", { duration: 1, queue: 'end', afterFinish: function(){ $("daytime1").hide(); $("daytime2").hide(); $("daytime3").hide(); $("daytime4").hide(); Effect.Appear( "daytime"+strDayPart, { duration: 1, queue: 'end' }) } });
			};

	};

	GetNewImage( strDayPart );
}
 
function GetNewImage( strDayPart, nPicID ){

//alert( ImgLoaderStart + '!' );

	if( ImgLoaderStart == 2 ) 
		return true;
	ImgLoaderStart = 2;
	var url = '/getimage/' + strDayPart;
	new Ajax.Request(url, {
		method: 'get',
		onSuccess: function(transport) {

			if( nPicID ){
				id = $(nPicID);
			} else {
	
				if( $('jgnt_bgimg1').style.zIndex == 1 )
					id = $('jgnt_bgimg1');
				else 
					id = $('jgnt_bgimg2');
			};
								
			if( transport.responseText && ! $(id).src.endsWith( transport.responseText )  ){
				$(id).src=transport.responseText + Math.random();
			};
		},
		onFailure: function() { ImgLoaderStart = 1; }
	});
}

function MakeSmall(){
	Effect.Fade('jgnt_content_main', { duration: 1 });
	Effect.SlideDown('jgnt_content_alt', { duration: 1, queue: 'end' });
	resizeToMax( );
}

function MakeBig(){
	Effect.SlideUp('jgnt_content_alt', { duration: 1 });
	Effect.Appear('jgnt_content_main', { duration: 1, queue: 'end' });
	resizeToMax( );
}

function JGNT_Start(){
	Effect.Fade('jgnt_content_start', { duration: 1 });
	$("jgnt_bg").show();
	Effect.Appear('jgnt_content_main', { duration: 1, queue: 'end' });
	resizeToMax( );
}

function pictureLoad( ) {

	resizeToMax( );

	if( ImgLoaderStart )
		JGNT_ImgSwitch();

}

function JGNT_ImgSwitch(){

 //alert( $('jgnt_bgimg1').style.zIndex + ' - ' + $('jgnt_bgimg2').style.zIndex );
 //alert( 'switch' );

	if( $('jgnt_bgimg1').style.zIndex == 1 ){
		upper_img = $('jgnt_bgimg2');
		lower_img = $('jgnt_bgimg1');
	} else {
		upper_img = $('jgnt_bgimg1');
		lower_img = $('jgnt_bgimg2');
	};
	
	Effect.Fade( upper_img, { duration: 1, queue: 'end', 
		afterFinish: function(){
			upper_img.style.zIndex = "1";
			lower_img.style.zIndex = "2";
			upper_img.show();
			} 
		});
	ImgLoaderStart = 1;
}

function resizeToMax( ) {

	
	if( $('jgnt_bgimg1').style.zIndex == 1 ){
		lower = $('jgnt_bgimg1');
		upper = $('jgnt_bgimg2');
	} else {
		lower = $('jgnt_bgimg2');
		upper = $('jgnt_bgimg1');
	};
		
    nHeight = document.viewport.getHeight();
    nWidth = document.viewport.getWidth();

//	alert( 'resize !' + $('jgnt_bgimg1').style.zIndex + '!' + nHeight + '!' + nWidth + '!' + upper.width + '!' + upper.height + '!' + lower.width + '!' + lower.height );
    
//	$('jgnt_bgimg1').setStyle( { height: nHeight, width: nWidth } );
//	$('jgnt_bgimg2').setStyle( { height: nHeight, width: nWidth } );

//$( 'testtest' ).update( upper.width + 'x' + upper.height + ' ' + nHeight + 'x' + nHeight + ' ' + $('jgnt_bgimg1').width / nWidth + 'x' + $('jgnt_bgimg1').height / nHeight );


if( lower.width > 0 && lower.height > 0 ){
	if( lower.width / nWidth >= lower.height / nHeight ){
		lower.style.height = "100%";
		lower.style.width = "auto";
	} else {
		lower.style.height = "auto";
		lower.style.width = "100%";
	};
} else {
	lower.style.height = "auto";
	lower.style.width = "auto";
}

if( upper.width > 0 && upper.height > 0 ){
	if( upper.width / nWidth >= upper.height / nHeight ){
		upper.style.height = "100%";
		upper.style.width = "auto";
	} else {
		upper.style.height = "auto";
		upper.style.width = "100%";
	};
} else {
	upper.style.height = "auto";
	upper.style.width = "auto";
}
	
}


var myLightBox;
var ImgLoaderStart = 0;

document.observe( 'dom:loaded', function () { 

	Event.observe( 'jgnt_bgimg1', 'load', function () { 
		pictureLoad( );
	});

	Event.observe( 'jgnt_bgimg2', 'load', function () { 
		pictureLoad( );
	});

	Event.observe( window, 'resize', function () { 
		resizeToMax( );
	});
	

	Event.observe( 'jgnt_bgimg1', 'onreadystatechange', function () { 
		resizeToMax( );
	});
	Event.observe( 'jgnt_bgimg2', 'onreadystatechange', function () { 
		resizeToMax( );
	});
	


	$('jgnt_content_alt').hide();
//	$('jgnt_content_main').hide();
	$('jgnt_bg').hide();

//	$('jgnt_bgimg1').style.msInterpolationMode = "bicubic";
//	$('jgnt_bgimg2').style.msInterpolationMode = "bicubic";	

	myLightBox = new Lightbox();
	
	JGNT_LinkBoxes( ".gallery_row", "gallery_row_hover", "a" );
	JGNT_LinkBoxes( ".promo_border", "promo_hover", "a" );
	JGNT_LinkBoxes( ".daytime_promo", "daytime_promo_hover", "a" );
	JGNT_LinkBoxes( ".gallery_foto_row", "gallery_foto_row_hover", "a", 0, myLightBox );

	if( $("daytime1") ){
		$("daytime1").hide();
		$("daytime2").hide();
		$("daytime3").hide();
		$("daytime4").hide();
	};
	
	if( $("daytime1") && CurrentDayTime )
		$("daytime" + CurrentDayTime ).show();

	if( ! $("jgnt_content_start") ){
		$("jgnt_bg").show();
		Effect.Appear('jgnt_content_main', { delay: 1, duration: 0.1 });
//		$('jgnt_content_main').show();
		};



	$('jgnt_bgimg2').src = strPictureInit;
	$('jgnt_bgimg1').src = strPictureInit;
	
	resizeToMax( );
});

function JGNT_SetDate( el, strDate ){
	var curEl = $$('a.selected');
	
	if( curEl.size() > 0 )
		curEl.first().removeClassName('selected');
	el.addClassName('selected');
	$("reserv_date").value = strDate;
};