function calculateSumOffset(idItem, offsetName){
        var totalOffset = 0;
        var item = eval('idItem');
        do{
            totalOffset += eval('item.'+offsetName);
            item = eval('item.offsetParent');
        } while (item != null);
        return totalOffset;
}


function getElementsByClassName(node, classname)
{
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0,j=els.length; i<j; i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

function roundDecimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return padWithZeros(result3, decimals)
}

function padWithZeros(rounded_value, decimal_places) {
    var value_string = rounded_value.toString()
    var decimal_location = value_string.indexOf(".")
    if (decimal_location == -1) {
        decimal_part_length = 0
        value_string += decimal_places > 0 ? "." : ""
    } else {
        decimal_part_length = value_string.length - decimal_location - 1
    }
    var pad_total = decimal_places - decimal_part_length
    if (pad_total > 0) {
        // Pad the string with 0s
        for (var counter = 1; counter <= pad_total; counter++) {
            value_string += "0";
        }
    }
    return value_string
}


function getItemAmount(rowNum)
{
    var price = $('cartRow'+cart_id[rowNum]+'-amount').innerHTML;
    var price = price.escapeHTML();
    var priceVal = '';
    for (var j=1; j<price.length; j++) {
       priceVal += price[j];
    }
    return parseFloat(priceVal);
}

function getTaxPercentage()
{
    var centage = $('cartTax-percentage').innerHTML;
    return parseFloat(centage);
}

function updateTotals(base, taxPercentage) {
    $('cartSubtotal').innerHTML = '$' + roundDecimals(base,2);
    $('cartTax').innerHTML = '$' + roundDecimals((taxPercentage * base),2);
    $('cartTotal').innerHTML = '$' + roundDecimals(((1+taxPercentage) * base),2);
}

