// ===================================================================
// HISTORY
// ------------------------------------------------------------------
/* 
USAGE:
1) Include the source file in your main document which contains the IFRAME
   tags. Make sure each iframe has a unique "ID" attribute. For best browser
   compatability, also include a "NAME" attribute in the IFRAME tag that
   has the same value as the "ID" attribute.
   
2) In the document content of each IFRAME which will be draggable, , do two
	things:
	a) Include the dragiframe.js file in the source
	b) add this code to the <BODY> tag:
   onLoad="addHandle(document.getElementById('toolbar'), window);"
   Where 'toolbar' is the ID of an element on the page which should be the 'handle'
   by which the IFRAME should be dragged (like a toolbar at the top).
   If you want the IFRAME to be draggable by clicking anywhere in the IFRAME
   document, use:
   onLoad="addHandle(document.getElementsByTagName('body').item(0), window);"
   
   NOTE: The code will automatically look up the window.parent tree to find a
   parent document with draggable iframes enabled. It will attach itself to the
   first document it finds, allowing it to work within a framed environment.

In your parent document (containing the IFRAMEs), you may set a couple of options:

// Set to true to always bring the selected IFRAME to the "top" of the zIndex.
// Defaults to false
bringSelectedIframeToTop(true|false);

// Set to true to allow IFRAME objects to be dragged off the screen. This may
// make the handle be no longer reachable by the mouse, causing the IFRAME to
// be stranded.
// Defaults to false
allowDragOffScreen(true|false);

// You may manually set this variable to define the highest zIndex used in 
// your main document. This is used to determine what zIndex to give an IFRAME
// if it is selected and "bringSelectedIframeToTop" is set to true.
// Defaults to 99.
DIF_highestZIndex=4;

NOTES:
1) If you have defined onmousedown or onmouseup event handlers for your "handle"
   object in the IFRAME, they will be over-written.
2) If you have defined an onmousemove handler in either the main document or
   the IFRAME document, they will be over-written.
3) All <IFRAME> objects must have an ID!
*/ 

// Variables used for "Draggable IFrame" (DIF) functions
var DIF_dragging=false;
var DIF_iframeBeingDragged="";
var DIF_iframeObjects=new Object();
var DIF_iframeWindows=new Object();
var DIF_iframeMouseDownLeft = new Object();
var DIF_iframeMouseDownTop = new Object();
var DIF_pageMouseDownLeft = new Object();
var DIF_pageMouseDownTop = new Object();
var DIF_handles = new Object();
var DIF_highestZIndex=99;
var DIF_raiseSelectedIframe=false;
var DIF_allowDragOffScreen=false;

// Set to true to always raise the dragged iframe to top zIndex
function bringSelectedIframeToTop(val) {
	DIF_raiseSelectedIframe = val;
	}
	
// Set to try to allow iframes to be dragged off the top/left of the document
function allowDragOffScreen(val) {
	DIF_allowDragOffScreen=val;
	}

