$(document).ready(function() {

    //***********************************************//
    // COMMON STUFF
    //***********************************************//
    am09_WMS.init();
    //homepage flash
    $(".homepageFlash h1").flash(
	    { width: 960, height: 380, autoplay: true, wmode: 'transparent' },
	    {},
	    function(htmlOptions) {
	        $this = $(this);
	        htmlOptions.src = "/flash/homepage_slideshow.swf";
	        $this.before($.fn.flash.transform(htmlOptions));
	    }
    );

    //random facts
    // Tells us how many images we have available.
    var numberOfImages = 5;
    var randomNumber = getRandom(numberOfImages);

    // Create an array to hold the names of all images.
    var image = new Array(numberOfImages);
    image[0] = "/images/facts/adrenalin_fact_1.png";
    image[1] = "/images/facts/adrenalin_fact_2.png";
    image[2] = "/images/facts/adrenalin_fact_3.png";
    image[3] = "/images/facts/adrenalin_fact_4.png";
    image[4] = "/images/facts/adrenalin_fact_5.png";
    if (randomNumber > 4) { randomNumber = 4; }
    if (randomNumber < 0) { randomNumber = 0; }
    // Write the img tag with a random image name.
    $(".randomFacts").append("<img src='" + image[randomNumber] + "' alt='Random Facts' />");

    //side blog tab
    $(".sideBlog .tabLinks li").each(function(index, domEle) {
        $(domEle).bind("click", function() {
            $(domEle).addClass("selected").siblings().removeClass("selected");
            $(".sideBlog .tabContent ul").removeClass("show");
            $(".sideBlog .tabContent ul:eq(" + index + ")").addClass("show");
            return false;
        });
    });

    //client & partner list image hover
    $(".clientLogos img, .partnerList img, .pageFunctions .socialSharing a img").hover(
        function() {
            var this_src = $(this).attr("src");
            if (this_src.indexOf("_hover.png") == -1) {
                $(this).attr("src", this_src.replace(/.png/, "_hover.png"));
            }
        },
        function() {
            var this_src = $(this).attr("src");
            $(this).attr("src", this_src.replace(/_hover.png/, ".png"));
        }
    );

    //watermarks    
    var swapValues = []; $(".watermarks").each(function(i) { swapValues[i] = $(this).val(); $(this).focus(function() { if ($(this).val() == swapValues[i]) { $(this).val("") } }).blur(function() { if ($.trim($(this).val()) == "") { $(this).val(swapValues[i]) } }) });

    $("a[href^='#']").each(function() {
        $(this).attr("href", window.location.href + $(this).attr("href"));
    })

    //blogCommentForm validation
    $(".blogCommentForm .inputName").blur(function() { $(this).validateInput({ watermarks: 'Your Name' }); });
    $(".blogCommentForm .inputEmail").blur(function() { $(this).validateInput({ watermarks: 'Your Email', validationType: 'email' }); });
    $(".blogCommentForm .inputComment").blur(function() { $(this).validateInput({ watermarks: 'Your Comment' }); });

    //enquiryForm validation
    $(".enquiryForm .inputName").blur(function() { $(this).validateInput({ watermarks: 'Name' }); });
    $(".enquiryForm .inputEmail").blur(function() { $(this).validateInput({ watermarks: 'Email', validationType: 'email' }); });
    $(".enquiryForm .inputPhone").blur(function() { $(this).validateInput({ watermarks: 'Telephone' }); });
    $(".enquiryForm .inputPosition").blur(function() { $(this).validateInput({ watermarks: 'Position' }); });
    $(".enquiryForm .inputCompany").blur(function() { $(this).validateInput({ watermarks: 'Company' }); });
    $(".enquiryForm .inputMessage").blur(function() { $(this).validateInput({ watermarks: 'Message...' }); });

    //subscribeForm validation
    $(".subscribeForm .inputSubscribe").blur(function() { $(this).validateInput({ watermarks: 'Your Email', validationType: 'email', subscribeForm: 'yes' }); });

    //back to top
    $(".backToTop span").hover(
        function() { $(this).addClass("hover"); },
        function() { $(this).removeClass("hover"); }
    );
    $(".backToTop span").click(function() {
        $('html, body').animate({ scrollTop: 0 }, 'slow');
    });

    // blog listing
    $(".componentBlog > ul li.itemGrid:first").addClass("fisrt");
    $(".componentBlog > ul li.itemGrid:last").addClass("last");
});

