// we have to do it globally to get the date as soon as possible
var dateLocal = new Date();

try {
	function blurLink(node) {
		this.blur();
	}
} catch (e) {}

// clock
$(function(){
	var timeNode = document.getElementById('theTime');
	var serverTimeArr = timeNode.innerHTML.split(":");
	var dateServer = new Date();
	dateServer.setHours(serverTimeArr[0]);
	dateServer.setMinutes(serverTimeArr[1]);
	dateServer.setSeconds(serverTimeArr[2]);
	timeDiff = dateServer - dateLocal;
	function updateClock() {
		var date = new Date();
		date.setTime(date.getTime() + timeDiff);
		timeNode.innerHTML = date.toLocaleTimeString();
		setTimeout(updateClock, 1000 - (date.getTime() % 1000));
	}
	// synchronize on real seconds only
	setTimeout(updateClock, 1000 - ((new Date()).getTime() % 1000));
});

// login overlay
$(function(){
	var signInOutJQ = $("#sign-in-out");
	var signInJQ = $("#sign-in");
	var signOutJQ = $("#sign-out");
	
	$("#sign-in-text, #sign-out-text").click(function(e){
		signInOutJQ.toggleClass("open");
		return false;
	});

	$(".login-logout .cancel").click(function(e) {
		signInOutJQ.removeClass("open");
	});

	$("#sign-in-text").click(function(e){
		signInJQ.addClass("act");
		signOutJQ.removeClass("act");
	});
	
	$("#sign-out-text").click(function(e){
		signOutJQ.addClass("act");
		signInJQ.removeClass("act");
	});
	
	$("#LoginForm").submit(function(){
		var errId = -1;
		var errIdArr = ['server', 'server', 'server'];
		errId += ($("#username").val() == "" ? 1 : 0);
		errId += ($("#password").val() == "" ? 2 : 0);
		var messageJQ = $("#login-overlay .message span");
		if (errId > -1) {
			messageJQ.removeClass("act");
			messageJQ.filter("." + errIdArr[errId]).addClass("act");
			return false;
		}
	});
});

// if the login was not successful show the login overlay again
$(function(){
	var urlParamArr = location.search.substr(1).split('&');
	var urlParamAArr = {};
	var l = urlParamArr.length;
	for (var i = 0; i < l; i++) {
		var urlParam = urlParamArr[i].split('=');
		urlParamAArr[urlParam[0]] = urlParam[1];
	}
	if (urlParamAArr['sid'] == -5) {
		$("#sign-in-out").addClass("open");
	}
});


$(function(){
	function setCookie(name, value, expire, path, domain) {
		var cookie = [];
		var date = new Date();
		if (expire) {
			date.setTime(date.getTime() + expire*1000);
		}
		if (!path) {
			path = '/';
		}
		cookie.push(name + '=' + value);
		cookie.push('expires=' + date.toGMTString());
		cookie.push('path=' + path);
		if (domain) {
			cookie.push('domain=' + domain);
		}
		document.cookie = cookie.join('; ');
	}
	
	function getCookie(name) {
		var cookies = document.cookie.split('; ');
		var value=false;
		for (var i=0; i<cookies.length; i++) {
			var cookie = cookies[i].split('=');
			if (cookie[0] == name) {
				value = cookie[1];
				break;
			}
		}
		return value;
	}
	
	// remember me
	try {
		$("#LoginForm").submit(function(e){
			if (document.getElementById("remember_me").checked) {
				setCookie("coral_username", document.getElementById("username").value, 31536000);
			} else {
				setCookie("coral_username", "");
			}
		});
		var username = getCookie("coral_username");
		if (username) {
			document.getElementById("username").value = username;
			document.getElementById("remember_me").checked = true;
		}
	} catch(e) {}
	
	try {
		var footerBounced = getCookie("coral_footer_bounced");
		var domain = "." + location.hostname.match(/[^\.]+\.(com|co.uk)$/i)[0];
		setCookie("coral_footer_bounced", 1, 31536000, "/", domain);
		if (footerBounced) {
			$("#ff-footer .highlighter").css({opacity: 1});
			$("#ff-footer").css({bottom: -1});
		} else {
			// functional footer initial bounce
			$("#ff-footer .wrapper").css({opacity: 0.80});
			$("#ff-footer").animate(
				{bottom: -1},
				{duration: 1100, easing: "easeOutBounce", complete: function() {
					var wrapperJQ = $("#ff-footer .wrapper"); 
					wrapperJQ.animate({opacity: 1}, {duration: 500, complete: function() {
						/* IE6 workaround */
						wrapperJQ.css({opacity: ''});
						var highlighterJQ = $("#ff-footer .highlighter");
//						setTimeout(function(){highlighterJQ.animate({opacity: 0}, {duration: 1000})}, 500);
					}})
				}}
			);
		}
	} catch (e) {}
});

// account options show/hide
$(function() {
	var accountOptionsJS = $("#acc-opt-box");
	$("#bb-hide-button, #notify-header .x_button").click(function(e) {
		accountOptionsJS.toggleClass("hidden");
	});
});

// balance show/hide
$(function(){
	var stars = false;
	var balanceJS = $("#cust-balance");
	var balance = balanceJS.html();
	$("#show-hide-balance").click(function(e) {
		if (stars) {
			balanceJS.html(balance);
			$(this).html("hide");
			stars = false;
		} else {
			balanceJS.html("******");
			$(this).html("show");
			stars = true;
		}
	})
});


