// WEBCAM BASE
// ims-rc

// http://www.dustindiaz.com/top-ten-javascript/
// getCookie(), setCookie(), deleteCookie()

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : '') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}


// replaceURL: para que los IFRAME de webcam no se agregen al History del Browser
function replaceURL(url) {
// Usage:
// <a href="javascript:replaceURL('page.html');">Got to a page</a>

    if (document.images)
        location.replace(url);
    else
        location.href = url;
}



// BEGIN GET QUERY STRING CODE 

// Get the query string with javascript just like a cgi script would
// so send it my_page.html?food=eggs&drink=beer 
// and you will have the variable query_food == eggs
// and the variable query_drink == beer
// everywhere in the document the whole variable _query_string == food=eggs&drink=beer
// is also available to you from http://www.summerholiday.org/freecode/

queryVar = this.location.href;
var inq = queryVar.indexOf('?');
queryVar = queryVar.substring(inq + 1);
if (inq > 0)
{
	_query_string = queryVar; // full query is also available to body
}
var text1 = "&";
var strLength = queryVar.length, txtLength = text1.length;

var i = queryVar.indexOf(text1);

while (i+txtLength < strLength)
{
	if (i > 0)
	{
		qrysplit(queryVar.substring(0,i),"=");
	}
	else
	{
		qrysplit(queryVar,"=");
	}
	queryVar = queryVar.substring(i+txtLength,strLength);
	var i = queryVar.indexOf(text1);
	if (i < 1)
	{
		i = strLength + 1; // end while
	}
}
qrysplit(queryVar,"="); // last one

function qrysplit(string,text)
{
	name = string.substring(0,string.indexOf(text));
	value = string.substring(string.indexOf(text) + 1);
	eval("query_" + name + " = value;");
	// comment the previous line and uncomment this next line if you want to
	// use UNESCAPE, that is, turn %20 into a space, %22 into double quotes, etc.
	// eval("query_" + name + " = unescape(value);");
}

// END GET QUERY STRING CODE


function callTab(tab){
			switch (tab){
				case 'Miraflores': 
					changeIframe('Miraflores','pcWebcamBaseID','700','490','/common/multimedia/webcams/viewer-flash/cam-miraflores.html');
				break;
				
				case 'MirafloresHi': 
					changeIframe('MirafloresHi','pcWebcamBaseID','700','490','/common/multimedia/webcams/viewer-flash/cam-miraflores-hi-thumb.html');
				break;		
				
				case 'Expansion': 
					changeIframe('Expansion','pcWebcamBaseID','700','490','/common/multimedia/webcams/viewer-flash/cam-ampliacion.html');
				break;
				
				case 'CerroLuisa': 
					changeIframe('CerroLuisa','pcWebcamBaseID','700','490','/common/multimedia/webcams/viewer-flash/cam-cerro-luisa.html');
				break;
				
				case 'Gatun': 
					changeIframe('Gatun','pcWebcamBaseID','700','490','/common/multimedia/webcams/viewer-flash/cam-gatun.html');
				break;

				case 'GatunHi': 
					changeIframe('GatunHi','pcWebcamBaseID','700','490','/common/multimedia/webcams/viewer-flash/cam-gatun-hi-thumb.html');
				break;

				default: 
					// llamar Miraflores
					//alert("default");
					changeIframe('Miraflores','pcWebcamBaseID','700','490','/common/multimedia/webcams/viewer-flash/cam-miraflores.html'); 
			}

} 


function divToggle(tabhere) {
	//alert("divToggle " + tabhere);
	for (var i=0; i<Tabs.length; i++) {
		divTab = document.getElementById( "Content" + Tabs[i] ); 
		//alert(Tabs[i]);
		if ( Tabs[i] == tabhere ) {
			divTab.style.display = "block";
			//alert(i + "block");					
		} else { 
			divTab.style.display = "none";
			//alert(i + "none");
		}
	}
}





// changeIframe
function changeIframe(tabhere,iframeidhere,widthhere,heighthere,srchere) {
	elem = document.getElementById(iframeidhere);
	elem.style.width = widthhere;
	elem.style.height = heighthere;
	elem.src = srchere;
	setCookie(TabStateCookieName, tabhere); // poner tercer parametro:  ,1   para que se mantenga por 1 dia.

	// marcar el tab con style
	for (var i=0; i<Tabs.length; i++) 	
		{
			liTab =  document.getElementById( "Li" + Tabs[i] ); 		
			cssjs('remove',liTab,'current')
		}
	currentTab = document.getElementById( "Li" + tabhere );
	cssjs('add', currentTab, 'current');
	
	divToggle(tabhere);
	//alert("tabtab" + tabhere);
}


function initializer() {
	//alert(getCookie(TabStateCookieName));

	if(typeof query_cam == "undefined" || query_cam == "") 	// si no hay tab querystring
		{
			//alert("cookie")
			var lastTab = getCookie(TabStateCookieName); // leer tab del cookie 
			callTab(lastTab);
		} else { // sino llamar por el querystring enviado
			//alert("query")
			callTab(query_cam); 
		}

}
addLoadEvent(initializer); //window.onload = initializer;




// ### MACROMEDIA SWAP IMAGE FUNCTIONS


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
