$(document).ready(function () {

    // make the searchbox default value clear on focus
    $('.searchBox').focus(function () { $(this).val(""); }).focusout(function () {
        if ($(this).val() == "") {
            $(this).val("Search Smoothwall");
        }
    });
    $('.searchSubmit').click(function () {
        if ($('.searchBox').val() == "Search Smoothwall") {
            $('.searchBox').val("");
        }
    });

    // if there is more than one banner rotate through.
    if ($('div#bannerContainer')) {
        if ($('div#bannerContainer .banner').length > 1) {
            $('div#bannerContainer .banner').each(function (index) {
                $(this).hide();
            });
            $('div#bannerContainer .banner:last').show();
            setInterval('rotate()', 8000); // 8 secs
        }
    }

    // make all links with a class of ext open in a new window (or tab)
    $('.ext').click(function () { window.open(this.href); return false; });

    // makes hide and shows.
    if ($(".hideShow").length) {
        hideShow();
    }

    // highlight area image rotation.
    // if the list contains more than one item an on click event will be added to rotate through them.
    if ($('.imageBlock li').size() > 1) {

        $('.imageBlock li').css('cursor', 'pointer');
        $('.imageBlock li').click(function () {
            var slide = $('.imageBlock li:first');
            slide.hide();
            $('.imageBlock ul').append(slide);
            slide.fadeIn('slow');
        });
    }

    /* show hide product features */
    $('span.showhide').click(function () {
        var id = $(this).attr('id');

        /* toggle display */
        $('.listFeatures li div.' + id).toggle();

        /* update label */
        if ($('.listFeatures li div.' + id).css('display') == 'none') {
            $(this).css('background-position', '60px top');
            $(this).html('Show');
        }
        else {
            $(this).css('background-position', '60px bottom');
            $(this).html('Hide');
        }

        Cufon.replace($(this));
    });

    /* news ticker */
    $('#newsTicker').list_ticker({
        speed: 5000,
        effect: 'slide',
        run_once: false
    })

    // Contact us form show/hide fields
    updateContactFormShowHide();

    $('span.contactByTelephone input, span.contactByEmail input, select.country').change(function () {
        updateContactFormShowHide();
    });

    /* product panel height */
    if ($('div.product-panels').length > 0) {
        equalHeight($('div.product-panels .panel'));
    }
});

function updateContactFormShowHide() {
    if ($('span.contactByTelephone input').is(':checked')) {
        $('div.contactByTelephone').show();
        $('div.contactByEmail').hide();
    }
    else if ($('span.contactByEmail input').is(':checked') == true) {
        $('div.contactByTelephone').hide();
        $('div.contactByEmail').show();
    }

    if ($('select.country').val() == 'uk') {
        $('div.postcode').show();
        $('div.state').hide();
    }
    else {
        $('div.postcode').hide();
        $('div.state').show();
    }
}

function equalHeight(group) {
    if (group) {
        var tallest = 0;
        group.each(function () {
            var thisHeight = $(this).height();
            if (thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        if (tallest > 0) {
            group.height(tallest);
        }
    }
}

// triggers hide and shows if the class is on page

function hideShow() {
    $(".hideShow div").addClass("hidden");
    $(".hideShow").addClass("active");
    $(".hideShow h3").each(function (i) {
        $(".hideShow h3").eq(i).toggle(function () {
            $(this).addClass("selected");
            $(this).parent().children("div").addClass("visible").removeClass("hidden");
        }, function () {
            $(this).removeClass("selected");
            $(this).parent().children("div").removeClass("visible").addClass("hidden");
        });
    });
}

function rotate() {
    var slide = $('div#bannerContainer .banner:first');
    slide.hide();
    $('div#bannerContainer').append(slide);
    slide.fadeIn('slow');
};

function getUrlVars(v) {
    var vars = [], hash;
    var hashes = v.slice(v.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;
}

function initGoogleMap(lat, lng, container)
    {
        var latlng = new google.maps.LatLng(lat, lng);

        var myOptions = {
            zoom: 15,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        
        var map = new google.maps.Map(document.getElementById(container), myOptions);
        
        var marker = new google.maps.Marker({
            position: latlng,
            map: map
        });
    }
