var BookingRoot = "https://booking-it.com" //GetSiteRoot();
function GetSiteRoot()
{		
	var scripts = document.getElementsByTagName('script');
	for(i=0; i<scripts.length; i++){
		if(scripts[i].src != null){		
			var temp = new Array();
			temp = scripts[i].src.split('/');
			if(temp.pop() == 'BookingInterface.js' && temp.pop() == 'clientscript'){
				return temp.join('/');
			}
		}
	}
	alert('Booking not properly initializied!');
}

function DynamicLoadCSS(filename) {	
	var html_doc = document.getElementsByTagName('head')[0];
	var css = document.createElement('link');
	css.type = 'text/css';
	css.rel = 'stylesheet';
	css.href = filename;
	html_doc.appendChild(css);
	return false;
}

var BookingDialogBox = {

	layer : null,	
	content : null,
	header : null,
	footer : null,
	event : null,					
	overlay : null, 					
	shim : null,	
	loader : null, 	
	closeBtn : null,					
	iframe : null,	
	status : 0,	
	height : 300,
	width : 400,
	url : '',
	
	init : function(){
	
		//učitaj potrebni CSS
		DynamicLoadCSS(BookingRoot + '/clientscript/BookingDialogBox/BookingDialogBox.css');
	
		// elements & properties	
		this.layer = document.createElement('div');
		this.layer.className = 'BookingDialogBox_box';
		this.layer.style.display = 'none';
		document.body.appendChild(this.layer);

	 	this.closeBtn = document.createElement('div');	//close dialog button 
	 	this.layer.appendChild(this.closeBtn);
	 	this.closeBtn.className =  'BookingDialogBox_closeBtn';

		// IE hack
		if((!window.ActiveXObject) ? false : ((window.XMLHttpRequest) ? 5 : 4))
			this.closeBtn.className = 'BookingDialogBox_closeBtnIE';

  		this.content = document.createElement('div');	//holds the loaded content
  		this.content.className = 'BookingDialogBox_content';
		this.layer.appendChild(this.content);

		this.overlay = document.createElement('div');	//overlay
		this.overlay.className = 'BookingDialogBox_overlay';	
		this.overlay.style.display = 'none';	
		document.body.appendChild(this.overlay);
		
		this.loader = document.createElement('div');	//loading notice
		this.loader.className = 'BookingDialogBox_loader';
		this.loader.style.display = 'none';	
		document.body.appendChild(this.loader);
	},
	
	//show overly and loader before content is loaded	
	loading : function(){	
		//overlay visible
		//this.shim.style.display = 'block';	
		this.overlay.style.display = 'block';	
		
		this.centerDialog();
		this.loader.style.display = 'block';
		
		//fadeElement(this.overlay, 0.6, 300);
		fadeElement(this.loader, 0.6, 500);	
		this.stretchOverlay();
	},

	show : function(url, w, h){				
		if(this.status == 2) 
			return;
		this.status = 1;					//loading		
		this.height = h;
		this.width = w;
		
		// create iframe
	 	var d = new Date();
		this.iframe = document.createElement('iframe'); 	
		this.iframe.name = 'box_iframe' +  d.getTime(); 	//must be unique name
		this.iframe.frameBorder = 0;
		this.iframe.src = url;						// start loading iframe content
	
		this.content.innerHTML = '';				// remove all prevoius content
		this.content.appendChild(this.iframe);		// insert iframe
	 	
	 	this.loading();
	 	
	 	var obj = this;
	 	var loaded = function(){
		 	//user can cancel loading by clicking on the overlay
			if(obj.status == 1){			
				obj.status = 2;							//loaded
				obj.render();				 			
			}
	 	}
		setTimeout(loaded, 800);
	},
	
	close : function(){								//hides dialog 
		if(this.iframe != null){
			this.iframe.src = '';
			this.content.removeChild(this.iframe);
		}
		
		this.overlay.style.display = 'none';		//hide overlay
		//this.shim.style.display = 'none';					//hide shim
		this.loader.style.display = 'none';			//hide loader
		this.layer.style.display = 'none';			//hide layer
		this.status = 0;							//hidden
	},
	
	render : function(){
	
		this.loader.style.display = 'none';			//loader hidden		
				
		//this.iframe.scrolling = 0;		
		this.iframe.className = 'BookingDialogBox_iframe';	
	 	this.iframe.height = this.height;		
	 	this.iframe.width = this.width;		

		var h = this.height + 36;		// iframe_height +  (closeBtn) + (2 * BookingDialogBox_iframe border)
		var w = this.width + 36;		// iframe_width + (closeBtn) + (2 * BookingDialogBox_iframe border)

		this.layer.style.margin =  (-h / 2) + 'px 0 0 ' + (-w / 2) + 'px';
		this.layer.style.height = h + 'px';		
	 	this.layer.style.width = w + 'px';	
		this.layer.style.display = 'block';		//layer visible	
		
		this.centerDialog();
		this.stretchOverlay();
		fadeElement(this.layer, 1, 700);
	},
	
	centerDialog : function(){
		var size = getWindowSize();
		var scroll = getScrollXY();
		var x = size[0] / 2;
		var y = size[1] / 2 + scroll[1];
		
		this.layer.style.left = x + 'px'; 
		this.layer.style.top = y + 'px';
		this.loader.style.left = x + 'px'; 
		this.loader.style.top = y + 'px';
	},
	
	stretchOverlay : function(){
		var docSize = getDocumentSize();
				
		//stretch overlay
		this.overlay.style.width = docSize[0] + 'px';
		this.overlay.style.height = docSize[1]  + 'px';		
	},
	
	resize : function(w, h){
		this.width = w;
		this.height = h;
		this.render();
	}
}

