/* Variables for values in textfields */
var active_color = '#5C5C5C'; // Colour of user provided text
var inactive_color = '#5C5C5C'; // Colour of default text

$(document).ready(function() {
    /*NOTE: Be aware of the fact that for example the productfinderconfiguration.ascx has "unbind" calls for 
    click events. This could result in strange behavior because it could remove new added click events from the main.js*/

    executeReady();
    $('.contactCountry, .productFinder select').sSelect();
    $('.blueLinkMiddle').click(function() { $(this).toggleClass('blueLinkMiddleCollapse'); }); // sidebar contact us blue bar changes to the collapsed one (with arrow down)
    $('.blueLinkMiddle').click(function() { $(this).next().slideToggle(); }); // when clicking on the blue bar in the sidebar the contact div opens (or closes)
    $('.blueLinkLong').click(function() { $(this).toggleClass('blueLinkLongCollapse'); }); // productfinder blue bar changes to the collapsed one (with arrow down)
    $('.blueLinkLong').click(function() { $('.productFinder').slideToggle(); }); // when clicking on the blue bar of the productfinder the productfinder opens (or closes)
    $('.grayLinkLonger').click(function() { $(this).next('div').slideToggle(); $(this).toggleClass('grayLinkLongerCollapse'); });

});

function executeReady() {
    Menu('top_menu');
    Menu('left_menu');
    $('.change_language').click(function() { open_closeDD(); return false; });
    $('.top_links li a').click(function() { ChangeLangage(this); return false; });


    $('.resultsSubBar').click(function() { $(this).toggleClass('resultsSubBarCollapse'); });  // when you click on a grey bar in the productfinder it changes to a collapsed one (minus instead of plus sign)

    $('.relatedArticles .article a.file').click(function() { // this opens or closes the related articles in the sidebar
        $(this).next().slideToggle();
    });

    $('.resultsArticles .article a.file').not('.relatedBtn').click(function() { $(this).next().slideToggle(); $('.RequestSent').hide(); }); // this opens closes the articles in the productfinder


    // this triggers the correct presentation for the select dropdowns

    $("select:disabled").next().removeClass("newListSelected").addClass("newListDisabled").children().remove("ul"); // this makes the replaceed select elements disabled when they should

    $("div.contact input:not(:submit), div.contact textarea, .relatedArticles input, .resultsArticles input").css("color", inactive_color); // hides the text in inputfields on focus
    var default_values = new Array();
    $("div.contact input:not(:submit), div.contact textarea, .relatedArticles input, .resultsArticles input").focus(function() { // hides the text in inputfields on focus
        if (!default_values[this.id]) {
            default_values[this.id] = this.value;
        }
        if (this.value == default_values[this.id]) {
            this.value = '';
            this.style.color = active_color;
        }
        $(this).blur(function() {
            if (this.value == '') {
                this.style.color = inactive_color;
                this.value = default_values[this.id];
            }
        });
    });


    // tell-a-friend popup when clicking on the email icon
    $(".email").click(function() {
        $(this).addClass("emailInactive");
        $(".emailPopup").show(); $(".overlay").show();
        $(this).attr("src", "/Content/purac/images/emailInactive.gif");
    });

    // the overlay for the tell-a-friend popup when clicking on the email icon
    $(".overlay").not(".emailPopup").click(function() {
        $(".email").removeClass("emailInactive");
        $(".emailPopup").hide(); $(".overlay").hide();
        $(".email").attr("src", "/Content/purac/images/email.gif");
    });



    // the overlay has a height of the body's full height, when scrolling this should also scroll
    $(window).scroll(function() {
        $('.overlay').css('top', $(this).scrollTop() + "px");
    });

    $('.contactCountry').next().addClass("countrySelect");
}
function onLoadFunction() {
    doCufon();
    DropDown();
    StylesFix();
}

function doCufon() {
    Cufon.replace('h1', { fontFamily: 'Fedra Sans' });
    Cufon.replace('h2', { fontFamily: 'Fedra Sans' });
    Cufon.replace('#top_menu a', { fontFamily: 'Fedra Sans', hover: true });
}

function DropDown() {
    var h = 0;
    var w = 0;
    h = $('.top_links ul').attr('offsetHeight');
    w = $('.top_links ul').attr('offsetWidth');

    if (browserType() == 'Opera') $('.top_links').css('margin-left', -w + 630 + 'px');
    else if (browserType() == 'MSIE' && versionMSIE() == '6') $('.top_links').css('margin-left', -w + 330 + 'px');
    else $('.top_links').css('margin-left', -w + 470 + 'px');

    $('.top_links .dropdownShadow1').css('height', h + 'px');
    $('.top_links .dropdownShadow2').css('height', h + 'px');
    $('.top_links .dropdownShadow3').css('width', w + 4 + 'px');

    $('.selectLanguage ul').css('width', w + 4 + 'px');
    //$('.selectLanguage').css('width', w + 4 + 'px');

    $('.top_links li').css('display', 'none');
    $('.change_language').css('display', 'block');
}

