﻿$(document).ready(function () {
    $("#sliderVideo").accessNews({
        headline: "",
        speed: "slow",
        slideBy: 1
    });

    $("#sliderProducts").accessNews({
        headline: "",
        speed: "slow",
        slideBy: 3
    });

    $("a.region").click(function () {
        $("div#region_menu").toggle();
        $("a.region").toggleClass("menu-open1");
    });

    $("div#region_menu").mouseup(function () {
        return false
    });

    $(document).mouseup(function (e) {
        if ($(e.target).parent("a.region").length == 0) {
            $("a.region").removeClass("menu-open1");
            $("div#region_menu").hide();
        }
    });

    $("a.iphone").click(function () {
        $("div#left").toggle();
        $("a.iphone").toggleClass("menu-open2");
    });

    $("div#left").mouseup(function () {
        return false
    });

    //Insert the search button
    if (location.pathname != '/search.aspx') {
        var searchHtml = '<span class="labelSearch">Search&nbsp;&nbsp;</span> ';
        searchHtml += '<input id="zoom_searchbox" class="inputSearch" type="text"> ';
        searchHtml += '<input class="btnSearch zoom_button" src="/images/btn-search-go.jpg" alt="Go" style="border-width:0px;" type="image"> ';

        $('.search div.searchBar').html(searchHtml);
    }

    //Search functionality
    $('input.zoom_button').live('click', function () {
        var searchParams = [];
        searchParams['zoom_query'] = $('#zoom_searchbox').val();
        searchParams['zoom_per_page'] = $('select[name="zoom_per_page"]').val();
        searchParams['zoom_and'] = $('input[name="zoom_and"]:checked').val();

        var tmpArray = []
        for (var key in searchParams) {
            if (searchParams[key] != undefined) {
                tmpArray.push(key + '=' + encodeURIComponent(searchParams[key]));
            }
        }

        var queryStr = tmpArray.join('&');
        //var currentUrl = window.location.href.split('?')[0];
        var searchUrl = '/search.aspx';
        var newUrl = searchUrl + '?' + queryStr;

        window.location = newUrl;

        return false;
    });

    //This is a fix for IE & Opera
    $('#zoom_searchbox').live('keydown', function (e) {
        if (e.keyCode == 13) {
            $('input.zoom_button').click();
            return false;
        }
    });

    //Fix for IE8... but wouldn't hurt to do for all browsers
    if ($('#searchpage-wrapper').length) {
        //Put the form stuff into a var
        var formContents = $('#searchpage-wrapper').html();

        //Remove all the html
        $('#searchpage-wrapper').html('');
        //all of the following is because IE doesn't play nice
        $('.searchheading').remove();
        $('.summary').remove();
        $('.result_pagescount').remove();
        $('.results').remove();
        $('.result_pages').remove();
        $('.suggestion').remove();

        //Remove the form tags
        formContents = formContents.replace(/<form[\w\s\=\".]+>/i, '');
        formContents = formContents.replace(/<\/form>/i, '');

        //And finally replace the html with the html without form tags
        $('#searchpage-wrapper').html(formContents);
    }
});
