// JavaScript Document

//javascript:void(prompt('',gApplication.getMap().getCenter()));

var LEASE_FREQ = "/mth" ;

var IMAGEDIR = "images/";

var STATEBUTTON = "news"; //set the initial state
var SECONDARYSTATE = ""; //empty 
var TERTIARYSTATE = "";

//alert('hello');

function highlightButton(buttonName){
	//unhighlight previous state button
	if (document.getElementById(STATEBUTTON)){
		document.getElementById(STATEBUTTON).style.color = "#666666";
	}
	if (document.getElementById(buttonName)){
		document.getElementById(buttonName).style.color = "#FF0000";
	}
}

function hover(buttonName){
	//flushmainmenu();
	if (buttonName!=STATEBUTTON){
		document.getElementById(buttonName).style.color = "#FF0000";
	}
}

function clearbutton(button){
	document.getElementById(button).style.color = "#666666";
}

function unhover(buttonName){
	//not if state button
	if (buttonName!=STATEBUTTON){
		document.getElementById(buttonName).style.color = "#666666";
	}
}

function hovers(buttonName){
	if (buttonName!=SECONDARYSTATE){
		document.getElementById(buttonName).style.color = "#FF0000";
	}
}

function unhovers(buttonName){
	//not if state button
	if (buttonName!=SECONDARYSTATE){
		document.getElementById(buttonName).style.color = "#FFFFFF";
	}
}

function clickeds(buttonName){
	//first click, no buttons are currently selected
	//alert("here");
	if (SECONDARYSTATE==""){
	//alert("statebutton initialized");
		SECONDARYSTATE = buttonName;
		document.getElementById(SECONDARYSTATE).style.color = "#FF0000";
		if (buttonName=="sinfo"){
			launch_info();	
		} else if (buttonName=="sltf"){
			launch_ltf();
		} else if (buttonName=="smort"){
			launch_mort();
		} else if (buttonName=="sschool"){
			launch_school();
		}
	} else if (buttonName!=SECONDARYSTATE){ //the STATEBUTTON was not clicked
	//alert("statebutton not clicked");
		document.getElementById(SECONDARYSTATE).style.color = "#FFFFFF";
		document.getElementById(buttonName).style.color = "#FF0000";
		SECONDARYSTATE = buttonName;
		if (buttonName=="sinfo"){
			launch_info();	
		} else if (buttonName=="sltf"){
			launch_ltf();
		} else if (buttonName=="smort"){
			launch_mort();
		} else if (buttonName=="sschool"){
			launch_school();
		}
	} else if (buttonName==SECONDARYSTATE){ //the STATEBUTTON was clicked
	//alert("statebutton clicked");
		document.getElementById(SECONDARYSTATE).style.color = "#FFFFFF";
		SECONDARYSTATE = ""; //no buttons are clicked
		if (buttonName=="sinfo"){
			clear_window();	
		} else if (buttonName=="sltf"){
			clear_window();
		} else if (buttonName=="smort"){
			clear_window();
		} else if (buttonName=="sschool"){
			clear_window();
		}
	}
}

function launch_ltf(){
	//alert("launching")
	writeTag("features",writeLTF());
	bumpup();
}

function launch_info(){
	writeTag("features",writeFeatures());
	bumpup();
}

function launch_mort(){
	writeTag("features",writeMortgageCalc());
	calculateMortgage();
	bumpup();	
}

function launch_school(){
	//alert("here");
	writeTag("features",writeSchool());
	//calculateMortgage();
	bumpup();	
}

function clear_window(){
	writeTag("features",writeSchool());
	bumpdown();	
}



function loadText(txt){
	document.getElementById()
}



function loadbgimage(id,url){
	document.getElementById(id).style.backgroundImage = "url("+url+")";
}

function changeOpacity(tag,num){
	document.getElementById(tag).style.opacity = num;
	document.getElementById(tag).style.mozOpacity = num;
	var ienum = num * 100;
	document.getElementById(tag).style.filter = "alpha(opacity="+ienum+")";
}