function open_closeDD() {
    var dd_opened = true;

    if ($('.top_links ul li:last').css('display') == 'none' || $('.top_links ul li:last').css('visibility') == 'hidden')
        dd_opened = false;

    if (dd_opened) hideDD();
    else showDD();
}

function showDD() {
    $('.top_links li').css('display', 'block');
    $('.top_links .dropdownShadow1').css('visibility', 'visible');
    $('.top_links .dropdownShadow2').css('visibility', 'visible');
    $('.top_links .dropdownShadow3').css('visibility', 'visible');
    $('.top_links ul').css('border-right', '0px solid #e1e1e1');
    $('.top_links ul').css('border-left', '0px solid #fbfbfb');
    $('.top_links ul').css('background-color', '#fff');
    $('.top_links ul li').css('display', 'none');
    $('.top_links ul li').css('visibility', 'visible');
    $('.top_links li').slideDown('fast');
}

function hideDD() {
    $('.top_links .dropdownShadow1').css('visibility', 'hidden');
    $('.top_links .dropdownShadow2').css('visibility', 'hidden');
    $('.top_links .dropdownShadow3').css('visibility', 'hidden');
    $('.top_links ul').css('border-right', '0px solid #e1e1e1');
    $('.top_links ul').css('border-left', '0px solid #fbfbfb');
    $('.top_links ul').css('background-color', 'transparent');
    $('.top_links ul li').css('visibility', 'hidden');
    $('.top_links ul li:first').css('visibility', 'visible');
    $('.top_links li').css('display', 'none');
    $('.change_language').css('display', 'block');
}

function ChangeLangage(obj) {
    $(obj).addClass('selected');
    hideDD();
    var selected_language = $('.top_links a.selected').html();
    $('.top_links a.selected').removeClass('selected');
    $('.change_language').html(selected_language);
}

function browserType() {
    var agent = navigator.userAgent;
    var browser;
    if (agent.lastIndexOf('MSIE') != -1) browser = 'MSIE';
    else if (agent.lastIndexOf('Firefox') != -1) browser = 'Firefox';
    else if (agent.lastIndexOf('Safari') != -1) browser = 'Safari';
    else if (agent.lastIndexOf('Opera') != -1) browser = 'Opera';
    else browser = 'Unknown';
    return browser;
}

function versionMSIE() {
    var agent = navigator.userAgent;
    var version;
    if (agent.lastIndexOf('MSIE') != -1) {
        if (agent.lastIndexOf('5.0') != -1) version = 5;
        if (agent.lastIndexOf('5.5') != -1) version = 55;
        if (agent.lastIndexOf('6.0') != -1) version = 6;
        if (agent.lastIndexOf('7.0') != -1) version = 7;
        if (agent.lastIndexOf('8.0') != -1) version = 8;
    };
    return version;
}

function StylesFix() {
    if (browserType() == 'MSIE' && versionMSIE() != '8') {
        $('.menuShadowOver').css('margin-left', '-490px');
        $('.blueLinkHome span.shadow').css('margin-top', '4px');
        $('.columnEvent .blueLinkHome span.shadow').css('margin-top', '4px');
        //$('.columnEvent .blueLinkHome:hover span.shadow').css('margin-top', '4px');
        $('.blueLinkLong span.shadow').css('margin-top', '4px');
        $('.blueLinkMiddle span.shadow').css('margin-top', '4px');
    }

    if (browserType() == 'Safari') {
        $('.footer_inner span').css('margin-top', '-15px');
        $('.footer_inner span.shadow').css('margin-top', '3px');

        //$('.blueLinkHome span').css('margin-top', '-13px');
        //$('.columnEvent .blueLinkHome span').css('margin-top', '-13px');
        //$('.columnEvent .blueLinkHome:hover span').css('margin-top', '-13px');
        $('.blueLinkLong span').css('margin-top', '-13px');
        $('.blueLinkMiddle span').css('margin-top', '-13px');

        $('.blueLinkHome span.shadow').css('margin-top', '2px');
        $('.columnEvent .blueLinkHome span.shadow').css('margin-top', '2px');
        $('.columnEvent .blueLinkHome:hover span.shadow').css('margin-top', '2px');
        $('.blueLinkLong span.shadow').css('margin-top', '2px');
        $('.blueLinkMiddle span.shadow').css('margin-top', '2px');
    }
}


