/*
Xeed JS
Author: Warren Buckley & Christian Melbye
*/


//Only run this if the DOM is ready
$(function() {


    /*
    Login Panel
    */
    $(".login a").click(function() {
        $("#loginPanel").slideToggle("slow");
        return false;
    });


    /*
    Login Panel watermark text
    */

    //This fires when user clicks into textbox
    $("#loginPanel .text").focus(function() {
        //Clear value from textbox using empty string
        $(this).val('');
    });

    //This fires when user clicks out of textbox
    $("#loginPanel .text").blur(function() {
        /*
        If user did not enter text into box and clicks away
        reapply HINT
        */
        if ($(this).val() == '') {
            //Set value of textbox to variable titleTxt
            var watermarkText = $(this).attr("title");
            $(this).val(watermarkText);
        }
    });


    /*
    Contact info panel
    */
    $("#team-grid li a").click(function() {

        //Slide down the contact info div
        $("#contact-panel").slideDown("slow");

        //Hide the free text div
        $("div.freeText").hide();

        //Find other active item and remove class
        $(this).parents().find("#team-grid li.active").removeClass("active");

        //Add active class to item clicked
        $(this).parent().addClass("active");

        //check if button has been added before and remove it
        $("div.team a.hideContact").empty().remove();



        //dynamically add image to collapse
        var imgBtnCollapse = "<a class='hideContact' href='#'><img src='Assets/Buttons/up_arrow.gif'/></a>";

        $("#contact-panel").after(imgBtnCollapse);

        /*
        Contact info panel - hide/collapse panel
        */
        $("a.hideContact").click(function() {

            //Remove the button
            $(this).empty().remove();

            //Slide up the contact info div
            $("#contact-panel").slideUp("slow");

            //Find other active item and remove class
            $("#team-grid li.active").removeClass("active");

            //Hide the free text div
            $("div.freeText").show();

            //Stop normal href from working
            return false;
        });

        /*
        This is where we will need to dynamically get the larger photo
        name, email address and web services from the contact url node
        */
        var userInfo = $(this).attr("href") + " .contact-panel";
        $("#contact-panel").load(userInfo);

        //Stop normal href from working
        return false;
    });






    /*
    Services tabs
    */
    $("#services-tabs li a").click(function() {

        //Find other active item and remove class
        $(this).parents().find("#services-tabs li.active").removeClass("active");

        //Add active class to item clicked
        $(this).parent().addClass("active");

        /*
        This is where we will need to dynamically get the text 
        from the services url node
        */
        var serviceText = $(this).attr("href") + " .pnl-text";
        $("#pnl-text").load(serviceText);

        return false;
    });


    /* Feature area */

    // NEED TO RANDOMLY apply active class to <img> and <li> item on load

    
    // ---- TEMPORARILY REMOVE RANDOM & JUST ALWAYS SHOW #1 ----
    var randomnumber = 1;
    
    // Get a random number between 1 and 6
    //var randomnumber = Math.floor(Math.random() * 6) + 1;

    // Close the currently active one
    $("#FeatureArea").find("li.active").removeClass("active");

    $("#item_" + randomnumber).find("div").slideDown();
    $("#item_" + randomnumber).addClass("active");
    ShowImage("item_" + randomnumber);
    
    
    
    


    /* When we click an li item for the feature area */
    $("#FeatureArea ul.right li").click(function() {

        if ($(this).hasClass("active") == false) {

            //Find current active item and slide up
            $(this).parents().find("#FeatureArea ul li.active div").hide();

            //Find other active item and remove class
            $(this).parents().find("#FeatureArea ul li.active").removeClass("active");

            //Lets slide down the new active item
            $(this).find("div").slideDown();

            //Add active class to new item
            $(this).addClass("active");

            //Display image
            ShowImage($(this).attr("id"));

            //Stop the <a> from tag kicking in
            return false;
        }

    });

    //Add link from a tag higher to the span
    $("#FeatureArea ul.right li a div span").click(function() {
        //$(this).stop();
        var link = $(this).parents().find("a").attr("href");
        window.location = link;
    });


});

// Handles feature part on frontpage
$(document).ready(function() {

    $("#myList > li > h1").bind('click', function(e) {

        Reset();
        $(this).parent().find("h2, p, a").slideDown("slow");
        $(this).parent().find("h1").slideDown("slow");
        ShowImage($(this).parent().attr("id"));

    });
});

function Reset() {
    $("li > p, li > h2, li > a").hide();
    $("li > h1").slideDown("slow");
}

function ShowImage(div) {
    var number = div.replace(/item_/, "");
    for (var i = 1; i <= 6; i++) {

        $("#feature_img_" + i).removeClass("active").hide();
    }
    $("#feature_img_" + number).fadeIn(1000).addClass("active");

}

// Handles mouseover on footer map
function HoverMap(img_name, img_src) {
    $("#" + img_name).attr('src', img_src);

}



// Article V2 Tab Switcher
$(document).ready(function() {

    $("#article-tabs li a").click(function() {

        //Find other selected item and remove class
        $(this).parents().find("#article-tabs li a.selected").removeClass("selected");

        //Add selected class to item clicked
        $(this).addClass("selected");

        /*
        This is where we will need to dynamically get the text 
        from the services url node
        */
        var tabText = $(this).attr("href") + " h2";
        $("#article_header div.left").load(tabText);

        return false;
    });
});