// Method to be used by iframe content document to specify what object can be draggable in the window
function addHandle(o, win) {
	if (arguments.length==2 && win==window) {
		// JS is included in the iframe who has a handle, search up the chain to find a parent window that this one is dragged in
		var p = win;
		while (p=p.parent) {
//			if (p.addHandle) { p.addHandle(o,win,true); return; }
			if (p==win.top) { return; } // Already reached the top, stop looking
			}
		return; // If it reaches here, there is no parent with the addHandle function defined, so this frame can't be dragged!
		}
	var topRef=win;
	var topRefStr = "window";
	while (topRef.parent && topRef.parent!=window) {
		topRef = topRef.parent;
		topRefStr = topRefStr + ".parent";
		}
	// Add handlers to child window
	if (typeof(win.DIF_mainHandlersAdded)=="undefined" || !win.DIF_mainHandlersAdded) {
		// This is done in a funky way to make Netscape happy
		with (win) { 
			eval("function OnMouseDownHandler(evt) { if(typeof(evt)=='undefined'){evt=event;}"+topRefStr+".parent.DIF_begindrag(evt, "+topRefStr+") }");
			eval("document.onmousedown = OnMouseDownHandler;");
			eval("function OnMouseUpHandler(evt) { if(typeof(evt)=='undefined'){evt=event;}"+topRefStr+".parent.DIF_enddrag(evt, "+topRefStr+") }");
			eval("document.onmouseup = OnMouseUpHandler;");
			eval("function OnMouseMoveHandler(evt) { if(typeof(evt)=='undefined'){evt=event;}"+topRefStr+".parent.DIF_iframemove(evt, "+topRefStr+") }");
			eval("document.onmousemove = OnMouseMoveHandler;");
			win.DIF_handlersAdded = true;
			win.DIF_mainHandlersAdded = true;
			}
		}
	// Add handler to this window
	if (typeof(window.DIF_handlersAdded)!="undefined" || !window.DIF_handlersAdded) {
		eval("function OnMouseMoveHandler(evt) { if(typeof(evt)=='undefined'){evt=event;}DIF_mouseMove(evt, window) }");
		eval("document.onmousemove = OnMouseMoveHandler;");
		window.DIF_handlersAdded=true;
		}
	o.style.cursor="move";
	var name = DIF_getIframeId(topRef);
	if (DIF_handles[name]==null) {
		// Initialize relative positions for mouse down events
		DIF_handles[name] = new Array();
		DIF_iframeMouseDownLeft[name] = 0;
		DIF_iframeMouseDownTop[name] = 0;
		DIF_pageMouseDownLeft[name] = 0;
		DIF_pageMouseDownTop[name] = 0;
		}
	DIF_handles[name][DIF_handles[name].length] = o;
	}

// Generalized function to get position of an event (like mousedown, mousemove, etc)
function DIF_getEventPosition(evt) {
	var pos=new Object();
	pos.x=0;
	pos.y=0;
	if (!evt) {
		evt = window.event;
		}
	if (typeof(evt.pageX) == 'number') {
		pos.x = evt.pageX;
		pos.y = evt.pageY;
	}
	else {
		pos.x = evt.clientX;
		pos.y = evt.clientY;
		if (!top.opera) {
			if ((!window.document.compatMode) || (window.document.compatMode == 'BackCompat')) {
				pos.x += window.document.body.scrollLeft;
				pos.y += window.document.body.scrollTop;
			}
			else {
				pos.x += window.document.documentElement.scrollLeft;
				pos.y += window.document.documentElement.scrollTop;
			}
		}
	}
	return pos;
}

// Gets the ID of a frame given a reference to a window object.
// Also stores a reference to the IFRAME object and it's window object
function DIF_getIframeId(win) {
	// Loop through the window's IFRAME objects looking for a matching window object
	var iframes = document.getElementsByTagName("IFRAME");
	for (var i=0; i<iframes.length; i++) {
		var o = iframes.item(i);
		var w = null;
		if (o.contentWindow) {
			// For IE5.5 and IE6
			w = o.contentWindow;
			}
		else if (window.frames && window.frames[o.id].window) {
			w = window.frames[o.id];
			}
		if (w == win) {
			DIF_iframeWindows[o.id] = win;
			DIF_iframeObjects[o.id] = o;
			return o.id; 
			}
		}
	return null;
	}

// Gets the page x, y coordinates of the iframe (or any object)
function DIF_getObjectXY(o) {
	var res = new Object();
	res.x=0; res.y=0;
	if (o != null) {
		res.x = o.style.left.substring(0,o.style.left.indexOf("px"));
		res.y = o.style.top.substring(0,o.style.top.indexOf("px"));
		}
	return res;
	}

// Function to get the src element clicked for non-IE browsers
function getSrcElement(e) {
	var tgt = e.target;
	while (tgt.nodeType != 1) { tgt = tgt.parentNode; }
	return tgt;
	}

