/* -------------------------------------------------------------------------------- */
/*  Taranaki Cricket 2011 - In Page JS Code  								        */
/*  Ver     : 1                                                                     */
/*  Created : 29 June 2011                                                          */
/* -------------------------------------------------------------------------------- */

/*-----------*/		
/* Functions */
/*-----------*/

var debug = false;
var currGallery;

function animatedFeatured(contentContainer, navContainer)
{		
    var $contentContainer = $(contentContainer),
        $navContainer = $(navContainer),
        $navItem = $(navContainer + " a"),
        currIndex = 0,
        modIndex = 0;
    
    // Display current active item
    $contentContainer.find("li.active").show();	
    
    // Setting the panel index
    $(contentContainer + ' li').each(function(index) {
        
        modIndex = index + 1;

        $(this).addClass("panel-item panel-" + modIndex);
    });

    // If a nav item is clicked
    $navItem.live("click", function () {
        
        // Get the index of clicked item
        currIndex = $navItem.index(this) + 1;
        
        // Remove selected class from current selected item
        $navContainer.find("li.selected").removeClass("selected");
        
        // Add selected class to the clicked item
        $(this).parent().addClass("selected");		
        
        // Fade out current active item
        $contentContainer.find("li.active").removeClass("active").fadeOut(400, function()
        {
            $(this).parent().find("li.panel-" + currIndex).fadeIn(400, function()
            {
                $(this).addClass("active");
            });
        });	
        
        // Preventing default behaviour
        return false;
    });
}

/*--------------------------*/		
/* Document Ready Functions */
/*--------------------------*/

$(document).ready(function () {

    /*--------------------*/
    /* MULTIMEDIA SCRIPTS */
    /*--------------------*/
    //photos
    $("div").removeClass("non-js");
    $("div.photo-info").remove();

    $(".tab-content").hide(); //Hide all content
    $("ul.gallery-navigation li:first").addClass("selected").show(); //Activate first tab
    $(".tab-content:first").show(); //Show first tab content
    // On Click Event
    $("ul.multimedia-items li").click(function () {
        showGallery(this);
    });
    //display the first gallery in the list, if there
    var galleryListItems = $(".galleryListItem");
    if (galleryListItems) {
        if (galleryListItems[0]) showGallery(galleryListItems[0]);
        if (debug) alert("--init--");
    }
    //videos
    $("ul.video-list > li").hide(); //hide all videos
    $("ul.video-list > li:first").show(); //show first video

    // secondary navigation init
    $("div.sec-lv-nav li").children().first().parent().addClass("first");

    // fancybox init
    $("a.fancybox-init").fancybox({
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'speedIn': 200,
        'speedOut': 200,
        'overlayShow': false,
        'titlePosition': 'inside'
    });

    // Notice board init
    $(".notice-title-list").children().first().addClass("selected");
    $(".notice-content-list").children().first().addClass("active");

    // news page init
    $("#news-filter").children().click(function () {
        var filter = $(this).text().toString().toLowerCase();

        if ($(this).hasClass("selected")) {
            $(this).removeClass("selected");

            // find all list items with a class of the value of filter and toggle it.
            $("li." + filter).hide();
            fixNewsDates();
        } else {
            $(this).addClass("selected");

            // find all list items with a class of the value of filter and toggle it.
            $("li." + filter).show();
            fixNewsDates();
        }
        return true;
    });

    // highlight the current value
    var currentFilter = $("[id$='currentFilter']").val();
    if (currentFilter != undefined) {
        if (currentFilter.toString().length > 0) {
            $("#noticeboard-filter li").each(function () {
                $(this).removeClass("selected");
            });
            $("[id$='" + currentFilter + "']").parent().addClass("selected");
        } else {
            $("[id$='all']").parent().addClass("selected");
        }
    }

    // Class init
    $("[id$='bodytag']").addClass("js");

    // Trigger Featured Notice Board
    if ($("body.section-home").length) {
        animatedFeatured("ul.notice-content-list", "ul.notice-title-list");
    }

    // Trigger Sponsor Slider
    $(function () {
        $("div.sponsor-slider").jCarouselLite({
            auto: 7000,
            speed: 400,
            visible: 5,
            easing: 'swing'
        });
    });
});

