//////////////////////////////
// INIT SCREEN DETERMINATION

function getCSSRule(ruleName, deleteFlag) {               // Return requested style obejct
   ruleName=ruleName.toLowerCase();                       // Convert test string to lower case.
   if (document.styleSheets) {                            // If browser can play with stylesheets
      for (var i=0; i<document.styleSheets.length; i++) { // For each stylesheet
         var styleSheet=document.styleSheets[i];          // Get the current Stylesheet
         var ii=0;                                        // Initialize subCounter.
         var cssRule=false;                               // Initialize cssRule. 
         do {                                             // For each rule in stylesheet
            if (styleSheet.cssRules) {                    // Browser uses cssRules?
               cssRule = styleSheet.cssRules[ii];         // Yes --Mozilla Style
            } else {                                      // Browser usses rules?
               cssRule = styleSheet.rules[ii];            // Yes IE style. 
            }                                             // End IE check.
            if (cssRule)  {                               // If we found a rule...
               if (cssRule.selectorText.toLowerCase()==ruleName) { //  match ruleName?
                  if (deleteFlag=='delete') {             // Yes.  Are we deleteing?
                     if (styleSheet.cssRules) {           // Yes, deleting...
                        styleSheet.deleteRule(ii);        // Delete rule, Moz Style
                     } else {                             // Still deleting.
                        styleSheet.removeRule(ii);        // Delete rule IE style.
                     }                                    // End IE check.
                     return true;                         // return true, class deleted.
                  } else {                                // found and not deleting.
                     return cssRule;                      // return the style object.
                  }                                       // End delete Check
               }                                          // End found rule name
            }                                             // end found cssRule
            ii++;                                         // Increment sub-counter
         } while (cssRule)                                // end While loop
      }                                                   // end For loop
   }                                                      // end styleSheet ability check
   return false;                                          // we found NOTHING!
}                                                         // end getCSSRule 

function killCSSRule(ruleName) {                          // Delete a CSS rule   
   return getCSSRule(ruleName,'delete');                  // just call getCSSRule w/delete flag.
}                                                         // end killCSSRule

function addCSSRule(ruleName) {                           // Create a new css rule
   if (document.styleSheets) {                            // Can browser do styleSheets?
      if (!getCSSRule(ruleName)) {                        // if rule doesn't exist...
         if (document.styleSheets[0].addRule) {           // Browser is IE?
            document.styleSheets[0].addRule(ruleName, null,0);      // Yes, add IE style
         } else {                                         // Browser is IE?
            document.styleSheets[0].insertRule(ruleName+' { }', 0); // Yes, add Moz style.
         }                                                // End browser check
      }                                                   // End already exist check.
   }                                                      // End browser ability check.
   return getCSSRule(ruleName);                           // return rule we just created.
} 

var browserHeight;
var browserHeightFlip = 640;
var global;
function adjustFixed() {

	if (self.innerHeight) {
		browserHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		browserHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		browserHeight = document.body.clientHeight;
	}

	if (browserHeight < browserHeightFlip) {
		global = getCSSRule('.dropShadow');
		global.style.position = "absolute";
		global = getCSSRule('#topContentWrapper');
		global.style.position = "relative";
		global = getCSSRule('#mainContent');
		global.style.paddingTop = "0";
		global = getCSSRule('#widgetsDiv');
		global.style.padding = "0 0 55px 0";
		global = getCSSRule('#subscribeDiv');
		global.style.position = "relative";
		global.style.height = "1px";
	} else {
		global = getCSSRule('.dropShadow');
		global.style.position = "fixed";
		global = getCSSRule('#topContentWrapper');
		global.style.position = "fixed";
		global = getCSSRule('#mainContent');
		global.style.paddingTop = "124px";
		global = getCSSRule('#widgetsDiv');
		global.style.padding = "1px 0 55px 0";
		global = getCSSRule('#subscribeDiv');
		global.style.position = "fixed";
	}
	
}
adjustFixed();


var fibernocciOne = 2;
var fibernocciTwo = 3;

////////////////////////////////////////////////////////////////////////////////////
// ZIPCODE

function updateZip(zip) {
	if (!isWorking && http) {
		http.open("GET", "/ajax/update_zip.html?zip=" + zip, true);
		http.onreadystatechange = handleHttpResponse;
		isWorking = true;
		http.send(null);
	}
}

function handleHttpResponse() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			results = http.responseText;
			location = "http://www.teamap.com/states/state_"+results+"_Distance.html";
			isWorking = false;
		}
	}
}

////////////////////////////////////////////////////////////////////////////////////
// ROLLOVER

function photosPreview(id, exteriorTest, interiorTest) {
	if (exteriorTest) {
		document.getElementById("previewExterior_"+id).src = "/images/exterior/exterior_"+id+".jpg";
	}
	if (interiorTest) {
		document.getElementById("previewInterior_"+id).src = "/images/interior/interior_"+id+".jpg";
	}
}

