// globals

var CO = new _CO();
window.onload = function() { CO.onPageLoad(); }

// classdef

function _CO()
{
	this._aImages = new Array();
	
	this.onPageLoad = _CO_onPageLoad;
	this.onPageLoad_Add = _CO_onPageLoad_Add;

	this.initImages = _CO_initImages;
	this.rollImage = _CO_rollImage;
	
	this.isOnIE4 = _CO_isOnIE4;
	
	this.playVideo = _CO_playVideo;
	this.clickToCall = _CO_clickToCall;
}

function _CO_onPageLoad()
{
	if (typeof(OnPageLoad) == "function") OnPageLoad();
	if (this._aOnPageLoadFunc != null) {
		for (var i = 0; i < this._aOnPageLoadFunc.length; i++) {
			var f = this._aOnPageLoadFunc[i];
			if (typeof(f) == "function") f();
		}
	}
}
	
function _CO_onPageLoad_Add(f)
{
	if (this._aOnPageLoadFunc == null) this._aOnPageLoadFunc = new Array();
	this._aOnPageLoadFunc[this._aOnPageLoadFunc.length] = f;
}
	
function _CO_initImages(/*arguments*/)
{
	var j = this._aImages.length;
	for (var i = 0; i < arguments.length; i += 2) {
		var s0 = arguments[i];
		var s1 = arguments[i+1], o1 = new Image(); o1.src = s1;
		this._aImages[j++] = new Array(s0, s1, o1);
	}
}

function _CO_rollImage(t, n)
{
	var sSrc = t.src;
	for (var i = 0; i < this._aImages.length; i++) {
		var a = this._aImages[i], sImg = a[n];
		if (sSrc.indexOf(sImg) == (sSrc.length - sImg.length)) {
			t.src = a[n == 0 ? 1 : 0];
			break;
		}	
	}
}

function _CO_isOnIE4()
{
	return (navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4);
}

function _CO_playVideo(u, b)
{
	var w = window.open("/playvideo.asp?u="+u, "wVideo", "scrollbars=0,menubar=0,width=410,height=310");
	w.focus();
}

function _CO_clickToCall()
{
	var w = window.open("https://prodca.click4talk.com/zt/plink/popup.php?cid=9045&linker=1&referral="+location.href,"_blank","height=380,width=530,left=20,top=20,resizable=no,menubar=0,toolbar=0,status=0");
	w.focus();
}


//Code for correct links

function GetSearchLink (selectedCity, selectedZip)
{
	var sUrl = "";
	var urchinParam = "";
	
	var aSearchLink = new Array();
	
	switch(selectedZip)
	{
		//ziprealty 100c links
		case "92003":
		case "92007":
		case "92008":
		case "92009":
		case "92010":
		case "92011":
		case "92130":
		case "92118":
		case "92014":
		case "92024":
		case "92037":
		case "92145":
		case "92107":
		case "92054":
		case "92056":
		case "92057":
		case "92058":
		case "92109":
		case "92106":
		case "92064":
		case "92127":
		case "92128":
		case "92067":
		case "92091":
		case "92121":
		case "92069":
		case "92078":
		case "92131":
		case "92075":
		case "92124":
		case "92122":
			urchinParam ="/cpc/ziprealty/100c/";
			sUrl = "ziprealty";
			break;
			
		//ziprealty 75c links
		case "91901":
		case "92110":
		case "91902":
		case "92117":
		case "92115":
		case "92120":
		case "92101":
		case "92025":
		case "92026":
		case "92027":
		case "92029":
		case "92028":
		case "92103":
		case "91932":
		case "91935":
		case "91941":
		case "91942":
		case "92040":
		case "92111":
		case "92126":
		case "92108":
		case "92116":
		case "92104":
		case "92065":
		case "92129":
		case "92123":
		case "92081":
		case "92083":
		case "92084":
			urchinParam ="/cpc/ziprealty/75c/";
			sUrl = "ziprealty";
			break;
		//Homegain 12c links
		case "92004":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/borrego.html?entryid=11670";
			break;
		case "91905":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/boulevard.html?entryid=11670";
			break;
		case "91906":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/campo.html?entryid=11670";
			break;
		case "91910":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/chula-vista.html?entryid=11670";
			break;
		case "91911":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/chula-vista.html?entryid=11670";
			break;
		case "91913":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/chula-vista.html?entryid=11670";
			break;
		case "91914":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/chula-vista.html?entryid=11670";
			break;
		case "91915":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/chula-vista.html?entryid=11670";
			break;
		case "92105":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/city-heights.html?entryid=11670";
			break;
		case "91916":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/descanso.html?entryid=11670";
			break;
		case "92019":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/el-cajon.html?entryid=11670";
			break;
		case "92020":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/el-cajon.html?entryid=11670";
			break;
		case "92021":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/el-cajon.html?entryid=11670";
			break;
		case "92114":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/encanto.html?entryid=11670";
			break;
		case "92102":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/golden-hill.html?entryid=11670";
			break;
		case "91931":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/guatay.html?entryid=11670";
			break;
		case "91934":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/jacumba.html";
			break;
		case "92036":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/julian.html?entryid=11670";
			break;
		case "91945":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/lemon-grove.html?entryid=11670";
			break;
		case "91948":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/mount-laguna.html?entryid=11670";
			break;
		case "91950":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/national-city.html?entryid=11670";
			break;
		case "92154":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/nestor.html?entryid=11670";
			break;
		case "92059":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/pala.html?entryid=11670";
			break;
		case "92060":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/palamor-mountain.html?entryid=11670";
			break;
		case "92139":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/paradise-hill.html?entryid=11670";
			break;
		case "92061":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/pauma-valley.html?entryid=11670";
			break;
		case "91962":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/pine-valley.html?entryid=11670";
			break;
		case "91963":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/portero.html?entryid=11670";
			break;
		case "92066":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/ranchita.html?entryid=11670";
			break;
		case "92119":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/san-carlos.html?entryid=11670";
			break;
		case "92173":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/santa-ysisdro.html";
			break;
		case "92070":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/santa-ysabel.html";
			break;
		case "92071":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/sante.html?entryid=11670";
			break;
		case "91977":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/spring-valley.html?entryid=11670";
			break;
		case "91978":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/spring-valley.html?entryid=11670";
			break;
		case "91980":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/tecate.html?entryid=11670";
			break;
		case "92082":
			urchinParam = "/cpc/homegain/12c/";
			sUrl = "http://bl.homegain.com/CA/valley-center.html?entryid=11670";
			break;
		//others
		case "91917":
			sUrl = "http://www.zteamrealestate.com/frames.asp?URL=http://idxpro.cisdata.net/AR188424/";
			break;
		case "92113":
			sUrl = "http://www.zteamrealestate.com/frames.asp?URL=http://idxpro.cisdata.net/AR188424/";
			break;			
		default:
			urchinParam = "";
			break;		
	}
	aSearchLink["url"] = sUrl;
	aSearchLink["urchinparam"] = urchinParam;
	return aSearchLink;	
}

function toTitleCase(str)
{
	// Change the case of characters in a string making the first letter of 
	//each word uppercase and the others lowercase.
	// Individual words are delimited by a space, a hypen, or an underscore
	var strOut = "";
	for (var i=0; i<str.length; i++)
	if (i==0 || str.charAt(i-1) == " " || str.charAt(i-1) == "-" || 
	str.charAt(i-1) == "_")
	strOut += str.charAt(i).toUpperCase();
	else strOut += str.charAt(i).toLowerCase();
	return strOut;
}
