/* -----------------------------------------------------------------------------------------
Title:				Global Javascript Functions for www. .com
File:				/scripts/global.js
Author:				George Komianos
Copyright:			2005 Acrodev.com - Unless otherwise noted.
					No portion of this document may be reproduced, unless otherwise stated.
					Any GNL code is noted accordingly.
			                 			
Description:		--	

Created:			12/11/05
Modified:			04/05/06

TODO:				--
BUG/FIX:			--		
----------------------------------------------------------------------------------------- */


/* -----------------------------------------------------------------------------------------
* Summary: 			Opens a new window.
* Parameters: 		theURL,winName,features
----------------------------------------------------------------------------------------- */
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

/* -----------------------------------------------------------------------------------------
* Summary: 			Opens links with attribute rel="external" in a new window.
----------------------------------------------------------------------------------------- */
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}

/* -----------------------------------------------------------------------------------------
* Summary: 			Toggles an element;s visibility.
* Parameters: 		targetId - the id of the element to be toggled.
----------------------------------------------------------------------------------------- */
function toggle (targetId) {
	if (document.getElementById) {
			var target = document.getElementById(targetId);
			
			if (target.style.display == "none") {
					target.style.display = "block";
			} else {
					target.style.display = "none";
			}
	}	
}


/* -----------------------------------------------------------------------------------------
* Summary: 			Makes an element visibile.
* Parameters: 		targetId - the id of the element to be displayed.
----------------------------------------------------------------------------------------- */
function show (targetId) {
	if (document.getElementById) {
			var target = document.getElementById(targetId);
			
			if (target.style.display == "none") {
					target.style.display = "block";
			} else return false;
	}	
}


/* -----------------------------------------------------------------------------------------
* Summary: 			Makes an element invisibile.
* Parameters: 		targetId - the id of the element to be hidden.
----------------------------------------------------------------------------------------- */
function hide (targetId) {
	if (document.getElementById) {
			var target = document.getElementById(targetId);
			
			if (target.style.display == "block") {
					target.style.display = "none";
			} else return false;
	}	
}


/* ------------- Image Popups ------------- */

// function to add the onclick popup attribute to appropriate links
function addPop() {
  if (!document.getElementsByTagName) return false;
		var links = document.getElementsByTagName("a");
		
		for (var i=0; i < links.length; i++) {
				if (links[i].className.match("popup")) {
					links[i].onclick = function() {
						popImage(this.href, this.title); 
						return false;
					}
				}
		} // end loop
}

/* -----------------------------------------------------------------------------------------
* Summary: 			Toggles the contents of a div element according to a radio button selection.
					In this case it is used to display form elements.
* Parameters: 		None required.
----------------------------------------------------------------------------------------- */

/* ------------- contact form display ------------- */
/*
function formDisplay () {
 
a) hide the reservation fields on load, so that javascript disabled browsers can see them
b) keep showing them if the "reservations" option is checked and the page is reloaded.

	var container = document.getElementById('reservations');
	var element = document.getElementById('contact').request[1]; //use form id for xhtml strict validation
	
	if (document.getElementById) {
			if (element.checked == true) {
					container.style.display = "block";
					return false;
			} else {
					container.style.display = "none";
			}
	} else return false;
}

*/




function closeWin () {
	self.close()	
}


/* -----------------------------------------------------------------------------------------
* Summary: 			Unobtrusive image gallery. Attaches the onclick event to show enlarged photo inside a container/placeholder.
					In this case it is used to display form elements.
* Last Edited:		02/12/05 - added the "loading" gif					
----------------------------------------------------------------------------------------- */

function prepareGallery() 
{
	
	if (!document.getElementsByTagName) return false;
	if (!document.getElementById) return false;
	if (!document.getElementById("imagegallery")) return false;
	
	var gallery = document.getElementById("imagegallery");
	var links = gallery.getElementsByTagName("a");
	
	for ( var i=0; i < links.length; i++) {		
		links[i].onclick = function() {
		  return showPic(this);
		}
	} // end loop
}

function lockGallery() 
{
	
	if (!document.getElementById("imagegallery")) return false;
	
	var gallery = document.getElementById("imagegallery");
	var links = gallery.getElementsByTagName("a");
	
	for ( var i=0; i < links.length; i++) {		
		links[i].onclick = function() {
		  alert ('Please wait for the photo you already requested to load.');
		  return false;
		}
	} // end loop
}

