/*
	
	couple own features added - such as dynamic text corresponding for images, extracted from database
	and alternate images view in fullsize mode
	http://zmolo.com

	thanks to Lokesh Dhakar - http://www.huddletogether.com for the base
*/

// If you would like to use a custom loading image or close button reference them in the next two lines.
var loadingImage = '/im/clock1.gif';
var currentfade = 100;
var transDirection=0;
var mdX=mdY=null;
var threshold=5;
var ajaxObj= null;
var tipIsOn=0;
var arrangeAdGo=0;
var movedLangs=0;
//

/*function putaj(){
	var fr=document.createElement('iframe');
	fr.src='javascript:false;';
	fr.name="ajaxObj";
	fr.id="ajaxObj";
	fr.style.position='absolute';
	fr.style.margin=fr.style.padding='0px';
	fr.style.zIndex=1;
	fr.style.width=fr.style.height='0px';
	document.getElementsByTagName('body')[0].appendChild(fr);	
} */


function isdefined(variable)
{
    return (typeof(window[variable]) == "undefined")?  false: true;
}

function getClear(obj){
	obj.value='';
}


function sbmt(formname,newparam){
	eval("document.forms['"+formname+"'].su.value=newparam");
	eval("document.forms['"+formname+"'].submit()");
}

/*updates security code picture*/
function refIm(imagename){
	var ima=document.getElementById(imagename);
	var src=ima.src;
	var regs=	/\&tst\=.*/;
	if (src.match(regs))
	{
		newshit="&tst="+Math.random();
		src = src.replace(regs,newshit);
	} else {
		src=src+"&tst="+Math.random();
	}
	ima.src = src;
	return false;
}
// Core code from - quirksmode.org

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}



// Core code from - quirksmode.org
// Edit for Firefox by pHaez

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight,xScroll,yScroll) 
	return arrayPageSize;
}


//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
//
function pause(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

//
// getKey(key)
// Gets keycode. If 'x' is pressed then it hides the lightbox.
//

function getKey(e){
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}	
	key = String.fromCharCode(keycode).toLowerCase();
	if(key == 'x'){ hideLightbox(); }
	if(keycode == 37){
		var objPrev = document.getElementById('prevButton');
		objPrev.onclick();
	}
	if(keycode == 39){ 
		var objNext = document.getElementById('nextButton');
		objNext.onclick();
	}
}


//
// listenKey()
//
function listenKey () {	document.onkeydown = getKey; }
	

//
// showLightbox()
// Preloads images. Pleaces new image in lightbox then centers and displays.
//
function showLightbox(objLink)
{
	// prep objects
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');
	var objImage = document.getElementById('lightboxImage');
	var objLoadingImage = document.getElementById('loadingImage');
	var objText=document.getElementById('fadeImageText');
	//objText.innerHTML=objLink.getAttribute("id");
	var objScript = document.getElementById ? document.getElementById("ajaxObj") : null;
	var currentID = objLink.getAttribute("id");
	var sstr="/get_ajax.php?act=gallery&id="+currentID+"&timestamp="+Math.random();
	objScript.src=sstr;


	var allIDS = new Array();

	var objPrev = document.getElementById('prevButton');
	var objNext = document.getElementById('nextButton');

	if (typeof(document.forms['photoform']) !=='undefined')
	{	 
		allIDS = document.forms['photoform'].allims.value.split(",");
		
		var ccid;

		for (m=0;m<allIDS.length;m++ ){
				if (allIDS[m] == currentID)
				{
					ccid=m;
				}
		}
		
		if(ccid > 0) {
			objPrev.style.display = '';
			objPrev.onclick = function () {
				ind = ccid-1;
				var previndex= document.getElementById(allIDS[ind]);
				return previndex.onclick();
				
			}
		} else {
			objPrev.style.display = 'none';
		}
		

		

		if(ccid < (allIDS.length-1)) {
			objNext.style.display = '';
			objNext.onclick = function () {
				ind = ccid+1;
				var nextindex= document.getElementById(allIDS[ind]);
				return nextindex.onclick();
				
			}
		}else {
			objNext.style.display = 'none';
		}
	}  else {
		 objNext.style.display = 'none';
		 objPrev.style.display = 'none';
	}


	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// center loadingImage if it exists
	if (objLoadingImage) {
		objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
		objLoadingImage.style.left = (((arrayPageSize[0] - 20 - objLoadingImage.width) / 2) + 'px');
		objLoadingImage.style.display = 'block';
	}

	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	objOverlay.style.top = (arrayPageScroll[1] + 'px');
	objOverlay.style.display = 'block';

	// preload image
	imgPreload = new Image();

	imgPreload.onload=function(){
		objImage.src = objLink.href;

		// center lightbox and make sure that the top and left values are not negative
		// and the image placed outside the viewport
		var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] - 35 - imgPreload.height) / 2);
		var lightboxLeft = ((arrayPageSize[0] - 35 - imgPreload.width) / 2);

		//objText.style.width=imgPreload.width-20+"px";
		
		objLightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
		objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";


		// A small pause between the image loading and displaying is required with IE,
		// this prevents the previous image displaying for a short burst causing flicker.
		if (navigator.appVersion.indexOf("MSIE")!=-1){
			pause(100);
		} 

		if (objLoadingImage) {	objLoadingImage.style.display = 'none'; }

		// Hide select boxes as they will 'peek' through the image in IE
		selects = document.getElementsByTagName("select");
        for (i = 0; i != selects.length; i++) {
                selects[i].style.visibility = "hidden";
        }

	
		objLightbox.style.display = 'block';

		// After image is loaded, update the overlay height as the new image might have
		// increased the overall page height.
		arrayPageSize = getPageSize();
		objOverlay.style.height = (arrayPageSize[1] + 'px');
		
		// Check for 'x' keypress
		listenKey();

		onloadDone=1; 
		currentfade=0;

		moveNavButts(objImage.width,objImage.height,20);
		setTextBlockSize(objImage.width);
		objImage.className = 'transparent';
		fadeObj(objImage,1);
		return false;
	}

	
	imgPreload.src = objLink.href;
	

}

