/*
Mano popup window
*/
var popupId = 'popupWindow';
var popupContainer = 'popupContainer';
var fader_id = 'popupFader';

function drawPopupWindow(title, src)
{
	var htmlData = '';
	var xPos = getPositionX();
	var yPos = getPositionY();

	htmlData += '<div id="popupWindow">';
	htmlData += '<div class="popupTop"></div>';
	htmlData += '<table class="popupFrame"><tr>';
	htmlData += '<td class="lSide"></td>';
	htmlData += '<td class="popupContent">';
	htmlData += '<div class="popupTitle">';
	htmlData += '<a href="javascript:void(0);" onclick="closePopup();" class="close"></a>';
	htmlData += '<span class="pTitle">' + title + '</span>';
	htmlData += '</div>';
	htmlData += '<div class="block">';
	htmlData += '<iframe src="' + src + '" class="popupScroll" id="popupScroll" frameborder="0">';
	htmlData += '</iframe>';
	htmlData += '<div class="closeBottom">';
	htmlData += '<a href="javascript:void(0);" onclick="closePopup();" class="mediumBlueButton mirror">' + _close_popup + '</a>';
	htmlData += '</div>';
	htmlData += '</div>';
	htmlData += '</td>';
	htmlData += '<td class="rSide"></td>';
	htmlData += '</tr></table>';
	htmlData += '<div class="popupBottom"></div>';
	htmlData += '</div>';

	var obj = document.getElementById(popupContainer);
	if (!obj)
		return;
	obj.innerHTML = htmlData;

	
	var t = document.getElementById(popupId);
	t.style.position = "absolute";
	t.style.left = xPos;
	t.style.top = yPos;
	t.style.zIndex = 1000;
}

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
function getPageScrolling() {

	var xScroll, yScroll;

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

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

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function _getPageSize() {

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		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;

//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth;
		} else {
			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;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// For small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = xScroll;
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

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


function getPositionX()
{
	return ((document.body.clientWidth-519)/2) +"px";
}

function getPositionY()
{
	var scroll = getPageScrolling();
	return 100 + scroll[1] + "px";
}

/*-------------------------------------
page masking object
Liran Oz - 2008
--------------------------------------*/
function pageMask(_opacity, _speed, _container, _callback)
{
	var cur_opacity = 0;
	var speed = _speed;
	var step = 10;
	var tar_opacity = _opacity;
	var container_id = _container;
	var mask_obj = null;
	var width;
	var height;
	var cycle_flag = false;
	var interval = null;
	var wait = 30;
	var callback = _callback;
	var select_box = null;
	var objects = null;
	var embeds = null;
	var done_fading = false;
	var inner_working = false;
	var background_color = 'black';


	function setOpacity(opc)
	{
		mask_obj.style.opacity = opc/100;
		mask_obj.style.filter = 'alpha(opacity=' + opc + ')';
	}


	function createObj()
	{
		//create new element
		var sz = _getPageSize();		//Thank you lightbox
		var cnt = document.getElementById(_container);
		if (!cnt) {
			return false;
		}


		mask_obj = document.createElement('div');
		mask_obj.style.width = sz[2] + "px";
		mask_obj.style.height = sz[1] + "px";
		mask_obj.style.position = "absolute";
		mask_obj.style.left = "0px";
		mask_obj.style.top = "0px";
		mask_obj.style.zIndex = 100;
		mask_obj.style.backgroundColor = background_color;

		cnt.appendChild(mask_obj);
	}

	function removeObj()
	{
		//create new element
		var sz = _getPageSize();		//Thank you lightbox
		var cnt = document.getElementById(container_id);
		if (!cnt) {
			return false;
		}
		cnt.removeChild(mask_obj);
		mask_obj = null;
	}

	this.getMaskObject = function()
	{
		return mask_obj;
	}

	this.setBackgroundColor = function(col)
	{
		background_color = col;
	}


	function cycleOpacity()
	{
		if (mask_obj == null) {
			createObj();
			setOpacity(cur_opacity);
		}

		done_fading = false;
		cur_opacity += step;
		if (cur_opacity >= tar_opacity) {		//no more cyclig
			inner_working = true;
			cur_opacity = tar_opacity;
			cycle_flag = false;
			setOpacity(cur_opacity);
			//stop cycling
			interval = null;
			//on complete, run callback
			done_fading = true;
			callback();
			inner_working = false;
			return true;
		}
		else if (cur_opacity <= 0) {
			cur_opacity = 0;
			cycle_flag = false;
			setOpacity(cur_opacity);
			//stop cycling
			interval = null;
			//bring back all select boxes (if any)
			if (select_box != null) {
				for (var i=0; i<select_box.length; i++) {
					select_box[i].style.visibility = "visible";
				}
			}

			if (objects != null) {
				for (var i=0; i<objects.length; i++) {
					objects[i].style.visibility = "visible";
				}
			}

			if (embeds != null) {
				for (var i=0; i<embeds.length; i++) {
					embeds[i].style.visibility = "visible";
				}
			}


			removeObj();
			return true;
		}
		//cycling is needed...
		setOpacity(cur_opacity);
		interval = setTimeout(cycleOpacity, wait);

	}


	this.fadeIn = function()
	{
		if (done_fading) {
			callback();
			return;
		}
		//hide all select box (thank you internet explorer..)
		var selects = document.getElementsByTagName('select');

		for (var i=0, j=0; i<selects.length; i++) {

			if (selects[i].style.visibility != "hidden") {
				if (select_box == null) {
					select_box = new Array();
				}
				select_box[j++] = selects[i];
				selects[i].style.visibility = "hidden";

			}
		}

		var flashes = document.getElementsByTagName('object');

		for (var i=0, j=0; i<flashes.length; i++) {

			if (flashes[i].style.visibility != "hidden") {
				if (objects == null) {
					objects = new Array();
				}
				objects[j++] = flashes[i];
				objects[i].style.visibility = "hidden";
			}
		}

		var flashes = document.getElementsByTagName('embed');

		for (var i=0, j=0; i<flashes.length; i++) {

			if (flashes[i].style.visibility != "hidden") {
				if (embeds == null) {
					embeds = new Array();
				}
				embeds[j++] = flashes[i];
				embeds[i].style.visibility = "hidden";
			}
		}



		step = speed / tar_opacity;
		interval = setTimeout(cycleOpacity, wait);
		return true;
	}


	this.fadeOut = function()
	{
		if (mask_obj == null) {
			return false;
		}
		//have we finished fadeing in?
		if (interval != null) {
			clearTimeout(interval);
		}
		step = -(speed / tar_opacity);
		interval = setTimeout(cycleOpacity, wait);
	}
}

var fader = null;
var popupTitle = null;
var popupSrc = null;

function openPopup(title, src)
{
	popupTitle = title;
	popupSrc = src;

	if (fader == null) {
		fader = new pageMask(20, 200, fader_id, openRealPopup);
	}
	fader.fadeIn();
}

function openRealPopup()
{
	drawPopupWindow(popupTitle, popupSrc);
}

function closePopup()
{
	var obj = document.getElementById(popupContainer);
	if (!obj)
		return;
	obj.innerHTML = '';
	fader.fadeOut();
}

function getTitleByid(id,type)
{
	var mtitle=" ";
	
	_baseLang=_baseLang.replace("/develop/develop/","/develop/");
	
	if(type=="show_room")
  	var url = _baseLang+"/ajax_gettitle.php?action=get_title_room&prdid="+id;
  else
  	var url = _baseLang+"/ajax_gettitle.php?action=get_title_area&prdid="+id;
	var xml = LoadXML(url);
	if(xml != null && xml.getElementsByTagName('itemtitle')[0] && xml.getElementsByTagName('itemtitle')[0].firstChild && xml.getElementsByTagName('itemtitle')[0].firstChild.data)
		{
			var mtitle = xml.getElementsByTagName('itemtitle')[0].firstChild.data;
			if(mtitle!="empty")
			{
				return mtitle;
			}
			else
			{
				mtitle=" ";
				
			}
			
		}
		return mtitle;
}

/*
Compatibility functions
*/

function openFrame(page, id)
{
	var page;
	var type=page;
	var title = null;
	_baseLang=_baseLang.replace("/develop/develop/","/develop/");
	switch (page) {

		case 'show_cruise':

		break;

		case 'show_room':
			page = _baseLang+'/popupRoom.php?roomId=' + id;
			title = '';
			break;

		case 'show_dest':

		break;

		case 'show_video':

		break;

		case 'show_tour':

		break;

		case 'show_area':
			page = _baseLang+'/popupArea.php?areaId=' + id;
			title = '';
			break;

		case 'show_deal':

		break;

		case 'show_terms':

		break;

	}
	title=getTitleByid(id,type);
	openPopup(title, page);

}
