// Width/height of the popup border
var weight = 4;
var woffset_right = weight;
var woffset_left = 0;

var direction;

var isFromDeeplink = false;
var deeplikPath = '../white/';

var Class = { 
	create: function() {
		return function() { this.initialize.apply(this, arguments); }
}};


var PopupControl = Class.create();
PopupControl.prototype = {
	initialize: function(callback, frequency) {
		this.callback = callback;
		this.frequency = frequency;
		this.currentlyExecuting = false;
		this.listcontrols = Array();
		this.selecteditems = Array();
		this.afterinit = false;
		this.target = false;
		this.targetid;
		this.mouseover = false;
		this.leftspace = 0;
		this.mouseover = false;
		this.noiframe = false;
		this.oldclass = null;
		this.autoleftspace = true;
		this.colorShift = 25;
	},
	
	setvalues: function() {
		var args = this.setvalues.arguments;
		this.popupname = args[0];
		this.visible = false;
		this.targetid = args[1];
		this.toggler = args[2];
		//this.writebefore();
		this.noiframe = (!$.browser.msie);
		return this;
	},

	initobjects: function() {
		//this.tbl = document.getElementById(this.popupname+'popuptable');
		this.tbl = $("#"+this.popupname+"popuptable");
//    this.tbl=document.body.insertBefore(this.tbl,document.body.firstChild);
		if (!this.noiframe)
			this.iframe = $("#"+this.popupname+'backgroundframe');
		this.target = $("#"+this.targetid);
		this.afterinit = true;
		this.tbl.popup = this;
		this.tbl.onmouseover = function(){this.popup.mouseover = true};
		this.tbl.onmouseout = function(){this.popup.mouseover = false};
	},
	
	toggle: function() {
		if (this.visible)
			this.hide(this.toggle.arguments.length == 1 ? this.toggle.arguments[0] : null);
		else
			this.show(this.toggle.arguments.length == 1 ? this.toggle.arguments[0] : null);
		if (this.ontoggle)
			this.ontoggle(this.visible);
	},
	
	hide: function(target) {
		if(!this.tbl) return;
		$("#"+this.toggler+" .tabTop").hide();
		$("#"+this.toggler+" .tabLeft").hide();
		$("#"+this.toggler+" .tabRight").hide();
		$("#"+this.toggler+" .labWrapper").removeClass("color9bg");
		if(this.popupname == "p3" || this.popupname == "p4" || this.popupname == "p5")
			$("#"+this.toggler+" .formfield").removeClass("color9bg");
		this.tbl.hide();
		if (!this.noiframe)
			this.iframe.css("display",'none');
		
		if (this.forcevisible) return;
		if (!this.afterinit) return true;
		this.visible = false;
		//this.tbl.style.display = 'none';
		this.tbl.css("display","none");
		if (!this.noiframe)
			this.iframe.css("display","none");
			//this.iframe.style.display = 'none';
		this.target.className = this.oldclass;
		this.oldclass = null;
		if (this.onhide)
			this.onhide();
		document.currentpopup = null;
	},
	
	show: function() {
		if (this.visible)
    {
			document.currentpopup = null;
      this.fixframe();
      return;
    }
		else if (document.currentpopup)
			document.currentpopup.hide(document.currentpopup.toggler);
		if (!this.afterinit) this.initobjects();
		
		document.currentpopup = this;
		pn = this.popupname+"popuptable";
		
		oWidth = 10; oHeight = 10; oTop = document.rounded_corners ? 5 : 4; oLeft = 5; oTabWidth = 4; oCalWidth = -2;
//    parentOffset = $("#"+this.toggler).offset();^M
//    parentOffset = $("#"+this.toggler).offset();
    var tg=$("#"+this.toggler)[0];
    for (var prTop=0,prLeft=0;tg && $(tg).css("position")=='static';prTop+=tg.offsetTop,prLeft+=tg.offsetLeft,tg=tg.offsetParent)

		parentWidth = $("#"+this.toggler+" .formfield").outerWidth();
		parentInnerWidth = $("#"+this.toggler+" .formfield").innerWidth();
		if(parentInnerWidth == 0) parentInnerWidth = parentWidth;
		parentHeight = $("#"+this.toggler).height();
		
		$("#"+this.toggler+" .tabTop").show().width(parentWidth + oWidth);
		$("#"+this.toggler+" .tabLeft").show().height(parentHeight).width(oTabWidth);
		$("#"+this.toggler+" .tabRight").show().height(parentHeight).width(oTabWidth);
    if (!this.noiframe)
      prLeft=prLeft+1;

		$("#"+this.toggler+" .tabTop").css("top", (prTop - oTop)+"px").css("left", (prLeft - oLeft)+"px");
		$("#"+this.toggler+" .tabLeft").css("top", prTop+"px").css("left", (prLeft - oLeft)+"px");
		$("#"+this.toggler+" .tabRight").css("top", prTop+"px").css("left", (prLeft + parentWidth)+"px");
    
		$("#"+this.toggler+" .labWrapper").addClass("color9bg");
	  this.prTop=prTop+oHeight+parentHeight;
		pn = this.popupname+"popuptable";
		tp2 = $("#"+pn+" .tp2");
		$("#"+pn+" .tp1").width(parentInnerWidth + oWidth);
		tp2.width(parentInnerWidth + oWidth);
		nw = this.tbl.innerWidth() - parentInnerWidth - oWidth - 6;
		if(this.popupname == "p3" || this.popupname == "p4" || this.popupname == "p5") {
			$("#"+pn+" .tp1").width(parentInnerWidth + oWidth + oCalWidth);
			tp2.width(parentInnerWidth + oWidth + oCalWidth);
			nw += -oCalWidth;
			$("#"+this.toggler+" .formfield").addClass("color9bg");
		}
		if(this.popupname == "p5") {
			$("#"+this.toggler+" .tabLeft").css("width", "5px");
		}
		
		if(ibe_searchform == "main" && this.popupname.indexOf("p5") == -1 || this.popupname.indexOf("_s4") != -1) {
			if(document.rounded_corners) {
				tp2.css("margin-right","5px");
				$("#"+pn+" .tp3").width($("#"+pn+" .tp3").width() - 4);
				$("#"+pn+" .tp3").css("border-right-width","2px").css("margin-right","3px");
				$("#"+pn+" .tp4").width($("#"+pn+" .tp4").width() - 2);
				$("#"+pn+" .tp4").css("margin-right","2px");
				$("#"+pn+" .tp5").width($("#"+pn+" .tp5").width() - 1);
				$("#"+pn+" .tp5").height(2);
				$("#"+pn+" .tp5").css("margin-right","1px");
			}
			tp2.css("border-right-width", nw + "px");
			this.tblLeft=(prLeft - oLeft);
      this.tbl.show().css("left",(this.tblLeft)+"px");
		}
		else {
			if(document.rounded_corners) {
				tp2.css("margin-left","5px");
				$("#"+pn+" .tp3").width($("#"+pn+" .tp3").width() - 4);
				$("#"+pn+" .tp3").css("border-left-width","2px").css("margin-left","3px");
				$("#"+pn+" .tp4").width($("#"+pn+" .tp4").width() - 2);
				$("#"+pn+" .tp4").css("margin-left","2px");
				$("#"+pn+" .tp5").width($("#"+pn+" .tp5").width() - 1);
				$("#"+pn+" .tp5").height(2);
				$("#"+pn+" .tp5").css("margin-left","1px");
			}
			tp2.css("border-left-width", nw + "px");
			$("#"+pn+" .tp1").addClass("fright");
			if(this.popupname == "p1" || this.popupname == "p2")
      {
        this.tblLeft=(prLeft - (this.tbl.outerWidth()) + parentInnerWidth + oWidth/2 + 2);       
				this.tbl.show().css("left",this.tblLeft+"px");
        
      }
			else
      {
        this.tblLeft=(prLeft - (this.tbl.outerWidth()) + parentInnerWidth + oWidth/2);
				this.tbl.show().css("left",this.tblLeft+"px");
      }
        
		}
//    this.tblLeft=Math.round(this.tblLeft);
//        alert();            
		
		this.visible = true;
		if (!this.noiframe) {
			this.iframe.css("display",'');
			this.fixframe();
		}
	},
	
	fixframe: function() {
		if (this.noiframe) return;
		if (!this.visible) return;
//    this.iframe:
		
		
		toff = this.tbl.offset({ scroll: false });
		twid = this.tbl.innerWidth();
		this.iframe.css({
			position: "absolute",
      top: this.prTop + "px",
			left: this.tblLeft,
			width: (this.tbl.innerWidth()-1) + "px",
			height: (this.tbl.innerHeight() - 15) + "px"
		});
  },
	
	focus: function() {
		this.show();
	},
	
	blur: function() {
		if (!this.mouseover) {
			this.hide();
			this.mouseover = false;
		}
	}
}
function d2h(d) { d = d.toString(16); if(d.length < 2) return "0"+d; else return d; }
function h2d(h) {return parseInt(h,16);} 