// Check if object clicked is a 'handle' - walk up the node tree if required
function isHandleClicked(handle, objectClicked) {
	if (handle==objectClicked) { return true; }
	while (objectClicked.parentNode != null) {
		if (objectClicked==handle) {
			return true;
			}
		objectClicked = objectClicked.parentNode;
		}
	return false;
	}
	
// Called when user clicks an iframe that has a handle in it to begin dragging
function DIF_begindrag(e, win) {
	// Get the IFRAME ID that was clicked on
	var iframename = DIF_getIframeId(win);
	if (iframename==null) { return; }
	// Make sure that this IFRAME has a handle and that the handle was clicked
	if (DIF_handles[iframename]==null || DIF_handles[iframename].length<1) {
		return;
		}
	var isHandle = false;
	var t = e.srcElement || getSrcElement(e);
	for (var i=0; i<DIF_handles[iframename].length; i++) {
		if (isHandleClicked(DIF_handles[iframename][i],t)) {
			isHandle=true;
			break;
			}
		}
	if (!isHandle) { return false; }
	DIF_iframeBeingDragged = iframename;
	if (DIF_raiseSelectedIframe) {
		DIF_iframeObjects[DIF_iframeBeingDragged].style.zIndex=DIF_highestZIndex++;
		}
	DIF_dragging=true;
	var pos=DIF_getEventPosition(e);
	DIF_iframeMouseDownLeft[DIF_iframeBeingDragged] = pos.x;
	DIF_iframeMouseDownTop[DIF_iframeBeingDragged] = pos.y;
	var o = DIF_getObjectXY(DIF_iframeObjects[DIF_iframeBeingDragged]);
	DIF_pageMouseDownLeft[DIF_iframeBeingDragged] = o.x - 0 + pos.x;
	DIF_pageMouseDownTop[DIF_iframeBeingDragged] = o.y -0 + pos.y;
	}

// Called when mouse button is released after dragging an iframe
function DIF_enddrag(e) {
	DIF_dragging=false;
	DIF_iframeBeingDragged="";
	}

// Called when mouse moves in the main window
function DIF_mouseMove(e) {
	if (DIF_dragging) {
		var pos = DIF_getEventPosition(e);
		DIF_drag(pos.x - DIF_pageMouseDownLeft[DIF_iframeBeingDragged] , pos.y - DIF_pageMouseDownTop[DIF_iframeBeingDragged]);
		}
	}

// Called when mouse moves in the IFRAME window
function DIF_iframemove(e) {
	if (DIF_dragging) {
		var pos = DIF_getEventPosition(e);
		DIF_drag(pos.x - DIF_iframeMouseDownLeft[DIF_iframeBeingDragged] , pos.y - DIF_iframeMouseDownTop[DIF_iframeBeingDragged]);
		}
	}

// Function which actually moves of the iframe object on the screen
function DIF_drag(x,y) {
	var o = DIF_getObjectXY(DIF_iframeObjects[DIF_iframeBeingDragged]);
	// Don't drag it off the top or left of the screen?
	var newPositionX = o.x-0+x;
	var newPositionY = o.y-0+y;
	if (!DIF_allowDragOffScreen) {
		if (newPositionX < 0) { newPositionX=0; }
		if (newPositionY < 0) { newPositionY=0; }
		}
		DIF_iframeObjects[DIF_iframeBeingDragged].style.left = newPositionX + "px";
		DIF_iframeObjects[DIF_iframeBeingDragged].style.top  = newPositionY + "px";
	if(newPositionX !=0) {
		DIF_pageMouseDownLeft[DIF_iframeBeingDragged] += x; }
	if(newPositionY !=0) {
		DIF_pageMouseDownTop[DIF_iframeBeingDragged] += y; }
	}

