function boxSecondState(element)
{
	element.css("background-image", "url(" + BASEURL + "assets/images/box-state-2.gif)");
}

function boxThirdState(element)
{
	$(".intro-boxes .box").each(function()
	{
		if ($(".intro-boxes .box").index($(this)) != $(".intro-boxes .box").index(element))
		{
			$(this).css("background-image", "url(" + BASEURL + "assets/images/box-state-1.gif)");
			$(this).find(".box-content").hide();
		}
	});
	
	element.css("background-image", "url(" + BASEURL + "assets/images/box-state-3.gif)");
	element.find(".box-content").show();
}

$(document).ready(function()
{	
	$(".intro-boxes .box").hover(
		function()
		{
			$(".intro-boxes .box").each(function()
			{
				$(this).css("background-image", "url(" + BASEURL + "assets/images/box-state-1.gif)");
				$(this).find(".box-content").hide();
			});
			
			var element = $(this);
            boxSecondState($(this));
			setTimeout(function(){ boxThirdState(element); }, 300);
		},
		function()
		{
			//$(this).css("background-image", "url(" + BASEURL + "assets/images/box-state-1.gif)");
			//$(this).find(".box-content").hide();
		}
	);
	
	$(".box input[type=checkbox]").each(function()
	{
		thisCheckbox = $(this);
		thisLabel = $(this).parent();
		
		customCheckbox = $('<span class="custom-checkbox" />');
		
		thisCheckbox.hide();
		thisLabel.prepend(customCheckbox);
		
		if (thisCheckbox.attr("checked"))
		{
			thisLabel.addClass("selectedCheckbox");
			thisLabel.find("input").attr("checked", "checked");
		}
			
		if (thisCheckbox.attr("disabled"))
		{
			thisLabel.addClass("disabledCheckbox");
			thisLabel.find("input").attr("checked", "");
		}
		else
		{
			thisLabel.click(function(e)
			{
				thisCheckbox = $(this).find("input[type=checkbox]");
				thisLabel = $(this);
				
				e.preventDefault();
				if (!thisCheckbox.attr("checked"))
				{
					thisLabel.addClass("selectedCheckbox");
					thisLabel.find("input").attr("checked", "checked");
				}
				else
				{
					thisLabel.removeClass("selectedCheckbox");
					thisLabel.find("input").attr("checked", "");
				}
			});
		}
	});
	
	$(".outer-links li a").hover(
		function()
		{
			$(this).find("img.icon").animate({"top": "-=10px"}, "fast");
		},
		function()
		{
			$(this).find("img.icon").animate({"top": "+=10px"}, "fast");
		}
	);
	
	$(".popup .btn-twitter").click(function()
	{
		if ($(".popup-holder").hasClass("popup-closed"))
		{
			$(".popup-holder").removeClass("popup-closed");
			$(".popup-holder").animate({"width": "+=322px"}, "slow");
		}
		else
		{
			$(".popup-holder").animate({"width": "-=322px"}, "slow",  function() {
				$(".popup-holder").addClass("popup-closed");
				});
			
		}
		
		return false;
	});

    $(".draw").click(function(){

        var element = $(this).parent().parent();
        element.effect("bounce", { times:3 }, 300);

        var email = $(this).prev();

        if (!$(email).val()) {
            alert("Please enter your email address");
            return;
        }
		
	    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	    var address = $(email).val();

        if(reg.test(address) == false) {
		    alert('Invalid email address, please enter a valid e-mail address');
		    return false;
	    }

        // Not required as per mantis ticket #2409
        /* if (!$(this).next().find('input').attr("checked")) {
            alert("Please subscribe to our newsletter to continue.");
            return;
        } */

        $.post(BASEURL + 'index.php/pages/enter', {email : $(email).val()}, function(data){
            if (data.allowed && data.result.win) {
                //$("#prize-title-"+).html("You have won &lt;" + data.result.prize.title + "&gt;!");
                $("#winner-email").val($(email).val());
                $('#item_id').val(data.result.prize.item_id);
                $("#contest-won").fadeIn('slow');
				$("#contest-won").fadeIn('slow');
				$("#prize-title-"+data.result.prize.prize_id).show();
            } else if (data.allowed && !data.result.win) {
                if(data.result.user.facebook_fan == 1){
					$("#contest-lost-fb").fadeIn('slow');
				}else{
					$("#contest-lost").fadeIn('slow');
				}
            } else {
				if(data.result.user.facebook_fan == 1){
					$("#contest-daily-limit").fadeIn("slow");
					$("#contest-daily-limit h2.fb").fadeIn("slow");
				}else{
					$("#contest-daily-limit").fadeIn("slow");
					$("#contest-daily-limit h2.common").fadeIn("slow");
				}
            }

        });
    });

    $("#winner-register").ajaxForm( { beforeSubmit: validate, success: greet } );
	$('#header_rotate').cycle();
	$(".intro-boxes .box").effect("shake");
	
	$("a.sign-up").click(function()
	{
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		
		$('.layer').show().css({
			'width': maskWidth,
			'height': maskHeight
		});
		
		var winH = $(window).height();
		var winW = $(window).width();
		var idH = $(".popsign-up").height();
		var yPos = winH/2 - idH/2 + $(window).scrollTop();
		$(".popsign-up").show().css('top', yPos);

		return false;
	});
	
	$(".popsign-up form").submit(function()
	{
		$(".popsign-up .alertbox").remove();
		$(".popsign-up").append($('<div class="alertbox" />'));
		
		if ($(".popsign-up input#your_name").val().length == 0)
		{
			$(".popsign-up .alertbox").append("Missing name!<br />");
		}
		if ($(".popsign-up input#your_email").val().length == 0)
		{
			$(".popsign-up .alertbox").append("Incorrect or missing email!<br />");
		}
		
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	    var address = $(".popsign-up input#your_email").val();

        if(reg.test(address) == false) {
		    $(".popsign-up .alertbox").append("Invalid email address, please enter a valid e-mail address<br />");
		    return false;
	    }
		
		if ($(".popsign-up .alertbox").text().length > 0)
			return false;
			
		$.ajax({
			type: "POST",
			url: "index.php/pages/signup",
			data: "your_name=" + $("#your_name").val() + "&email=" + $("#your_email").val() + "&age=" + $("#your_age").val() + "&province=" + $("#your_province").val() + "&action=Sign me up"
		});
		
		$(".popsign-up").html('<a href="" class="close"></a><span class="signup-success"></span>');
		
		
		
		return false;
	});
	
	$(".popsign-up .close").click(function() { 
		$(".popsign-up").hide(); 
		$('.layer').hide();
		return false; 
	});

});

function validate(formData, jqForm, options)
{
    for (var i=0; i < formData.length; i++) {
        if(formData[i].name != 'address_2'){
			if (!formData[i].value) {
				alert('Please fill all the information.');
				return false;
			}
		}
    }

	var reg = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
	var dob = $("#winner-register input[name='dob']").val();

    if(reg.test(dob) == false) {
		alert('Please enter valid Date of birth (i.e DD/MM/YYYY)');
		return false;
	}

    if (!$('#register-agree').attr("checked")) {
        alert("You have to agree with the rules and regulations of this contest.");
        return false;
    }

    return true;
}

function greet(data)
{
    if (data.success) {
        $("#contest-won").hide();
        $('#winner-submit-success').fadeIn();
    }
}

function is_int(value){
  if((parseFloat(value) == parseInt(value)) && !isNaN(value)){
      return true;
  } else {
      return false;
  }
}

function open_terms() {
	 $('#terms').lightbox_me({
		centered: true
	});
}

function open_rules() {
	 $('#rules').lightbox_me({
		centered: true
	});
}
function open_email_signup() {
	 $('#email-signup').lightbox_me({
		centered: true
	});
}


