var timer;
var defaultTimeout = 10000;
var animating = false;
var currentLayer = 1;
$.validator.addMethod("above18", function(obj) {
    var inputDate = new Date(obj);
    inputDate.setFullYear(inputDate.getFullYear() + 18);
    if (inputDate > new Date()){
        return false;
    }else{
        return true;
    }
}, "You need to be above 18 to become a registered member");

// a new class rule to group all three methods
$.validator.addClassRules({
	requiredAge: {required:true, date:true, above18:true}
});

$().ready(function(){
    $("#forumSelector").change(function() {
        if ($(this).val() != ""){
            window.location.href = $(this).val();
        }
    });

    $("#timeslot").change(function() {
        if ($(this).val() != ""){
            window.location.href = $(this).val();
        }
    });
    $("#fxregister").validate({
          errorLabelContainer: "#messageBox ul",
          wrapper: "li",
          errorElement: "em"
    });

    $("#fxProfileUpdate").validate({
        errorLabelContainer: "#messageBox ul",
        wrapper: "li",
        errorElement: "em"
    });

    $("#bday").datepicker({ dateFormat: 'mm/dd/yy', yearRange:'-100:-1', defaultDate:'-18y'});

    $("a.homeButton").click(function() {
        var layer = $(this).attr("title");
        switchHomeBanner(layer);
        return false;
    });

    $("a.print").click(function() {
        window.open( $(this).attr('href'), "_blank", "menubar=0,location=0,toolbar=0,scrollbars=1,width=850,height=650");
        return false;
    });

    var settings = {countWhat: 'words',countDirection: 'up'};

    $("#fxcontest").textCounting(settings);

    $("#contestSubmit").click(function() {
        var wordCount = parseInt($("#fxcontestDown").html());
        var maxLength = parseInt($("#fxcontest").attr("maxLength"));
        $.log( 'word count: ' + wordCount + ', maxlen:' + maxLength);
        var privacyCheck = $("#contestPrivacy").attr("checked");
        $.log('Privacy Check ' + privacyCheck);
        if (!privacyCheck) {
            alert("Please make sure you agree and read our privacy policy and terms & conditions.");
            return false;
        }
        if (wordCount > maxLength){
            alert("Please make sure your submission under " + maxLength + " words");
        }else if (wordCount <= 0){
            alert("Please input your submission");

        }else{
            $("#online_contest").submit();
        }
        return false;
    });

	$(".galleryLink").click(function() {
		$.log("Gallery Link Click: " + $(this).attr('href') + " " + $("#photoContainer").attr('src'));
    	$("#photoContainer").attr('src', $(this).attr('href'));
        return false;
    });

	$(".videoLink").click(function() {
		$("#videoPlayer").html('<embed id="videoPlayer" width="608" height="394" align="middle" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowscriptaccess="sameDomain" name="main" bgcolor="#9c9c9c" quality="high" autostart="false" src="/_images/videoplayer.prt2.swf?playlist='+$(this).attr('href')+'"/>');
		$(".videoSelected").removeClass('videoSelected');
		$(this).parent().parent().parent().addClass('videoSelected');
		$.log('VideoContainer SRC: ' + $("#videoPlayer").attr('src'));
		return false;
	});

	$("a.selectLink").click(function() {
        var layer = $(this).attr("title");
        switchSample(layer);
        return false;
    });

	if ($("#mugshot").length > 0) {
		if ($("#action").val() ==  "shockFriend") {
			switchCrime($('#offence')[0].selectedIndex);
			if ($("#offence").val() == "") {
				$("#displayOffence").text("Complex Crime");
			} else {
				$("#displayOffence").text($("#offence").val());
			}

			if ($('#offence')[0].selectedIndex == 0) {
				$("#crime_date").attr("value", $("#displayDay").text() + "-" + $("#displayMonth").text() + "-" + $("#displayYear").text());
				$("#location").attr("value", "Hong Kong, China");
				if ($("#offence").val() == "") {
				$("#displayOffence").text("Complex Crime");
			} else {
				$("#displayOffence").text($("#offence").val());
			}
			}
		}

	}
	$("#name").keyup(function() {
        $("#displayName").text($(this).val());
    });
	$("#name").change(function() {
        $("#displayName").text($(this).val());
		if ($("#name").val() == "") {
			$("#displayName").text("HAWARD DAVIS");
		}
    });
	$("#offence").change(function() {
		switchCrime($('#offence')[0].selectedIndex);
		if ($('#offence')[0].selectedIndex == 0) {
			$("#displayOffence").text("Complex Crime");
			$("#displayLocation").text("Hong Kong, China");
		} else {
			 $("#displayOffence").text($(this).val());
			 $("#displayLocation").text($("#location").val());
		}


		if ($("#action").val() ==  "shockFriend") {
			var formDate = "";
			var formLocation = "";
			switch($('#offence')[0].selectedIndex) {
			case 1:
				formDate = "18-05-09";
				formLocation = "Bangkok Thailand";
				break;
			case 2:
				formDate = "16-05-09";
				formLocation = "Ho Chi Minh City Vietnam";
				break;
			case 3:
				formDate = "16-05-09";
				formLocation = "Central Hong Kong";
				break;
			case 4:
				formDate = "16-05-09";
				formLocation = "Singapore";
				break;
			case 5:
				formDate = "18-05-09";
				formLocation = "Shanghai China";
				break;
			case 6:
				formDate = "15-05-09";
				formLocation = "Manila, Philippines";
				break;
			case 7:
				formDate = "17-05-09";
				formLocation = "Seoul Korea";
				break;
			default:
				formDate = "-";
				formLocation = "-";
			}

			$("#crime_date").val(formDate);
			$("#location").val(formLocation);

			if ($('#offence')[0].selectedIndex == 0) {
				$("#displayDay").text($("#currentDay").val());
				$("#displayMonth").text($("#currentMonth").val());
				$("#displayYear").text($("#currentYear").val());
				$("#displayLocation").text("Hong Kong, China");
			} else {
				$("#displayDay").text($("#crime_date").val().substring(0,2));
				$("#displayMonth").text($("#crime_date").val().substring(3,5));
				$("#displayYear").text($("#crime_date").val().substring(6,8));
				$("#displayLocation").text(formLocation);
			}


		}
    });

	// Police Report
    var settings = {countWhat: 'words',countDirection: 'up'};

    $("#police_report").textCounting(settings);


	$("#location").keyup(function() {
        $("#displayLocation").text($(this).val());
    });
	$("#crime_date").change(function() {
        $("#displayDay").text($(this).val().substring(0,2));
		$("#displayMonth").text($(this).val().substring(3,5));
		$("#displayYear").text($(this).val().substring(6,8));
    });
	$("#enterPreview").click(function() {

		var privacyCheck = $("#privacy").attr("checked");
        $.log('Privacy Check ' + privacyCheck);
        if (!privacyCheck) {
            alert("Please make sure you have the right to distribute this picture and that it does not violate the Terms of use.");
            return false;
        }
        var wordCount = parseInt($("#police_reportDown").html());

        var maxWord = parseInt($("#police_report").attr("maxLength"));

		if (wordCount > maxWord) {
			//alert(wordCount);
			//alert(maxWord);
			alert("Please make sure your entrance is within " + maxWord + " words");
			return false;
		}
		$("#mugshot").submit();
	});
	$("#enterEdit").click(function() {
		$("#mugshotPreviewEdit").submit();
	});
	$("#enterShareContest").click(function() {
		$("#mugshotShareContest").submit();
	});
	$("#enterShareContestAgain").click(function() {
		$("#mugshotShareContest").submit();
	});
	$("#enterSubmit").click(function() {
		//$("#mugshotPreview").attr("action", "/mugshot/enter/");
		//alert($("#mugshotPreview").attr("action"));
		//document.mugshotPreview.action = "/mugshot/friendinfo";
		//document.forms[0].action = "/mugshot/friendinfo";
		//$("#mugshotPreview").attr("action", "/mugshot/enter/");
		//alert($("#mugshotPreview").attr("action"));
		$("#mugshotPreviewSubmit").submit();
	});
	$("#enterSend").click(function() {
		$("#mugshotPreviewSend").submit();
	});
	$("#contestInfoSubmit").click(function() {
		$("#mugshotContestSubmit").submit();
	});
	$("a.viewSample").click(function() {
        window.open( $(this).attr('href'), "_blank", "menubar=0,location=0,toolbar=0,scrollbars=1,resizable=1,width=590,height=900");
        return false;
    });

});