function handleResize()
{
var lwinW = 630, lwinH = 460;

//		alert(document.body.leftMargin);
//		alert(document.body.topMargin);

if (parseInt(navigator.appVersion)>3)
	{
	if (navigator.appName=="Netscape")
		{
  		lwinW = window.innerWidth;
 		lwinH = window.innerHeight;
 		}
 	if (navigator.appName.indexOf("Microsoft")!=-1) 
		{
  		lwinW = document.body.offsetWidth;
  		lwinH = document.body.offsetHeight;
 		}
	}

if (window.innerWidth) {
lwinW=window.innerWidth;
}
else if (document.documentElement && document.documentElement.clientWidth) {
lwinW=document.documentElement.clientWidth;
}
else if (document.body) {
lwinW=document.body.clientWidth;
}
 	if (navigator.appName.indexOf("Microsoft")!=-1) 
		{
  		lwinW = lwinW+20;
 		}
	else
		{
  		lwinW = lwinW+2;
 		}

	if(document.getElementById('div_arrow'))
		{
		document.getElementById('div_arrow').src = 'http://www.hotel-apps.co.uk/apps/images/left_arrow.png';
		}
	
if(glob_x_from=='centre')
	{
	jsfx_glob_sx=(lwinW)/2-(310/2)-10+glob_sx;
	}
if(glob_x_from=='left')
	{
	jsfx_glob_sx=glob_sx;
	}
if(glob_x_from=='right')
	{
	jsfx_glob_sx=lwinW-310-20-glob_sx;
	}
}

function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent) 
	{
do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
	} while (obj = obj.offsetParent);
	return [curleft,curtop];
	}
}

function ha_toggleBookingForm()
{
	if(document.getElementById('PopUpDiv').style.visibility=='hidden')
		{
		document.getElementById('PopUpDiv').style.visibility='visible';
		document.getElementById('div_arrow').style.visibility='visible';
		document.getElementById('div_eye').style.visibility='visible';
//		document.getElementById('ha_mv_div').style.visibility='visible';
//		document.getElementById('ha_hd_div').style.visibility='visible';
		}	
	else
		{
		document.getElementById('PopUpDiv').style.visibility='hidden';
		document.getElementById('div_arrow').style.visibility='hidden';
		document.getElementById('div_eye').style.visibility='hidden';
//		document.getElementById('ha_mv_div').style.visibility='hidden';
//		document.getElementById('ha_hd_div').style.visibility='hidden';
		}
}

var winW = 630, winH = 460;

var ns = (navigator.appName.indexOf("Netscape") != -1);
var happs_d = document;
var pX, pY;
var firstTime = 0;
var glob_sy;
var glob_sx;
var menu_height = 980;
var dishes_height = 500;
var first_2 = 0;
var jsfx_glob_sy = 0;
var jsfx_glob_sx = 0;
var last_jsfx_glob_sx = 0;
var prev_jsfx_glob_sx;
var first_2 = 0;
var glob_x_from;

