﻿$(document).ready(function() {


    /** Cufon **/
    Cufon.replace('h2, .cufon1, #call-head', { fontFamily: 'din-medi' });
    Cufon.replace('h1, h3', { fontFamily: 'infintmi' });
    Cufon.replace('h3.panel', { fontFamily: 'din-medi' });

    /** Sitemap Slider **/
    $('#btn-sitemap-toggle').click(function() {

        if ($('#sitemap').is(':hidden')) {
            $('#sitemap').slideDown('slow', function() { $('#btn-sitemap-toggle').attr('src', '/gfx/btn-sitemap-close.png'); });
        }
        else {
            $('#sitemap').slideUp('slow', function() { $('#btn-sitemap-toggle').attr('src', '/gfx/btn-sitemap-open.png'); });
        }

    });


    /** Rounded Corders for images */
    $("img.rounded").load(function() {

        var img = $(this);

        // build wrapper
        var wrapper = $('<div class="rounded_wrapper"></div>');
        wrapper.width(img.width());
        wrapper.height(img.height());

        // move CSS properties from img to wrapper
        wrapper.css('float', img.css('float'));
        img.css('float', 'none')

        wrapper.css('margin-right', img.css('margin-right'));
        img.css('margin-right', '0')

        wrapper.css('margin-left', img.css('margin-left'));
        img.css('margin-left', '0')

        wrapper.css('margin-bottom', img.css('margin-bottom'));
        img.css('margin-bottom', '0')

        wrapper.css('margin-top', img.css('margin-top'));
        img.css('margin-top', '0')

        wrapper.css('display', 'block');
        img.css('display', 'block')

        // wrap image
        img.wrap(wrapper);

        // add rounded corners
        img.after('<div class="tl"></div>');
        img.after('<div class="tr"></div>');
        img.after('<div class="bl"></div>');
        img.after('<div class="br"></div>');

    });

    /** Image Caption **/
    /*$('.showcaption').each(function() {
    var altText = $(this).attr("alt");
    var imgWidth = $(this).width();
    var imgPosition = $(this).position();
    var captionText = "<div class=\"caption\" style=\"width: " + imgWidth + "px; top: " + imgPosition.top + "; left: " + imgPosition.left + ";\">" + altText + "</div>";
    $(this).after(captionText);
    });*/

    /** Search Box **/
    $("#searchBtn").click(function() {

        $("#search-pop").toggle();
        if ($(this).find("img").attr("src") == "/gfx/btn-menu-search.png") {
            $(this).find("img").attr("src", "/gfx/btn-menu-search-on.png");
            $("#search-pop input").focus();
        } else {
            $(this).find("img").attr("src", "/gfx/btn-menu-search.png");
        }

        return false;
    });

    $("#search-pop input").bind("keydown", function(e) {
        if (e.keyCode == 13) {
            location.replace("/search/?configid=1,2,3&criteria=" + $("#search-pop input").val());
            return false; //prevent default behaviour
        }


    });

    $("#search-pop input").focus(function() { $(this).val(''); });


    /** Column heights **/
    var minHeight = 450;

   
    //EqualiseColumnHeights();

    /** Clear textbox on focus **/
    var clearMePrevious = '';

    // clear input on focus
    $('.clearMeFocus').focus(function() {
        if ($(this).val() == $(this).attr('title')) {
            clearMePrevious = $(this).val();
            $(this).val('');
        }
    });

    // if field is empty afterward, add text again
    $('.clearMeFocus').blur(function() {
        if ($(this).val() == '') {
            $(this).val(clearMePrevious);
        }
    });

    // Facebox
    $('a[rel*=facebox]').facebox();

    // Smartnav Login
    $("#toolbar-smartnav-login").click(function() {
        $("#smartnav-login").toggle();
        $(this).toggleClass("on");
        $("#txtUserName").focus();
        return false;
    });

    $("#btn-forgotten-password").click(function() {
        $("#smartnav-login").hide();
        $("#toolbar-smartnav-login").removeClass("on");
    });

    $("#btn-process-login").click(function() {
        $("#smartnav-login").hide();
        $("#toolbar-smartnav-login").removeClass("on");
    });

});


function EqualiseColumnHeights() {

    var leftcolHeight = $('.leftcol').height();
    var rightcolHeight = $('.rightcol').height();

    if (leftcolHeight > rightcolHeight) {
        $('.rightcol').height(leftcolHeight);
    }

    if (rightcolHeight > leftcolHeight) {
        $('.leftcol').height(rightcolHeight+48);
    }
}

