// JavaScript Document

// This function saves a cookie
function setCookie2(name, value, expiresDays) {
	var today = new Date();
	today.setTime(today.getTime());
	
	if (expiresDays)
		expiresDays = expiresDays * 1000 * 60 * 60 * 24;
	
	var expires = new Date(today.getTime() + (expiresDays));	
	
	document.cookie = name + "=" + escape(value) + "; path=/" + ((expires == null) ? "" : "; expires=" + expires.toGMTString());
}

// This function retrieves a cookie
function getCookie2 (name) {
    var dc = document.cookie;
    var cname = name + "=";

    if (dc.length > 0) {              
		begin = dc.indexOf(cname);       
			if (begin != -1) {           
			begin += cname.length;       
			end = dc.indexOf(";", begin);
				if (end == -1) end = dc.length;
				return unescape(dc.substring(begin, end));
			} 
		}
		return null;
}

function setCookie(name, value, expiresDays, path, domain, secure)
{
	var today = new Date();
	today.setTime(today.getTime());
	
	if (expiresDays)
		expiresDays = expiresDays * 1000 * 60 * 60 * 24;
	
	var expires_date = new Date(today.getTime() + (expiresDays));
	var cookieStr = name + '=' + escape(value) +
		(expiresDays ? ";expires=" + expires_date.toGMTString() : "") +
		(path ? ";path=" + path : "") +
		(domain ? ";domain=" + domain : "") +
		(secure ? ";secure" : "");
	document.cookie = cookieStr;
}

function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
a_temp_cookie = a_all_cookies[i].split( '=' );
cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );	
//alert(cookie_value);		
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
alert(cookie_name);	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}	
function divDisplay(tDiv){
	var tDivDisplay = document.getElementById(tDiv).style.display
	if(tDivDisplay == 'block'){document.getElementById(tDiv).style.display = 'none'}
	else {document.getElementById(tDiv).style.display = 'block'}
}
function launchQuickstart(){
	cookie=getCookie("SITESEERQUICKSTARTVIEWED");
	if (!cookie || (cookie == 0)) {
		height = screen.availHeight;
		width = screen.availWidth / 2;
		left = screen.availWidth - width;
		w=window.open('quickStart.cfm','qswindow','height=' + height + ',width='+ width + ',top=5,left=' + left + ',scrollbars=yes,resizable=yes');
		if (w && w.open) w.moveTo(5,left);
	}
}
function goPlugin(urlParams, sessionID){ //agentid, teamid, officeid, mlsid, runsearch, searchtype, listingstartdate, basefly, sessionID, user, xml_dto,tour,env){	
//	alert("Error: Need to comment out this alert\n and uncomment one of the next 3 lines\n in the code.");	
	var url;
	var bDev;
	if (document.URL.toLowerCase().indexOf("dev.siteseer3d") < 0)
		bDev = false;
	else
		bDev = true;
	if  (urlParams.toLowerCase().indexOf("genv=1") > 0){                             //(env == "1")
		// Gold environment
		if (bDev)
			url = 'http://www.skylineglobe.com/SkylineGlobe/WebClient/PresentationLayer/webclient/3dwebclient.aspx?referrer=SS3DWeb&platdev&initialize=1&';
		else
			url = 'http://www.skylineglobe.com/SkylineGlobe/WebClient/PresentationLayer/webclient/3dwebclient.aspx?referrer=SS3DWeb&platprod&initialize=1&';
		}
	else{
		// Platinum environment
		if (bDev)
			url = 'http://www.skylineglobe.com/SkylineGlobe/WebClient/PresentationLayer/webclient/3dwebclient.aspx?referrer=ss3ddev&initialize=1&';
		else
			url = 'http://www.skylineglobe.com/SkylineGlobe/WebClient/PresentationLayer/webclient/3dwebclient.aspx?referrer=siteseer3d&initialize=1&';
		}
//	url = 'http://www.skylineglobe.com/SkylineGlobe/WebClient/PresentationLayer/webclient/3dwebclient.aspx?referrer=ss3dstaged&initialize=1&';

/*
	if (agentid != "")
		url = url + 'agentid=' + agentid;
	else if (teamid != "")
		url = url + 'teamid=' + teamid;
	else if (officeid != "")
		url = url + 'officeid=' + officeid;
	if (runsearch != "")
		url = url + '&runsearch=' + runsearch;
	if (mlsid != "")
		url = url + '&mlsid=' + mlsid;
	if (searchtype != "")
		url = url + '&searchtype=' + searchtype;
	if (listingstartdate != "")
		url = url + '&listingstartdate=' + listingstartdate;
	if (basefly === undefined) basefly = 'skylineglobeAE.fly';
	url = url + '&basefly=' + basefly;
	url = url + '&sessionID=' + sessionID;
	url = url + '&user=' + user;
	url = url + '&xml_dto=' + xml_dto;
	url = url + '&tour=' + tour;
*/
	if (urlParams.toLowerCase().indexOf("basefly") < 0)
		urlParams = urlParams + "&basefly=skylineglobeAE.fly";
	if (urlParams.toLowerCase().indexOf("sessionid") < 0)
		urlParams = urlParams + '&sessionID=' + sessionID;

	url = url + urlParams;
	SGStartURL(url);
	if (bDev)
		setCookie("SKYLINEINSTALLED", "1", 10 * 365, '/', 'dev.siteseer3d.com:85', '');
	else
		setCookie("SKYLINEINSTALLED", "1", 10 * 365, '/', 'www.siteseer3d.com', '');
}

var COOKIE_NAME = "GEODATA_DESKTOP_ICON";

function installDesktopIcon(params,force) {
	var popWin = true;
	
	var bDev;
	if (document.URL.toLowerCase().indexOf("dev.siteseer3d") < 0)
		bDev = false;
	else
		bDev = true;
		
	if (typeof force=='undefined') force=false;
	if ( force || !wasDesktopIconAttempted() ) {  // if forcing install OR not previously attempted
//		document.getElementById("start").style.visibility="visible"; // Not racing to map, so show the page

	// invoke the URL to install the desktop icon
	if (bDev)
		var iconInstall = "http://dev.siteseer3d.com:3011/icon";
	else
		var iconInstall = "http://v2ss3d.siteseer3d.com/icon";

		iconInstall = iconInstall + params;
//		iconInstall = iconInstall + "?urlstring=http://dev.siteseer3d.com:85/download.cfm?officeid=25649";
//		iconInstall = iconInstall + "&run";
	    // window.location.href = iconInstall;

		var width = 440;
		var height = 180;
		var left = parseInt((screen.availWidth/2) - (width/2));
		var top = parseInt((screen.availHeight/2) - (height/2));
		var features = 'resizable=no,width='+width+',height='+height+',left='+left+',top='+top;
		var features = features + ',alwaysRaised=1';
		
		if (window.parent.frames.length == 0){  //suppress the window for iframes because the cookie doesn't get set 
			popWin = window.open(iconInstall,'installIconDialog',features);
			if (!popWin){
				document.getElementById("loading").style.display="none";
				document.getElementById("popupBlocked").style.display="block"; // Show the poup blocker info  	
				}
			}
		
		// Set the cookie in the icon installer like below
		//setCookie2(COOKIE_NAME, "installed", 3650); // 10 years
	}

	return popWin;
}

function wasDesktopIconAttempted() {
	//  if the cookie exists, we attmpted so don't try again...
	var cookie=getCookie2(COOKIE_NAME);
//	alert( cookie );
	if (!cookie || (cookie == 0))
		return false;
	else
		return true;
}
