var ui = {};

$().ready(function(){
	ui.init();
});

(function ($) {
    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();
        ui.messagepost();

        //Handle which functions are executed
        if (page === "photos") {
            ui.photoLabels();
        }

        if (page === "music") {
            ui.musicLabels();
        }

        ui.formHelper();
    };
    //Social media buttons slide
    ui.socialSlide = function () {
        //Get original height of the social media buttons
        var openHeight = $('#socialmedia').height();
        $('#socialmedia').addClass('close').removeClass('open');
        var closeHeight = $('#socialmedia').height();

        //Animate when mouse enters
        $('#socialmedia').mouseenter(function () {
            $(this).animate({
                height: openHeight
            });
        });

        //Close animate when mouse leaves
        $('#socialmedia').mouseleave(function () {
            $(this).animate({
                height: closeHeight
            });
        });
    };
    //Social media buttons slide
    ui.dynamicLabels = function () {
        //Hide labels (accessibility)
        $("label").hide();
    };
    //Get photo labels.
    ui.photoLabels = function () {
        //Hide labels (accessibility)
        $(".photo-container img").tooltip({ gravity: 's', fade: 'true' });
        $('.photos-container a').lightBox();
    };
    //Get twitter from json feed provided by service.
    ui.twitterGet = function () {
        if ($("#twitterFeed").val() !== undefined) {
            $.ajax({
                type: "POST",
                contentType: "application/json",
                dataType: "json",
                url: "/Home/TwitterFeed",
                success: function (data, textStatus) {
                    if ((tweet !== data || tweet === "") && textStatus === "success") {
                        $("#twitter").fadeOut(function () {
                            var parseStatus = ui.twitterStringHelper(data);
                            $("#twitterStatus").html(parseStatus);
                            tweet = data;
                            $(this).fadeIn();
                        });

                        window.setTimeout("ui.twitterGet()", 10000);
                    }
                },
                error: function () {

                }
            });
        }
    };
    ui.twitterStringHelper = function (twitterStatus) {
        twitterStatus = twitterStatus.replace(/(http:\/\/\w+)(\s|$)/g, '<a href="$1">$1</a>');        
        return twitterStatus.replace(/@(\w+)\s/g, '<a href="http:\/\/www.twitter.com\/$1">@$1<\/a> ');
    };
    //set form labels to hide if placeholder is compatible with browser.
    ui.formHelper = function () {
        if (!Modernizr.input.placeholder) {
            $('.form-label').show();
        }
    };
    ui.messagepost = function () {
        $('#contact-submit').click(function() {
            var form = $('#contact-form');

            var message = { 
                Name: $('#Name').val(), 
                EmailAddress: $('#EmailAddress').val(), 
                Message: $('#Message').val()
            };
            
            $(this).button('loading');
            $('span.loader').html($('#loader').tmpl()).fadeIn();
            
            var jsonData = JSON.stringify(message);
            var status = ui.postData('#contact-submit', jsonData, form);

            return false;
        });
    };
    ui.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"));
            }
        });
    };
    ui.postData = function (buttonId, jsonData, form) {
        $.ajax({
              type: 'POST',
              dataType: 'json',
              data: jsonData,
              url: form.attr('action'),
              contentType: 'application/json; charset=utf-8',
              beforeSend: function () {
                    $(buttonId).attr("disabled", "disabled");
              },
              error: function (data) {
                  return 'false';
              },
              success: function(data){
                  $('#validation').fadeOut(function() {
                      $('#validation').html($('#js-message').tmpl(data)).fadeIn();
                  });

                  $(buttonId).button('complete');
              }
            });
    };
} (jQuery));