function showPic (whichpic) 
{	
	container = document.getElementById('placeholder');
	
	// if target img is the current enlarged img, don't do anything
	if (container.src == whichpic.href) return false;
	
	// create the 2 images required
	loading_img = new Image();
	loading_img.src = '/images/loading_med.gif';
	
	target_img = new Image();
    target_img.src = whichpic.href;
	
	// show the loading gif first
	container.src = loading_img.src;
	
	// set timerid here
	timerid = ''; 
	
	replace_img();
  	return false;
}

function replace_img()
{		
	// if the image has not been loaded, set time interval and load the target_img
	// if it has already been loaded, check if a timer exists and:
	// clear it and load the target_img if it does, or just load the target_img if it doesn't.
	
	if (!img_loaded(target_img) )
	{	
		if (timerid == '') {
			lockGallery();
			timerid = window.setInterval("replace_img()", 500);
		}
	} 
	else 
	{
		container.src = target_img.src;		
		if (timerid != '') {
			clearInterval(timerid);			
		}
		prepareGallery();
	}
}



function img_loaded(img)
{
    // During the onload event, IE correctly identifies any images that
    // weren't downloaded as not complete. Others should too. Gecko-based
    // browsers act like NS4 in that they report this incorrectly.
    if (!img.complete)
        return false;

    // However, they do have two very useful properties: naturalWidth and
    // naturalHeight. These give the true size of the image. If it failed
    // to load, either of these should be zero.
    if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0)
        return false;

    // No other way of checking: assume it's ok.
    return true;
}



/* -----------------------------------------------------------------------------------------
* Summary: 			adds the onclick popup attribute to appropriate links
* Parameters: 		None
----------------------------------------------------------------------------------------- */

function addPopTrigger () 
{
	if (!document.getElementsByTagName) return false;
	
	var links = document.getElementsByTagName("a");
	
	if (links) 
	{
		for (var i=0; i < links.length; i++) 
		{
			var anchor = links[i];
			if (anchor.className.match("popup")) 
			{
				anchor.onclick = function() 
				{
					imgPop (this.href, this.title); 
					return false;
				}
			}
		} // end loop
		
	} else return false;
	return null;	
}

function imgPop (imageURL,imageTitle) 
{
	//pass both parameters in the URL
	var newWin = window.open( "/assets/pop_img.html?"+imageURL+"?"+imageTitle, "popImg",  'status=yes,scrollbars=no,resizable=yes,width=600,height=600');
}

/* -----------------------------------------------------------------------------------------
* Summary: 			
* Parameters: 		
----------------------------------------------------------------------------------------- */
var win= null;
function NewWindow(mypage,myname,w,h,scroll) 
{
	var winl = (screen.width-w)/2;
	var wint = (screen.height-h)/2;
	var settings ='height='+h+',';	
	settings +='width='+w+',';
	settings +='top='+wint+',';
	settings +='left='+winl+',';
	settings +='scrollbars='+scroll+',';
	settings +='resizable=yes';
	
	win=window.open(mypage,myname,settings);

	if(parseInt(navigator.appVersion) >= 4)	
	{
		win.window.focus();
	}
}

/* -----------------------------------------------------------------------------------------
* Summary: 			Adds the onclick attribute to trigger the photo Gallery window to appropriate link
* Parameters: 		None
----------------------------------------------------------------------------------------- */
function galleryWin() {
  if (!document.getElementsByTagName) return false;
		var links = document.getElementsByTagName("a");
		
		for (var i=0; i < links.length; i++) {
				if (links[i].className.match("gallery-win")) {
					links[i].onclick = function() {
						galleryPop(this.href); 
						return false;
					}
				}
		} // end loop
}

/* -----------------------------------------------------------------------------------------
* Summary: 			Pops the Gallery Window	
* Parameters: 		None
----------------------------------------------------------------------------------------- */
function galleryPop(targetURL) {
	var galleryURL = targetURL;
	NewWindow (galleryURL, 'PicGallery','640','570','yes');
}

/* -----------------------------------------------------------------------------------------
* Summary: 			
* Parameters: 		
----------------------------------------------------------------------------------------- */
function popw(o)
{
 var newimg;
	// if there is already an image in the parentNode (li) 
	 if(o.parentNode.getElementsByTagName('img').length>0)
		 {
		// delete it
		  o.parentNode.removeChild(o.parentNode.getElementsByTagName('img')[0]);
		 } else {
		// else, create a new image and add a handler that removes it when you 
		// click it
		  newimg=document.createElement('img');
		  newimg.style.display='block';
		  newimg.onclick=function(){this.parentNode.removeChild(this);};
		  newimg.src=o.href;
		  o.parentNode.appendChild(newimg)
		 }
		 return false;
}

