$(document).ready(function(){

	// adds tab functionality through jQ
	$("#tabs").tabs();
	
	//removing "current" class from non js element
	$("#tabs li.current").each(function(){
		$(this).removeClass("current");
	});

	// the cookie thingy for remembering tabs
	var selectedTab = $.cookie("selectedTab");
	$("#tabs").tabs("select", parseInt(selectedTab));

	$("#tabs li a").click(function(){
		$.cookie("selectedTab", $("#tabs").tabs("option", "selected"));
	});

});

function addHover(img1, img1Hover, img1Click, img2, img2Hover, img2Click){
	//adds mouse over effects for CTA buttons
	var cta = $(".cta");
	if ((cta) && (cta.length > 0)) {
		// preloading images
		image1 = new Image(1, 1);
		image1.src = img1Hover;
		image1Hover = new Image(1, 1);
		image1Hover.src = img1Click;
		image2 = new Image(1, 1);
		image2.src = img2Hover;
		image2Hover = new Image(1, 1);
		image2Hover.src = img2Click;

		var apply = cta.find(".apply img");
		var finish = cta.find(".finish img");

		apply.hover(
				function() { 
					$(this).attr("src", img1Hover);
				}, 
				function() { 
					$(this).attr("src", img1); 
				}
		);
		apply.click(function() {
			$(this).attr("src", img1Click);
		});

		finish.hover(
				function() { 
					$(this).attr("src", img2Hover);
				}, 
				function() { 
					$(this).attr("src", img2); 
				}
		);
		finish.click(function(){
			$(this).attr("src", img2Click);
		});
	}
}

/** used by the table component */
function setNormalTableStyle(tableId) {
	$(document).ready(function(){

		var tableSelector = "div#" + tableId + " table";
		var table = $(tableSelector);
		//table.attr("class", "transBankingTable");
		table.attr("width", "100%").attr("cellSpacing","0");
		table.prepend("<thead/>");
		$(tableSelector + " thead").append($(tableSelector + " tr:first"));

		var numberOfColumns=$(tableSelector + " tbody tr:eq(1) td").size();
		$(tableSelector + " thead tr td").each(function(){
			$(this).replaceWith("<th colspan='" + numberOfColumns + "'>" + $(this).html() + "</th>")
		});

		$(tableSelector + " td").each(function(){
			$(this).replaceWith("<td><div>" + $(this).html() + "</div></td>")
		});

		//$(tableSelector + " tbody tr:odd").attr("class", "shaded");

		//yep, I know it's stupid, but hey, it wasn't me doing the CSS
		$(tableSelector + " tbody tr:odd").attr("class", "even"); 

		$(tableSelector + " tbody tr").attr("valign", "top");
		$(tableSelector + " tbody tr:last-child").addClass("last");
		$(tableSelector + " tbody tr:last-child td:last-child").attr("class", "last");
		//$(tableSelector + " tbody tr th:last-child, " + tableSelector + " tbody tr td:last-child").css("border-right", "none");
	});
}
/** used by the table component */
function setCustomTableStyle(tableId) {
	$(document).ready(function(){
		var table = $("div#" + tableId + " table");
		table.attr("class", "transBankingTable");
	});
}

/** used by the table component */
function tablePopup(link) {
	window.open(link ,"",'menubar=no, toolbar=no, location=no, scrollbars=yes, resizable=yes, status=no, width=800, height=600');
}

function alterEditFieldsBehaviour(){
	$("input[type='text']").unbind("click");
	$("input[type='text']").click( function() {
		$(this).select();
	});
	$("textarea").unbind("click");
	$("textarea").click( function() {
		$(this).select();
	});

	$("div.formPageWrapper input[type='text']").unbind("click");
	$("div.formPageWrapper textarea").unbind("click");
}

function isInternationalPhoneNumber(phone){
	return (4 <= phone.length) && (phone.length <= 21) && (phone.match(/^((\+{1})|(0{2}[1-9]{1}))(\d){3,19}$/));
}

function isNonInternationPhoneNumber(phone){
	return phone.match(/^\d{3,18}$/);		
}

