// JavaScript Document

var domain = "homeimprovementmag.com";

// check for IE6
function detectIE() {
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
	var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
	if (ieversion>=6 && ieversion<7) {
 	return true;
 	}
	else {
	return false;
	}
}
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

function switchCities(option, isResourcePage) {
    if (option == "") {
		return;
	}
	var currentHref = location.href;
	var currentHostName = location.hostname;
	var newHostName = option+"."+domain;
	var newHref = new String();
	newHref = currentHref.replace(currentHostName,newHostName);
	
	if (isResourcePage) {
	    if (window.seoCategory && ("string" == typeof window.seoCategory))
	        newHref = "http://" + newHostName + "/" + window.seoCategory;    
	    else 
	        newHref = "http://" + newHostName;    
	} else {
	    var listingPosition = newHref.indexOf('?');
	    if (listingPosition > -1) {
		    newHref = newHref.slice(0,listingPosition);
	    }
	    if (window.category && ("string" == typeof window.category)) {
	        newHref += "?category=" + window.category;
	    }
	    if (currentHref.match(/areaname/g) != null) {
	        newHref += "?areaname="+areanames[option];
	    }
	}    
	location.href = newHref;
}

var IEFixed = function(){
        var citybox = $('city-selector');
        var scroll = document.documentElement.scrollTop;
        var s_top = scroll + Math.round((document.viewport.getDimensions().height - (citybox.offsetHeight || 0)) / 2);
        citybox.style.top = s_top + 'px';
    };

function showSelector(category,cssClass,seoCategoryName) {
	var city = $("city");
	var scrim = $("scrim");
	var citySelector = $("city-selector");
	var arrayPageSize = getPageSize();
	scrim.style.height = (arrayPageSize[1] + 'px');
	scrim.style.display = 'block';
	city.style.display = "block";
	citySelector.style.display = "block";
	var dimensions = citySelector.getDimensions();
	citySelector.style.left = (((arrayPageSize[2]/2)-(dimensions.width/2)) + 'px');
	citySelector.style.top = (((arrayPageSize[3]/2)-(dimensions.height/2)) + 'px');
	if (detectIE() == true) {
		 IEFixed();
		 Event.observe(window, 'scroll', IEFixed);
	}
	var categoryTitle = $("category-title");
	categoryTitle.innerHTML = category;
	var label = $("resource-label");  // set the color
	label.className = cssClass;
	var categoryName = category.replace(/ /g,'+');  // set the link
	categoryName = categoryName.replace(/&/g,'%26');
	categoryName = categoryName.replace(/\//g,'%2F');
	var cityList = $('vertical_container');

	for (state in states) {
		var stateText = document.createTextNode(state);
		var h4 = new Element('div');
		h4.className = "accordion_toggle";
		h4.appendChild(stateText);
		cityList.insert({'bottom':h4});
		var div = new Element('div');
		div.className = "accordion_content";
		var ul = new Element('ul');
		for (s=0; s<states[state].length;s++) {
			var li = new Element('li');
			var a = new Element('a').update(states[state][s]);
			if (seoCategoryName) {
				a.href = "http://" + regions[states[state][s]] + "." + domain+ "/" + seoCategoryName;
			}
				else if(categoryName) {
				a.href = "http://"+regions[states[state][s]]+"."+domain+"/Home-Improvement-Online/Resources/index.php?category=" + categoryName;
			}	else  {
				a.href = "http://"+regions[states[state][s]]+"."+domain+"/Home-Improvement-Online/Resources/index.php";  
			}
			li.update(a);
			ul.insert({'bottom':li});
		}
		div.update(ul);
		cityList.insert({'bottom':div});
	}
	
	/* messaging */
/*
	var h = cityList.innerHTML;
	var messaging = $('messaging');
	var html = document.createTextNode(h);
	messaging.appendChild(html);
*/
	// set up accordion //
	geoAccordion = new accordion('vertical_container');

	var articleLink = $("articleLink");
	articleLink.href = "/Home-Improvement-Online/Articles/index.php?category="+categoryName;

}

function hideSelector() {
	var cityList = $('vertical_container');
	var city = $("city");
	var scrim = $("scrim");
	var citySelector = $("city-selector");
	city.style.display = "none";
	citySelector.style.display = "none";
	scrim.style.display = "none";
	cityList.innerHTML = "";
	if (detectIE() == true) {
		 Event.stopObserving(window, 'scroll', IEFixed);
	}
}


		