/* -----------------------------------------------------------------------------------------
* Summary: 		Romoves the ractangle that appears around link elements in Firefox
* Parameters: 	None	
----------------------------------------------------------------------------------------- */
var remove_rectangle = function() {
    var lnks = document.links;
    if (lnks) {
        for (var i = 0; i < lnks.length; i++) {
            lnks[i].onmousedown = function() {
                this.blur();
                return false;
            }
        }
    }
}


/* -----------------------------------------------------------------------------------------
* Summary: 		Romoves the ractangle that appears around link elements in Firefox
* Parameters: 	None	
----------------------------------------------------------------------------------------- */
function nav () {

	var menu = document.getElementById('side-nav');	// the navigation element
	
	var activeSubClass 		= 'activesub';			// class used to for the active (open) sublist
	var inactiveSubClass 	= 'notactivesub';		// class used to for the inactive (hidden) sublists
	
	var parentClass 		= 'isParent';				// class assigned to all LIs with a sublist
	var parentOpen 			= 'parentOpen';				// class assigned to all LIs with a sublist
	var activeParentClass 	= 'isActive';			// class assigned to the parent LI of the active sublist
	var activeOpen 			= 'activeOpen';
	
	var toHideClass 		= 'hideChild';				// class used to hide an element
	var toShowClass 		= 'showChild';				// class used to show an element
	var currentClass 		= 'current';				// class of the current link
	var a;
	var targetUL;
	
	var txt_open 			= 'open links';				// class of the current link
	var txt_close 			= 'close links';				// class of the current link
	
	// Fist of all... stop if DOM isn't available
	if (!document.getElementById && !document.createTextNode)
	{
		return;
	}	
	
	// only proceed if the navigation element exists
	if (menu) {
		
		// set the variables to be used
		var lis, i, firstUL, j, apply;
		
		lis = menu.getElementsByTagName('li');		// get all the LIs in the menu
		
		// loop through all LIs and check which ones have a nested UL
		for (i=0; i<lis.length; i++)
		{
			firstUL = lis[i].getElementsByTagName('ul')[0]; // since we're looping there is only one sublist at a time, thus [0]
			
			// if there is a nested UL, deactivate the first nested link and apply the parentClass
			if (firstUL)
			{
				lis[i].childNodes[0].onclick = function() { return false; } 				// deactivate 
				lis[i].className += lis[i].className == ''?parentClass:' '+parentClass; 	// add the parentClass
				//lis[i].setAttribute ('title', txt_open);									// add title txt
				
				//var sublist = lis[i].getElementsByTagName('ul');
				var sublinks = lis[i].getElementsByTagName('li');
				
				for (a=0; a<sublinks.length; a++) 
				{					
					var parentUL = firstUL;			// the sublist		
					
					// if an active LI is found, apply the activeSubClass to its UL and stop the loop, otherwise apply the inactiveSubClass.
					if (sublinks[a].className == currentClass)
					{
						
						firstUL.setAttribute ('class', activeSubClass);
						firstUL.setAttribute ('className', activeSubClass);
																	
						lis[i].setAttribute ('class', activeOpen);
						lis[i].setAttribute ('className', activeOpen);
						//lis[i].setAttribute ('title', txt_close);	// add title txt
						
						//asdfasd = firstUL.className;
						//alert (asdfasd);
						
						//firstUL.className += lis[i].className == ''?activeSubClass:' '+activeSubClass;
						//lis[i].className += lis[i].className == ''?activeParentClass:' '+activeParentClass;		
						//asdfasd = firstUL.className;
						//alert (asdfasd);
						break
					} else
					{
						//firstUL.className += lis[i].className == ''?inactiveSubClass:' '+inactiveSubClass;
						firstUL.setAttribute ('class', toHideClass);
						firstUL.setAttribute ('className', toHideClass);
						
					}
					
				} // loop of sub LIs
			} // loop of LIs with sublists 
		} // loop of all menu LIs
		
		
		// apply the onclick event to the Parent LIs
		// find all the subULs and apply the onclick event to their Parent LI
		lis = menu.getElementsByTagName('li');
		for (i=0; i<lis.length; i++)
		{
			if ( (lis[i].className.match(parentClass)) || (lis[i].className.match(activeParentClass)) || (lis[i].className.match(activeOpen))  )
			{
				lis[i].onclick = function() { doMenuToggle (this); }
			}
		}
		
	} // end if menu
	
	
	function doMenuToggle (o)
	{
				
		lis = menu.getElementsByTagName('li');
		targetUL = o.childNodes[2];
		
		for (i=0; i<lis.length; i++)
		{
			//var subUL = lis[i].getElementsByTagName('ul')[0];
			var subUL = lis[i].getElementsByTagName('ul');
			
			/*
				if it's an active UL and it's the target UL, toggle it and hide the others
				if it's an active UL and it's not the target UL, toggle it and hide the others
					
				if it's an inactive and it's the target UL, toggle it and hide the others including the active one.
				if it's an inactive and it's not the target UL, toggle it and hide the others including the active one.
					
			*/
			
			if (subUL) 
			{
				for (j=0; j<subUL.length; j++)
				{
					var parentLI = subUL[j].parentNode;
					//alert (parentLI.className);
					if ( ((subUL[j].className.match (activeSubClass)) || (subUL[j].className.match (inactiveSubClass))) &&  (targetUL == subUL[j]) )
					{ 
						if ( (subUL[j].className.match (activeSubClass)) || (subUL[j].className.match (inactiveSubClass)) )
						{ //alert ('found active target');
							if (subUL[j].style.display == "none") 
							{
								subUL[j].setAttribute ('class', activeSubClass);
								subUL[j].setAttribute ('className', activeSubClass); 
								subUL[j].style.display = "block";
								parentLI.setAttribute ('class', activeOpen);
								parentLI.setAttribute ('className', activeOpen);
								//parentLI.setAttribute ('title', txt_open);	// add title txt
								
							} else 
							{
								subUL[j].setAttribute ('class', inactiveSubClass);
								subUL[j].setAttribute ('className', inactiveSubClass); 
								subUL[j].style.display = "none";
								parentLI.setAttribute ('class', activeParentClass);
								parentLI.setAttribute ('className', activeParentClass);
								//parentLI.setAttribute ('title', txt_close);	// add title txt
							}
						}
						
						
					}
					 
					if ( ((subUL[j].className.match (activeSubClass)) || (subUL[j].className.match (inactiveSubClass))) &&  (targetUL != subUL[j]) )
					{ //alert ('found active but NOT target');

								subUL[j].setAttribute ('class', inactiveSubClass);
								subUL[j].setAttribute ('className', inactiveSubClass); 
								subUL[j].style.display = "none";
								parentLI.setAttribute ('class', activeParentClass);
								parentLI.setAttribute ('className', activeParentClass);
						
					}
					
					if ( ((subUL[j].className.match (toHideClass)) || (subUL[j].className.match (toShowClass))) &&  (targetUL == subUL[j]) )
					{  
						//alert ('found inactive target');
						if ( subUL[j].className.match (toHideClass) )
						{  
							subUL[j].setAttribute ('class', toShowClass);
							subUL[j].setAttribute ('className', toShowClass); 	// IE understands this
							o.setAttribute ('class', parentOpen);
							o.setAttribute ('className', parentOpen);
						}
						else if ( targetUL.className.match (toShowClass) )
						{  
							subUL[j].setAttribute ('class', toHideClass);
							subUL[j].setAttribute ('className', toHideClass); 	// IE understands this
							parentLI.setAttribute ('class', parentClass);
							parentLI.setAttribute ('className', parentClass);
						}
						
					} 
					
					if ( ((subUL[j].className.match (toHideClass)) || (subUL[j].className.match (toShowClass))) &&  (targetUL != subUL[j]) )
					{ //alert ('found inactive - NOT target');
						
							subUL[j].setAttribute ('class', toHideClass);
							subUL[j].setAttribute ('className', toHideClass); 	// IE understands this
							parentLI.setAttribute ('class', parentClass);
							parentLI.setAttribute ('className', parentClass);
						
					} //else return false;
					//alert (parentLI.className);
										
					} // end subULs loop
				
				} // end subUL conditional
				
		} // end all LIs loop
		
	} // doMenuToggle
	
} 
// end menu function

window.onload=function()
{
	nav ();
	addPopTrigger ();
	externalLinks ();
	galleryWin();
	prepareGallery ();
	remove_rectangle();
}
