$(document).ready(function () {


    thisPage = location.href.substring((location.href.lastIndexOf("/")) + 1);
    if (thisPage.indexOf("#") > 0) {
        thisPage = thisPage.substring(0, thisPage.indexOf("#"));
    }
    if (thisPage.indexOf("?") > 0) {
        thisPage = thisPage.substring(0, thisPage.indexOf("?"));
    }

    $("ul#nav  li a[href='" + thisPage + "']'").each(function () {
        $(this).parent().addClass('current'); 
    });
    $("#submenu ul li a[href='" + thisPage + "']'").each(function () {
        $(this).parent().addClass('current');
        thisPage = $("#submenu ul").attr("title");
        $("ul#nav  li a[title='" + thisPage + "']'").each(function () {
            $(this).parent().addClass('current');
        });
    });




    // Homepage Slideshow
    $('#slideshow').cycle({
        fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
    });

    thisPage = location.href.substring((location.href.lastIndexOf("/")) + 1);
    if (thisPage.indexOf("#") > 0) {
        thisPage = thisPage.substring(0, thisPage.indexOf("#"));
    }
    if (thisPage.indexOf("?") > 0) {
        thisPage = thisPage.substring(0, thisPage.indexOf("?"));
    }

    $(".menu ul li a[href='" + thisPage + "']'").each(function () {
        $(this).addClass('current');
    });
    $(".menu ul li ul li a[href='" + thisPage + "']'").each(function () {
        $(this).parent().parent().parent().addClass('current');
    });

    // Calendar Hover
    $('#calendarMonth a').hover(function () {
        $(this).addClass('over')
    }, function () {
        $(this).removeClass('over');
    });

    $('#contact_form').submit(function () {
        var errorcount = validate_form();

        //check the error count
        if (errorcount === 0) {
            //alert(errorcount);
            //all good - submit the form and carry on
            //$(this).text('huzzah!');
            //var form = $(this).parents('form:first');
            var form = $('#contact_form');
            //alert(form.attr('name'));
            form.submit();
            return true;
        } else if (errorcount === 1) {
            //something's wrong.
            $('body').animate({ scrollTop: 0 }, 'slow');
            $('p.intro').after('<p class="intro error">There is an error with the form. Please check that all the fields have been entered correctly.</p>');
        } else if (errorcount > 1) {
            $('body').animate({ scrollTop: 0 }, 'slow');
            $('p.intro').after('<p class="intro error">There are ' + errorcount + ' errors with the form. Please check that all the fields have been entered correctly.</p>');
        }
        return false;
    });


    /*$("#payment_credit").click(function () {
        var payment_amount = $("#cost").val();
        var payment_reference = encodeURIComponent($("#event_ref").val().toUpperCase());
        var url = "https://www.payway.com.au/MakePayment?biller_code=122804&payment_amount=" + payment_amount + "&payment_reference=WORKSHOP-" + payment_reference;
        window.location = url;
    });*/


    $('#workshop_registration_submit').click(function () {
        var errorcount = validate_form();

        //check the error count
        if (errorcount === 0) {
            //alert(errorcount);
            //all good - submit the form and carry on
            //$(this).text('huzzah
            //var form = $(this).parents('form:first');
            var form = $('#contact_form');
            //alert(form.attr('name'));
            form.submit();
            return true;
        } else if (errorcount === 1) {
            //something's wrong.
            $('body').animate({ scrollTop: 0 }, 'slow');
            $('p.intro').after('<p class="intro error">There is an error with the form. Please check that all the fields have been entered correctly.</p>');
        } else if (errorcount > 1) {
            $('body').animate({ scrollTop: 0 }, 'slow');
            $('p.intro').after('<p class="intro error">There are ' + errorcount + ' errors with the form. Please check that all the fields have been entered correctly.</p>');
        }
        return false;
    });

    //clear errors if the form field is corrected
    $('input.error, select.error, textarea.error').live('blur', function () {
        //$('input, select, textarea').blur(function(){
        //if ($(this).hasClass('error') == false) {
        //	return;
        //} else {
        if (!$(this).val()) {
            return;
        } else {
            this_id = $(this).attr('id');
            $('label[for="' + this_id + '"]').removeClass('error');
            $(this).removeClass('error');
            $('p#' + this_id + '_error.error').fadeOut(300);
            //	}
        }
    });






});
// Awards Gallery 
$(function() {
	$('.awards a.thumb').lightBox({fixedNavigation:true});
	//$('ul.articles li a').lightBox({fixedNavigation:true});
	$('ul.gallery li a').lightBox({fixedNavigation:true});
});

function validate_form() {

    //required fields checkermajigger

    //reset the error count
    errorcount = 0;
    $('p.error').remove();
    $('.error').removeClass('error');


    //for textboxes, textareas and selects
    $('label:contains("*")').each(function () {
        //if a label has a * in it, and the next form element is empty
        if ($(this).next('input, select, textarea').val() == "" || $(this).next('fieldset').children('input, select, textarea').val() == "") {
            //add .error to the label
            $(this).addClass('error');

            //OFFSET (IF IT'S REQUIRED)
            //get the label's position (relative to the top)
            //var offset = $(this).position();
            //var offset_correction = -80;
            //var offset_correction = $('#offset_correction').html();
            //$(this).text(offset.top);

            //add .error to the field as well
            $(this).next('input, select, textarea').addClass('error');
            $(this).next('fieldset').children('input, select, textarea').addClass('error');

            //WITH OFFSET show an error message with <label> + "is required" (hidden by default)
            //$(this).next('input, select, textarea').after('<p id="'+$(this).attr('for')+'_error" class="error" style="top:'+(offset.top-offset_correction)+'px;">'+$(this).text().replace('*','')+'  is required</p>');
            //$(this).next('fieldset').children('input, select, textarea').after('<p class="error" style="top:'+(offset.top-offset_correction)+'px;">'+$(this).text().replace('*','')+'  is required</p>');

            //WITHOUT OFFSET
            $(this).next('input, select, textarea').after('<p id="' + $(this).attr('for') + '_error" class="error">' + $(this).text().replace('*', '') + '  is required</p>');

            //add one to the error count

            errorcount = errorcount + 1;

            //show all the error messages
            $('p.error').slideDown('slow');
        }
    });


    return errorcount;

}