function HotelAppsInit(id, x_from, sx, y_from, sy, behaviour, show)
{
	if(first_2==0 && show != 'hide')
		{
		document.getElementById('PopUpDiv').style.visibility='visible';
		document.getElementById('ha_mv_div').style.visibility='visible';
		document.getElementById('ha_hd_div').style.visibility='visible';
		}
		
	if(first_2==0)
		{
	
if (window.innerWidth) {
//alert('1');
winW=window.innerWidth;
}
else if (document.documentElement && document.documentElement.clientWidth) {
//alert('2');
winW=document.documentElement.clientWidth;
}
else if (document.body) {
//alert('3');
winW=document.body.clientWidth;
}
if (window.innerHeight) {
winH=window.innerHeight;
}
else if (document.documentElement && document.documentElement.clientHeight) {
winH=document.documentElement.clientHeight;
}
else if (document.body) {
winH=document.body.clientHeight;
}
	
	
 	if (navigator.appName.indexOf("Microsoft")!=-1) 
		{
  		winW = winW+20;
 		}
	else
		{
  		winW = winW+2;
 		}		
		if(y_from=='centre')
			{
			sy = winH/2-90;
			}

		glob_x_from = x_from;
		if(x_from=='centre')
			{
			glob_sx=sx;
			sx=(winW)/2-(310/2)-10+sx;
			prev_jsfx_glob_sx = sx;
			}
		if(x_from=='left')
			{
			glob_sx=sx;
			sx=sx;
			prev_jsfx_glob_sx = sx;
			}
		if(x_from=='right')
			{
			glob_sx=sx;
			sx=winW-310-20-sx;
			prev_jsfx_glob_sx = sx;
			}
		first_2 = 1;
		}
	
	var el=happs_d.getElementById?happs_d.getElementById(id):happs_d.all?happs_d.all[id]:happs_d.layers[id];
	var px = document.layers ? "" : "px";
	window[id + "_obj"] = el;
	if(happs_d.layers)el.style=el;
	el.cx = el.sx = sx;el.cy = el.sy = sy;

	el.sP=function(x,y)
		{
		this.style.left=x+px;this.style.top=y+px;
		};

	el.floatIt=function()
	{
		if(jsfx_glob_sy > 0)
			{
			this.sy = jsfx_glob_sy;
			jsfx_glob_sy = 0;
			}
		if(jsfx_glob_sx != last_jsfx_glob_sx)
			{
			this.sx = jsfx_glob_sx;
			last_jsfx_glob_sx = jsfx_glob_sx;
			}
			
		var pX, pY;
		pX = (this.sx >= 0) ? 0 : ns ? innerWidth : 
		document.documentElement && document.documentElement.clientWidth ? 
		document.documentElement.clientWidth : document.body.clientWidth;
		pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? 
		document.documentElement.scrollTop : document.body.scrollTop;
		if(this.sy<0) 
		pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ? 
		document.documentElement.clientHeight : document.body.clientHeight;
		this.cx += (pX + this.sx - this.cx)/8;
if(behaviour=='float')
		{
		this.cy += (pY + this.sy - this.cy)/8;
//	alert(this.cy);
//	alert(document.documentElement.margin-top);
	
		}
		this.sP(this.cx, this.cy);
if(behaviour!='fixed')
		{
		setTimeout(this.id + "_obj.floatIt()", 30);
		}
	}

//		document.getElementById('PopUpDiv').style.visibility='hidden';
//		document.getElementById('div_arrow').style.visibility='hidden';
//		document.getElementById('div_eye').style.visibility='hidden';

	return el;
}


function Browser() {
 
  var ua, s, i;
 
  this.isIE    = false;
  this.isNS    = false;
  this.version = null;
 
  ua = navigator.userAgent;
 
  s = "MSIE";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isIE = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
 
  s = "Netscape6/";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = parseFloat(ua.substr(i + s.length));
    return;
  }
 
  // Treat any other "Gecko" browser as NS 6.1.
 
  s = "Gecko";
  if ((i = ua.indexOf(s)) >= 0) {
    this.isNS = true;
    this.version = 6.1;
    return;
  }
}
 
var browser = new Browser();
 
// Global object to hold drag information.
 
var dragObj = new Object();
dragObj.zIndex = 0;
 