function ep_setBgColor (el, mode, source) {
	var colors = $(source).html().split("|");
	if(mode == "over")
		el.css("background-color",colors[1]);
	else
		el.css("background-color",colors[0]);
}

function ep_calculateColor(id) {
	var colorShift = 25;
	var cout = "";
	var cover = "";
	var target = $(id);
	var curCol = target.css("background-color");
	if(!curCol || curCol == "transparent") return false;
	if(curCol.indexOf("#") != -1) { // Color as Hex value
		curCol = curCol.substring(1);
		
		r = h2d(curCol.substring(0,2));
		g = h2d(curCol.substring(2,4));
		b = h2d(curCol.substring(4,6));
		cout = "#"+d2h(parseInt(r))+d2h(parseInt(g))+d2h(parseInt(b));
		r = h2d(curCol.substring(0,2)) - colorShift;
		g = h2d(curCol.substring(2,4)) - colorShift;
		b = h2d(curCol.substring(4,6)) - colorShift;
	}
	else if(curCol.indexOf("rgb") != -1) { // Color as RGB value
		curCol = curCol.substring(4, curCol.length - 1);
		
		colors = curCol.split(",");
		r = parseInt(colors[0].replace(/\s*/,""));
		g = parseInt(colors[1].replace(/\s*/,""));
		b = parseInt(colors[2].replace(/\s*/,""));
		cout = "#"+d2h(parseInt(r))+d2h(parseInt(g))+d2h(parseInt(b));
		r = parseInt(colors[0].replace(/\s*/,"")) - colorShift;
		g = parseInt(colors[1].replace(/\s*/,"")) - colorShift;
		b = parseInt(colors[2].replace(/\s*/,"")) - colorShift;
	}
	else
		return false;
	
	if(r<0) r += 2 * colorShift;
	if(g<0) g += 2 * colorShift;
	if(b<0) b += 2 * colorShift;
	
	cover = "#"+d2h(r)+d2h(g)+d2h(b);
	target.html(cout+"|"+cover);
}