// Function that return a number between 0 and "nums - 1"
function getRandom(nums) {
    var ranNum = Math.round(Math.random() * nums);
    return ranNum;
}

function validateBlogCommentForm() {
    $(".blogCommentForm .inputName").validateInput({ watermarks: 'Your Name' });
    $(".blogCommentForm .inputEmail").validateInput({ watermarks: 'Your Email', validationType: 'email' });
    $(".blogCommentForm .inputComment").validateInput({ watermarks: 'Your Comment' });
    if ($(".blogCommentForm li span.inputInvalid").length > 0) { return false; }
    else {return true;}
}

function validateEnquiryForm() {
    $(".enquiryForm .inputName").validateInput({ watermarks: 'Name' });
    $(".enquiryForm .inputEmail").validateInput({ watermarks: 'Email', validationType: 'email' });
    $(".enquiryForm .inputPhone").validateInput({ watermarks: 'Telephone' });
    $(".enquiryForm .inputPosition").validateInput({ watermarks: 'Position' });
    $(".enquiryForm .inputCompany").validateInput({ watermarks: 'Company' });
    $(".enquiryForm .inputMessage").validateInput({ watermarks: 'Message...' });
    if ($(".enquiryForm li span.inputInvalid").length > 0) { return false; }
    else { return true; }
}

function validateSubscribeForm() {

    $(".subscribeForm .inputSubscribe").validateInput({ watermarks: 'Your Email', validationType: 'email', subscribeForm: 'yes' });

    if ($(".subscribeForm").next("span.inputInvalid").length > 0) { return false; }
    else { return true; }
}


var am09_WMS = {
init: function() {
if (typeof document.body.style.maxHeight === "undefined") {
    $("body", "html").css({ height: "100%", width: "100%" });
    $("html").css("overflow-x", "auto");
}
$(window).resize(function() {
    am09_WMS.getWidth();
    $("#popUp, #pnlRTEWindow").css('left', (am09_WMS.width - 800) / 2 + 'px');    
    // special case for enlarged window

    if ($.browser.msie && $.browser.version < 7) {
        $("#popUp, #pnlRTEWindow").css('top', (am09_WMS.height - 560) / 2 + 'px');
    }
    else {
        $("#popUp, #pnlRTEWindow").css('top', (am09_WMS.height - 560) / 2 + 'px');
    }

    $("#blockUIPopup").css({ height: $(document).height(), width: $(document).width() });
});

// do not do this!!!
$('#popUp .close a.closeBtn, #blockUIPopup').click(function() {
        am09_WMS.closePopup();     
       return false;
}).attr('href', 'javascript:{};');

if ($.browser.msie && $.browser.version < 7) {
    $(window).scroll(function() {
        $("#blockUIPopup").css({ height: $(document).height(), width: $(document).width() });
    });
}

},
closePopup: function() {
    $("#blockUIPopup, #popUp").hide();         
},
    loadPopup: function() {
        am09_WMS.getWidth();
        $("#blockUIPopup").fadeIn();
        $("#popUp").fadeIn('slow').css('left', (am09_WMS.width - 800) / 2 + 'px');
        $("#popUp").show().css('left', (am09_WMS.width - 800) / 2 + 'px');
        if ($.browser.msie && $.browser.version < 7)
            $("#popUp").css('top', (am09_WMS.height - 560) / 2 + 'px');
        else
            $("#popUp").css('top', (am09_WMS.height - 560) / 2 + 'px');
    },
    getWidth: function() {
        var de = document.documentElement;
        am09_WMS.width = window.innerWidth || self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
        am09_WMS.height = window.innerHeight || self.innerHeight || (de && de.clientHeight) || document.body.clientHeight;
    }
};