﻿$(document).ready(function () {

    //flag show/hide
    $('#selectCountryCue').click(function () {
        if ($('#flags').css('display') == 'none') {
            $('#flags').stop().fadeIn(250);
        }
        else {
            $('#flags').stop().fadeOut(250);
        }
    }
    );

    //    nav 1 menus
    $("#nav1 .menu").hide();
    $("#nav1 li").hover(function () {
        $('.theFrame').remove();
        $('select').blur();
        $(this).addClass("shown");
        $("#nav1 li.shown").children(".link").addClass('selected');
        delay = setTimeout(function () {
            $("#nav1 li.shown").children(".menu").stop().fadeTo(250, 1);
            if ($.browser.msie && $.browser.version == "6.0") {
                var getIFrame = $('<iframe class="theFrame" frameborder="0" src="" style="display:block; filter:Alpha(Opacity=0);-moz-opacity:0.0;-khtml-opacity: 0.0;opacity: 0.0;z-index:-1;"></iframe>');
                $('#nav1 li.shown').append(getIFrame);

                getIFrame.css('position', 'absolute');
                getIFrame.css('top', $("#nav1 li.shown .menu").css('top'));
                getIFrame.css('height', $("#nav1 li.shown .menu").outerHeight(true));
                getIFrame.css('left', $("#nav1 li.shown .menu").css('left'));
                getIFrame.css('width', $("#nav1 li.shown .menu").css('width'));
                getIFrame.css('margin-left', $('#nav1 li.shown .menu').css('margin-left'));
            }
        }, 250)
    },
    function () {
        clearTimeout(delay);
        $('.theFrame').remove();
        $("#nav1 li.shown").children(".link").removeClass('selected');
        $("#nav1 li.shown").removeClass("shown");
        $("#nav1 .menu").stop().hide();
    });
    $("wrapper").mouseover(function () {
        $("#nav1 .menu").stop().hide();
    });
});

function showPopup(id) {
    $('.checkoutPopupBackground:first').show();
    $(id).fadeIn('fast');
}

function hidePopup(id) {
    $('.checkoutPopupBackground:first').hide();
    $(id).fadeOut('fast');
}

currentChannelID = -1;

var navigation1ItemCount = 0;
var showNavigation1Menu = function(id) {
    navigation1ItemCount = parseInt(document.getElementById("hiddenNavigation1ItemCount").value);
    hideNavigation1Menus()
    objID = "navigation1Menu" + id.substr((id.lastIndexOf('__') + 2), id.length - 1);
    document.getElementById(objID).style.display = "block";
    $('select').blur();
}

var hideNavigation1Menus = function() {
    navigation1ItemCount = parseInt(document.getElementById("hiddenNavigation1ItemCount").value);
    for (i = 1; i <= navigation1ItemCount; i++) {
        newID = "navigation1Menu" + i;
        document.getElementById(newID).style.display = "none";
    }
}

function switchCurrencyWarn(obj) {
    obj = document.getElementById(obj);
    if (obj) {
        if (!confirm('This basket will be cleared if you switch currencies.\nIs this OK?')) {

            for (i = 0; i < obj.options.length; i++) {
                if (obj.options[i].value == currentChannelID) {
                    obj.selectedIndex = i;
                }
            }
            return false;
        }
    }
    __doPostBack(obj.id, obj.value);
}

function switchCurrencyWarnImages(value, basketValue) {
    if (document.getElementById(hfChannelOverrideClientID)) {
        obj = document.getElementById(hfChannelOverrideClientID);
    }
    if (obj) {
        if (obj.value != value) {

            if (basketValue != null) {
                if (!confirm('This basket will be cleared if you switch currencies.\nIs this OK?')) {
                    return false;
                }
            }
            obj.value = value;
            __doPostBack(obj.id, obj.value);
        }
    }
}



/// <summary>
/// Assign Appropriate Target Button Or ImageButton Control etc., On 'Return Key' Press
/// </summary>
/// <param name="e">The key press</param>
/// <param name="target">The control to do a postback with</param>
function AnyInput_KeyDown(e, target) {
    if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
        //Uncomment and assign breakpoint to debug:
        //alert('Enter Key Press, DoPostback On ' + target);

        //Assign Control
        __doPostBack(target, '');

        //Return False To Prevent Postback Occurring Twice On Enter Keypress
        return false;
    }
    return true;
}

/**
* Whether we can detect that the browser has flash
* @type Boolean
*/
var _isFlashInstalled;


/**
* Full version information of flash installed, in form 7.0.61
* @type String
*/
var _flashVersion;

