
// JavaScript Document

/*basic DHTML logging for our test page*/
function log(msg) {
	var logNode = document.getElementById("content");
	var content = "<p>" + msg + "</p>" + logNode.innerHTML;
	logNode.innerHTML = content;
}
function clearLog(msg) {
	var logNode = document.getElementById("content");
	logNode.innerHTML = "";
}

/*set up test cases for dhtmlHistory*/
var safari = $.browser.safari();
var safari_var = false;
var rawIndex = 0;
var rawMaterial = [
	["one", true],
	["two", true],
	["three", true],
	["four", true],
	["five", true],
	["six", true],
	["seven", true],
	["eight", true],
	["nine", true],
	["ten", true],
	["eleven", true],
	["twelve", true],
	["thirteen", true],
	["fourteen", true],
	["fifteen", true],
	["sixteen", true],
	["seventeen", true],
	["eighteen", true],
	["nineteen", true],
	["twenty", true],
	["twentyone", true],
	["twentytwo", true],
	["twentythree", true],
	["twentyfour", true],
	["twentyfive", true],
];

function callHSTest(hash,val) {
	historyStorage.put(hash,val);
	//log("Setting key <b>" + hash + "</b> to value <b>" + val + "</b>.");
}

function testComplexXML(path) {
	loadTopSubscriptions(path);
}	

function historyChange(newLocation, historyData) {
	var historyMsg = (typeof historyData == "object" && historyData != null
		? historyStorage.toJSON(historyData)
		: historyData
	);
	var msg = "<b>A history change has occured:</b> | newLocation=" + newLocation + " | historyData=" + historyMsg + " |";
	//log(msg);
	logTopSubscriptions(historyMsg);
};

/* Our event handler to add history change events */
addHistoryEvent = function() {
	if (!rawMaterial[rawIndex]) {
		rawIndex = 0;
	}
	var a = rawMaterial[rawIndex];
	//a[1] = "bankruptcy";
	dhtmlHistory.add(a[0],a[1]);
	if (typeof a[1] == "object") {/*stringify this so we can log it better*/
		a[1] = historyStorage.toJSON(a[1]);
	}
	var msg = "<b>A history event has been added:</b> [newLocation=" + a[0] + " | historyData=" + a[1] + "]";
	//log(msg);
	rawIndex++;
}

addHistoryEventURL = function(page) {
	if (!rawMaterial[rawIndex]) {
		rawIndex = 0;
	}
	var a = rawMaterial[rawIndex];
	a[1] = page;
	dhtmlHistory.add(a[0],a[1]);
	if (typeof a[1] == "object") {/*stringify this so we can log it better*/
		a[1] = historyStorage.toJSON(a[1]);
	}
	var msg = "<b>A history event has been added:</b> [newLocation=" + a[0] + " | historyData=" + a[1] + "]";
	//log(msg);
	rawIndex++;
}

/*basic Ajax wrapper*/
function getXHR() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
}


