var gLangEnabled = false;
var gLang = 'en';

if ( gLangEnabled == true ) {	gLang = readCookie('hclan');  } 
if ( gLang != 'en' || gLang != 'es' ) { gLang = 'en';  }


if ( gLang == 'en' ) {
	var GALLERY_VIEW_SIGNATURE		= "www.hiddencanopy.com";
	var GALLERY_EXIT 			= "Close";		
} else if ( gLang == 'es' ) {
	var GALLERY_VIEW_SIGNATURE		= "www.hiddencanopy.com";
	var GALLERY_EXIT 			= "Close";			
}

/*------------------------------------------------------*/
function changeLang(nln) {
	gLang = nln;
	
	eraseCookie('hclan');
	createCookie('hclan',nln,180);
	window.location = window.location;
	window.location = window.location;
}


/*------------------------------------------------------*/
function handleAjaxError(o){
	alert(o.statusText);
}


/*------------------------------------------------------*/
function whichBrowser () {

   browser_name    = navigator.appName;
   browser_version = parseFloat(navigator.appVersion);
  
   if (browser_name == "Netscape")   {
       ret_value = 'Firefox';
   } else if (browser_name == "Microsoft Internet Explorer")  {
       ret_value = 'IE';
   } else {
       ret_value = 'Other';
   }
   return ret_value;
}

/*------------------------------------------------------*/
function openBrowserWindow( pUrl, pWinName, pAttributes, pFocus  ) {

  newwindow = window.open( pUrl, pWinName, pAttributes );
  if ( pFocus == 'y' ) {
    newwindow.focus();
  }
  return newwindow;
 }
 
/* ----------------------------------------- */
function scrollFunc( x, y ) {
	if (whichBrowser()=='Firefox')  window.scroll(x, y);
	else  window.scrollTo(x, y); 	
}


/*------------------------------------------------------*/
function pad( tstr, wchr, wlen) {
	tstr = String(tstr);
	var ret = "";
	for ( var i=0; i< (wlen-tstr.length); i++ ) {
		ret += wchr; 
	}
	ret += tstr;
	return ret;
}



/*------------------------------------------------------*/
function convertReturnToBr(strWithReturns){
	var strFinal = strWithReturns.replace(/\r\n/g, '<br>');
	strFinal = strFinal.replace(/\r/g, '<br>');
	strFinal = strFinal.replace(/\n/g, '<br>');
	strFinal = strFinal.replace(/\"/g, '&#34;');	//encode " because json strings seperated by it
	return strFinal;
	//return strWithReturns.replace(/\\n/g, '<br>');
}

/*------------------------------------------------------*/
function validEmail(email){
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email)) 
	   return true;
	else
	   return false;
}

/*------------------------------------------------------*/
function validUrl(url) {
     var lc_url = url.toLowerCase();
     var ret = lc_url.match(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/);
     if ( ret == null || ret == '' ) {
     	return false;
     } else {
     	return true;
     }
}

/*------------------------------------------------------*/
function delay(ts){
	if ( ts != '' ) {
		var date = new Date();
		var curDate = null;
		do { curDate = new Date(); }
		while(curDate-date < ts);
	}
}
/*------------------------------------------------------*/
function pause(milliseconds) {
    var now = new Date();
    var exitTime = now.getTime() + milliseconds;
    while(true)  {
      now = new Date();
      if(now.getTime() > exitTime) return;
    }
}

/*------------------------------------------------------*/
String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

/*------------------------------------------------------*/
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

/*------------------------------------------------------*/
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return "";
}

/*------------------------------------------------------*/
function eraseCookie(name) {
	createCookie(name,"",-1);
}


/*------------------------------------------------------*/
function getImgHeight( imgURL ) {
	var newImg = new Image();
	newImg.src = imgURL;
	return newImg.height;

}
/* -------------------------------------------------------------- */
function getImgWidth( imgURL ) {
	var newImg = new Image();
	newImg.src = imgURL;
	return newImg.width;
}

/* -------------------------------------------------------------- */
function getClientHeight() {
	  var myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    myHeight = document.body.clientHeight;
	  }
	  return myHeight;
}

/* -------------------------------------------------------------- */
function getClientWidth() {
	 var myWidth = 0;
	 if( typeof( window.innerWidth ) == 'number' ) {
	   //Non-IE
	   myWidth = window.innerWidth;
	 } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	   //IE 6+ in 'standards compliant mode'
	   myWidth = document.documentElement.clientWidth;
	 } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	   //IE 4 compatible
	   myWidth = document.body.clientWidth;
	 }
	 return myWidth;
}

