﻿$(document).ready(function () {

    $("a[rel]").overlay({
        mask: '#CCC',
        api: true,
        fixed: false,
    });

    $("#facebookLikeSite-Anchor").click(function() {
        $("#facebookLikeSite").slideToggle("fast");
        return false;
    });

    if ($('.monitorImages').length > 0) {
        $('.monitorImages').cycle({
            fx: 'fade'
        });
    }

    if ($('.tooltip').length > 0) {
        $('.tooltip').hide();
    }

    attachExpandoEnhancements();
    attachEnquiryFormEnhancements();

    var enquiryFormOptions = {
        url: $('#enquiryForm').attr("action"),
        target: '#formContainer',
        beforeSerialize: function () {
            $("#enquiryFormSubmit").attr("disabled", "disabled").val("Please wait...");
        },
        success: function () {
            $("#enquiryFormSubmit").removeAttr("disabled").val("Send enquiry");
            attachEnquiryFormEnhancements();
        }
    };

    // bind to the form's submit event 
    $('#enquiryForm').live('submit', function () {
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        $(this).ajaxSubmit(enquiryFormOptions);

        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false;
    });

    var newsletterFormOptions = {
        url: $('#newsletterForm').attr("action"),
        target: '#newsletterFormContainer',
        beforeSerialize: function () {
            $("#newsletterFormSubmit").attr("disabled", "disabled").val("Please wait...");
        },
        success: function () {
            $("#newsletterFormSubmit").removeAttr("disabled").val("Register");
        }
    };

    // bind to the form's submit event 
    $('#newsletterForm').live('submit', function () {
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        $(this).ajaxSubmit(newsletterFormOptions);

        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false;
    });

    $("a[href $= '.pdf']").each(function (index) {
        var title = $(this).attr("title") == '' ? $(this).text() : $(this).attr("title");
        $(this).attr("title", title + " [PDF document, opens in a new window]");
        $(this).attr('target', '_blank').addClass('pdf');
    });

    $("a[href$='.xls'], a[href$='.xlsx']").each(function (index) {
        var title = $(this).attr("title") == '' ? $(this).text() : $(this).attr("title");
        $(this).attr("title", title + " [Excel spreadsheet, opens in a new window]");
        $(this).attr('target', '_blank').addClass('xls');
    });

    $("a[href$='.doc'], a[href$='.docx']").each(function (index) {
        var title = $(this).attr("title") == '' ? $(this).text() : $(this).attr("title");
        $(this).attr("title", title + " [Word document, opens in a new window]");
        $(this).attr('target', '_blank').addClass('doc');
    });


    $("a[target = '_blank']").not(".pdf, .xls, .doc").each(function (index) {
        var title = $(this).attr("title") == '' ? $(this).text() : $(this).attr("title");
        $(this).attr("title", title + " [Opens in a new window]");
    });
});

function attachEnquiryFormEnhancements() {
    $("fieldset.contactus :input[title], textarea[title], select[title]").not("input[type=submit]").tooltip({ tipClass: "tooltip rounded input", opacity: 0.95, effect: "toggle", position: "center left", offset: [0, -10] });

    $('fieldset.contactus input, textarea, select').not("input[type=submit]").parent().addClass("idleField");
    $('fieldset.contactus input, textarea, select').not("input[type=submit]").focus(function () {
        $(this).parent().removeClass("idleField").addClass("focusField");
        if (this.value == this.defaultValue) {
            this.value = '';
        }
        if (this.value != this.defaultValue) {
            this.select();
        }
    });
    $('fieldset.contactus input, textarea, select').not("input[type=submit]").blur(function () {
        $(this).parent().removeClass("focusField").addClass("idleField");
        if ($.trim(this.value == '')) {
            this.value = (this.defaultValue ? this.defaultValue : '');
        }
    });
}

function attachExpandoEnhancements() {
    $('.section').hide();
    $('.intro').append('<p class="right"><a class="readMore" href="#">Read more &raquo</a></p>');
    $('.intro a.readMore').click(
            function () {
                if ($(this).parent().parent().next('.section').css("display") == "none") {
                    $(this).parent().parent().next('.section').slideDown();
                    $(this).html('&laquo Read less');
                }
                else {
                    $(this).parent().parent().next('.section').slideUp();
                    $(this).html('Read more &raquo');
                }
                return false;
            });
    $('a.readAll').toggle(
            function () {
                $('.section').slideDown();
                $(this).html('&laquo Collapse all sections');
                $('.intro a.readMore').html('&laquo Read less');
            },
            function () {
                $('.section').slideUp();
                $(this).html('Expand all sections &raquo');
                $('.intro a.readMore').html('Read more &raquo');
            });
}