////////////////////////////////////////////////////////////////////////////////////
// EVENTS

var eventsStatus = "stopped";
var eventsHeight = 0;
var eventsHeightMax = 0;
var eventsNumber = 0;

function showEvents(number) {
	eventsNumber = number;
	if (eventsStatus != "in transit") {
		fibernocciOne = 1;
		fibernocciTwo = 2;
		if (document.getElementById("eventsExtra").style.height == "1px") {
			eventsStatus = "in transit";
			eventsHeight = 1;
			openEventsLoop=setInterval("openEvents()",100);
		} else {
			eventsStatus = "in transit";
			eventsHeight = 1;
			eventsHeightMax = document.getElementById("eventsExtra").clientHeight;
			closeEventsLoop=setInterval("closeEvents()",100);
		}
	}
}

function openEvents() {
	eventsHeight = fibernocciOne + fibernocciTwo;
	fibernocciOne = fibernocciTwo;
	fibernocciTwo = eventsHeight;

	if (eventsHeight >= 50) {
		document.getElementById("eventsExtra").style.height = "auto";
		clearInterval(openEventsLoop);
		eventsStatus = "stopped";
		document.getElementById("eventsMoreCopy").innerHTML = "hide";
	} else {
		document.getElementById("eventsExtra").style.height = eventsHeight + "px";
	}
}

function closeEvents() {
	eventsHeight = fibernocciOne + fibernocciTwo;
	fibernocciOne = fibernocciTwo;
	fibernocciTwo = eventsHeight;
	
	if (eventsHeight >= eventsHeightMax) {
		document.getElementById("eventsExtra").style.height = "1px";
		clearInterval(closeEventsLoop);
		eventsStatus = "stopped";
		document.getElementById("eventsMoreCopy").innerHTML = "show " + eventsNumber + " more";
	} else {
		document.getElementById("eventsExtra").style.height = (eventsHeightMax - eventsHeight) + "px";
	}
}


////////////////////////////////////////////////////////////////////////////////////
// DEALS

var dealsStatus = "stopped";
var dealsHeight = 0;
var dealsHeightMax = 0;
var dealsNumber = 0;

function showDeals(number) {
	dealsNumber = number;
	if (dealsStatus != "in transit") {
		fibernocciOne = 1;
		fibernocciTwo = 2;
		if (document.getElementById("dealsExtra").style.height == "1px") {
			dealsStatus = "in transit";
			dealsHeight = 1;
			openDealsLoop=setInterval("openDeals()",100);
		} else {
			dealsStatus = "in transit";
			dealsHeight = 1;
			dealsHeightMax = document.getElementById("dealsExtra").clientHeight;
			closeDealsLoop=setInterval("closeDeals()",100);
		}
	}
}

function openDeals() {
	dealsHeight = fibernocciOne + fibernocciTwo;
	fibernocciOne = fibernocciTwo;
	fibernocciTwo = dealsHeight;

	if (dealsHeight >= 50) {
		document.getElementById("dealsExtra").style.height = "auto";
		clearInterval(openDealsLoop);
		dealsStatus = "stopped";
		document.getElementById("dealsMoreCopy").innerHTML = "hide";
	} else {
		document.getElementById("dealsExtra").style.height = dealsHeight + "px";
	}
}

function closeDeals() {
	dealsHeight = fibernocciOne + fibernocciTwo;
	fibernocciOne = fibernocciTwo;
	fibernocciTwo = dealsHeight;
	
	if (dealsHeight >= dealsHeightMax) {
		document.getElementById("dealsExtra").style.height = "1px";
		clearInterval(closeDealsLoop);
		dealsStatus = "stopped";
		document.getElementById("dealsMoreCopy").innerHTML = "show " + dealsNumber + " more";
	} else {
		document.getElementById("dealsExtra").style.height = (dealsHeightMax - dealsHeight) + "px";
	}
}


////////////////////////////////////////////////////////////////////////////////////
// SUBSCRIBE

var subscribeStatus = "stopped";
var subscribeHeight = 0;

function showSubscribe() {
	if (subscribeStatus != "in transit") {
		fibernocciOne = 1;
		fibernocciTwo = 2;
		if (document.getElementById("subscribe").style.height == "1px") {
			subscribeStatus = "in transit";
			subscribeHeight = 1;
			openSubscribeLoop=setInterval("openSubscribe()",100);
		} else {
			subscribeStatus = "in transit";
			subscribeHeight = 1;
			closeSubscribeLoop=setInterval("closeSubscribe()",100);
		}
	}
}

function openSubscribe() {
	subscribeHeight = fibernocciOne + fibernocciTwo;
	fibernocciOne = fibernocciTwo;
	fibernocciTwo = subscribeHeight;

	if (subscribeHeight >= 150) {
		document.getElementById("subscribe").style.height = "150px";
		clearInterval(openSubscribeLoop);
		subscribeStatus = "stopped";
	} else {
		document.getElementById("subscribe").style.height =subscribeHeight + "px";
	}
}

