// TODO
// 1	fix attachEvent/event.srcElement code for firefox

// find body element and bind init event
window.onload = function() {
	var o = document.getElementsByTagName("BODY")
	if (o) { cp.init() }
}
// creditplus prime object literal - encapsulates all sg client script
cp = {
	oTimer:undefined,
	oFixedSubNav:undefined,
	init:function() {
		cp.makeRollovers()
		cp.targetNav()
		if (!document.all) {			// firefox
			if ($("innerwrapper")) {
				if ($("innerwrapper").childNodes.length > 0) {
					var o = $("innerwrapper").childNodes[1]
					if (o.nodeName == "IMG") {
						o.style.cssFloat = "left"
					}
				}
			}
			if ($("navbar")) {
				 $("navbar").style.marginTop = "-4px"
			}
		}
	},
	preload:function(sURL) { var o = new Image; o.src = sURL },
	makeRollovers:function() {
		var a = document.getElementsByClassName("rollover")
		for (i=0;i<a.length;i++) {
			var o = a[i]
			if (o.src.indexOf(".gif") != -1) { cp.preload(o.src.replace(".gif","-a.gif")) }
			if (o.src.indexOf(".jpg") != -1) { cp.preload(o.src.replace(".jpg","-a.jpg")) }
			o.onmouseover = cp.roll
			o.onmouseout = cp.unroll
//ff		o.addEventListener("mouseover",cp.roll,false)
//ie		o.attachEvent("onmouseover", cp.roll)
//ie		o.attachEvent("onmouseout", cp.unroll)
			o.style.cursor = "pointer"
		}
	},
	targetNav:function() {
		var a = document.getElementsByClassName("navcurrent")
		for (i=0;i<a.length;i++) {
			var o = a[i]
			a[i].src = a[i].src.replace(".jpg","-a.jpg")
		}
		//var a = document.getElementsByClassName("nav")

		//for (i=0;i<a.length;i++) {
		//	var o = a[i]
			//o.attachEvent("onmouseover", cp.showSubNav)
			//o.attachEvent("onmouseout", cp.resetSubNavMouse)
		//}
		//$("subnavbarwrapper").attachEvent("onmouseover", cp.subNavActive)
		//$("subnavbarwrapper").attachEvent("onmouseout", cp.subNavInactive)
		
		// find the default subnav for this page
		if (document.getElementsByClassName("subnavcurrent").length > 0) {
			cp.oFixedSubNav = document.getElementsByClassName("subnavcurrent")[0]
		} else {
			cp.oFixedSubNav = undefined
		}
	},
	subNavActive:function() { $("subnavbarwrapper").ismouseover = true },
	subNavInactive:function() {
		if (cp.oTimer) { window.clearTimeout(cp.oTimer) }
		cp.oTimer = window.setTimeout(cp.clearSubNav,1000); $("subnavbarwrapper").ismouseover = false
	},
	resetSubNavMouse:function() {
		event.srcElement.ismouseover = false
	},
	showSubNav:function() {
		event.srcElement.ismouseover = true
		var o = $(event.srcElement.id.replace("imgNav","subnav"))
		if (o) {
			if (cp.oTimer) { window.clearTimeout(cp.oTimer) }
			var p = document.getElementsByClassName("subnavtemp")
			for (i=0;i<p.length;i++) {
				Element.removeClassName(p[i],"subnavtemp")
			}
			Element.removeClassName(cp.oFixedSubNav,"subnavcurrent")
			Element.addClassName(o,"subnavtemp")
			if (cp.oTimer) { window.clearTimeout(cp.oTimer) }
			cp.oTimer = window.setTimeout(cp.clearSubNav,2000)
		}
	},
	clearSubNav:function() {
		var o = document.getElementsByClassName("subnavtemp")
		for (i=0;i<o.length;i++) {
			var oi = $(o[i].id.replace("subnav","imgNav"))
			if (oi.ismouseover == false && $("subnavbarwrapper").ismouseover == false) {
				Element.removeClassName(o[i],"subnavtemp")
				Element.addClassName(cp.oFixedSubNav,"subnavcurrent")
			} else {
				if (cp.oTimer) { window.clearTimeout(cp.oTimer) }
				cp.oTimer = window.setTimeout(cp.clearSubNav,2000)
			}
		}
	},
	roll:function() {
		if (this.src.indexOf("-a.") == -1) {
			this.src = this.src.replace(".gif","-a.gif")
			this.src = this.src.replace(".jpg","-a.jpg")
		}
	},
	unroll:function() {
		if (!Element.hasClassName(this,"navcurrent")) {
			if (this.src.indexOf("-a.") != -1) {
				this.src = this.src.replace("-a.gif",".gif")
				this.src = this.src.replace("-a.jpg",".jpg")
			}
		}
	},
	
	// business
	calcLoanAmount:function(cDeposit,cMonthlyBudget,iTerm) {
		// calc globals
		// ************
		var calcAPR = 5.5
		var calcDeposit = cDeposit
		var calcMonthlyBudget = cMonthlyBudget
		var calcTerm = iTerm
		
		var calcBase = calcAPR / 2.2
		var calcTotalPay = calcMonthlyBudget * calcTerm
		var calcPctFrac = 100/(calcBase+100)
		
		// calc amount of interest over term
		var calcTempVal = calcTotalPay
		for (i=0;i<calcTerm/12;i++) { calcTempVal = calcTempVal * calcPctFrac }
		var calcAmountToSpend = parseFloat(calcTempVal) + parseFloat(calcDeposit)
		var calcTotalInterest = calcTotalPay - calcAmountToSpend
		return calcAmountToSpend
	}
}
