var timer;
var defaultTimeout = 10000;


$.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-gallery.swf?playlist='+$(this).attr('href')+'"/>');
		$(".videoSelected").removeClass('videoSelected');
		$(this).parent().parent().parent().addClass('videoSelected');
		$.log('VideoContainer SRC: ' + $("#videoPlayer").attr('src'));
		return false;
	});

});


var switchHomeBanner = function (layer) {

        if (timer){
            clearTimeout(timer);
        }
        for (var i=1;i<4;i++){
            if (i == layer){
                $("#l"+i).show("slow");
                $("#i"+i).attr("src","_images/home-promo-top/frame-"+i+"-on.gif");
            }else{
                $("#l"+i).hide("slow");
                $("#i"+i).attr("src","_images/home-promo-top/frame-"+i+".gif");
            }
        }
        var nextLayer = ++layer;
        if (nextLayer > 3) {
            nextLayer = 1;
        }
        
        timer = setTimeout("switchHomeBanner("+nextLayer+")", defaultTimeout);
        
};