function closeSubscribe() {
	subscribeHeight = fibernocciOne + fibernocciTwo;
	fibernocciOne = fibernocciTwo;
	fibernocciTwo = subscribeHeight;
	
	if (subscribeHeight >= 140) {
		document.getElementById("subscribe").style.height = "1px";
		clearInterval(closeSubscribeLoop);
		subscribeStatus = "stopped";
	} else {
		document.getElementById("subscribe").style.height = (140 - subscribeHeight) + "px";
	}
}

function sendSubscribe() {
	if (!isWorking && http) {
		if (document.forms['subscribeForm']['email'].value) {
			http.open("GET", "ajax/subscribe.html?email=" + document.forms['subscribeForm']['email'].value + "&firstName=" + document.forms['subscribeForm']['firstName'].value + "&lastName=" + document.forms['subscribeForm']['lastName'].value + "&zip=" + document.forms['subscribeForm']['zip'].value, true);
			http.onreadystatechange = handleHttpResponseTwo;
			isWorking = true;
			http.send(null);
		}
	}
}

function handleHttpResponseTwo() {
	if (http.readyState == 4) {
		if (http.responseText.indexOf('invalid') == -1) {
			results = http.responseText;
			
			rexp = /success/;
			if (rexp.test(results)) {
				document.getElementById("subscribe").innerHTML = "<DIV STYLE='font-size:9pt;padding-top:17px;color:#786b05;'><CENTER>Thank you for signing up</CENTER></DIV>";
			}
			
			rexp = /duplicate/;
			if (rexp.test(results)) {
				document.getElementById("subscribe").innerHTML = "<DIV STYLE='font-size:9pt;padding-top:17px;color:#786b05;'><CENTER>You are already signed up</CENTER></DIV>";
			}
			
			isWorking = false;
		}
	}
}


////////////////////////////////////////////////////////////////////////////////////
// GOOGLE MAPS

function pickState(stateAbbr) {
	if (stateAbbr) {
		if (stateAbbr.match( "UK_" )){
			location = "/United_Kingdom/"+stateAbbr+"_Reviews.html";
		} else if (stateAbbr.match( "CA_" )){
			location = "/Canada/"+stateAbbr+"_Reviews.html";
		} else {
			location = "/states/state_"+stateAbbr+"_Reviews.html";
		}
	}
}

var map = null;

function StartMap() {
	if (GBrowserIsCompatible()) {
		document.getElementById("mapWrapper").innerHTML="<div id='map' style='width: 707px; height: 400px; border: solid #a6a38b 1px;'></div>";
		document.getElementById("iconWrapper").innerHTML="<A HREF='#' ONCLICK='HideMap();return false'>hide map</A><BR>";
		initMap();
	}
}

function expand() {
	newHeight = newHeight + 10;
	if (newHeight > 400) {newHeight = 400;stop();}
	document.getElementById("map").style.height = newHeight;
	
	newWidth = newWidth + 18;
	if (newWidth > 707) {newWidth = 707;}
	document.getElementById("map").style.width = newWidth;
}

function stop() {
	clearInterval(googlemap);
	document.getElementById("map").style.height = 400;
	document.getElementById("map").style.width = 707;
}

function HideMap() {
	newHeight = 400;
	newWidth = 707;
	googlemap=setInterval('contract()',10);
	setTimeout('erase()',2500)
}

function contract() {
	newHeight = newHeight - 10;
	if (newHeight < 50) {newHeight = 50;erase();}
	document.getElementById("map").style.height = newHeight;
	
	newWidth = newWidth - 18;
	if (newWidth < 100) {newWidth = 100}
	document.getElementById("map").style.width = newWidth;
}

function erase() {
	clearInterval(googlemap);
	document.getElementById("mapWrapper").innerHTML="<IMG WIDTH='1' HEIGHT='1' BORDER='0' SRC='/images/blank.gif'><BR>";
	document.getElementById("iconWrapper").innerHTML="<A HREF='#' ONCLICK='StartMap();return false'><IMG WIDTH='100' HEIGHT='50' BORDER='0' SRC='/images2/google_map_icon_small.gif'><BR>show map<BR></A>";
}

function createTabbedMarker(point,htmls,labels) {
	  
	var marker = new GMarker(point);
	
	GEvent.addListener(marker, "click", function() {
		// adjust the width so that the info window is large enough for this many tabs
		if (htmls.length > 1) {
			htmls[0] = '<div style="min-width:'+htmls.length*88+'px;">' + htmls[0] + '</div>';
		}
		var tabs = [];
		for (var i=0; i<htmls.length; i++) {
			tabs.push(
				new GInfoWindowTab(
					labels[i],
					'<div class="width:300px;overflow:auto;text-align:left;">' + htmls[i] + '</div>'
					)
				);
		}
		marker.openInfoWindowTabsHtml(tabs);
		});
		
	return marker;

}