function createTerLink(href,profile_num, id){
	var identity = profile_num+"_"+id;
	var identityQ = "'"+identity+"'";
	return '<a href="'+href+'"><img src="images/'+identity+'.jpg" id="'+identity+'" height="30px" class="tertiaryThumb" onMouseOver="javascript:imghoverT('+identityQ+');" onmouseout="javascript:imgunhoverT('+identityQ+');"></img></a>&nbsp;';
	
	//<a href="javascript:launchProfile('0001',5);"><img src="images/0001_00.jpg" id="0001" class="secondaryThumb" onMouseOver="javascript:imghover('0001');" onmouseout="javascript:imgunhover('0001');"/></a>
}

function createTerTextLink(href,profile_num, id, text){
	var identity = profile_num+"_"+id;
	var identityQ = "'"+identity+"'";
	return '<a href="'+href+'"><span id="'+identity+'" height="30px" class="tertiaryThumb" onMouseOver="javascript:imghoverT('+identityQ+');" onmouseout="javascript:imgunhoverT('+identityQ+');">'+text+'</span></a>&nbsp;';
	
	//<a href="javascript:launchProfile('0001',5);"><img src="images/0001_00.jpg" id="0001" class="secondaryThumb" onMouseOver="javascript:imghover('0001');" onmouseout="javascript:imgunhover('0001');"/></a>
}

function loadTertiaryBar(profile_num,num_images){
	var temp = document.getElementById("tertiaryBar");
	
	var html="";
	var i;
	var id;
	//create all thumbnail links
	for (i=0;i<=num_images-1;i++){
		if(i<10){
			id = "0"+i;	
		}
		html += createTerLink("javascript:void(0);",profile_num,id);					  	
	}
	
	//create map link
	html += createTerLink("http://maps.google.com/maps?q=7+Sulkara+Court",profile_num,"map");
	
	//create info link
	html += createTerTextLink("javascript:void(0);",profile_num,"info","INFO");	
	
	//alert(html);
	temp.innerHTML = html;
}

function highlightSelectedImage(profile_num){
	//unhighlight previous
	var temp;
	if (SECONDARYSTATE!=""){
		temp = document.getElementById(SECONDARYSTATE);
		temp.style.borderColor = "#330066";
	}
	temp = document.getElementById(profile_num);
	temp.style.borderColor = "#FF0000";
	SECONDARYSTATE = profile_num;
}

function imghover(profile_num){
	if (profile_num!=SECONDARYSTATE){
		document.getElementById(profile_num).style.borderColor = "#FF0000";
	}
}

function imgunhover(profile_num){
	//not if state button
	if (profile_num!=SECONDARYSTATE){
		document.getElementById(profile_num).style.borderColor = "#CC66FF";
	}
}

function imghoverT(profile_num){
	if (profile_num!=TERTIARYSTATE){
		document.getElementById(profile_num).style.borderColor = "#FF0000";
		var mode = profile_num.substring(5,profile_num.length);
		if (mode=="info"){
			//show info window	
		} else if (mode=="map") {
			//show map and location	
		} else {
			//load image
			loadbgimage("profileMainWin",IMAGEDIR+profile_num+".jpg");
			//alert("here");	
		}
	}
}

function imgunhoverT(profile_num){
	//not if state button
	if (profile_num!=TERTIARYSTATE){
		document.getElementById(profile_num).style.borderColor = "#CC66FF";
	}
}

function launchProfile(profile_num,num_images){
	//alert("launching"+profile_num);
	highlightSelectedImage(profile_num);
	//loadFirstImage();
	loadTertiaryBar(profile_num,num_images);
	//selectFirstTerThumb();
}

function changeCursor(){
	//document.getElementById(id).style.cursor;	
}

/*
_00 to _NN full bleed background images: dim 545px x 380px
_info toggle translucent info window (pop-up div at 80-90% opacity)
_map queryable embedded google map?, track all properties? giant map as profiles home page... search by geographical zone?
aggregate view to detailed view back to aggregate view
map icon
_print download a pdf version of this listing (for simple printing purposes)
print icon
*/

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  return "";//alert('Query Variable ' + variable + ' not found');
}

