/***
 Get URL parameters
 Example: http://www.site.com/#floorplans
***/
function getHash(url) {

	if(!url) url = document.URL;
	
	var arrReturn = [];
	
	url = url.split("#");
	if(url.length > 1) {
		url.shift();
		url = url.join("#");
		arrReturn = url.split("/");
	}
	
	return(arrReturn);
	
}



/***
 Set div dimension, whith content
 Description: Resize divContent element on main html.
***/
function setContentDimension() {
	
	var w = document.body.clientWidth;
	var h = document.body.clientHeight;
	var div = document.getElementById("divContent");
	
	if(is_full_flash) {
		if(w > content_w) w = "100%";
		else w = content_w + "px";
		
		if(h > content_h) h = "100%";
		else h = content_h + "px";
	} else {
		w = content_w + "px";
		h = content_h + "px";
	}
	
	div.style.width = w;
	div.style.height = screen.height;
	setTimeout(function() {
		div.style.height = h;
	}, 100);
	
}



/***
 Popup window
 Usage: popup(url:String, title:String, w:Number, h:Number, scroll:Boolean);
***/
popup = function(url, title, w, h, allow_scrol) {
	
	var left = (screen.width) ? ((screen.width - w) / 2) : 100;
	var top = (screen.height) ? ((screen.height-h) / 2) - 28 : 20;
	if(left < 0) left = 0;
	if(top < 0) top = 0;
	
	var conf = "width=" + w + ", height=" + h + ", left=" + left + ", top=" + top + ", scrollbars=" + allow_scrol + ", status=0";
	
	window.open(url, title, conf);
	
}



/***
 Logger methods
 Description: log user navigation into robots. Currently uses only Google Analitics (GA).
***/
Logger = new Object();

Logger.arrIDs = new Array();

Logger.is_enabled = true;
Logger.is_debugging = false;
Logger.is_initialized = false;

Logger.init = function(obj) {
	
	if(obj.is_enabled === true) Logger.is_enabled = true;
	if(obj.is_debugging === true) Logger.is_debugging = true;
	
	if(Logger.is_enabled) {
		for(var i = -1, id; id = obj.ids[++i], (id !== undefined && id !== null);) {
			Logger.arrIDs.push(id);
			
			_uacct = id;
			urchinTracker();
			_uff = 0;
		}
	}
	
}

Logger.logGA = function(value) {
	
	if(value == undefined || value == null) value = "";
	if(value && Logger.is_debugging) alert("[JS] logGA: " + value);
	
	if(!Logger.is_enabled) return;
	
	/*
	if(!) {
		Logger.init({ids:google_url_ids, is_enabled:reporting, is_debugging:false});
	}*/

	
	for(var i = -1, id; id = Logger.arrIDs[++i], (id !== undefined && id !== null);) {
		_uff = 0;
		_uacct = id;
		urchinTracker(value);
	}
	
}



/***
 BrowserHistory methods
 Description: enable navigation by browser buttons (back/forward)
***/
BrowserHistory = new Object();

BrowserHistory.flashElement = null;
//BrowserHistory.page_title = top.document.title;
BrowserHistory.page_title = document.title;
/*
BrowserHistory.may_hash = (top.location.href.indexOf("#") == -1);
BrowserHistory.dont_hash = true;
*/
BrowserHistory.iframe_url = "browser_history_iframe.html";
BrowserHistory.actual = null;

BrowserHistory.init = function() {
	
	// Find flash object
	try {
		BrowserHistory.flashElement = document.getElementById(flash_name);
	} catch(e) {
		try {
			BrowserHistory.flashElement = document[flash_name];
		} catch(e) { }
	}
	
	if(BrowserHistory.flashElement != null) {
		BrowserHistory.flashElement.SetVariable("browser_history_init", "1");
	} else {
		setTimeout(BrowserHistory.init, 1000);
	}
	
}

// Changes the browser's title and address bar
BrowserHistory.updateBrowserInfo = function(title, add_main_title) {
	//BrowserHistory.updateBrowserAddrBar(BrowserHistory.actual);
	BrowserHistory.updateBrowserTitle(title, add_main_title);
}
BrowserHistory.updateBrowserAddrBar = function(value) {
	if(BrowserHistory.dont_hash) BrowserHistory.dont_hash = false;
	//else if(BrowserHistory.may_hash && (top.location.hash != "#" + value)) top.location.hash = value;
	else if(BrowserHistory.may_hash && (location.hash != "#" + value)) location.hash = value;
}
BrowserHistory.updateBrowserTitle = function(title, add_main_title) {

	if(add_main_title !== true) title = BrowserHistory.page_title + " - " +  title;

	title = unescape(title);

	try {
		//top.document.title = title;
		document.title = title;
		BrowserHistory.ifr.contentWindow.document.title = title;
	} catch(e) { };

}

// Set "flash listener" back or foward (navigation) value
BrowserHistory.setFlashVariable = function(value) {

	if(value != BrowserHistory.actual) {
		BrowserHistory.flashElement.SetVariable("browser_history_listener", value);
		BrowserHistory.actual = value;
	}

}

// Open iframe with rights arguments for history navigation
BrowserHistory.setBackSection = function(section, name, iframe_url) {
	
	BrowserHistory.actual = section;
	
	if(!iframe_url) var iframe_url = BrowserHistory.iframe_url;
	iframe_url += "?section=" + section + "&name=" + name;
	
	if(BrowserHistory.ifr) BrowserHistory.ifr.setAttribute("src", iframe_url);
	
}