function moveNavButts (imgW,imgH,margin){
	var prev = document.getElementById('prevButton');
	var next = document.getElementById('nextButton');
	
	prev.style.marginTop=parseInt(imgH/2)+30+'px';
	prev.style.marginLeft='-55px';
	next.style.marginTop=parseInt(imgH/2)+30+'px';
	next.style.marginLeft=parseInt(imgW+2*margin-5)+'px';	  
}





//
// hideLightbox()
//
function hideLightbox()
{
	// get objects
	objOverlay = document.getElementById('overlay');
	objLightbox = document.getElementById('lightbox');

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';

	// make select boxes visible
	selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}

	// disable keypress listener
	document.onkeypress = '';
}



function buildFader()
{
	
	if (!document.getElementsByTagName){ return; }
	var anchors = document.getElementsByTagName("a");

	// loop through all anchor tags
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];

		if (anchor.getAttribute("href") && (anchor.getAttribute("rel") == "fdpop")){
			anchor.onclick = function () {showLightbox(this); return false;}
		} 
		else if (anchor.getAttribute("rel") == "ask")
		{
			anchor.onmousemove =	function (event)	{	showSimpleTip(this,event,'tip');	}
			anchor.onmouseout =		function  ()		{	hideTip(this,'tip');	}
		}
	}


	/*iframe object to process DB*/
	var objBody = document.getElementsByTagName("body").item(0);
	var fr=document.createElement('iframe');
	fr.src='javascript:false;';
	fr.name="ajaxObj";
	fr.id="ajaxObj";
	fr.style.position='absolute';
	fr.style.margin=fr.style.padding='0px';
	fr.style.zIndex=1;
	fr.style.display='none';
	fr.style.width=fr.style.height='0px';
	objBody.appendChild(fr);	


	
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.onclick = function () {hideLightbox(); return false;}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '90';
 	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);
	
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	// preload and create loader image
	var imgPreloader = new Image();
	
	// if loader image found, create link to hide lightbox and create loadingimage
	imgPreloader.onload=function(){

		var objLoadingImageLink = document.createElement("a");
		objLoadingImageLink.setAttribute('href','#');
		objLoadingImageLink.onclick = function () {hideLightbox(); return false;}
		objOverlay.appendChild(objLoadingImageLink);
		
		var objLoadingImage = document.createElement("img");
		objLoadingImage.src = loadingImage;
		objLoadingImage.setAttribute('id','loadingImage');
		objLoadingImage.style.position = 'absolute';
		objLoadingImage.style.zIndex = '201';
		objLoadingImageLink.appendChild(objLoadingImage);

		imgPreloader.onload=function(){};	//	clear onLoad, as IE will flip out w/animated gifs

		return false;
	}

	imgPreloader.src = loadingImage;

	var objLightbox = document.createElement("div");
	objLightbox.setAttribute('id','lightbox');
	objLightbox.style.display = 'none';
	objLightbox.style.position = 'absolute';
	objLightbox.style.zIndex = '100';	
	objBody.insertBefore(objLightbox, objOverlay.nextSibling);

	var objLinkn = document.createElement("a");
	objLinkn.setAttribute('href','#');
	objLinkn.setAttribute('title','Наступна');
	objLinkn.setAttribute('id','nextButton');
	objLightbox.appendChild(objLinkn);


	var objLinkn = document.createElement("a");
	objLinkn.setAttribute('href','#');
	objLinkn.setAttribute('title',' Попередня ');
	objLinkn.setAttribute('id','prevButton');
	objLightbox.appendChild(objLinkn);

	var objLink = document.createElement("a");
	objLink.setAttribute('href','#');
	objLink.setAttribute('title',' Клікни щоб закрити ');
	objLink.onclick = function () {hideLightbox(); return false;}
	objLightbox.appendChild(objLink);


	// preload and create close button image
	var objCloseButton = document.createElement("a");
	objCloseButton.setAttribute('id','closeButton');
	objCloseButton.setAttribute('title','Вийти');
	objCloseButton.setAttribute('alt','Вийти');
	objCloseButton.style.position = 'absolute';
	objLink.appendChild(objCloseButton);


	var objImage = document.createElement("img");
	objImage.setAttribute('id','lightboxImage');
	objLink.appendChild(objImage);

	var objText = document.createElement("div");
	objText.setAttribute('id','fadeImageText');
	objLightbox.appendChild(objText);

	
	
	
	
}