/*********************************************
	WINDOW ONLOAD  				     
*********************************************/

var winOnLoad = window.onload || function(){};
window.onload = function(){

	winOnLoad();
	BookingDialogBox.init();
	
	// events					
	window.onresize = function(){
		BookingDialogBox.stretchOverlay();
		BookingDialogBox.centerDialog();
	}

	document.onkeyup = function(e){
		if (!e) var e = window.event
	
		if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;
	
		//esc is pressed
		if(code == 27)
			BookingDialogBox.close();
	}
	BookingDialogBox.closeBtn.onclick = function(){
		BookingDialogBox.close();
	}
	
	BookingDialogBox.overlay.onclick = function(){
		//user can cancel loading by clicking on the overlay
		if(BookingDialogBox.status != 2)
			BookingDialogBox.close();
	}
}

// element fading, uses time intervals for smooth animation
function fadeElement(el, finalOpacity, duration) {
		
	var fop = finalOpacity != null ? finalOpacity : 1;
	var opacity = 0;
	var dur = duration != null ? duration : 250;
	var timeLeft = dur;
	var last = new Date().getTime();
	
	function f() {
		var t = new Date().getTime();
		var elapsed = t - last;
		last = t;
		
		if(timeLeft <=  elapsed || opacity >= fop){
			el.style.opacity = fop;
			el.style.filter = 'alpha(opacity=' + (fop * 100) + ')';
			return;
		}
		timeLeft -= elapsed;
		opacity = 1 - timeLeft/dur;
		el.style.opacity = opacity;
		el.style.filter = 'alpha(opacity=' + (opacity * 100) + ')';

		setTimeout(f, 33);
 	};
  	f();
}

//vraća punu velicinu dokumenta
function getDocumentSize() {

	var myWidth = document.body.scrollWidth;
	var myHeight = document.body.scrollHeight;
	
	return [ myWidth, myHeight ];
	
	/*
	if (!window.scrollMaxX || window.scrollMaxX==0)
		myWidth = document.body.scrollWidth;
	else
	  	myWidth = window.scrollMaxX;
	
	if (!window.scrollMaxY || window.scrollMaxY==0)
		myWidth = document.body.scrollHeight;
	else
		myHeight = window.scrollMaxY;
		
	return [ myWidth, myHeight ]; */
}

//Window size and scrolling 
//http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [ myWidth, myHeight ];
}

