var hasFlash = DetectFlashVer(7, 0, 0);

sub_path = "";
im1 = new Image();
im1.src = sub_path + "/common/im/loading_sym1.gif";


function show_wait_msg(x)
{
    image_name = "wait_msg" + x
    document.images[image_name].src = sub_path + "/common/im/loading_sym1.gif"
}

function goURL(url)
{
    window.location = url
}

submitted = 0;
function sub()
{
    if (submitted) return;

    act = arguments[0]; //action
    conf = arguments[1]; //confirmation - set to 1 if requiring confirmation. else don't set it at all.

    OK = 0;
    if (conf == 1)
    {
        OK = confirm('Are you sure?');
    }

    if (OK || !isset(conf))
    {
        submitted = 1;
        df = document.form1;
        df.act.value = act;
        df.action = location.href;
        df.submit();
    }
}


function runCountryCheck(country_var)
{
    id = document.getElementById(country_var); //get country selected
    county_div = document.getElementById('counties');

    if (county_arr[id.value])
    {
        county_div.innerHTML = county_arr[id.value];
        county_div.style.display = "block"
    }
    else
    {
        county_div.innerHTML = county_arr['unsel'];
        county_div.style.display = "none"
    }
}


/* NAVIGATION */
var main_menu_bg_color = "#877D6D";
var main_menu_bg_hover_color = "#BAAFA2";

function get_start_pos(elem)
{
    var start_pos = 0;
    var loops1 = 0;
    while (elem.prev().width() > 0)
    {
        elem = elem.prev();
        start_pos += elem.width();
        if (loops1++ > 50) return;
    }
    return start_pos;
}

function do_load()
{
}

function scroll_header()
{
    if (!location.hash) window.scrollTo(0, 150);
}

function isset(variable)
{
    return typeof(variable) != "undefined" && variable !== null;
}

var win1

function newWin(url, w, h)
{
    if (win1 && win1.open || win1 && !win1.closed)
    {
        win1.close()
    }
    win1 = window.open(url, "win1", "width=" + w + ",height=" + h + ",toolbar=no,location=no,menubar=no,status=no,scrollbars=no,resizable=yes")
}

function top_menu()
{
//    $('#top_nav .sub_menu').hide(); //hide drop downs on entry

    $('#top_nav ul li').hoverIntent(function ()
        {
            //show submenu
            $(this).find("div.sub_menu").stop(true, true).fadeIn('fast');
        },
        //else hide it
        function ()
        {
            $(this).find("div.sub_menu").stop(true, true).fadeOut('fast');
        });

    $('#top_nav .sub_menu .subsub .shorten ul li:first-child a').click(function (e)
    {
        //show submenu
        if (!$(this).hasClass('is_down'))
        {
            $(this).addClass('is_down');
            $(this).parent().parent().find("li").stop(true, true).slideDown('fast');
        }
        else
        {
            $(this).removeClass('is_down');
            $(this).parent().parent().find("li").stop(true, true).slideUp('slow');
            $(this).parent().parent().find("li:first-child").stop(true, true).show();
        }
        e.preventDefault();
    });

    $('#top_nav .sub_menu .subsub .shorten ul li:first-child a').dblclick(function (e)
    {
        window.location = $(this).attr('href');
    });

    $('#top_nav .sub_menu .subsub .shorten ul li a').click(function ()
        {
            $(this).parent().parent().find("li").stop(true, true).slideDown('slow');
        }
        ,
        //else hide it
        function ()
        {
            $(this).find("li").stop(true, true).slideUp(2000);
            $(this).find("li:first-child").stop(true, true).show('slow');
        }
    );
}

$(document).ready(function ()
{
    top_menu();
    basket_preview();
    expand_div();
//	menu_expand_div();
//	show_search();
    if (ENBL_admin_menu) toggle_admin_menu();
    if (ENBL_fancy_box) fb();
});

function show_basket1()
{
    $('#basket_preview').fadeIn('100');
    t1 = setTimeout(function ()
    {
        $('#basket_preview').fadeOut('100');
    }, 5000)
}

function basket_preview()
{
    $('#basket_link').mouseover(
        function ()
        {
            $('#basket_preview').fadeIn('100');
        }
    );

    $('div#basket_preview').mouseleave(
        function ()
        {
            $('#basket_preview').fadeOut('100');
        }
    );

    $('div#basket_preview').mouseenter(
        function ()
        {
            clearTimeout(t1);
        }
    );
}

//image expander
function fb()
{
    $("a.fb").fancybox({
        'transitionIn':'fade',
        'speedIn':700,
        'speedOut':300,
        'hideOnContentClick':true,
        'overlayColor':'#FFFFFF',
        'overlayOpacity':0.8,
        'padding':1
    });
}

//Search stuff --------------------------------

im_s = new Image();
im_s.src = sub_path + "/im_cms/but_search_ani.gif"
im_s1 = new Image();
im_s1.src = sub_path + "/im_cms/but_search_ani1.gif"

var retain_search_box = 0;

function submitSearch(form_name)
{
    retain_search_box = 1;
    if (document.getElementById(form_name).searchfield.value.length >= 3)
    {
        if (form_name == "searchform")
        {
            document.getElementById('searchButton').src = "/im_cms/but_search_ani.gif"; //dark button
        }
        else
        {
            document.getElementById('searchButton1').src = "/im_cms/but_search_ani1.gif"; //light button
        }
        return true;
    }
    else
    {
        document.getElementById(form_name).searchfield.focus();
        return false;
    }
}