var switchSample = function (layer) {
        for (var i=1;i<=($(".mugshotSample").length);i++){
            if (i == layer){
                $("#mugshotSample"+i).show();
				$("#link"+i).addClass("selectLinkSelected");
            }else{
                $("#mugshotSample"+i).hide();
				$("#link"+i).removeClass("selectLinkSelected");
            }
        }
};
var switchCrime = function (layer) {
        for (var i=1;i<=($(".mugshotCrime").length);i++){
            if (i == layer){
                $("#mugshotCrime"+i).show();
            }else{
                $("#mugshotCrime"+i).hide();
            }
        }
};

var switchHomeBanner = function (layer) {

        if (!animating){
            animating = true;

            if (timer){
                clearTimeout(timer);
            }


            $("#i"+currentLayer).attr("src","/_images/home-promo-top/frame-"+currentLayer+".gif");
            $("#i"+layer).attr("src","/_images/home-promo-top/frame-"+layer+"-on.gif");
            $("#l"+currentLayer).hide("slide", { direction: "left" }, 750, function () {

                $("#l"+layer).show("slide", { direction: "left" }, 750, function() {
                    currentLayer = layer;
                    var nextLayer = ++layer;
                    if (nextLayer > 3) {
                        nextLayer = 1;
                    }

                    timer = setTimeout("switchHomeBanner("+nextLayer+")", defaultTimeout);
                    animating = false;
                });


            });

        }




};

var showNextLayer = function (layer) {



}