function ep_setEmptyDestination() {
	document.dstlist.selectapt(null);
	$('#desCode').val('');
	document.dstlist.deleteselection(document.dstlist.lastselection_h, "h");
	document.dstlistpopup.hide('dstWrapper');
}

function ep_hideAll() {
	if(document.deplistpopup) document.deplistpopup.hide('depWrapper');
	if(document.dstlistpopup) document.dstlistpopup.hide('dstWrapper');
	if(document.kidsagepopup) document.kidsagepopup.hide('kidaltWrapper');
	if(document.startdatepopup) document.startdatepopup.hide("depCalWrapper");
	if(document.enddatepopup) document.enddatepopup.hide("retCalWrapper");
}

function ep_listOver(el, cls_list, price) {
	if($(el).attr("mover") == "false") return;
	var classes = cls_list.split(",");
	var cc = null;
	var le = classes.length;
	for(var i = 0; i < le; i++) {
		if(price)
			$(el).find(".price").toggleClass(classes[i]);
		else
			$(el).toggleClass(classes[i]);
	}
}

function ep_toggleWizardLink(el, mode) {
	var colors = $("#store_wizlink_color").html().split("|");
	if(mode == "over") el.css("color",colors[1]);
	else el.css("color",colors[0]);
}

function ep_filterReg() {
	document.getElementById("fihtl").value = '';  //clicked on htlRegion, then empty htlName
	window.document.forms['form_filters'].submit();
}

function ep_filterHtl() {
	document.getElementById("fireg").value = '';  //clicked on htlName, then empty htlRegion
	window.document.forms['form_filters'].submit();
}

function hotelinfo(purl,path){
  window.open(purl,'hotelinfowindow','channelmode=0,directories=0,fullscreen=0,location=0,menubar=0,resizable=1,scrollbars=0,status=1,titlebar=0,toolbar=0,width=700,height=680')
}

function ep_validateOffer(id) {
	if($('#offer_'+id).attr("status") != "BA") {
		$('#hide_'+id).css("display","block");
		return validate(id); // ajax.js
	}
}

function ep_getOrderBy(ordnr) {
	var act_url = document.URL;
	var regex = /ob=./g;          //pattern
	if ( regex.test(act_url) )   //param "ob" exists
		act_url = act_url.replace(regex, 'ob=' + ordnr);
	else
		act_url += '&ob=' + ordnr;
	window.location.href = act_url;
}