/* assigns events to elements using behaviour.js */
var topic = "";
var eventsAssign = {
	'#shopCart input' : function(el) {

		el.onchange = function() {
			var rowNum = el.name.replace('cart','');
			$('cartRow'+rowNum+'-indicator').src = "/images/indicator.gif";
			var price = $('cartRow'+rowNum+'-price').innerHTML;
			var price = price.escapeHTML();
			var priceVal = '';
			for (var j=1; j<price.length; j++) {
			    priceVal += price[j];
			}
			var newVal = priceVal * el.value;
			$('cartRow'+rowNum+'-amount').innerHTML = '$'+ newVal;
			$('cartRow'+rowNum+'-indicator').src = "/images/indicatorWH.gif";
			totalPrice = 0;

			for (var j=1; j<=$('cart-item-count').value; j++) {
			    totalPrice += getItemAmount(j);
			}

                        updateTotals(totalPrice, getTaxPercentage());
		}
	 },

	'#top-nav ul.cat-list li' : function(el) {
		el.onmouseover = function() {
			if (el.className == "empty" || el.className == "last empty" || el.className == "empty ") {
				el.style.background = '#fffbf5 url(/images/comingsoon2.gif) no-repeat right';
				if(el.style.setProperty) {
					el.style.setProperty('background', '#fffbf5 url(/images/comingsoon2.gif) no-repeat right', null);
				}
				el.style.cssText = 'background:#fffbf5 url(/images/comingsoon2.gif) no-repeat right';

			} else {
			el.style.background = "#fffbf5";

			}
		}
		el.onmouseout = function() {
			if (el.className == "empty" || el.className == "last empty" || el.className == "empty ") {
				el.style.background = 'transparent url(/images/comingsoon2.gif) no-repeat right';
				if(el.style.setProperty) {
					el.style.setProperty('background', 'transparent url(/images/comingsoon2.gif) no-repeat right', null);
				}
				el.style.cssText = 'background:transparent url(/images/comingsoon2.gif) no-repeat right';
			} else {
			el.style.background = 'transparent';
			}
		}
	},

	'a.helpLink' : function(el) {
		el.onmouseover = function() {
			el.getElementsByTagName('img')[0].src = "/images/helpButton02.gif";
		}
		el.onmouseout = function() {
			el.getElementsByTagName('img')[0].src = "/images/helpButton01.gif";
		}
	},

	'a.helpLink2' : function(el) {
		el.onmouseover = function() {
			el.getElementsByTagName('img')[0].src = "/images/helpButton02.gif";
		}
		el.onmouseout = function() {
			el.getElementsByTagName('img')[0].src = "/images/helpButton01.gif";
		}
	},
	'img#marriage' : function(el) {
		el.onmouseover = function() {
			el.src = "/images/marriage-Roll.gif";
		}
		el.onmouseout = function() {
			el.src = "/images/marriage.gif";
		}
	},
	'img#parenting' : function(el) {
		el.onmouseover = function() {
			el.src = "/images/parenting-Roll.gif";
		}
		el.onmouseout = function() {
			el.src = "/images/parenting.gif";
		}
	},
	'img#intimacy' : function(el) {
		el.onmouseover = function() {
			el.src = "/images/intimacy-Roll.gif";
		}
		el.onmouseout = function() {
			el.src = "/images/intimacy.gif";
		}
	},'img#healthyliving' : function(el) {
		el.onmouseover = function() {
			el.src = "/images/healthyliving-Roll.gif";
		}
		el.onmouseout = function() {
			el.src = "/images/healthyliving.gif";
		}
	},'img#legal' : function(el) {
		el.onmouseover = function() {
			el.src = "/images/leg-Roll.gif";
		}
		el.onmouseout = function() {
			el.src = "/images/leg.gif";
		}
	},
	'img#money' : function(el) {
		el.onmouseover = function() {
			el.src = "/images/money-Roll.gif";
		}
		el.onmouseout = function() {
			el.src = "/images/money.gif";
		}
	},
	'img#career' : function(el) {
		el.onmouseover = function() {
			el.src = "/images/career-Roll.gif";
		}
		el.onmouseout = function() {
			el.src = "/images/career.gif";
		}
	},
	'img#faith' : function(el) {
		el.onmouseover = function() {
			el.src = "/images/faith-Roll.gif";
		}
		el.onmouseout = function() {
			el.src = "/images/faith.gif";
		}
	},
	'img#entertainment' : function(el) {
		el.onmouseover = function() {
			el.src = "/images/entertainment-Roll.gif";
		}
		el.onmouseout = function() {
			el.src = "/images/entertainment.gif";
		}
	},
	'img#exp' : function(el) {
		el.onmouseover = function() {
			el.src = "/images/exp-Roll.gif";
		}
		el.onmouseout = function() {
			el.src = "/images/exp.gif";
		}
	},
	'#forumPopup' : function(el) {
		el.onclick = function() {
			Dialog.alert({url: "/rules.html", options: {method: 'get'}}, {windowParameters: {className: "alphacube", width:500, height:540}, okLabel: "Close"} );
		}
	},
	'#termsPopup' : function(el) {
		el.onclick = function() {
			Dialog.alert({url: "/termsofuse.html", options: {method: 'get'}}, {windowParameters: {className: "alphacube", width:500, height:540}, okLabel: "Close"});
		}
	},
	'#embedtermsPopup' : function(el) {
		el.onclick = function() {
			Dialog.alert({url: "/embedtermsofuse.html", options: {method: 'get'}}, {windowParameters: {className: "alphacube", width:500, height:540}, okLabel: "Close"});
		}
	},
	'a#giveback' : function(el) {
		el.onmouseover = function() {
			el.getElementsByTagName('img')[0].src = "/images/givback-roll.gif";

		}
		el.onmouseout = function() {
			el.getElementsByTagName('img')[0].src = "/images/giveback.gif";
		}
		el.onclick = function() {
			 openVid('231');
		}
	},
	'a#welcome' : function(el) {
		el.onclick = function() {
			 openVid('724');
		}
	},
	'a#ugc' : function(el) {
		el.onmouseover = function() {
			el.getElementsByTagName('img')[0].src = "/images/ugc-roll.gif";
		}
		el.onmouseout = function() {
			el.getElementsByTagName('img')[0].src = "/images/ugc.gif";
		}
		el.onclick = function() {
			openVid('242');
		}
	},
	'a#comKit' : function(el) {
		el.onclick = function() {
			openVid('228');
		}
	},
	'a#expLink' : function(el) {
		el.onclick = function() {
			openVid('236');
		}
	},
	'#learnLink' : function(el) {
		el.onmouseover = function(){
			el.getElementsByTagName('img')[0].src = "/images/tinylogo-roll.gif";
		}
		el.onmouseout = function(){
			el.getElementsByTagName('img')[0].src = "/images/tinylogo.gif";
		}
		el.onclick = function() {
			openVid('240');
		}
	},
	'#storeLink' : function(el) {

		el.onclick = function() {
			openVid('237');
		}
	},
	'#logLink' : function(el) {
		el.onclick = function() {
			openVid('235');
		}
	},
	'#authLink' : function(el) {
		el.onclick = function() {
			openVid('232');
		}
	},
	'#emailLink' : function(el) {
		el.onclick = function() {
			openVid('230');
		}
	},
	'#newsLink' : function(el) {
		el.onclick = function() {
			openVid('241');
		}
	},
	'#sendUsLink' : function(el) {
		el.onclick = function() {
			openVid('234');
		}
	},
	'#betaLink' : function(el) {
		el.onclick = function() {
			openVid('239');
		}
	},
	'form#searchForm' : function(el) {
		el.onkeydown = function(e) {
            e = e || window.event;
            if (e.keyCode == 13) {
                submitSearch(el);
                return false;
            }
        }
	},
	'#email-i' : function(el) {
		el.onfocus = function() {
			
			if (el.value == "Enter Your Email Address") {
				el.value = "";
			}
		}
		el.onblur = function() {
			if (el.value == "") {
				el.value = "Enter Your Email Address";
			}
		}
	},

	'li.cat' : function(el){
		el.onmouseover = function() {
			el.style.background = '#ffebd2';
			el.style.cursor = 'pointer';
			if (el.getElementsByTagName('ul').length > 0) {
                            listObj = el.getElementsByTagName('ul')[0];
			    listObj.style.display = 'block';
                            //$('shim').style.top = (calculateSumOffset(listObj, 'offsetTop')) + 'px';
                            //$('shim').style.left = (calculateSumOffset(listObj,'offsetLeft') - listObj.offsetWidth) +'px';
                            //$('shim').style.height = listObj.offsetHeight + 'px';
                            //$('shim').style.width = listObj.offsetWidth + 'px';
                            //$('shim').style.zIndex = 500;
                            //$('shim').style.display = 'block';
                            if (listObj.id == 'catlist_4' || listObj.id == 'catlist_5' || listObj.id == 'catlist_6' || listObj.id == 'catlist_7') { //currently this hides the search type select box so they don't peek through the Career and Faith drop-down menus
                                if ($('searchtype')) {
                                    if (BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 7) {
                                        $('searchtype').style.display = 'none';
                                        $('searchtypefake').style.display = 'block';
                                       }
                                }
                            }


			}
		}
		el.onmouseout = function() {
			el.style.background = 'transparent';
			el.style.color = '#f68a35';
			el.style.cursor = 'default';
			if (el.getElementsByTagName('ul').length > 0) {
                            el.getElementsByTagName('ul')[0].style.display = 'none';
                            if ($('searchtype')) {
                                $('searchtype').style.display = 'block';
                                $('searchtypefake').style.display = 'none';
                            }

										}

		}
    },

	'li.cat li.tertiary' : function(el){
		el.onmouseover = function() {
			if (el.getElementsByTagName('ul').length > 0) {
                            listObj = el.getElementsByTagName('ul')[0];
							if (listObj.getElementsByTagName('li').length > 0) {
				    			listObj.style.display = 'block';
							}
                            


			}
		}
		el.onmouseout = function() {
			el.style.cursor = 'default';
			if (el.getElementsByTagName('ul').length > 0) {
				el.getElementsByTagName('ul')[0].style.display = 'none';
			}

		}
    },
	
	'a.imgPopUp' : function(el) {
		el.onmouseover = function() {
			var spans = el.getElementsByTagName('span');
			spans[0].style.display = 'block';
		}
		
		el.onmouseout = function() {
			var spans = el.getElementsByTagName('span');
			spans[0].style.display= 'none';
		}
	},
	'li.tert' : function(el) {
		el.onmouseover = function() {
			//var div = el.getElementsByTagName('div')[0];
			var uls = el.getElementsByTagName('ul');
			var ul = uls[0];
			var lis = ul.getElementsByTagName('li');
			if (lis.length > 0 && ul.style.display == "none") {
				ul.style.display = "block";
			}
			
		}
		
		el.onmouseout = function() {
			//var div = el.getElementsByTagName('div')[0];
				var uls = el.getElementsByTagName('ul');
			var ul = uls[0];
			var lis = ul.getElementsByTagName('li');
			if (lis.length > 0 && ul.style.display != "none") {
				ul.style.display = "none";
			}
		}
	}
};

/* register the events */
Behaviour.register(eventsAssign);