var Page = {
	Start : function(nHotelCode, nLanguageID, nServiceType, sSalesChannel, bPosaljiUpit)
	{
		var strPage = BookingRoot + '/e-booking/' + 'start.aspx'
		strPage += '?HotelCode=' + nHotelCode;
		strPage += '&nLanguageID=' + nLanguageID;
		strPage += '&ServiceType=' + nServiceType;
				
		if (sSalesChannel!=null && sSalesChannel!='')
			strPage += '&SalesChannel=' + sSalesChannel;
			
		if (bPosaljiUpit==true)
			strPage += '&PosaljiUpit=true';
			
		return strPage;
	},		
	ViewCancel : function(sResCode, nServiceType, nLanguageID, sSalesChannel)
	{
		var strPage = BookingRoot + '/e-booking/' + 'viewcancel.aspx';
	
		strPage += '?nResCode=' + sResCode;
		
		if (nServiceType!=null)
			strPage += '&ServiceType=' + nServiceType;
		if (nLanguageID!=null)
			strPage += '&nLanguageID=' + nLanguageID;			
		if (sSalesChannel!=null && sSalesChannel!='')
			strPage += '&SalesChannel=' + sSalesChannel;
			
		return strPage;
	},	
	Offer : function(nHotelCode, nChainCode, nLanguageID, sStartDate, sEndDate, nNumOfAdults, nServiceType, sSalesChannel)
	{
		var strPage = BookingRoot + '/e-booking/' + 'offer.aspx';
		
		if (nHotelCode>0)
			strPage += '?HotelCode=' + nHotelCode;
		else if (nChainCode>0)
			strPage += '?ChainCode=' + nChainCode;
						
		strPage += '&StartDate=' + sStartDate;
		strPage += '&EndDate=' + sEndDate;
		strPage += '&NumOfAdults=' + nNumOfAdults;
		strPage += '&nLanguageID=' + nLanguageID;
		strPage += '&ServiceType=' + nServiceType;
		
		if (sSalesChannel!=null && sSalesChannel!='')
			strPage += '&SalesChannel=' + sSalesChannel;
			
		return strPage;
	},
	Reservation : function(nHotelCode, nLanguageID, nServiceType, sSalesChannel, bPosaljiUpit, sStartDate, sEndDate, nNumOfAdults, nSelectedService)
	{
		var strPage = BookingRoot + '/e-booking/' + 'reservation.aspx';		
		strPage += '?HotelCode=' + nHotelCode;
		strPage += '&nLanguageID=' + nLanguageID;
		strPage += '&ServiceType=' + nServiceType;
					
		if (sSalesChannel!=null && sSalesChannel!='')
			strPage += '&SalesChannel=' + sSalesChannel;
			
		if (bPosaljiUpit==true)
			strPage += '&PosaljiUpit=true';
			
		if (sStartDate!=null && sEndDate!=null && nNumOfAdults!=null && nSelectedService!=null)
		{	 
			 strPage += '&StartDate=' + sStartDate;
			 strPage += '&EndDate=' + sEndDate;
			 strPage += '&NumOfAdults=' + nNumOfAdults;
			 strPage += '&SelectedService=' + nSelectedService;
		}
		
		return strPage;
	}
}

function OpenNextPage(sURL)
{
	if (window!=top)
	{
		//ako je prozor u iframeu, znaci da je otvoren magicbox
		//i da moramo signalizirat glavnom prozoru da napravi resize iframea
		var top_href = window.location.href.split('#')[1];
		if (!top_href)
		{
			//alert("Detektiran pogresni poziv Bookingu. Nedostaje Hash dio!");
			ShowPage(sURL, ShowPageStyle.ReplaceCurrent);			
		}
		else
		{
			// gives a signal to the main window to resize inline popup
			top.location.href = top_href + '#resize';
		
			//sljedeća stranica isto mora znati koji joj je top prozor
			ShowPage(sURL + '#' + top_href, ShowPageStyle.ReplaceCurrent);
		}		
	}
	else
	{	
		var bIsPopup = false;
		
		//detektiranje popupa
		//u FFu se jednostavno pogleda jeli toolbar aktivan ili nije
		//i IEu doduse moramo koristiti HACK sa window.opener kojeg smo kod otvaranja postavili
		//na null ako nije popup
	    bIsPopup = (window.toolbar!=null && window.toolbar.visible==false);
	    bIsPopup = bIsPopup || (window.opener!=null && window.opener!=window.self);
				
		if (bIsPopup)
		{
			//ako se iz popupa otvara sljedeca stranica
			//popup se zatvara, i stranica se otvara u novom prozoru
			window.close();
			ShowPage(sURL, ShowPageStyle.NewWindow);
		}		
		else
		{
			//inače je stranica u normalnom browseru
			ShowPage(sURL, ShowPageStyle.ReplaceCurrent);
		}
	}
}