function show_search()
{
    $('#searchDivInner').hide();

    $('#searchfield').focus(
        function ()
        {
            retain_search_box = 1;
        }
    );

    $('a.showSearch').mouseover(
        function ()
        {
            $('#searchDivInner').fadeIn();
        }
    );
    $('div#searchDivInner').mouseleave(
        function ()
        {
            if (!retain_search_box)
            {
                $('#searchDivInner').fadeOut();
            }
        }
    );
    $('a#but_search_tips').click(
        function ()
        {
            $('#search_tips').toggle('fast');
            return false;
        }
    );
}


//end search stuff ---------------------------

//preview menu stuff ------------------------
function toggle_admin_menu()
{
    $('a#but_pm_show_hide').click(
        function ()
        {
            if ($.cookie("pm_toggle") == "hide") //switch to showing div
            {
                $.cookie("pm_toggle", "show", { expires:30 });
                $('#admin_menu').addClass("admin_menu_show");
                $('#admin_menu').removeClass("admin_menu_hide");
            }
            else
            {
                $.cookie("pm_toggle", "hide", { expires:30 });
                $('#admin_menu').addClass("admin_menu_hide");
                $('#admin_menu').removeClass("admin_menu_show");
            }
            this.blur();
        }
    );

    $('a#but_amedit_show_hide').click(
        function ()
        {
            if ($.cookie("amedit_toggle") == "hide") //switch to showing div
            {
                $.cookie("amedit_toggle", "show", { expires:30 });
                $('a.aelink0').css('display', 'block');
                $('a.aelink1').css('display', 'block');
                $('a#but_amedit_show_hide').text("hide edit buttons");
            }
            else
            {
                $.cookie("amedit_toggle", "hide", { expires:30 });
                $('a.aelink0').css('display', 'none');
                $('a.aelink1').css('display', 'none');
                $('a#but_amedit_show_hide').text("show edit buttons");
            }
            this.blur();
        }
    );

    $('a#but_amborder_show_hide').click(
        function ()
        {
            if ($.cookie("amborder_toggle") == "hide") //switch to showing div
            {
                $.cookie("amborder_toggle", "show", { expires:30 });
                $('td.td_st0').css('border', '1px solid #CCC');
                $('td.td_st1').css('border', '1px solid #CCC');
                $('a#but_amborder_show_hide').text("hide story borders");
            }
            else
            {
                $.cookie("amborder_toggle", "hide", { expires:30 });
                $('td.td_st0').css('border', 'none');
                $('td.td_st1').css('border', 'none');
                $('a#but_amborder_show_hide').text("show story borders");
            }
            this.blur();
        }
    );
}

//end pm stuff --------------------------------

function expand_div()
{
    $('.slideContent').hide(); //hide all these slides initially
    //expand div if title clicked
    $('a.slideTitle').click(
        function ()
        {
            $(this).blur();
            //next div may be on "open display" in which case its class name is slideContentOpen, not slideContent
            var slider_classname = $(this).parent().next('div').attr('class');

            //toggle next div
            if (!$(this).parent().next('div.' + slider_classname).is(':visible'))
            {
                openDiv($(this).parent().next('div.' + slider_classname));
            }
            else
            {
                closeDiv($(this).parent().next('div.' + slider_classname));
            }
            return false;
        }
    );
    //on close-click close this div
    $('a.slideClose').click(
        function ()
        {
            var slider_classname = $(this).parent().parent().parent('div').attr('class');
            closeDiv($(this).parent().parent().parent('div.' + slider_classname));
            return false;
        }
    );
    function openDiv(this_div)
    {
        $(this_div).parent().prev('a.slideTitle').addClass("slideTitleBold");
        $(this_div).slideDown(500);
        $.cookie("slides", "open", { path:'/' });
        return false;
    }

    function closeDiv(this_div)
    {
        //switch div class to slideContent
        if ($(this_div).hasClass('slideContentOpen'))
        {
            $(this_div).addClass("slideContent").removeClass("slideContentOpen");
        }
        $(this_div).parent().prev('a.slideTitle').removeClass("slideTitleBold");
        $(this_div).slideUp(500);
        $.cookie("slides", "closed", { path:'/' });
        return false;
    }
}

function hide_slide_divs()
{
    $('.slideContent').hide(); //hide all slideable divs
}

// -------------------------- Product Menu
function menu_expand_div()
{
    $('.menu_slideContent0').hide(); //hide all these slides initially
    $('.menu_slideContent1').hide(); //hide all these slides initially
    $('.menu_open').show(); //show these slides initially
    //expand div if title clicked
    $('a.menu_slideTitle0').click(
        function ()
        {
            $(this).blur();
            if (!$(this).next().is(':visible')) var openThis = 1;
            //close all sliders
            $('.menu_slideContent0:visible').slideUp('fast');
            $('.menu_slideContent0Open:visible').slideUp('fast');
            //open this one
            if (openThis)
            {
                $(this).next().slideDown('fast');
            }
            return false;
        }
    );
    $('a.menu_slideTitle1').click(
        function ()
        {
            $(this).blur();
            if (!$(this).next().is(':visible')) var openThis = 1;
            //close all sliders
            $('.menu_slideContent1:visible').slideUp('fast');
            $('.menu_slideContent1Open:visible').slideUp('fast');
            //open this one
            if (openThis)
            {
                $(this).next().slideDown('fast');
            }
            return false;
        }
    );
}

function menu_hide_slide_divs()
{
    $('.menu_slideContent').hide(); //hide all slideable divs
}

// -----------------------------------------

function hideIt(div_id)
{
    document.getElementById(div_id).style.display = 'none';
}

function showIt(div_id)
{
    document.getElementById(div_id).style.display = 'block'
}

//world model
var wm_rollover_text = new Array();
function sh(id)
{
    document.getElementById('l1').innerHTML = "<p style=\"margin-top:10px\">" + wm_rollover_text[id] + "</p>";
    showIt('l1');
}

function hi(id)
{
    hideIt('l1');
}
