/*
 * Requires JQuery (Written using ver 1.2.6)
 * http://www.jquery.com
 * http://code.google.com/apis/ajaxlibs/documentation/index.html#jquery
 */

$(document).ready(function() {
    initLightBox();
    initNav();
	initSlides();
//    initValidation();
});

function initLightBox() {
    $('dt.gallery-icon a').lightBox();
}

function initNav() {
    var links = $('ul.right-nav li a');
    var url = window.location.href;
    var url_parts = url.split('/');
    
    /* Make a new array to copy any matching links to */
    var matching_links = new Array();

    links.addClass('inactive');

    links.each(function() {
        var anchor = $(this).attr('href');

        /* Begin by assuming no matching tab */
        var match = false;

        /* Is the link's href text a substring of the url */
        if(url.indexOf(anchor) != -1) {

            /* If we get a match then add this link to the matching links array */
            if((anchor.length > 1) && (url_parts.length > 4)) {
                match = true;
                matching_links.push($(this));
            }

        }
    });

    /* Update the class of the last matching link */
    $(matching_links[matching_links.length - 1]).parent()
        .removeClass('inactive')
        .addClass('active');

    setNavListeners();
}

function initSlides() {
	var images = new Array('boys-canoeing.jpg', 'climbing.jpg', 'made-it.jpg', 'pipe-band.jpg', 'upper-school.jpg');
	
	for (image in images) {
		var tag = $('<img src="/wp-content/homepage/' + images[image] + '" alt="" style="z-index: 1;" />');
	
		$(tag).appendTo('#console-index-flash');
	}	
	
	$('#console-index-flash').cycle();
}

function setNavListeners() {
    var links = $('ul.right-nav li.inactive a');

    links
        .mouseover(
            function() {
                $(this).parent().addClass('hover');
            }
        )
        .mouseout(
            function() {
                $(this).parent().removeClass('hover');
            }
        );
}

function initValidation() {
    $.validator.setDefaults({
            submitHandler: function() { return; }
    });

    $().ready(function() {
            // validate form on keyup and submit
            $("#mailer_cargil_new").validate({
                    rules: {
                            forename: "required",
                            surname: "required",
                            address1: "required",
                            postcode: "required",
                            email: "required"
                    },
                    messages: {
                            forename: "Please enter your forename",
                            surname: "Please enter your surname",
                            address1: "Please enter your address",
                            postcode: "Please enter your postcode",
                            email: "Please enter a valid email address"
                    }
            });

            $(this).focus();
    });
}