var ShowPageStyle = {
	MagicBox : 1,
	NewWindow: 2,
	NewPopup: 3,
	ReplaceCurrent: 4
}

function ShowPage(sURL, nStyle, nWidth, nHeight)
{	
	if (nStyle==ShowPageStyle.MagicBox)
	{	
		// zbog komunikacije iframea s glavnim prozorom treba poslati href hotelske stranice u iframe
		// iz hrefa se izdvoji samo dio prije znaka '#' 
		sURL += '#' + top.location.href.split('#').shift();
	
		//ovo je sa boxom
		BookingDialogBox.show(sURL, nWidth, nHeight);		
		
		// poll BookingDialogBox href, if hash matches 'resize', resize inline popup
		var nIntervalID = setInterval(function(){
			var hash = window.location.hash.split('#').pop();
			if(hash == 'resize'){
				clearInterval(nIntervalID);
				BookingDialogBox.resize(900, 600);
				
				//ovo moramo staviti da se zaustavi refreshanje stranice
				window.location.hash = '#none';
			}
		}, 200);		
	}
	else if (nStyle==ShowPageStyle.NewWindow)
	{
		//ovo je u novom tabu
		//postavljamo opener na null da bi kasnije mogli detektirat
		//jeli stranica otvorena u Popupu ili nije	
		var oWin = window.open(sURL, '_blank');
		if (oWin) {
			if (oWin.focus) oWin.focus();
			oWin.opener = null;
		}
		oWin = null;
	}
	else if (nStyle==ShowPageStyle.NewPopup)
	{
		//ovo je u popupu
		page = window.open(sURL, '', 'height=' + nHeight + ',width=' + nWidth + ',toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1');		
	}
	else if (nStyle==ShowPageStyle.ReplaceCurrent)
	{
		//zamjenjujemo trenutačnu
		location.href = sURL;
	}
}

function GetLanguageId(sLanguage)
{	
	var nLanguageID = 2;
	switch (sLanguage)
	{
	    case 'hr':
	        nLanguageID = 1;
	    break;
	    case 'en':
	        nLanguageID = 2;
	    break;
	    case 'fr':
	        nLanguageID = 3;
	    break;
	    case 'it':
	        nLanguageID = 4;
	    break;
	    case 'si':
	        nLanguageID = 5;
	    break;
	    case 'es':
	        nLanguageID = 6;
	    break;
	    case 'de':
	        nLanguageID = 7;
	    break;
	    case 'cz':
	        nLanguageID = 8;
	    break;
	    default:
            nLanguageID = 2;
    }
	
	return nLanguageID;
}

function MakeReservation(nHotelCode, sLanguage, sSalesChannel, bPosaljiUpit, sStartDate, sEndDate, nNumOfAdults, nSelectedService)
{	
	var nLangID = GetLanguageId(sLanguage);
	var sURL = Page.Reservation(nHotelCode, nLangID, 1, sSalesChannel, bPosaljiUpit, sStartDate, sEndDate, nNumOfAdults, nSelectedService);
	ShowPage(sURL, ShowPageStyle.MagicBox, 900, 600);
}

function StartBooking(nHotelCode, sLanguage, sSalesChannel, bPosaljiUpit)
{	
	var nLangID = GetLanguageId(sLanguage);
	var sURL = Page.Start(nHotelCode, nLangID, 1, sSalesChannel, bPosaljiUpit);
	ShowPage(sURL, ShowPageStyle.MagicBox, 540, 440);
}

function ViewReservation(sResCode, sLanguage, sSalesChannel)
{
	var nLangID = GetLanguageId(sLanguage);
	var sURL = Page.ViewCancel(sResCode, 1, nLangID, sSalesChannel);
	ShowPage(sURL, ShowPageStyle.MagicBox, 900, 600);
}