﻿$(function() {

    /* == Adds the print page functionality == */
    $(".print").click(function(event) { window.print(); });

});

/* == Loads before page content == */
$(document).ready(function () {

    $("#nav li a:first").addClass("first");
    $("#nav li a:last").addClass("last");

    /* == Hover effects for the ctas on the homepage ==
    $('.cta ul li').hover(function() {
    if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
    $(this).siblings().css({ "opacity": "0.3" });
    }
    else {
    $(this).siblings().fadeTo("fast", 0.3);
    }
    }, function() {
    $(this).css({ "opacity": "1" });
    $(this).siblings().css({ "opacity": "1" });
    });*/

    /* == Clears the search input field == */
    $("#header .inputbox").focus(function () {
        if ($(this).val() == "Search") {
            $(this).val("");
        }
    });
    $("#header .inputbox").blur(function () {
        if ($(this).val() == "") {
            $(this).val("Search");
        }
    });

    /* == Clears the additional cards input field == */
    $(".additionalcards .inputbox").focus(function () {
        if ($(this).val() == "1") {
            $(this).val("");
        }
    });
    $(".additionalcards .inputbox").blur(function () {
        if ($(this).val() == "") {
            $(this).val("1");
        }
    });

    /*$('a.zoom').fancyZoom();

    $('a.zoom').hover(function() {
    $(this).fancyZoom();
    });*/

    /*
    $('.cta li').hover(function () {

    var iDelay = 200;

    var href = $(this).find('a').attr('href'),
    liIndex = $('.cta li').index(this) + 1;

    var lielement = $(this);

    $(this).css("z-index", "999");
    $(this).append('<div id="zoom" class="popup' + liIndex + '">' + $(href).html() + '</div>');
    $(this).parents('.cta').before('<div id="blkoverlay" class="ie6fix" style="height:' + $('#div-bg').height() + 'px;"></div>');
    $('#footer').before('<div id="footeroverlay"></div>');
    $('#footeroverlay').css('top', document.body.offsetHeight - 30);

    //added a delay in here to stop the animation happening straight away
    $('#zoom').delay(iDelay).animate({
    opacity: 1
    }, 400);

    $('#blkoverlay, #footeroverlay').delay(iDelay).animate({
    opacity: .5
    }, 400);


    }, function () {
    $('#zoom, #blkoverlay, #footeroverlay').remove();

    $(this).css("z-index", "");
    });
    */


    /* Homepage overlays */
    $(document).ready(function () {

        $('.selector1').hover(function () {

            var p = $(".selector1");
            var offset = p.offset();

            $("#popup1").overlay(
                        {
                            top: offset.top - 308,
                            left: offset.left,
                            mask: {
                                color: '#000',
                                opacity: 0.4
                            },
                            fixed: true,
                            closeOnClick: false
                        });

            var api = $("#popup1").overlay();
            api.getConf().left = offset.left;
            api.getConf().top = offset.top - 308;

            api.load();
        });

        $('.selector2').hover(function () {

            var p = $(".selector2");
            var offset = p.offset();

            $("#popup2").overlay(
                        {
                            top: offset.top - 375,
                            left: offset.left - 145,
                            mask: {
                                color: '#000',
                                opacity: 0.4
                            },
                            fixed: true,
                            closeOnClick: false
                        });

            var api = $("#popup2").overlay();
            api.getConf().left = offset.left - 145;
            api.getConf().top = offset.top - 308;

            api.load();
        });

        $('.selector3').hover(function () {

            var p = $(".selector3");
            var offset = p.offset();

            $("#popup3").overlay(
                        {
                            top: offset.top - 375,
                            left: offset.left - 275,
                            mask: {
                                color: '#000',
                                opacity: 0.4
                            },
                            fixed: true,
                            closeOnClick: false
                        });

            var api = $("#popup3").overlay();
            api.getConf().left = offset.left - 275;
            api.getConf().top = offset.top - 308;

            api.load();
        });

        /* Close popups */
        $('#popup1').hover(function () {
            //do nothing 
        }, function () {
            $('#popup1').data('overlay').close();
        });

//        $('#exposeMask').live('hover', function () {
//            // Live handler called.
//            $('#popup1').data('overlay').close();
//            $('#popup2').data('overlay').close();
//            $('#popup3').data('overlay').close();
//        });

        $('#popup2').hover(function () {
            //do nothing 
        }, function () {
            $('#popup2').data('overlay').close();
        });

        $('#popup3').hover(function () {
            //do nothing 
        }, function () {
            $('#popup3').data('overlay').close();
        });
    });
    /* end homepage overlays */


    $('a.leftnavarrow').click(function () {
        $('#timeline ul').animate({ left: '0' });
        return false;
    });

    $('a.rightnavarrow').click(function () {
        $('#timeline ul').animate({ left: '-116' });
        return false;
    });

    $('#timeline li').click(function () {
        $('.awardslist').css("display", "none");

        $('#awards-' + $(this).html()).css("display", "");
    });

    $(".inputbox").blur(function () {
        if ($(this).attr("class").match(/required/)) {

            if ($(this).attr("value") == "") {
                $(this).addClass("error");
                validationError = true;
            }
            else {
                $(this).removeClass("error");
            }

        }

        if ($(this).attr("class").match(/email/)) {

            if (!$(this).attr("value").match(/^\w[\w|\.|\-]+@\w[\w|\.|\-]+\.[a-zA-Z]{2,4}$/)) {
                $(this).addClass("error");
                validationError = true;
            }
            else {
                $(this).removeClass("error");
            }

        }
        if ($(this).attr("class").match(/inputfile/)) {
            var aFileName = value.split('.');
            var sDoctype = aFileName[aFileName.length - 1];

            if (!sDoctype.match(/[doc,docx]/gi)) {
                $('#inputfiletype strong').css("color", "#ff0000");
                validationError = true;
            }
            else {
                $('#inputfiletype strong').css("color", "#000");
            }
        }

    });

    var url = "/SubmitTo/SendEmail.aspx";

    $('#contactfrm').submit_ajax_form({ sendPath: url });

    var urlsendcv = "/SubmitTo/SendCV.aspx";

    $('#sendcvfrm').submit_ajax_form({ sendPath: urlsendcv, sendAjaxForm: '#sendcvfrm' });

    var urldownloads = "/SubmitTo/RequestDownload.aspx";

    $('#downloadsfrm').submit_ajax_form({ sendPath: urldownloads });

    $(".download span a").click(function () {
        $('.ajaxresponse').slideUp(400);
        $('#downloadsfrm .sendbutton').css("display", "block");
        var spanId = "#" + $(this).parent().attr("id") + " strong";

        if ($(this).attr("href") == "#") {
            $(this).css("background-position", "0 -24px");
            $(this).attr("href", "");

            switch ($(this).parent().attr("id")) {
                case "one":
                    $("#NonIRRHeader").css("display", "block");
                    $("#IRRHeader").css("display", "none");
                    $('input:hidden[name=stbarometer]').val($(spanId).html());
                    break;
                case "two":
                    $("#NonIRRHeader").css("display", "block");
                    $("#IRRHeader").css("display", "none");
                    $('input:hidden[name=stsurvey]').val($(spanId).html());
                    break;
                case "three":
                    $("#NonIRRHeader").css("display", "none");
                    $("#IRRHeader").css("display", "block");
                    $('input:hidden[name=stirrcards]').val($(spanId).html());
                    $(".additionalcards").css("display", "block");
                    $('input:text[name=stadditionalcards]').val("1");
                    break;
            }

            $('#downloadsfrm').slideDown(400);
        }
        else {
            $(this).css("background-position", "0 0");
            $(this).attr("href", "#");

            switch ($(this).parent().attr("id")) {
                case "one":
                    $("#NonIRRHeader").css("display", "block");
                    $("#IRRHeader").css("display", "none");
                    $('input:hidden[name=stbarometer]').val("");
                    break;
                case "two":
                    $("#NonIRRHeader").css("display", "block");
                    $("#IRRHeader").css("display", "none");
                    $('input:hidden[name=stsurvey]').val("");
                    break;
                case "three":
                    $("#NonIRRHeader").css("display", "none");
                    $("#IRRHeader").css("display", "block");
                    $('input:hidden[name=stirrcards]').val("");
                    $(".additionalcards").css("display", "none");
                    $('input:text[name=stadditionalcards]').val("0");
                    break;
            }

            var bKeepFrmOn = false;

            $(".download span a").each(function () {
                if ($(this).attr("href") == "") {
                    bKeepFrmOn = true;
                }
            });

            if (!bKeepFrmOn) {
                $('#downloadsfrm').slideUp(400);
            }
        }
        return false;
    });

});