// Define "Home" initial values
BrowserHistory.setHomeSection = function(section, name, iframe_url) {
	
	BrowserHistory.actual = section;
	
	if(!iframe_url) var iframe_url = "browser_history_iframe.html";
	iframe_url += "?section=" + section + "&name=" + name;
	
	var ifr = document.createElement("iframe");
	ifr.setAttribute("src", iframe_url);
	ifr.style.position = "absolute";
	ifr.style.width = "1px";
	ifr.style.height = "1px";
	ifr.style.top = "-10px";
	document.body.appendChild(ifr);
	
	BrowserHistory.ifr = ifr;
	
}

BrowserHistory.init();



/***
 Internal(7th) reporting methods
 Description: report user navigation actions/behavior.
***/
//reporting = (document.URL.indexOf("/accounts/") == -1 && document.URL.indexOf("/localhost/") == -1);

function Report(website, div, url, name) {

	this._website = website;
	this.div = (div) ? div : "report";
	this._url = (url == undefined) ? "http://the7thart.dreamhosters.com/reports/php/" : url;
	this._name = (name == undefined) ? "report" : name;
	
	this._visitid = 0;
	
	this.loaded = false;
	
	
	this._getCookie = function(name) {
	
		var start = document.cookie.indexOf(name + "=");
		var len = start + name.length + 1;
		
		if((!start) && (name != document.cookie.substring(0, name.length))) return(null);
		else 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)));
		
	}
	
	this._setCookie = function(name, value) {
		
		var today = new Date();
		today.setTime(today.getTime());
		var expires_date = new Date(today.getTime() + (3650 * 1000 * 60 * 60 * 24));
		
		document.cookie = name + "=" + value + ";expires=" + expires_date.toGMTString();
		
	}
	
	this._getUser = function() {
		return(this._getCookie("user"));
	}
	this._saveUser = function(id) {
		this._setCookie("user", id);
	}
	
	this.setVisitId = function(visitid, userid) {
		
		this._visitid = visitid;
		
		this._userid = userid;
		this._saveUser(userid);
		
		this.loaded = true;
		if(typeof(this.onLoad) == "function") this.onLoad();
		
	}
	
	this.getBasicQS = function() {
		var qs =  this._url + "report.php?";
		qs += 'r=' + (new Date() * 1);
		qs += "&visitid=" + this._visitid;
		qs += "&userid=" + this._userid;
		qs += "&objname=" + this._name + "&action=";
		return(qs);
	}
	
	
	this.noFlash = function(version) {
		if(!visitid || !userid || this.loaded) return;
		this.setVisitId(visitid, userid);
		document.getElementById(this.div).innerHTML += '<img src="' + this.getBasicQS() + 'userinfo&flashversion=' + version + '&noflash=1">';
	}
	this.start = function(version) {
		if(!visitid || !userid || this.loaded) return;
		this.setVisitId(visitid, userid);
		document.getElementById(this.div).innerHTML += '<img src="' + this.getBasicQS() + 'userinfo&flashversion=' + version + '">';
	}
	
	this._userid = this._getUser();
	if(!this._userid) this._userid = 0;
	
	document.write('<script language="javascript" src="' + this.getBasicQS() + 'init&website=' + this._website + '"></script>');
	document.write('<div id="' + this.div + '" style="width:1px; height:1px; overflow:hidden; position:absolute; top:-100px;"></div>');

}

function ReportTime(objReport) {
	
	reportTime = this;
	
	this._objReport = objReport;
	
	this._timeinterval = 20; // format is hhmmss
	
	this._getBasicQS = function() {
		var qs = reportTime._objReport.getBasicQS();
		qs += "time_spent&time=" + reportTime._timeinterval;
		return(qs);
	}
	
	this._saveTime = function() {
		if(reportTime._objReport.loaded) 
			document.getElementById(reportTime._objReport.div).innerHTML = '<img src="' + reportTime._getBasicQS() + '" />';
	}
	
	this._saveTime();

	this._interv = setInterval(this._saveTime, this._timeinterval * 1000);

}

function ReportResolution(objReport) {
	
	reportResolution = this;
	
	this._objReport = objReport;
	this._interv = null;
	this._count = 1;
	this._onresize = window.onresize;
	
	this._getBasicQS = function() {
		var qs = reportTime._objReport.getBasicQS();
		qs += "resolution";
		qs += "&sw=" + screen.width;
		qs += "&sh=" + screen.height;
		qs += "&aw=" + document.body.clientWidth;
		qs += "&ah=" + document.body.clientHeight;
		return(qs);
	}
	
	window.onresize = function() {
		
		clearInterval(reportResolution._interv);
		
		reportResolution._interv = setInterval(function() {
		
			clearInterval(reportResolution._interv);
			
			if(typeof(reportResolution._onresize) == "function") reportResolution._onresize();
			
			reportResolution._interv = setInterval(function() {
		
				clearInterval(reportResolution._interv);
				
				if(reportResolution._count++ > 3) {
					window.onresize = reportResolution._onresize;
					return;
				}
				
				document.getElementById(reportResolution._objReport.div).innerHTML = '<img src="' + reportResolution._getBasicQS() + '" />';
				
			}, 3000);
			
		}, 50);
		
	}
	
	window.onresize();

}

function initReport(url) {

	if(reporting !== false) {
		try {
			report = new Report(url);
			
			report.onLoad = function() {
				reportTime = new ReportTime(report);
				reportResolution = new ReportResolution(report);
			}
			
			if(report._loaded) report.onLoad();
		} catch (e) { };
	}
	
}

if(reporting) initReport(site_url);

is_JS_loaded = true;