function dressPrice(invar){
	var temp;
	var temp2;
	var temp3;
	var templast = "";
	
	//if a decimal exists, extract this portion
	if (invar.substring(invar.length-3,invar.length-2)=="."){
		templast = invar.substring(invar.length-3,invar.length);
		invar = invar.substring(0,invar.length-3);
	}
	
	temp = invar.substring(invar.length-3,invar.length);
	temp2 = invar.substring(invar.length-6,invar.length-3);
	if (invar.length==7){
		temp3 = invar.substring(0,1);
		return temp3 + "," + temp2 + "," + temp + templast;
	} else if (invar.length<=3){
		return temp + templast;	
	}

	return temp2 + "," + temp + templast;
}


function getSelectedValue(id){
	sel = document.getElementById(id);
	return sel.options[sel.selectedIndex].value;
}

function disableSelect(sel) {
	document.getElementById(sel).disabled = true;
}

function enableSelect(sel) {
	document.getElementById(sel).disabled = false;
}

function getRadioValue() {
	var num_radio = 5;
	var i;
	for (i = 0; i <= num_radio-1; i++) {
		if (document.myform.RadioGroup1[i].checked) {
			return document.myform.RadioGroup1[i].value;
		}
	}
	return "";
}

function getBrowser(){
	//Microsoft Internet Explorer
	//Netscape (for Firefox and Safari)
	return navigator.appName;
}

function getBrowserVersion(){
	return navigator.appVersion.substring(22,23);
}

function isChecked(id){
	if (document.getElementById(id).checked==true){
		return true;	
	} else {
		return false;	
	}
}

function getTextFieldValue(textfield){
	return document.getElementById(textfield).value;	
}


function writeSelect(id,opts,vals){
	//if (BROWSER=="Microsoft Internet Explorer"){
	//	for (var i=0;i<=opts.length-1;i++){
	//	addOption(opts[i],vals[i]);
	//	}
	//} else { //"Netscape"
	//	var temp = document.getElementById(id);
	//	var html = "";
		for (var i=0;i<=opts.length-1;i++){
	//		html += '<option value="'+vals[i]+'">'+opts[i]+'</option>';
			addOption(id,opts[i],vals[i]);
		}
	//	temp.innerHTML = html;
	//}
}

function addOption(id,opt,val){
	//var anOption = document.createElement("OPTION");
	var temp = document.getElementById(id);
	var selLength = temp.options.length;
	//temp.options.add(anOption);
	//anOption.innerText = opt;
	//anOption.Value = val;
	
	temp.options[selLength] = new Option(opt,val);
}

function valueExists(inarray,val){
	for (var i=0;i<=inarray.length-1;i++){
		if (inarray[i]==val){
			return true;
		}
	}
	return false;
}

function sortSelect(vals,opts){
	var n = vals.length;
	var swapped;
	var i;
	var temp;
	var temp2;
	do {
		swapped = false;
		n = n-1;
		for (i=0;i<=n-1;i++){
			if (vals[i] > vals[i+1]){
				//swap
				temp = vals[i];
				temp2 = opts[i];
				vals[i] = vals[i+1];
				opts[i] = opts[i+1];
				vals[i+1] = temp;
				opts[i+1] = temp2;
				swapped = true;
			}
		}
	}while(swapped);
}

function getDisplayPrice(price){
	return "$"+dressPrice(price);
}

function getDisplayLeasePrice(lease_price){
	return getDisplayPrice(lease_price)+LEASE_FREQ;
}

function getDisplayPriceAndLease(price,lease_price){
	return getDisplayPrice(price) + " or " + getDisplayLeasePrice(lease_price);
}
	
function eco_button_hover(){
	document.getElementById("ecobutton").style.color = "#92CD81";	
}

function eco_button_unhover(){
	document.getElementById("ecobutton").style.color = "#FFFFFF";	
}

function tip_button_hover(){
	document.getElementById("tipbutton").style.color = "#92CD81";	
}

function tip_button_unhover(){
	document.getElementById("tipbutton").style.color = "#FFFFFF";	
}

function econews_button_hover(id){
	document.getElementById(id).style.color = "#92CD81";
	//alert("hover");
}

function econews_button_unhover(id){
	document.getElementById(id).style.color = "#FFFFFF";	
	//alert("unhover");
}