/// <reference  path="jquery-1.3.2.min-vsdoc2.js"/>

$(document).ready(function() { init(); });
var init = function() {
	initMenu();
	initSelect();
	initErrorMessageBox();
	initTooltip();
	initModals();
};
var hideSelect = true;
var initMenu = function() {
	$('#navigation li .subNav').parent().addClass('subNavParent');
	$('#navigation li.subNavParent').mouseenter(function(e) { showSubNav(e) });
	$('#navigation li.subNavParent').mouseleave(function(e) { hideSubNav(e) });
	$('#navigation li').hover(function() { $(this).addClass("lHover") }, function() { $(this).removeClass('lHover') });
	if ($.browser.msie && parseInt($.browser.version) <= 7) {
		$('.subNavParent').css('backgroundPosition', '0 -177px');
	}

};

var hovering = true;

var showSubNav = function(e) {
	hovering = true;

	var t = e.target.tagName.toLowerCase() == 'a' ? e.target.parentNode : e.target;
	$(t).css('backgroundPosition', '0 bottom');
	if ($(t).hasClass('subNavParent')) { $(t).animate({ height: 100 }, { duration: 200, queue: true }).addClass('subActive'); }
};

var hideSubNav = function(e) {
	var t = e.target.tagName.toLowerCase() == 'a' ? e.target.parentNode : e.target;
	var t = $(t).hasClass('subNavParent') ? t : ($(t.parentNode.parentNode).hasClass('subNavParent') ? t.parentNode.parentNode : t.parentNode);
	//alert(t.className + "(" + t.parentNode.parentNode.className + ") == " + $(t).hasClass('subNavParent'));
	$(t).delay(500, function() {
		if (hovering == false) {
			$(t).animate({ height: 23 }, { duration: 200, queue: true });
			$(t).delay(200, function() { $(t).removeClass('subActive'); if ($.browser.msie && parseInt($.browser.version) <= 7) { $(t).css('backgroundPosition', '0 -177px'); } });
			//			$(t).css('backgroundImage', 'none');

		}
		else {
			return;
		}
	});
	hovering = false;
};

var initSelect = function() {
	$(".customSelectGoal, .customSelectAccount").selectbox({
		animationSpeed: 'fast'
	});
};

var initTooltip = function() {
	$("#bdayQmark").hover(function() { $("#qmarkTooltip").show(); }, function() { $("#qmarkTooltip").hide(); });
};

var initErrorMessageBox = function() {
	$(".errorWrap .errors").wrapInner('<div class="errorsInnerWrap"></div>');
	$(".errorWrap .errors").prepend('<div class="errorOverlay"></div>');
	$(".errorWrap .errorsInnerWrap").append('<div class="closeBtn ie_png_fixer"><a href="#"><span class="hideOS">Close</span></a></div>');
	$(".errorWrap .errors .closeBtn a").click(function() {
		$(".errorWrap,.errors").hide();
	});

	$(".errorWrap").show();
	var h = $(".errorWrap .errorsInnerWrap").height();
	$(".errorWrap .errorsInnerWrap").css("margin-top", -(h / 2));
	if (isIE6OrLess()) {
		$(".errorWrap .errorsInnerWrap").css("margin-left", '-300px');
	}
};

/* Hide Select boxes in IE when a modal dialog is open */
var modalActivate = function() {
	if (isIE6OrLess() && hideSelect == true) {
		$("select").hide();
	}
}
var modalDeActivate = function() {
	if (isIE6OrLess() && hideSelect == true) {
		$("select").show();
	}
}