//
// addLoadEvent()
// Adds event to window.onload without overwriting currently assigned onload functions.
// Function found at Simon Willison's weblog - http://simon.incutio.com/
//
function addLoadEvent(func)
{	
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
		oldonload();
		func();
		}
	}

}


function setvalue(name,value){
	var obj = document.getElementById ? document.getElementById(name) : null;
	obj.innerHTML = value;
}



// fade in, fade out
function fadeObj(obj,direction){
	var objPassed=document.getElementById(obj.getAttribute("id"));
	var iid=obj.getAttribute('id');
	eval("window.Oppa"+iid+"= function() {goFade(objPassed,direction,iid);}");

	if (!direction)
	{  
		if (eval(isdefined("downTimer"+iid)))
		{
			eval("clearTimeout(downTimer"+iid+")");
		}
	} else {
		if (eval(isdefined("upTimer"+iid)))
		{
			eval("clearTimeout(upTimer"+iid+")");
		}
	}

	goFade(objPassed,direction,iid);
}



function goFade(OP,direction,objID){
	var step=10;
	var duration=30;
	if (direction)
	{	
		if (currentfade < 100)
		{
			if (OP.filters)
			{ 
				OP.filters[0].opacity= currentfade+step;
			} else {
				OP.style.opacity =  (currentfade+step)/100;
				OP.style.MozOpacity =  (currentfade+step)/100;
			}
			currentfade+=step;
			eval("downTimer"+objID+"="+setTimeout("Oppa"+objID+"()",duration));
		} else {
			eval("clearTimeout(downTimer"+objID+")");
		}
			/*window.alert(currentfade); */
	} else {
		if (currentfade > 0)
		{
			if (OP.filters)
			{ 
				OP.filters[0].opacity= currentfade-step;
			} else {
				OP.style.opacity =  (currentfade-step)/100;
				OP.style.MozOpacity =  (currentfade-step)/100;
			}
			currentfade-=step;
			eval("upTimer"+objID+"="+setTimeout("Oppa"+objID+"()",duration));
		}   else {
			eval("clearTimeout(upTimer"+objID+")");
		}
	}
}

function setTextBlockSize(width) {
	// get objects
	objtext = document.getElementById('fadeImageText');
	objtext.style.width = width+'px';
}


function continueReservationStep(apptID,stepID) {
	
	document.forms.reserv.appCode.value = apptID;
	document.forms.reserv.su.value = 'reserv'+stepID;
	/*for (var t=0; t<document.forms.reserv.length;t++ )
	{
		var element = document.forms.reserv[t];
		window.alert(document.forms.reserv[t].name+'==='+document.forms.reserv[t].value);
	}  */
	document.forms.reserv.submit();
}


