/* 
 * @About      : Javascript file for custom jQuery functions.
 * @File       : jQuery.JayHealey.js
 * @Version    : 1.0
 * @Author     : James Healey
 */

$(document).ready(function(){
    jQuery_Setup_ContactForm();

    $(".scrollOverHighlight").scrollOverHighlight();

    $("div#twitter").tweet({
        username: "jayhealey",
        join_text: "auto",
        avatar_size: 35,
        count: 3,
        auto_join_text_default: "",
        auto_join_text_ed: "",
        auto_join_text_ing: "",
        auto_join_text_reply: "In reply to ",
        auto_join_text_url: "",
        loading_text: ""
    });

    $(".menu_top a,.welcome").click(function(){
        var str_URL = this.href;
        var str_Split = str_URL.split("#");
        $('html,body').animate({scrollTop: ($("#"+str_Split[1]).offset().top - 22)},'slow');
        return false;
    });

    $('div#lastfm').lastFM({
        username: 'healeyj',
        apikey: 'a27736899fdb4566e20a9d427616157d',
        number: 4, // There's a bug in this plugin - you can't pull back just 1 item
        artSize: 'small',
        noart: 'images/lastfm.gif',
        onComplete: function(){
            $('div.lastfm_container').fadeIn(2000);
        }
    });
    $('div.twitter_container').fadeIn(2000);
    $('body').pngFix();
    $(window).scroll(function(){
        $("ul#menu").removeClass('welcome_content projects_content portfolio_content employers_content skills_content contact_content');
        $(".scrollOverHighlight").scrollOverHighlight();
    });
});


(function($){
  $.fn.scrollOverHighlight = function() {
    int_ScrollPos = $(window).scrollTop();
    int_MenuHeight = $("ul#menu").outerHeight();
    int_ScrollPos += int_MenuHeight; // Border
    var int_PaddingOffset = 25;
   
    return this.each(function() {
        obj = $(this);
        
        /* If top of window position hovering over this item */
        if ((((obj.offset().top - (int_PaddingOffset)) - int_ScrollPos)) < 0 &&
            (int_ScrollPos < (obj.offset().top + obj.height())))
        {
            $("ul#menu").addClass(obj.attr("id"));
        }
     });
  };
 })(jQuery);

function jQuery_Setup_ContactForm()
{
    $("input#form_Name,input#form_Email,textarea#form_Message").focus(function () {
        if ($(this).val() === $(this).attr("title"))
        {
            $(this).val("");
        }
    }).blur(function () {
        if ($(this).val() === "") {
            $(this).val($(this).attr("title"));
        }
    });


    jQuery("form#contact").submit(function() {

    jQuery("span.error").fadeOut();
    bool_FormClean = true;

    var str_Name = jQuery("input#form_Name").val();

    if (js_vf_isTextSet(str_Name) == false || str_Name === $("input#form_Name").attr("title"))
    {
        jQuery("span#error_Name").fadeIn();
        jQuery("input#form_Name").focus();
        bool_FormClean = false;
    }

    var str_Email = jQuery("input#form_Email").val();

    if (!js_isEmailValid(str_Email) || str_Email === $("input#form_Email").attr("title"))
    {
        jQuery("span#error_Email").fadeIn();
        if (bool_FormClean == true)
            jQuery("input#form_Email").focus();
        bool_FormClean = false;
    }

    var str_Message = jQuery("textarea#form_Message").val();

    if (js_vf_isTextSet(str_Message) == false || str_Message === $("textarea#form_Message").attr("title"))
    {
        jQuery("span#error_Message").fadeIn();
        if (bool_FormClean == true)
            jQuery("textarea#form_Message").focus();
        bool_FormClean = false;
    }

    if (bool_FormClean == true)
    {
        str_FormData = jQuery("form#contact").serialize();
        jQuery_AJAXContactForm(str_FormData);
    }
    return false;
});	
}

function jQuery_AJAXContactForm(str_FormData)
{
    jQuery("button#input_contact_submit").html ="Please wait...";
    jQuery("button#input_contact_submit").attr("disabled", "true");
    jQuery.ajax({
        type: "POST"
        ,
	url: "/ajax/ajax_FormContact.php"
        ,
        data: str_FormData
        ,
        success: function() {
            jQuery("#form_ContactArea").fadeOut(1500, function() {
                jQuery("#form_ContactArea").html("<h2>Thank you!</h2><p>Your message has been sent. I'll be in touch soon.</p>")
                .hide()
                .fadeIn(1500)
            });
        }
    });
    return false;
}

function js_vf_isTextSet(str_FormValue)
{
    return (js_Trim(str_FormValue) == "") ? false : true;
}

function js_Trim(str_Value) 
{ 
    while (str_Value.substring(0,1) == ' ')
    {
        str_Value = str_Value.substring(1, str_Value.length);
    }

    while (str_Value.substring(str_Value.length-1, str_Value.length) == ' ')
    {
        str_Value = str_Value.substring(0,str_Value.length-1);
    }

    return str_Value;
}

function js_isEmailValid(str_Email) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(str_Email);
}
