var workInterval;

$(document).ready(function(){
    
    fixie6();
	$("#footer_cols aside").equalHeights();
	$(window).bind("resize", function(){
	    $("#footer_cols aside").height('auto');
        $("#footer_cols aside").equalHeights();
        fixie6();
    });
	
	setupWorkChooser();
	
	setupContactForm();
	
	// new window for 3rd party sites
	$("nav li.menu-item-type-custom a, #links li a, a[rel='blank']").attr('target', '_blank');
	
	// placeholder attribute hack
	if(!Modernizr.input.placeholder) {
		$("#txt_name").placeholdme('name');
		$("#txt_email").placeholdme('email address');
	}
	
});

function fixie6(){
    if($("html.ie").length == 1){
        var height = $(window).height();
        if(height < 420){
            height = 420;
        }
        $(".lte7 #navbar").height(height);
    }
    
}

function setupWorkChooser(){
    
    $("#cycle").height('auto');
    $("#cycle").height($("#cycle").height());
    $("#cycle a").height($("#cycle").height());
    
    $("#cycle figure").css({
        "position": "absolute",
        "width": "100%",
        "display": "block"
    });
    
    var currentIndex = 0;
    var currentAttachment = $("#cycle figure").eq(currentIndex).css("z-index", 2);
    $("#cycle_pager a").eq(currentIndex).addClass("active");
    
    var numAttchments = $("#cycle figure").length;
    
    var nextAttachment = function(){
        $("#cycle figure").css("z-index", 0);
        $("#cycle figure").eq(currentIndex).css("z-index", 1);
        currentIndex++;
        if(currentIndex >= numAttchments){
            currentIndex = 0;
        }
        currentAttachment = $("#cycle figure").eq(currentIndex).hide().css("z-index", 2).fadeIn("slow");
        $("#cycle_pager a").removeClass("active");
        $("#cycle_pager a").eq(currentIndex).addClass("active");
    }
    
    $("#cycle_pager a").click(function(){
        clearInterval(workInterval);
        var attachmentID = $(this).attr("href").split("#")[1];
        $("#cycle figure").css("z-index", 0);
        currentAttachment.css("z-index", 1);
        currentAttachment = $("#" + attachmentID).hide().css("z-index", 2).fadeIn("slow");
        $("#cycle_pager a").removeClass("active");
        $(this).addClass("active");
        
        $f("*").each(function(){
            this.stop();
        });
        
    });
    
    $(window).bind("resize", function(){
        $("#cycle").height(getMaxImageHeight());
        $("#cycle a").height($("#cycle").height());
    });
    
    $("#cycle .poster").load(function(){
        $("#cycle").height(getMaxImageHeight());
        $("#cycle a").height($("#cycle").height());
    });
    
    if(numAttchments > 1){
        $("#cycle_pager").show();
        workInterval = setInterval(nextAttachment, 6000);
    }
    
    var getMaxImageHeight = function(){
        var height = 472;
        $("#cycle .poster").each(function(){
            var fHeight = $(this).height();
            if(fHeight > height){
                height = fHeight;
            }
        });
        return height - 1;
    }
    
}

function setupContactForm(){
    // contact submit effect
    $("#btn_submit").hover(
        function(){$(this).addClass('over')},
        function(){$(this).removeClass('over')}
    );

    // contact validation
    $("#form_contact").validate({
        onfocusout: false,
        onkeyup: false,
        rules: {
            txt_name: {
                required: true,
                notname: true
            },
            txt_email: {
                required: true,
                email: true
            }
        },
        messages: {
            txt_name: "enter your name",
            txt_email: "enter a valid email"
        },
        errorPlacement: function(error, element) {
            error.insertAfter(element);
        },
        submitHandler: function(form) {
            var form_height = $("#form_contact").height();
            $("#form_contact").hide();
            $("#form_result").html("Processing...").css('height', form_height).show();
            $.ajax({
                type: 'POST',
                url: '/wp-content/themes/brightblueday/inc_email.php',
                data: $(form).serializeArray(),
                success: contactResponse
            });
        },
        debug: true
    });
    
    function contactResponse(data){
        //alert(data);
        if(data==1){
            $("#form_result").html("Thank you for your email. We will get back to you shortly.").addClass('okay').show();
        }
        else{
            $("#form_contact").show();
            $("#form_result").html("An error occured").css('height', 'auto').addClass('alert').show();
        }
    }
}


