﻿/*****
    PLUGINS / FUNCTIONS
*****/

// fadeToggle
jQuery.fn.fadeToggle = function(speed, easing, callback) {
    return this.animate({opacity: 'toggle'}, speed, easing, callback);
};

// Footer 'More' links plugin.
function listSlider(te,trimAmount){
    
    // Hide list items after first 7
    var i=0;
    var numberOfLists = $(te).length - 1;

    for (i=0;i<=numberOfLists;i++){
        
        var curList = te[i];
                
        //alert($(curList).find("li").length);
        
        if($(curList).find("li").length > trimAmount){
            $(curList).find("li").slice(trimAmount,50).hide().css("opacity",0);
        }
        else{
        $(curList).find(".more").hide();
        }
        //innerList[i].find('li').slice(7,50).hide().css("opacity",0);
         
         //$(te).find('li').slice(7,50).hide().css("opacity",0);
         
        /*if((innerLists.children()) < 7){
            // if number of child items is greater than 7 do this
            $(te).find('li').slice(7,50).hide().css("opacity",0);
        }*/
    }
    // Toggle the list items after the first 7 on click
    var toggleStatus = false;
    var moreLink =  $(te).find('.more');    
   $(moreLink).click(
        function () {
            toggleStatus = !toggleStatus;   
            if(toggleStatus){
                $(this).parent().find('ul li').slice(trimAmount,50).show(400).animate({opacity:1}, 400);
                $(this).parent().find('.more').addClass("more-active");
                $(this).parent().find('.more').html($(moreLink).html().replace('More','Less'));
            }else{
                $(this).parent().find('ul li').slice(trimAmount,50).animate({opacity:0}, 400).slideUp(400); 
                $(this).parent().find('.more').removeClass("more-active");
                $(this).parent().find('.more').html($(moreLink).html().replace('Less','More'));
        }}
    );
};

function initialiseSearch() {
	$('fieldset.search a').bind('click', function(e){
	    fireSearch();
	    e.preventDefault();
	});

	$('fieldset.search input').bind('keyup', function(e) {
	    if(e.keyCode==13){
		fireSearch();
	    }
	});

	$('fieldset.search input').bind('keyup', function(e) {
	    if (e.keyCode == 13) {
	        fireSearch();
	    }
	});

	$('coursefinderbox.coursefinder-text-input input').bind('keyup', function(e) {
	    if (e.keyCode == 13) {
	        fireCourseSearch();
	    }
	});

	
}

function fireCourseSearch() {
    $('coursefinderbox.finderSearch').click();
}

function fireSearch(btn, event, URL) {
     
    var searchTerm = $('.searchbox').attr("value");    
    // alert(searchTerm);   
    
    searchTerm = "/pages/home/search.aspx?s=" + searchTerm;
    // navigate the url with the value as query string
    window.location.href =searchTerm;
    
}


// Applicatiion form tabs/sections - - Requires the jQuery UI Library
$(document).ready(function() {

    Cufon.replace('#home-header .inner #header-links .promotion .header'); 

    //$("body").show();
    initialiseSearch();
    
    $(".lightbox").lightbox({fitToScreen: true});
    
    $('input:text').hint();
    
    // 'More' links in footer. Toggle expands list on click
    listSlider($(".footer-links-list"),7);
    
    $('#application-form > ul').tabs({fx: { height: 'toggle', opacity: 'toggle' }});
    
    // Toggle Course Finder search box 
    $(".course-finder-toggle").click(function () {
        $("#coursefinderbox").fadeToggle(175); 
        return false;
    });
    
    // Fade in & out the close Sourse Finder box (within box)
    $('.course-finder-close').animate({"opacity": .3 },{duration:100});
    $('.course-finder-close').hover(function() {
            $(this).stop().animate({ "opacity": 1 }, 175);
                    }, function() {
            $(this).stop().animate({ "opacity": .3 }, 175);
    });
    
    //tooltips
    $(".nav-full-time").bind("mouseenter mouseleave", function(e){
        //alert("hi");
        switch ($(this).attr("class"))
        {
            case "nav-full-time":
              
            break;
            case "nav-part-time":
              alert("nav-part-time");
            break;
            default:
        }
    });
    
    $("#tab-nav li").hover( function() {
        var popup = ("#home-header .inner ." + $(this).children("a").attr("rel"));
        
        $(popup).css("display","block");
        $(popup).animate({
          bottom: '+=' + 7 + 'px',
          opacity: 1
        }, 250, 'swing');
        
        // $(currentBubble).fadeIn(400);
        
    }, function() {
        var popup = ("#home-header .inner ." + $(this).children("a").attr("rel"));
        
        $(popup).animate({
          bottom: '-=' + 7 + 'px',
          opacity: 0
        }, 250, 'swing', function() { $(popup).css("display","none"); });
    });

});



// Toggle Basket box
/*$(function() {
    $("#basket").click(function () { 
        $("#basket-box").fadeToggle(175); 
        return false;
    });
});*/



// JScript File
// SET VARIABLES
var stoaf = ".sendToAFriendForm";

// SEND TO A FRIEND ----------------------------------------------------------------------------------------------------------------
function stoafSlide() {
    // on click of either the stoaf button or the close button slideToggle the panel
    //$("#accessibilityPanel .controls a.stoaf, .sendToAFriendForm .closeBtn a").unbind("click");
    //$("#accessibilityPanel .controls a.stoaf, .sendToAFriendForm .closeBtn a").bind("click", function(event){
    $(stoaf).slideToggle(600);
    //event.preventDefault();
    //});
}

// display, change and hide stoaf form
function stoafMsgSent() {
    // after two seconds change to message sent, swap image, swap text, change color
    $(stoaf).delay(2000, function() {
        $(stoaf + ' .stoafSent img').attr("src", "/images/shd/loaded.gif");
        $(stoaf + ' .stoafSent p').css("color", "#A5C718");
        $(stoaf + ' .stoafSent p').text("Message sent");

        // after 3 seconds hide the panel    
        $(stoaf).delay(3000, function() {
            $(stoaf).slideToggle(600);

            // once the panel is hidden change the message back to loading
            $(stoaf).delay(700, function() {
                $(stoaf + ' .stoafSent img').attr("src", "/images/shd/loading.gif");
                $(stoaf + ' .stoafSent p').css("color", "#666666");
                $(stoaf + ' .stoafSent p').text("Sending message...");
                $('#pnlMain').attr("style", "display:block;");
                $('#pnlThanks').attr("style", "display:none;");
            });
        });
    });
}

function validateForm(element) {
    var bOK
    bOK = true;
    $(element).each(
        function() {

            var textBox = this;
            var boxValue = $(this).val();

            if (boxValue == "") {
                $(this).css("background-color", "#EDFFA4");
                bOK = false;
            }
        }
    );
    return bOK;
}

// SEARCH FUNCTION -------------------------------------------------------------------------------------------------
function search() {
    searchTerm = $('.search .search_input').attr('value');
    window.location.href = "/pages/home/search.aspx?s=" + searchTerm;
}