$(document).ready(function(){
    $('a.external').attr('target', '_blank');

    // IE only related JavaScript
    if($.browser.msie){
        // PNG Fix (IE 5.5 and 6)
        if($.browser.version >= 5.5 && $.browser.version < 7){
            // Load jQuery ifixpng plugin
            $.getScript('/js/jquery/jquery.ifixpng2.js', function(){
                // PNG Fix
                $.ifixpng("/media/jquery/pixel.gif");
                $(".homepageTabs,img[src$='.png']").ifixpng();
            });
        }
    }
    
    $(".back-button").click(function () {
        history.back();
        return false;
    });
    
    
    if ($('#seealso').length > 0) {
        $('#seealso').change(function () {
            location = $('#seealso').val();
            if (location.len > 0) {
                document.location = location;
            }
        });
    }
    
    if ($('a.popup').length > 0) {
        $('a.popup').each(function () {
            $(this).click(function () {
                window.open(this.href,'popup','status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=1,width=780,height=640');
                return false;
            });
        });
    }
    
    if ($('.mailtofriend').length > 0) {
        $('.mailtofriend').click(function () {
            mail_str = "mailto:?subject=I thought you would be interested in this item";
            mail_str += "&body=";
            mail_str += "Hi,%0A%0A I saw the following page on http://www.wholebuild.co.uk and thought you would be interested: %0A%0A ";
            mail_str += document.title + " %0A "+ location.href;
            location.href = mail_str;
            return false;
        });
    }
    
    if ($('a.lightbox').length > 0) {
        $('a.lightbox').lightbox();
    }
    
    if ($('span.shorten').length > 0) {
        count = 0;
        $('span.shorten').each(function () {
            count++;
            text = $(this).text();
            if (text.length > 200) {
                a = $('<a href="#"> View All</a>').click(function () {
                    text = $(this).parent().find('span').text();
                    $(this).parent().parent().html(text);
                    return false;
                });
                t = $('<span id="fulltext-' + count + '">' + text + '</span>').hide();
                s = $('<span id="shortened-' + count + '">' + text.substr(0, 200) + '...</span>').append(a).append(t);
                $(this).html(s);
            }
        });
    }
    
    defaultPopup = {
        'frameWidth': 902,
        'zoomSpeedIn': 600,
        'hideOnContentClick': false,
        'centerOnScroll': true,
        'overlayOpacity': 0.9
    };
    
    // Add external link classes to external links - 
    $('a').filter(function(){
        //Compare the anchor tag's host name with location's host name
        return this.hostname && this.hostname !== location.hostname;
    }).attr('target', '_blank');
    
    
    if ($('a.external-popup').length > 0 && $('#menu').length > 0) {
        $('a.external-popup').fancybox(defaultPopup);
    }
    if ($('a.orangeBox').length > 0 && $('#menu').length > 0) {
        $('a.orangeBox').each(function() {
            var hrefValue = $(this).attr('href');
            var qsGlue = (hrefValue.indexOf('?') > -1 ? '&' : '?');
            $(this).attr('href', hrefValue + qsGlue + 'popup=true');
        });
        $('a.orangeBox').fancybox(defaultPopup);
    }
    
    // Hover effect for 'Related Info' boxes
    $('.hover-panel').each(function(){
        var $this = $(this);
        $this.hover(
            function(){
                $this.addClass('hover-panel-hover');
            },
            function(){
                $this.removeClass('hover-panel-hover');
            }
            );
    });

    // Live Chat button
    $('#livechat-widget').click(function() {
        $('a','#livechat-widget').click();
    });
         

});

// Used in all pages to submit a form and optionally set a hidden
// form varaible called 'navigate' to direct navgiation
function submitForm(formName, navigateValue) {
    if (navigateValue != null && navigateValue != "") {
        document.forms[formName].navigate.value = navigateValue;
    }
    document.forms[formName].submit();
}