/*-----------------------*/		
/* Window Load Functions */
/*-----------------------*/

$(window).load(function(){		
});

function fixNewsDates() {
    // we need to iterate through every UL.
    // if $(this) UL contains no VISIBLE li then hide the element directly before the $(this) UL.
    // else, display it.

    var ParentDiv = $("#news-list");
    $(ParentDiv).find("h3").hide();
    $(ParentDiv).children().each(function () {
        // child li = $(this);
        if ($(this).is(":visible")) {
            $(this).prev().show();
        }
    });
}

/*-------------------*/
/* Email Fields      */
/*-------------------*/
$(document).ready(function () {
    var $mailField = $("input.email");
    var mailText = "Enter your email address";
    $mailField.val(mailText);

    $mailField.focus(function () {
        if ($(this).val() == mailText) {
            $(this).val("");
        }
    });
    $mailField.blur(function () {
        if ($(this).val() == "") {
            $(this).val(mailText);
        }
    });
});

/*-------------------*/
/* Password Fields   */
/*-------------------*/
$(document).ready(function () {
    var $passwordField = $("input.password");
    var passwordText = "Enter your password";
    $passwordField.val(passwordText);

    $passwordField.focus(function () {
        if ($(this).val() == passwordText) {
            $(this).val("");
        }
    });
    $passwordField.blur(function () {
        if ($(this).val() == "") {
            $(this).val(passwordText);
        }
    });
});

/*----------------*/
/*  SHOW GALLERY  */
/*----------------*/
function showGallery(elem) {
    //alert('hello.');
    //$("div.ad-image").hide();
    $("ul.multimedia-items li").removeClass("selected"); //Remove any "active" class
    $(elem).addClass("selected"); //Add "selected" class to selected tab		
    $(".tab-content").hide(); //Hide all tab content

    //Find the href attribute value to identify the active tab + content
    //alert(elem.href);
    var activeTab = "";

    activeTab = $(elem).find("a").attr("href");
    activeTab = activeTab.substring(activeTab.indexOf("#"));


    //var destination = $(".multimedia-tab-content").offset().top; //Set the destination of scrolling to the multimedia container	
    //$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination - 20 }, 500);

    $(activeTab).fadeIn(); //Fade in the active ID content


    if ($("ul.multimedia-items").hasClass("photo")) //if the tab's content is photo gallery then ... load gallery 
    {

        // Removing gallery control elements if they are exist
        $('p.ad-info').remove();
        $('span.ad-slideshow-start').remove();
        $('span.ad-slideshow-stop').remove();
        $('span.ad-slideshow-countdown').remove();
        $('span.ad-slideshow-controls').remove();

        //set and append the control elements		
        currGallery = $(".ad-gallery").adGallery({ loader_image: 'img/jquery-ad-gallery/loader.gif' });

        //currGallery[0].slideshow.toggle();
    };

    return false;
}

/* SUBMIT A RESULT */
function unselectOtherTeams(sender) {
    //iterate through
    $("select.team-selector").each(function () {
        if (!($(this).attr("id") == sender.id)) {
            $(this).val(0);
        }
    });
}

/*-----------------*/
/* SEARCH TEXT BOX */
/*-----------------*/
$(document).ready(function () {
    var $searchField = $("[id$='SearchBox']");
    var searchText = "search for ...";
    $searchField.val(searchText);

    $searchField.focus(function () {
        if ($(this).val() == searchText) {
            $(this).val("");
        }
    });
    $searchField.blur(function () {
        if ($(this).val() == "") {
            $(this).val(searchText);
        }
    });
});

function clubMenu(element) {
    var ul = $(element).parent().parent();
    var li = $(element).parent();
    var next = $(li).next();

    if ($(li).hasClass("selected")) {
        $(li).next().hide();
        $(li).removeClass("selected");
        return;
    }

    // hide all li.submenu
    $(ul).children("li.submenu").hide();
    $(ul).children("li").removeClass("selected");

    // expand this submenu
    if ($(next).is(":hidden")) {
        $(next).show();
    } else {
        $(next).hide();
    }

    // set selected state
    $(li).addClass("selected");
}