$(function() {
    $('ul#nav').superfish({
        delay: 0,
        autoArrows: false,
        speed: 'fast',
        animation: { opacity: 'toggle', height: 'show' }
    });
});

(function($) {
    $.fn.submit_ajax_form = function(options) {
        var defaults =
		{
		    sendPath: '/SubmitTo/SendEmail.aspx',
		    responseContainer: 'ajaxresponse',
		    sendAjaxForm: ''
		};

        var options = $.extend(defaults, options);

        return this.each(function() {
            var form = $(this),
				send =
				{
				    formElements: form.find('textarea, select, input:text, input[type=hidden], input[type=file]'),
				    validationError: false,
				    button: form.find('input:image'),
				    datastring: ''
				};

            send.button.bind('click', checkElements);

            function send_ajax_form() {
                send.button.fadeOut(300);

                $.ajax({
                    type: "POST",
                    url: options.sendPath,
                    data: send.datastring,
                    success: function(response) {

                        var message = $("<div></div>").addClass(options.responseContainer)
														.css('display', 'none')
														.css('clear', 'both')
														.insertBefore(form)
														.html(response);

                        form.slideUp(400, function() { message.slideDown(400), send.formElements.val(''); });

                    }
                });

            }

            function checkElements() {
                // reset validation var and send data
                send.validationError = false;
                send.datastring = 'ajax=true';

                send.formElements.each(function(i) {
                    var currentElement = $(this),
						surroundingElement = currentElement.parent(),
						value = currentElement.val(),
						name = currentElement.attr('name'),
					 	classes = currentElement.attr('class'),
					 	nomatch = true;

                    send.datastring += "&" + encodeURIComponent(name) + "=" + encodeURIComponent(value);

                    if (classes.match(/required/)) {
                        if (value == '') {
                            $(this).addClass("error");
                            send.validationError = true;
                        }
                        else {
                            $(this).removeClass("error");
                        }
                        nomatch = false;
                    }

                    if (classes.match(/email/)) {
                        if (!value.match(/^\w[\w|\.|\-]+@\w[\w|\.|\-]+\.[a-zA-Z]{2,4}$/)) {
                            $(this).addClass("error");
                            send.validationError = true;
                        }
                        else {
                            $(this).removeClass("error");
                        }
                        nomatch = false;
                    }

                    if (classes.match(/inputfile/)) {
                        var aFileName = value.split('.');
                        var sDoctype = aFileName[aFileName.length - 1];

                        if (!sDoctype.match(/[doc,docx]/gi)) {
                            $('#inputfiletype strong').css("color", "#ff0000");
                        }
                        else {
                            $('#inputfiletype strong').css("color", "#000");
                        }
                        nomatch = false;
                    }
                });

                if (send.validationError == false) {
                    if (options.sendAjaxForm == '') {
                        send_ajax_form();
                    }
                    else {
                        $(options.sendAjaxForm).submit();
                    }
                }
                return false;
            }
        });
    }
})(jQuery);