var showCalculator = function(strCalcId) {
	if (strCalcId != '' && strCalcId != undefined) {
		var strId = 'calcHeading_';
		if (strCalcId == 'regular') {
			$("#ich_calc_goal").hide();
			$("#ich_calc_regular").show();
			$('#calcModalTitle_regular').show();
			$('#calcModalTitle_goal').hide();
		}
		else if (strCalcId == 'goal') {
			$("#ich_calc_goal").show();
			$("#ich_calc_regular").hide();
			$('#calcModalTitle_regular').hide();
			$('#calcModalTitle_goal').show();
		}

		$('#calcModalTitle h2').attr('id', strId + strCalcId);

		$("#calcOverlay").show();
		$("#calcModal").show();
		middleIt('calcModal');
		$("#calcOverlay, #calcModalClose").click(function() { hideCalculator(); });
	}
}
var middleIt = function(strElementId) {
	elId = '#' + strElementId;
	w = $(elId).width();
	h = $(elId).height();
	mL = w / 2;
	mT = h / 2;
	$(elId).css({ 'margin-top': -mT, 'margin-left': -mL });
}
var hideCalculator = function() {
	$("#calcOverlay").hide();
	$("#calcModal").hide();
}

var initModals = function() {
	$('.jModal').wrap('<div class="jModalWrap"></div>');
	$('.jModal').prepend('<div class="jModalOverlay"></div>');
	$(".jModal .jModalInner").wrapInner('<div class="jModalInnerScroll"></div>');
	$(".jModal .jModalInnerScroll").wrapInner('<div class="jModalInnerWrap"></div>');
	$(".jModal .jModalInner").prepend('<div class="jModalTitleBar"><div class="closeBtn ie_png_fixer"><a href="#"><span class="hideOS">Close</span></a></div></div>');
	$(".jModal .jModalTitleBar").prepend('<div class="jModalTitle"><h2>Information</h2></div>');
	$('.jModalLink').click(function() { var mId = $(this).attr("href"); $(mId).show(); modalActivate() });
	$('.jModal .closeBtn a').click(function() { mId = '#' + $(this).parent().parent().parent().parent().attr('id'); $(mId).hide(); modalDeActivate() });

};




/** getUrlParams
* This method will retrieve the parameters passed into the URL
* The parameters passed must follow url encoding and GET method standards
*/
var getUrlParams = function() {
	curURI = window.location.href;
	if (curURI.indexOf("?")) { //check if we have a ?
		//we need to take a substring - starting at the char AFTER the '?'
		qString = curURI.substring(curURI.indexOf('?') + 1);
		//if there are ampersands, split to multiple params
		if (qString.indexOf('&')) {
			qString = qString.split('&');
		}
		var qArray = Array();
		for (var i = 0; i < qString.length; i++) {
			var paramSet = qString[i].split('=');
			var k = paramSet[0];
			var v = '';
			if (paramSet[1] != null) {
				v = paramSet[1];
			}
			qArray[k] = v;
		}
		return qArray;
	}
};


/** isIE6OrLess()
*  for some of our methods its neccesary to know if we're dealing with an old browser
*
*  @returns {bool}
*/
var isIE6OrLess = function() {
	//Detect < IE6
	version = 0
	if (navigator.appVersion.indexOf("MSIE") != -1) {
		temp = navigator.appVersion.split("MSIE");
		version = parseFloat(temp[1]);
	}
	if (version <= 6.0 && version != 0) { //NON IE browser will return 0
		return true;
	}
	return false;
}


function OpenSM() {
	var mstrSizeParams = 'width=1011,height=709';
	var mstrSMURL = "https://www.ingdirect.com.au/oa/sm/index.aspx";
	pageTracker._trackPageview('/SavingsMaximiserApply/');
	window.open(mstrSMURL, "Apply", "toolbar=0,status=1,location=no,menubar=no,directories=no,scrollbars=yes,resizable=yes,screenX=0,screenY=0,left=0,top=0," + mstrSizeParams)
}


/* Atlas tracking function */
var atdmtProto = (("https:" == document.location.protocol) ? "https://" : "http://");
var TrackTag = atdmtProto+'switch.atdmt.com/action/';
function GetActionTag(URL){ document.action_tag.src = TrackTag + URL; }


function BuildCustomSelect()
{
	$(".customSelectGoal, .customSelectAccount").selectbox({
		animationSpeed: 'fast'
	});
	hideSelect = false; /* if we have a custom select we don't have to toggle it in IE6 */
	$("#select_goal select").change(function() {
		UpdateReward(this.options[this.selectedIndex].value);
	}
	);		
}
