$(document).ready(function() {

    // Replace the typo
    Cufon.replace("#header h1", { fontFamily: 'Gotham Rounded Bold' });
    Cufon.replace("#header h2",  {fontFamily: 'Gotham Rounded Light' });
    Cufon.replace("#navigation li",  {fontFamily: 'Gotham Rounded Light' });
    Cufon.replace("div.line h3", { fontFamily: 'Gotham Rounded Book' });
    Cufon.replace("div.line .anchor", { fontFamily: 'Gotham Rounded Book' });
    Cufon.replace("#work ul li h1", { fontFamily: 'Gotham Rounded Book' });
    Cufon.replace("#about h2", { fontFamily: 'Gotham Rounded Book' });
    Cufon.replace("p.next", { fontFamily: 'Gotham Rounded Book' });

    // Initialice the Lazyload Plugin
    $("img").lazyload({
        effect : "fadeIn"
    });

    // Initialice the tipTip Plugin
	$(".tip").tipTip({
        maxWidth: "auto"
    });

    // Initialice the Anchor Plugin
    $(".anchor").anchorAnimate();

   // Cyrcle Plugin Initialization
    $("li#project_1 a.slide").cycle({
        fx: 'fade',
        timeout: 0,
        prev: '#prev_1',
        next: '#next_1'
     });

    // Josemi images
    $("#josemi").hover(function () {
        $('#josemi_bw').stop().animate( { opacity: 0}, 1000);
    }, function() {
        $('#josemi_bw').stop().animate( { opacity: 1}, 1000);
    });

    // Initialice the Textarea Autogrow Plugin
    $("textarea.expandible").autogrow();

    // Disable right click
     $(document).bind("contextmenu",function(e){
        return false;
    });

    // Add target="_blank" for each <a rel="external">
    // for XHTML Validation
    $('a[rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });

    // Show/Hide navigation for images in projects
    $('#work div.images').hover(function() {
        $(this).find("div.nav").css('visibility', 'visible');
    }, function() {
        $(this).find("div.nav").css('visibility', 'hidden');
    });

    /* Add class rounded to every class unit
       only visible when hover. See next... */
    $(".unit").addClass('rounded');

    /* Add Hover effect for columns (class unit) */
    $(".unit").hover(function() {
        $(this).addClass('unit-hover');
    }, function() {
        $(this).removeClass('unit-hover');
    });

    /* Login effect */
    $(".signin").click(function() {
        $("fieldset#signin_menu").toggle();
		$(".signin").toggleClass("menu-open")
            .removeClass("rounded")
            .addClass("round-top");
    });

    $("fieldset#signin_menu").mouseup(function() {
        return false
    });

    $(document).mouseup(function(e) {
        if($(e.target).parent("a.signin").length==0) {
            $(".signin").removeClass("menu-open")
                .addClass("rounded")
                .removeClass("round-top");
            $("fieldset#signin_menu").hide();
        }
    });

    /* Function to use messagefy */
    function msg(message) {
        $("#message").messagefy({
            msg: message.msg,
            css:  {
               'color': 'white',
               'background': '#4C5E6D'
            },
            sticky: message.sticky
         });
    }

    /* Recaptcha Ajax */
    $("fieldset#recaptcha").remove();

    var contact_send_text = $("#contact_send").val();
    $("#contact_send").remove();
    $("form#contact_form").attr('action', "");
    $("fieldset#form").append('<a id="contact_send_formular" class="home_submit">' + contact_send_text + '</a>');
    
    $("#contact_send_formular").live('click', function(e) {

        e.preventDefault();

        $.post('home/validate',
           {
               name: $('input#name').val(),
               email: $('input#email').val(),
               comments: $('textarea#comments').val(),
               ajax: true,
               view: 'formular'
           }, 
           function(result) {
               //console.log(result);
               if (result.error)
               {
                   var message = {
                      msg: result.msg,
                      sticky: false
                   };

                   msg(message);

               }
               else
               {

                  Recaptcha.create("6LdiiboSAAAAAMeFemKhW8BUjsbv0diZmCAX_BUs",
                        "message", {
                        theme: "clean",
                        callback: function actions() {
                            var send_text = $("#contact_send_formular").html();
                            $("#recaptcha_area").append('<a id="contact_send_recaptcha" class="home_submit">' + send_text + '</a>');

                            var message_temp = $("#message").html();
                            $("#message").html('');
                            message = {
                               msg: message_temp,
                               sticky: true
                            };

                            msg(message);
                            Recaptcha.focus_response_field();
                        }
                     
                    });
               }
           }, "json");

    });

    $("#contact_send_recaptcha").live('click', function () {
       $.post('home/validate',
           {
           name: $('input#name').val(),
           email: $('input#email').val(),
           comments: $('textarea#comments').val(),
           recaptcha_challenge_field: $('#recaptcha_challenge_field').val(),
           recaptcha_response_field: $('#recaptcha_response_field').val(),
           ajax: true,
           view: 'recaptcha'
       }, function(result) {
               if (result.error) {
                    $("#message").append('<div id="recaptcha_response"></div>');
                    $("#recaptcha_response").html(result.msg);
                    Recaptcha.reload();
               } else {
                   var message = {
                      msg: result.msg,
                      sticky: false
                   };

                   msg(message);
               }
        }, "json");

        return false;
    });

});