$(document).ready(function(){
	ui.init();
	
	//Initialize the ajax history item.
	//$.ajaxHistory.initialize();
});

var tweet = "";

ui = {
    init: function () {
        ui.socialSlide();

        //Split by pages
        var url = window.location.toString();
        url = url.substring(0, url.length - 1); //remove last /
        var page = url.substring(url.lastIndexOf("/") + 1);
        page = page.toLowerCase();
        ui.twitterGet();

        //Handle which functions are executed
        if (page == "photos") {
            ui.photoLabels();
        }

        if (page == "music") {
            ui.musicLabels();
        }

        if (page == "contact") {
            ui.formPost();
            ui.formHelper();
        }
    },
    //Social media buttons slide
    socialSlide: function () {
        //Get original height of the social media buttons
        var openHeight = $('#socialmedia').height();
        $('#socialmedia').addClass('close').removeClass('open');
        var closeHeight = $('#socialmedia').height();

        $('#socialmedia').mouseenter(function () {
            $(this).animate({
                height: openHeight
            });
        });

        $('#socialmedia').mouseleave(function () {
            $(this).animate({
                height: closeHeight
            });
        });
    },
    //Social media buttons slide
    dynamicLabels: function () {
        //Hide labels (accessibility)
        $("label").hide();
    },
    photoLabels: function () {
        //Hide labels (accessibility)
        $(".photoContainer img").tipsy({ gravity: 's', fade: 'true' });
        $('.photosContainer a').lightBox();
    },
    musicLabels: function () {
        //Hide labels (accessibility)
        /*
        $(".music a").tipsy(
        {
        gravity: 'w',
        fade: 'true',
        html: true
        }
        );
        */
    },
    twitterGet: function () {
        if ($("#twitterFeed").val() != undefined) {
            $.ajax({
                type: "POST",
                contentType: "text/plain; charset=utf-8",
                url: "/Home/TwitterFeed",
                success: function (data, textStatus, XMLHttpRequest) {
                    if ((tweet != data || tweet == "") && textStatus == "success") {
                        $("#twitter").fadeOut(function () {
                            $("#twitterStatus").html(data);
                            tweet = data;
                            $("#twitter").fadeIn();
                        });
                    }
                }
            });

            window.setTimeout("ui.twitterGet()", 10000);
        }
    },
    formHelper: function () {
        //Clear when HTML 5 is more ubiquitous
        $("input[id='Name']").val($("input[id='Name']").attr("placeholder")).addClass("grey").focus(function () {
            $(this).val("").removeClass("grey");
        });

        $("input[id='EmailAddress']").val($("input[id='EmailAddress']").attr("placeholder")).addClass("grey").focus(function () {
            $(this).val("").removeClass("grey");
        });

        $("#Message").val($("#Message").attr("placeholder")).addClass("grey").focus(function () {
            $(this).val("").removeClass("grey");
        });

        $("label[for='Name']").hide();
        $("label[for='EmailAddress']").hide();
        $("label[for='Message']").hide();
    },
    formPost: function () {
        $("#ContactForm").validate({
            validClass: "valid",
            errorClass: "invalid",
            messages: {
                Name: "Please enter your name",
                EmailAddress: { required: "Please enter your email", email: "Please enter a valid email" },
                Message: "Please enter a message"
            },
            submitHandler: function (form) {
                var options = {
                    beforeSubmit: function () {
                        //Show loader
                        $(".loader").show();
                    },
                    success: function () {
                        //Form posted
                        $(form).fadeOut(function () {
                            $("div#confirm").fadeIn();
                        });
                    }
                };

                // bind 'myForm' and provide a simple callback function
                $("#ContactForm").ajaxSubmit(options);
                return false;
            }
        });
    },
    fader: function () {
        //Prepend each link with a rel of "fader" to have a # at the beginning.
        $("a").each(function () {
            if ($(this).attr("rel") == "fader" && $(this).attr("href").indexOf("#") == -1) {
                $(this).attr("href", "#" + $(this).attr("href"));
            }
        });
    }
}