function logTopSubscriptions(path) {
	//log(prettyPrintXml(historyStorage.get('mySubscriptions')));
	
	
	if (!path){
		var path = 'main';
        var frontMain = true;
	}
	if ((path == 'areas') || (path == 'spa-areas')) {
		document.getElementById("content").innerHTML='';
		document.getElementById("head_mid_left_mid_left").innerHTML=historyStorage.get(path);
		document.getElementById("head_mid_left_bottom_left").style.backgroundImage = "url(images/left_capitol/left.jpg)";
		document.getElementById("head_mid_top_right").style.backgroundImage = "url(images/right_hands/right.jpg)";
        document.title = 'Elizabeth Rosar Chermack: Practice Areas'; 
	}
	else {
        switch (path) {
    		case 'main':
            document.title = 'Elizabeth Rosar Chermack, Attorney at Law, Minneapolis, St. Paul, Apple Valley, Burnsville and Lakeville, Minnesota - Family Law - Bankruptcy - Housing'; 
            break;
            case 'spa-main':
            document.title = 'Elizabeth Rosar Chermack, Abogada, Minneapolis, St. Paul, Apple Valley, Burnsville and Lakeville, Minnesota - Ley de Familia - Bancarrota - Ley de Albergue'; 
            break;
            case 'bio':
            document.title = 'Elizabeth Rosar Chermack: Compassionate and Practical Attorney'; 
            break;
            case 'spa-bio':
            document.title = 'Perfil de la Abogada: Elizabeth Rosar Chermack';
            break;
            case 'contact':
            document.title = 'Contact Elizabeth Rosar Chermack';
            break;
            case 'spa-contact':
            document.title = 'Elizabeth Rosar Chermack: Contacto';
            break;
            case 'bankruptcy':
            document.title = 'Elizabeth Rosar Chermack: Bankruptcy Lawyer, St. Paul, Minneapolis, Apple Valley, Burnsville, and Lakeville Minnesota - Chapter 7 Bankruptcy';
            break;
            case 'spa-bankruptcy':
            document.title = 'Elizabeth Rosar Chermack: Abogada de Bancarrota, St. Paul, Minneapolis, Apple Valley, Burnsville, and Lakeville Minnesota - Bancarrota de Capitulo 7'; 
            break;
            case 'family':
            document.title = 'Elizabeth Rosar Chermack: Family Law Services, St. Paul, Minneapolis, Apple Valley, Burnsville, and Lakeville Minnesota - Divorce - Child Support - Visitation - Parenting Time - Custody'; 
            break;
            case 'spa-family':
            document.title = 'Elizabeth Rosar Chermack: Ley de Familia, St. Paul, Minneapolis, Apple Valley, Burnsville, and Lakeville Minnesota - Divorcio - Manutención de hijos - Visitation - Parenting Time - Custodia parental y tiempo  con el niño';
            break;
            case 'housing':
            document.title = 'Elizabeth Rosar Chermack: Housing Law Attorney, St. Paul, Minneapolis, Apple Valley, Burnsville, and Lakeville Minnesota - Foreclosures - Landlord/Tenant - Reasonable Accommodations - Eviction Defense';
            break;
            case 'spa-housing':
            document.title = 'Elizabeth Rosar Chermack: Ley de Albergue, St. Paul, Minneapolis, Apple Valley, Burnsville, and Lakeville Minnesota - Ejecución de hipoteca - Landlord/Tenant - Reasonable Accommodations - Defensa contra desahucio';
            break;
            case 'blog':
            document.title = 'Elizabeth Rosar Chermacks Blog'; 
            break;
    	}
		document.getElementById("content").innerHTML=historyStorage.get(path);
		if ((path == "main") || (path == "spa-main") || (path == "areas") || (path == "spa-areas") || (path == "contact") || (path == "spa-contact"))
		{
			document.getElementById("head_mid_left_bottom_left").style.backgroundImage = "url(images/left_capitol/left.jpg)";
			document.getElementById("head_mid_top_right").style.backgroundImage = "url(images/right_hands/right.jpg)";
		}
		else
		{
        	document.getElementById("head_mid_left_bottom_left").style.backgroundImage = "url(images/" + path + "/left.jpg)";
			document.getElementById("head_mid_top_right").style.backgroundImage = "url(images/" + path + "/right.jpg)";
		}
		document.getElementById("head_mid_left_mid_left").innerHTML='';
	}
}
/* Gets the top 100 blog subscriptions as an OPML outline. - for a historyStorage test case */
function loadTopSubscriptions(path) {
	//log("Loading top subscriptions...");
	if (path == "main") {
    	var url="php/get_rss2.php";
    }
    else {
    	if (path == "spa-main")
        {
        var url="php/get_rss2.php?lang=spa";
        }
        else
        {
    	var url="php/getpage.php";
		url=url+"?q="+path;
		url=url+"&sid="+Math.random();
		}
    }
	var request = getXHR();
	var processRequest = function() {
		if (request.readyState == 4) {
			if (request.status == 200) {
				historyStorage.put(path, request.responseText);
				//log("Top subscriptions loaded");
				//log('Put complex XML document into key "mySubscriptions"');
				//log('<a href="javascript:logTopSubscriptions();">Click here to log the entire XML document.</a> Warning: it\'s huge.');
				topLoaded = true;
				logTopSubscriptions(path);
				addHistoryEventURL(path);
			}
			else {
				var message = "There was a problem retrieving the page: " + request.statusText
				log(message);
			}
		}
	};
	request.onreadystatechange = processRequest;
	request.open("GET", url, true);
	request.send("");
}




$(function() { $(".lavaLamp").lavaLamp({ fx: "backout", speed: 700 })});
	
window.onload = function() {
	//initialize our DHTML history
	dhtmlHistory.initialize();
	//log("<b>dhtmlHistory initialized</b>");

	//subscribe to DHTML history change events
	dhtmlHistory.addListener(historyChange);
	//log("<b>dhtmlHistory listener created</b>");
	testComplexXML('main');
};
    
