var cookieOptions = { domain: "ebus.tv", path: "/", secure: false, expires: 3650 };

$("#formContainer").remove().prependTo($("body"));
var loginText = $.cookie("sl") == "true" ? "Hide Login" : "Login";

$('<a href="/login" id="loginToggle">' + loginText + '</a>').click(function() {
	var a = $(this);
	$("#formContainer").slideToggle(timer.LoginSlide);
	if (a.text() == "Login") 
	{
		a.text("Hide Login");
		$.cookie("sl", null);
       		$.cookie("sl", true, cookieOptions); 
	}
	else {
		a.text("Login");
		$.cookie("sl", null);
       		$.cookie("sl", false, cookieOptions);
	}        
	$("#loginForm #login").focus();
	return false;
}).appendTo("#container");

/* because IE is hellspawn */
if ($.browser.msie) {
	$("#homeProducts .product").click(function(){ window.location = $(this).find("h2 a").attr("href"); }).css({ cursor: "pointer" });
}

$("#remember").click(function() { 
	var checked = $(this).attr("checked");	
	if (checked) return;
	$.cookie("un", null); 
	$.cookie("pw", null);
	return true;
});

$("#loginForm").submit(function() { 
	var checked = $("#remember").attr("checked");	
	if (!checked) return;
	$.cookie("un", $("#login").val(), cookieOptions);
	$.cookie("pw", $("#password").val(), cookieOptions);
	return true;
});

$(function() { 
	if (!$.cookie("un")) return;
	$("#login").val($.cookie("un"));
	$("#password").val($.cookie("pw"));
	$("#remember").attr("checked", true);
});

(function(){
	var menuTimeout;
	$("#menu").removeClass("noJS");
	$("#menu>li").hover(
		function() {
			clearTimeout(menuTimeout);
			var t = $(this);
			$("#menu .subMenu:visible").hide();
			t.find(".subMenu").show();
		},
		function() {
			var t = $(this);	
			if (t.is(".selected")) return;
			$("#menu .selected .subMenu").show();
			menuTimeout = setTimeout( function(){ t.find(".subMenu").hide(); }, 500 );
		}
	);
})();

$("#newsFeed p.date").text(getCurrentDate());

$("ul.whatsNew li span").click(function() { 
	$(this).next().slideToggle();
});

$("#" + $("#formTabs li.selected a").attr("rel")).show();
$("#formTabs li a").click(function() {
	var a = $(this);
	if ($(this).parent().hasClass("selected")) return false;
	$("#" + $("#formTabs li.selected a").attr("rel")).hide();
	$("#formTabs li.selected").removeClass("selected");
	a.parent().addClass("selected");
	$("#" + a.attr("rel")).show();
	return false;
});

/* product gallery */
$(".productGallery img").each(function(){
	var t = $(this);
	if (!t.attr("src")) t.remove();
});
(function($) { 
	$(".productGallery img:first").fadeIn();
	var interval = 0;
	if ($(".productGallery img").length > 1) interval = setInterval(nextImage, 7000);
	function nextImage() {
		var current = $(".productGallery img:visible");
		var next = current.next("img").length > 0 ? current.next("img") : $(".productGallery img:first");
		current.fadeOut(function() { next.fadeIn(); });
	}
	$(".productGallery img").click(function(){ 
		clearInterval(interval);
		nextImage();
		interval = setInterval(nextImage, 7000);
	});
})(jQuery);

    function getCurrentDate() {
	var now = new Date();
	var day = now.getDate();
	var dow = getDayName(now.getDay());
	var month = getMonthName(now.getMonth());
	var year = now.getFullYear();
	return dow + ", " + day + " " + month + " " + year;
	function getDayName(index) {
	    switch(index) {
		case 0:
  		    return "Sunday";
		case 1:
  		    return "Monday";
		case 2:
  		    return "Tuesday";
		case 3:
  		    return "Wednesday";
		case 4:
  		    return "Thursday";
		case 5:
  		    return "Friday";
		case 6:
  		    return "Saturday";
		default:
  		    return "";
	    }
	}
	function getMonthName(index) {
	    switch(index) {
		case 0:
  		    return "Jan";
		case 1:
  		    return "Feb";
		case 2:
  		    return "Mar";
		case 3:
  		    return "Apr";
		case 4:
  		    return "May";
		case 5:
  		    return "Jun";
		case 6:
  		    return "Jul";
		case 7:
  		    return "Aug";
		case 8:
  		    return "Sep";
		case 9:
  		    return "Oct";
		case 10:
  		    return "Nov";
		case 11:
  		    return "Dec";
		default:
  		    return "";
	    }
	}
    }
if ($.browser.msie && $.browser.version < 7) {
	$(function(){ 
		$("#formContainer").css("z-index", 2000).css("position", "relative");
		if ($("#loginToggle").text() == "Login") {
			$("#loginToggle").click();
		}
	});
}

/* validation */
$(function() {
	var validationOptions = {
		rules: { email: { required:true, email: true } },
		messages: { email: { required: "Please enter your email address", email: "Please enter a valid email address" } },
	        errorClass: "error",
	        errorElement: "p" 
	};
	$("#contactUsForm").validate(validationOptions);
	$("#contactYouForm").validate(validationOptions);
	$("#registerForm").validate(validationOptions);
});

/* geo ip */
(function(){
	if(!window.geoip_country_code) return;
	var dc = "NZ, IN, TH, SG";
	var cc = geoip_country_code();
	if (dc.indexOf(cc) == -1) cc = "DEF";
	//for each product check ac contains cc if it doesnt then remove product
	$(".product").each(function(){ 
		var p = $(this);
		var rel = p.find("h2 a").attr("rel");
		if (rel.length > 0 && rel.indexOf(cc) == -1) p.remove();
	});
})();