function dragStart(event, id) {
 
	
  var el;
  var x, y;
 
  // If an element id was given, find it. Otherwise use the element being
  // clicked on.
 
  if (id)
    dragObj.elNode = document.getElementById(id);
  else {
    if (browser.isIE)
      dragObj.elNode = window.event.srcElement;
    if (browser.isNS)
      dragObj.elNode = event.target;
 
    // If this is a text node, use its parent element.
 
    if (dragObj.elNode.nodeType == 3)
      dragObj.elNode = dragObj.elNode.parentNode;
  }
 
  // Get cursor position with respect to the page.
 
  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }
 
  // Save starting positions of cursor and element.
 
  dragObj.cursorStartX = x;
  dragObj.cursorStartY = y;
  dragObj.elStartLeft  = parseInt(dragObj.elNode.style.left, 10);
  dragObj.elStartTop   = parseInt(dragObj.elNode.style.top,  10);
 
  if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
  if (isNaN(dragObj.elStartTop))  dragObj.elStartTop  = 0;
 
  // Update element's z-index.
 
  dragObj.elNode.style.zIndex = ++dragObj.zIndex;
 
  // Capture mousemove and mouseup events on the page.
 
  if (browser.isIE) {
    document.attachEvent("onmousemove", dragGo);
    document.attachEvent("onmouseup",   dragStop);
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS) {
    document.addEventListener("mousemove", dragGo,   true);
    document.addEventListener("mouseup",   dragStop, true);
    event.preventDefault();
  }
}
 
function dragGo(event) {
 
  var x, y;
 
  // Get cursor position with respect to the page.
 
  if (browser.isIE) {
    x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
    y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
  }
  if (browser.isNS) {
    x = event.clientX + window.scrollX;
    y = event.clientY + window.scrollY;
  }
 
  // Move drag element by the same amount the cursor has moved.
 
  dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
  dragObj.elNode.style.top  = (dragObj.elStartTop  + y - dragObj.cursorStartY) + "px";
 
  if (browser.isIE) {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
  }
  if (browser.isNS)
    event.preventDefault();
}
 
function dragStop(event) {
 
  // Clear the drag element global.
 
  dragObj.elNode = null;
 
  // Stop capturing mousemove and mouseup events.
 
  if (browser.isIE) {
    document.detachEvent("onmousemove", dragGo);
    document.detachEvent("onmouseup",   dragStop);
  }
  if (browser.isNS) {
    document.removeEventListener("mousemove", dragGo,   true);
    document.removeEventListener("mouseup",   dragStop, true);
  }
}

function mvIframeLeft()
{
//	alert(document.getElementById('div_arrow').src);
	
	if(document.getElementById('div_arrow').src == 'http://www.hotel-apps.co.uk/apps/images/left_arrow.png')
		{
		document.getElementById('div_arrow').src = 'http://www.hotel-apps.co.uk/apps/images/right_arrow.png';
		}
	else
		{
		document.getElementById('div_arrow').src = 'http://www.hotel-apps.co.uk/apps/images/left_arrow.png';
		}
		
//	alert(document.getElementById('div_arrow').src);

	if(jsfx_glob_sx==10)
		{
		jsfx_glob_sx = prev_jsfx_glob_sx;
//		glob_sx = prev_jsfx_glob_sx;
		}
		else
		{
		if(jsfx_glob_sx != 0)
			{
			prev_jsfx_glob_sx = jsfx_glob_sx;
			}
		jsfx_glob_sx = 10;
//		glob_sx = 10;
		}
}

function hdIframe()
{
	document.getElementById('PopUpDiv').style.visibility='hidden';
	document.getElementById('div_arrow').style.visibility='hidden';
	document.getElementById('div_eye').src = 'http://www.hotel-apps.co.uk/apps/images/hide_iframe_hide.png';

}
function shIframe()
{
	document.getElementById('PopUpDiv').style.visibility='visible';
	document.getElementById('div_arrow').style.visibility='visible';
	document.getElementById('div_eye').src = 'http://www.hotel-apps.co.uk/apps/images/hide_iframe.png';
}

function resizeIframe()
{
//	alert('hello');
	
	if(document.getElementById('book_iframe').style.height == '29px')
		{
//		document.getElementById('PopUpDiv').style.height = '310px';
		document.getElementById('book_iframe').style.height = '310px';
		}
	else
		{
//		document.getElementById('PopUpDiv').style.height = '30px';
		document.getElementById('book_iframe').style.height = '29px';
		}
	
}

window.onresize=handleResize;