function setupCallbackForm(toName, toEmail) {
	$("form#popupCallbackForm").unbind("submit").submit(function() {
		return false;
	});

	var varScript = "<script>var popupEmailAddress='" + toEmail + "'";
	varScript += "<";
	varScript += "/script>";
	$("div#popupContainer").prepend(varScript);

	$("form#popupCallbackForm input#submit").unbind("click").click(
			function() {
				// clear errors:
				$("div#calltimeBorder").removeClass("errorField");
				$("input").removeClass("errorField");
				$("label#errorMessage").text("").hide();

				var name = $("#popupCallbackForm #name").val();
				var phone = $("#popupCallbackForm #phone").val();
				var calltime = $("#popupCallbackForm #calltime").val();
				var type = $("#popupCallbackForm #type").val();
				var pageUrl = getPageUrl();
				var pageName = getPageTitle();

				var clearPhone = phone.replace(/[\s()-]/g, '');
				if (!isInternationalPhoneNumber(clearPhone)) {
					$("input#phone").addClass("errorField");
					if (isNonInternationPhoneNumber(clearPhone)) {
						$("label#errorMessage").text(
								getCallbackNoCountryCodeInPhone()).fadeIn(
								"slow");
					} else {
						$("label#errorMessage").text(getCallbackNoPhone())
								.fadeIn("slow");
					}
					return false;
				}

				if (calltime == "none") {
					$("label#errorMessage").text(getCallbackNoTime())
							.fadeIn("slow");
					$("#calltimeBorder").addClass("errorField");
					return false;
				}

				$.ajax( {
					type : "POST",
					url : getFormPage(),
					data : "pageUrl=" + encodeURIComponent(pageUrl)
							+ "&pageName=" + encodeURIComponent(pageName)
							+ "&toName=" + encodeURIComponent(toName)
							+ "&name=" + encodeURIComponent(name)
							+ "&phone=" + encodeURIComponent(phone)
							+ "&calltime=" + encodeURIComponent(calltime)
							+ "&type=" + type,
					success : function(msg) {
						// alert( "Data Sent: " + msg ); // uncomment this
						// line for debug
					$("#dialog").dialog("close");
					popupMessage(getCallbackSuccess());
				},
				error : function(msg) {
					// alert( "Error: " + msg ); // uncomment this line for
					// debug
					$("#dialog").dialog("close");
					popupMessage(getCallbackError());
				}
				});
			});
}

function popupMessage(wText){
	$("div#dialog").remove();
	$("body").append('<div id="dialog"><div id="popupMessage">'+wText+'</div></div>');

	$("#dialog").dialog({
		title: 			getPopupMessageHeader(), 
		modal: 			true,
		draggable: 		false,
		resizable: 		false,
		width: 			279,
		height: 		90,
		autoOpen:       false,
		overlay: 		{
			opacity: 		0.7,
			background: 	"white"
		}
	}).bind({
		dialogopen: 			function(){
			if ($.browser.msie) { $('body').css("overflow", "hidden"); } /* INVTWO-435 */
			$(".ui-dialog-overlay").unbind("click").unbind("dblclick").click(function(){
				$("#dialog").dialog("close");
				return false;
			});
		},
		dialogclose: 			function(){
			$(this).dialog("destroy");
			$("#dialog").remove();

			if ($.browser.msie) { $('body').css("overflow", ""); } /* INVTWO-435 */
		}
	});
	$("#dialog").resizable("destroy");
	$("#dialog").dialog('open');
	return false;
}


/** for popups*/
function setupPopupForms(popupId){
	$(popupId).unbind("click").click(function(){
		$("div#dialog").remove();
		$("body").append('<div id="dialog"></div>');

		$("#dialog").dialog({
			title: 			getCallbackHeader(), 
			modal: 			true,
			draggable: 		false,
			resizable: 		false,
			width: 			279,
			height: 		160,
			autoOpen:		false,
			overlay: 		{
				opacity: 		0.7,
				background: 	"white"
			}
		}).bind({
			dialogopen: 			function(){
				if ($.browser.msie) { $('body').css("overflow", "hidden"); } /* INVTWO-435 */
				
				$(".ui-dialog-overlay").unbind("click").unbind("dblclick").click(function(){
					$("#dialog").dialog("close");
					return false;
				});
			},
			dialogclose: 			function(){
				$(this).dialog("destroy");
				$("#dialog").remove();

				if ($.browser.msie) { $('body').css("overflow", ""); } /* INVTWO-435 */
			}
		});
		$("#dialog").resizable("destroy");

		// need to additionally grab the person this applies to, i.e. the person the email will go to
		var toName = $(this).attr("data");
		var toEmail = $(this).attr("emaildata");
		//toName = toName.substring(toName.indexOf(":")+1);

		$("div#dialog").load(getPopupPage() + ' #popupContainer', {}, function(){
			alterEditFieldsBehaviour();
			setupCallbackForm(toName, toEmail);
		});

		$("#dialog").dialog('open');
		return false;
	});
}