(function() {
    /**
    * Derived from Apple's suggested sniffer.
    * @param {String} desc e.g. Shockwave Flash 7.0 r61
    * @return {String} 7.0.61
    */
    function getFlashVersion(desc) {
        var matches = desc.match(/[\d]+/g);
        matches.length = 3;  // To standardize IE vs FF
        return matches.join('.');
    }

    var hasFlash = false;
    var flashVersion = '';

    if (navigator.plugins && navigator.plugins.length) {
        var plugin = navigator.plugins['Shockwave Flash'];
        if (plugin) {
            hasFlash = true;
            if (plugin.description) {
                flashVersion = getFlashVersion(plugin.description);
            }
        }

        if (navigator.plugins['Shockwave Flash 2.0']) {
            hasFlash = true;
            flashVersion = '2.0.0.11';
        }

    } else if (navigator.mimeTypes && navigator.mimeTypes.length) {
        var mimeType = navigator.mimeTypes['application/x-shockwave-flash'];
        hasFlash = mimeType && mimeType.enabledPlugin;
        if (hasFlash) {
            flashVersion = getFlashVersion(mimeType.enabledPlugin.description);
        }

    } else {
        try {
            // Try 7 first, since we know we can use GetVariable with it
            var ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.7');
            hasFlash = true;
            flashVersion = getFlashVersion(ax.GetVariable('$version'));
        } catch (e) {
            // Try 6 next, some versions are known to crash with GetVariable calls
            try {
                var ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
                hasFlash = true;
                flashVersion = '6.0.21';  // First public version of Flash 6
            } catch (e) {
                try {
                    var ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
                    hasFlash = true;
                    flashVersion = getFlashVersion(ax.GetVariable('$version'));
                } catch (e) {
                    try {
                        var ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.8');
                        hasFlash = true;
                        flashVersion = getFlashVersion(ax.GetVariable('$version'));
                    } catch (e) {
                        try {
                            var ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.9');
                            hasFlash = true;
                            flashVersion = getFlashVersion(ax.GetVariable('$version'));
                        } catch (e) {
                            try {
                                var ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.10');
                                hasFlash = true;
                                flashVersion = getFlashVersion(ax.GetVariable('$version'));
                            } catch (e) {
                                try {
                                    var ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.11');
                                    hasFlash = true;
                                    flashVersion = getFlashVersion(ax.GetVariable('$version'));
                                } catch (e) {
                                    try {
                                        var ax = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.12');
                                        hasFlash = true;
                                        flashVersion = getFlashVersion(ax.GetVariable('$version'));
                                    } catch (e) {
                                        // No flash
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    /**
    * Whether we can detect that the browser has flash
    * @type Boolean
    */
    _isFlashInstalled = hasFlash;


    /**
    * Full version information of flash installed, in form 7.0.61
    * @type String
    */
    _flashVersion = flashVersion;

})();

//this needs revising for .net JR 121109
function isSuperZoomEnabled() {
    try {
        if (_isFlashInstalled) {
            version = _flashVersion.split('.').shift();
            if (version >= 9) {
                return true
            }
            return false;
        }
    }
    catch (e) {
        return false;
    }
}

function createAddThisBookmarkObject(obj) {

    var el = document.getElementById(obj);
    if (el) {
        //var version = getFlashVersion().split(',').shift();
        version = _flashVersion.split('.').shift();
        //alert("getFlashVersion()=" + version.toString());
        if (version >= 9 || !_isFlashInstalled) {
            var output = "<a href='http://www.addthis.com/bookmark.php'"
            output += " onmouseover=";
            output += "\"return addthis_open(this, \'\'";
            output += ", \'[URL]\', \'[TITLE]\');\"";
            output += " onmouseout=";
            output += "\'addthis_close();\'";
            output += " onclick=" + "\'return addthis_sendto();\'";
            output += "><img src=";
            output += "\'http://s9.addthis.com/button1-bm.gif\'";
            output += " width=";
            output += "\'125\'";
            output += " height=";
            output += "\'16\'";
            output += " border=";
            output += "\'0\'";
            output += " alt=";
            output += "\'\'";
            output += " \/></a>";
            //alert(output);

            document.getElementById("addThisBookmark").innerHTML = output;
            document.write('<script type=\"text\/javascript\" src=\"http:\/\/s7.addthis.com/js/200/addthis_widget.js\"><\/script>');
        }
    }
}

function toggleObjectDisplay(id, disp) {

    var obj = document.getElementById(id);
    if (obj) {
        obj.style.display = disp;
    }
}

function goBack() {
    history.back();
}

function clearText(tb) {
    var obj = document.getElementById(tb);
    if (obj) {
        if (typeof (obj.value == String)) {
            obj.value = "";
        }
    }
}

// returns true if the current browser is IE
function isBrowserIE() {
    if (typeof (window.innerWidth) != 'number') { //ie only
        return true;
    } else {
        return false;
    }
}

//only submit search if textbos is not blank. if the default text still exists then show all products (hayley request)
function submitSearch(textboxID, defaultText) {
    var obj = document.getElementById(textboxID);
    if (obj) {
        if (obj.value == "") {
            return false;
        }
        else if (obj.value == defaultText) {
            window.location = "/ProductList.aspx?Search="
            return false;
        }
    }
    return true;
}

//this needs revising for .net JR 121109

function superZoomEnabled() {
    try {
        if (_isFlashInstalled) {
            version = _flashVersion.split('.').shift();
            if (version >= 9) {
                return true
            }
            return false;
        }
    }
    catch (e) {
        return false;
    }
}

// Removes leading whitespaces
function LTrim(value) {
    var re = /\s*((\S+\s*)*)/;
    return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim(value) {
    var re = /((\s*\S+)*)\s*/;
    return value.replace(re, "$1");
}

// Removes leading and ending whitespaces
function trim(value) {
    return LTrim(RTrim(value));
}

// Removes leading and ending whitespaces
function Trim(value) {
    return LTrim(RTrim(value));
}

function leaveMembers() {
    var answer = confirm("This will log you out of the Members Area.\n\nContinue?");
    if (answer){
        window.location = "/default.aspx";
    }
}

//disable autocomplete for webkit (safari/chrome) browsers.
$(document).ready(function () {
    if ($.browser.webkit) {
        //alert("webkit");
        $("#aspnetForm").attr("autocomplete", "off");
    }
}); 