/* -------------------------------------------------------------- */
function displayImage( galleryName, imgName, imgURL, imgText ) {
	
	ImageWindow=window.open("", "viewImage");
	ImageWindow.document.write("<html><head>\n")
	ImageWindow.document.write("   <title>Hidden Canopy Galleries</title>\n")
	ImageWindow.document.write("   <script src='/js/utils.js'  type='text/javascript'></script>\n")
	ImageWindow.document.write("   <script>\n")
	ImageWindow.document.write("      thisImg = new Image() \n" )
	ImageWindow.document.write("      function resizeImg() { \n" )
	ImageWindow.document.write("         thisImg.src  = '" + imgURL + "'; \n" )	
	ImageWindow.document.write("         var cw = getClientWidth() - 40; \n" )
	ImageWindow.document.write("         var ch = getClientHeight() - 80; \n" )
	ImageWindow.document.write("         var iw = getImgWidth( '" + imgURL + "' ); \n" )
	ImageWindow.document.write("         var ih = getImgHeight( '" + imgURL + "' ); \n" )
	ImageWindow.document.write("         if ( ch >= ih &&  cw < iw ) { w=cw; h=ih; } \n" )
	ImageWindow.document.write("         else if ( cw >= iw &&  ch < ih ) { w=iw; h=ch; } \n" )
	ImageWindow.document.write("         else if ( cw < iw && ch < ih ) { w=cw; h=ch; } \n" )
	ImageWindow.document.write("         else { w=iw; h=ih; } \n" )
	ImageWindow.document.write("         if ( ch >= ih && cw < iw ) { \n")
	ImageWindow.document.write("            imgCont = '<img src=' + thisImg.src + ' border=0 width='+w+'>'\n")
	ImageWindow.document.write("         } else if ( cw >= iw && ch < ih ) { \n")
	ImageWindow.document.write("            imgCont = '<img src=' + thisImg.src + ' border=0 height='+h+'>'\n")
	ImageWindow.document.write("         } else if ( cw < iw && ch < ih ) { \n")	
	ImageWindow.document.write("         	if ( iw-cw > ih-ch ) { \n")
	ImageWindow.document.write("          		imgCont = '<img src=' + thisImg.src + ' border=0 width='+w+'>'\n")
	ImageWindow.document.write("         	} else { \n")
	ImageWindow.document.write("         		imgCont = '<img src=' + thisImg.src + ' border=0 height='+h+'>'\n")
	ImageWindow.document.write("         	} \n")
	ImageWindow.document.write("         } else { \n")
	ImageWindow.document.write("            imgCont = '<img src=' + thisImg.src + ' border=0 >'\n")
	ImageWindow.document.write("         } \n")
	ImageWindow.document.write("         document.getElementById('img').innerHTML = imgCont \n" )
	ImageWindow.document.write("      } \n")
	ImageWindow.document.write("   </script>\n")
	ImageWindow.document.write("</head>\n")	
	ImageWindow.document.write("<body bgcolor='white' onresize='resizeImg()' onload='resizeImg()'>\n")
	ImageWindow.document.write("   <table bgcolor='#cacaca' border='0' width='100%'>\n")
	ImageWindow.document.write("   <tr height='20' ><td align='center' style='background-color:#efefef; font-size:14px; color:blue;font-weight:bold' >"+galleryName+"</td><td align='middle' width='17' style='background-color:#efefef'><a href='javascript:close()'><img border='0' src='/images/smallX.png' title='"+GALLERY_EXIT+"'></a></td></tr>\n")
	ImageWindow.document.write("   <tr><td id='img' colspan='2' align='center' valign='middle'>\n")

	cw = getClientWidth() - 40
	ch = getClientHeight() - 80
	iw = getImgWidth( imgURL )
	ih = getImgHeight( imgURL )

	if ( ch >= ih &&  cw < iw ) { w=cw; h=ih; }
	else if ( cw >= iw &&  ch < ih ) { w=iw; h=ch; }
	else if ( cw < iw && ch < ih ) { w=cw; h=ch; }
	else { w=iw; h=ih; }

	if ( ch >= ih && cw < iw ) { 
		ImageWindow.document.write("<img src='" +imgURL+ "' border='0' width='"+w+"'>\n")
	} else if ( cw >= iw && ch < ih ) {
		ImageWindow.document.write("<img src='" +imgURL+ "' border='0' height='"+h+"'>\n")			
	} else if ( cw < iw && ch < ih ) {
		if ( iw-cw > ih-ch ) {
			ImageWindow.document.write("<img src='" +imgURL+ "' border='0' width='"+w+"'>\n")
		} else {
			ImageWindow.document.write("<img src='" +imgURL+ "' border='0' height='"+h+"'>\n")
		}
	} else {
		ImageWindow.document.write("<img src='" +imgURL+ "' border='0'>\n")
	}	

	ImageWindow.document.write("</td></tr>\n")
	ImageWindow.document.write("<tr><td colspan='2' align='center' style='background-color:#efefef;color:brown;font-size:11px;'>"+imgName+"<div id='ImageText'>"+imgText+"</div></td></tr>\n")
	ImageWindow.document.write("</table>\n")

	ImageWindow.document.write("<table border='0' width='100%'><tr height='5' width='100%'><td height='5' colspan='2' align='center' valign='middle' style='background-color:white;color:gray;font-size:10px;'>"+GALLERY_VIEW_SIGNATURE+"</td></tr></table>\n")

	ImageWindow.document.write("</body></html>\n")
	ImageWindow.document.close()
	ImageWindow.focus()

}



