﻿//url parser
$.extend({   
getUrlVars: function(){     
    var vars = [], hash;     
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');     

    for(var i = 0; i < hashes.length; i++)     
    {       
    hash = hashes[i].split('=');       
    vars.push(hash[0]);       
    vars[hash[0]] = hash[1];     
    }     return vars;   },   

    getUrlVar: function(name){     
        return $.getUrlVars()[name];   
        } 
});


$(document).ready(function () {

    //adds an alert to any link that doesn't have a "href" defined except for the
    //links that are the current ones in the menu
    $("a").not("a.current").each(function () {
        if ($(this).attr("href") == undefined) {
            $(this).attr("href", "javascript:void(0);");
            $(this).bind("click", function () {
                alert("This function is currently unavailable.");
            });
        }
    });

    //removes the "href" from any links that are current in the menus
    //so that they don't reload the page
    $("a.current").each(function () {
        $(this).removeAttr("href");
    });

    //set career panel height
    setCareersPanelsHeight();

    //set location of vacancies pane
    setCareersPanelLocation();

    //temp fix till larger images ready
    disableProductImageLinks();

    //Web tools comparison window
    $("a.fancybox").fancybox({
        transitionIn: 'elastic',
        transitionOut: 'elastic',
        overlayOpacity: 0.85,
        overlayColor: '#000',
        centerOnScroll: true,
        hideOnOverlayClick: false

    });

    //checks for the "portfolio" class and adds slideshow functionality
    $('ul#portfolio').innerfade({
        speed: 2000,
        timeout: 15000,
        type: 'sequence',
        containerheight: '300px'
    });

    //AFFILIATES    
    //show the text blocks for the main section only when dom ready
    $(".mainBlock").removeClass("hidden");

    //news feed
    //checks for the "portfolio" class and adds slideshow functionality
    $('ul#news').innerfade({
        speed: 2000,
        timeout: 8000,
        type: 'sequence',
        containerheight: '15px'
    });

    $('.news').show();

    // accordion
    $(".accordion").accordion();

    //show accordion
    $(".accordion").removeClass("jsHide");

    if (document.getElementById("thanks") != null) {
        TimedRedirectToUrl(5000, document.referrer.toString());
    }

    if (document.getElementById("downloadThanks") != null) {
        TimedRedirectToUrl(30000, document.referrer.toString());
    }
});

//Function called after ajax posts **CC**
var AjaxCallbackHandler = function () {
    //set career panel height
    setCareersPanelsHeight();
    
    setCareersPanelLocation();

    //temp fix till larger images ready
    disableProductImageLinks();

    $(".accordion").accordion();

    //Web tools comparison window
    $("a.fancybox").fancybox({
        transitionIn: 'elastic',
        transitionOut: 'elastic',
        overlayOpacity: 0.85,
        overlayColor: '#000',
        centerOnScroll: true,
        hideOnOverlayClick: false
            
    });

    if (typeof applyIECorners != "undefined") {
        applyIECorners();
    };
};

var TimedRedirectToUrl = function (delay, url) {
    if (url == '') { url = '/'; };
    setTimeout(function () { RedirectToUrl(url); }, delay);
};

var RedirectToUrl = function(url)
{
    window.location = url;
};

var disableProductImageLinks = function () {
    $(".multiItem .item .itemContent div a.fancybox img").parent().attr("href", "javascript:void(0)");
    $(".multiItem .item .itemContent div a.fancybox img").parent().css("cursor", "default");
    $(".multiItem .item .itemContent div a.fancybox img").parent().removeClass("fancybox");
};


var setCareersPanelLocation = function() {
    var bottom = 0;
    var parentHeight = 0;

    //parent height - myheight - padd

    if ($("#vacancyList").length > 0) {
        parentHeight = $("#vacancyList").parent().parent().height();

        bottom = (parentHeight - $("#vacancyList").height());
        bottom = bottom - 22;
        $("#vacancyList").css('top', bottom + "px");
    }
};

var setCareersPanelsHeight = function () {
    var largestPanelHeight = 0;

    largestPanelHeight = $("#careersLeft").height();

    if ($("#careersRight").height() > largestPanelHeight) 
    { 
        largestPanelHeight = $("#careersRight").height()
    }

    $("#careersLeft, #careersRight").css('min-height', largestPanelHeight + "px");
};

var disableSendButton = function () {
    $('#enquirySend').hide();
    $('#fakeButton').show();
    return true;
}