function screenCoordinates(evnt) {
	var px = 0;
	var py = 0;
	if (!evnt) var evnt = window.event;
	if (evnt.pageX || evnt.pageY) 	{
		px = evnt.pageX;
		py = evnt.pageY;
		/*window.alert(px+"---"+py); */
	}
	else if (evnt.clientX || evnt.clientY) 	{
		px = evnt.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		py = evnt.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	return [px, py];
}




function hideTip(obj, tipid){
	var currenttip = (tipid) ? tipid : "tip";
	var ObjTip = document.getElementById ? document.getElementById(currenttip) : null;
	ObjTip.style.display='none';
	mdX=mdY=null;
	tipIsOn=0;
	
}


function showSimpleTip(obj,event){

	if (obj.getAttribute('simpletext'))
	{
		var message=obj.getAttribute('simpletext');
		showTip(obj,event,message,'tip','direct','');
	}  else {
		var message=obj.getAttribute('id');
		var language= obj.getAttribute('lang');
		showTip(obj,event,message,'tip','ajax',language);
	}
	 
}


function showTip(obj,event,message, tipid,way,language){
	var currenttip = (tipid) ? tipid : "tip";
	var ObjTip = document.getElementById ? document.getElementById(currenttip) : null;
	var coordinates = screenCoordinates(event);
	if (mdX==null || mdY==null)
	{
		mdX=coordinates[0];
		mdY=coordinates[1];
	}
	deltaX=Math.abs(coordinates[0]-mdX);
	deltaY=Math.abs(coordinates[1]-mdY);
	if ((deltaX>threshold || deltaY>threshold) || (deltaX==0 & deltaY==0))
	{
		mdX=coordinates[0];
		mdY=coordinates[1];
		var X =	mdX -310 ;
		var Y =	mdY -35;
		ObjTip.style.top =	Y+'px';
		ObjTip.style.left = X+'px';
			
		//if (ObjTip.style.display == 'none') 
		//{
		ObjTip.style.display='block';
		setTipContent(ObjTip,message,way,language);
		//}
	}
	
	
	
}

function buildTip(){
	var objBody = document.getElementsByTagName("body")[0];

	/**/var objTip = document.createElement("div");
	objTip.setAttribute('id','tip');
	objTip.style.display = 'block';
	objTip.style.top = '0';
	objTip.style.teft = '0';
	objTip.style.position = 'absolute';
	objTip.style.width = '300px';
	objTip.style.zIndex = '205';

	var objClock = document.createElement("div");
	objClock.setAttribute('id','loading');
	objClock.style.display="none";
	objClock.style.position="absolute";
	objClock.style.width="50px";
	objClock.style.height="50px";
	objClock.style.zIndex = '206';
	objClock.innerHTML = "<img src='"+loadingImage+"'>";


	objBody.insertBefore(objTip, objBody.firstChild);
	objBody.insertBefore(objClock, objBody.firstChild);
}



function setTipContent(object,content,way,language){
	if(tipIsOn) return;
	if (way=='direct')
	{  
		object.innerHTML="<div id='tipcontent'>"+content+"</div>";
	}  else {
		object.innerHTML="<div id='tipcontent'><div style='text-align:center;padding:20px;'><img src='"+loadingImage+"'></div></div>";
		var ajObj= document.getElementById ? document.getElementById('ajaxObj') : null;
		getAjaxed('/get_ajax.php?act=tipcontent&parrset='+content+"&returnObj=tipcontent&lang="+language);
	}
	tipIsOn=1;
	  
}


function getAjaxed(src){
	if (ajaxObj == null)
	{
		ajaxObj = document.getElementById ? document.getElementById('ajaxObj') : null
	}
	ajaxObj.src=src;
}


function switchTab(id,tabs){
	var style='none';
	var stylea='tabin'
	for (var i=1; i<=tabs ;i++)
	{
		if (id==i)
		{
			stylea='tab';
			style='block';
		}	else {
			stylea='tab tabin';
			style ='none';
		}

		obja=document.getElementById('tab'+i);
		objc=document.getElementById('tabContent'+i);
		obja.className=stylea;
		objc.style.display=style;
	}

	return false;
}


function arrangeAd(){

	if (!arrangeAdGo)  return false;
	var screenPro= getPageSize();
	var corner=  document.getElementById ? document.getElementById("corner") : null;
	corner.style.left = parseInt(screenPro[4] - 350)+"px";
	corner.style.top = "0px";

	var langs=  document.getElementById ? document.getElementById("langs") : null;
	if (screenPro[4] < 1150)
	{
		
		langs.style.marginLeft=370-(1150-screenPro[4])/2+'px';
		movedLangs=1;
	} else {
		if (movedLangs)
		{
		  langs.style.marginLeft='370px';
		  movedLangs=0;
		}
	}


}


function outerReservation(){

	document.forms.reservOUT.begindate.value=	document.forms.reserv.arrivalye.value+'-'+document.forms.reserv.arrivalmo.value+'-'+document.forms.reserv.arrivalday.value;
	
	document.forms.reservOUT.enddate.value=	document.forms.reserv.departureye.value+'-'+document.forms.reserv.departuremo.value+'-'+document.forms.reserv.departureday.value;
	document.forms.reservOUT.submit();
}

function resize(){
	var t=setTimeout("arrangeAd()",500); /*Explorer tweak*/
	clearTimeout("t");
}

addLoadEvent(buildFader);// run buildFader onLoad
/*addLoadEvent(showBody);*/	// shows body



window.onresize= resize;




