/* variables for getting current mouse x,y coords */
var px = 0;
var py = 0;
var userid = 0;
var username = "";
var isadmin = false; //EIP-ADDED
var templateEdit = false; //EIP-ADDED

//rotator functions
var cur = 2;
var total;
var ctime;


(function ($) {
    var cache = [];
    // Arguments are image paths relative to the current page.
    $.preLoadImages = function () {
        var args_len = arguments.length;
        for (var i = args_len; i--; ) {
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }
})(jQuery)
window.onresize=resize;

function resize(){
    var i, a;
    for( i = 0; ( a = document.getElementsByTagName("link")[i] ); i++ )
    {
        if( a.getAttribute("rel").indexOf("style") != -1 )
        {
            a.disabled = true;
            a.disabled = false;
        }
    } 
}
/*  THIS FUNCTION EXECUTES AFTER THE DOM IS READY, BUT BEFORE THE PAGE RENDERS...  */
$(document).ready(function () {

    //rotator functions
    $("#ctl00_RotatorWrap").load('/scripts/widgets/rotator/gethtml.aspx', function () {
        total = $(".panel").length;
        ctime = setTimeout(rotateDiv, 6000);
        highlightControlDiv(1);
    });

    var name = window.location.search.substring(1);
    if (name.match("Content=Seal_Oil")) {
        $(".innerContent").css({ 'min-height': '450px' });
    }

    // Track mousemouse co-ords for login box popup
    $(document).mousemove(function (e) {
        px = e.pageX;
        py = e.pageY;
    });

    setHover(".hover");

    //rewrite urls via javascript to ajax
    //    $(".inner a, ul.breadcrumbs li a, #logo a, a.ajax").each(function () {
    //        var href = $(this).attr("href");
    //        $(this).attr("href", "javascript:doNav('" + href + "');");
    //    });
    $('#searchkeybtn').click(function (e) {
        window.location = '?Content=Search+' + $('#searchkey').val();
    });

    $('#searchkey').keydown(function (e) {
        //alert(e.keyCode);
        if (e.keyCode == 13) {
            window.location = '?Content=Search+' + $('#searchkey').val();
            return false;
        }
    });

    $("#searchkeybtn").bind('click', function (e) { window.location = '?Content=Search+' + $('#searchkey').val(); });

    $("#searchkey").bind('focus', function () {
        if ($('#searchkey').val() == 'Search...') {
            $('#searchkey').val("");
            $('#searchkey').css({ 'color': '#000000' });
        }
    });
    $("#searchkey").bind('blur', function () {
        if ($('#searchkey').val() == '') {
            $('#searchkey').css({ 'color': '#666666' });
            $('#searchkey').val("Search...");
        }
    });

    /* Setup the login box popup click function*/
    $("#loginlink").click(function (e) {
        $("#voice-login").remove();
        $("body").append("<div id='voice-login'><img src='/img/triware.png'><div>Website Login<div id='loginmsg'>&nbsp;</div><div class='floatleft'>Username: </div><div class='shortboxdiv floatright'><input style='width:150px;' type='text' id='username' /></div><div class='floatleft'>Password:</div><div class='floatright'><input style='width:150px;' type='password' id='password' /></div><div class='floatright hover'><input class='button' type='button' value='Submit' onclick='doLogin();' />&nbsp;&nbsp;<input class='button' type='button' value='Cancel' onclick='hideLogin();'  /></div></div>");
        $('#voice-login').css({ left: e.pageX - 255, top: e.pageY - 210 });
        $("#username").val('');
        $("#password").val('');
        $("#voice-login").fadeIn(1500);
        $("#username").focus();
        $("#password").keyup(function (event) {
            if (event.keyCode == 13) { doLogin(); }
        });
        $("#username").keyup(function (event) {
            if (event.keyCode == 13) { doLogin(); }
        });
    });

    /* $().piroBox({
    my_speed: 300, //animation speed
    bg_alpha: 0.5, //background opacity
    radius: 4, //caption rounded corner
    scrollImage: false, // true == image follows the page _|_ false == image remains in the same open position
    // in some cases of very large images or long description could be useful.
    slideShow: 'true', // true == slideshow on, false == slideshow off
    slideSpeed: 3, //slideshow
    pirobox_next: 'piro_next', // Nav buttons -> piro_next == inside piroBox , piro_next_out == outside piroBox
    pirobox_prev: 'piro_prev', // Nav buttons -> piro_prev == inside piroBox , piro_prev_out == outside piroBox
    close_all: '.piro_close' // add class .piro_overlay(with comma)if you want overlay click close piroBox
    });*/


});

// fix for @FONTFACE style flash in CMS
function fixFontFace() {
    $("#dropdownpanel .control").css({ 'font-family': 'arial' });
    $("#dropdownpanel .control").css({ 'font-family': 'WinterthurCondensedRegular' });

    $("#nav-sub ul li a").css({ 'font-family': 'arial' });
    $("#nav-sub ul li a").css({ 'font-family': 'BebasNeueRegular' });
    $("#nav-main").css({ 'font-family': 'arial' });
    $("#nav-main").css({ 'font-family': 'BebasNeueRegular' });
    $("#secondary-nav ul li a").css({ 'font-family': 'arial' });
    $("#secondary-nav ul li a").css({ 'font-family': 'BebasNeueRegular' });
    //secondary styles
    $("#nav-top ul li a").css({ 'font-family': 'arial' });
    $("#nav-top ul li a").css({ 'font-family': 'BebasNeueRegular' });

    $(".crumb ul li a, .crumb li").css({ 'font-family': 'arial' });
    $(".crumb ul li a, .crumb li").css({ 'font-family': 'BebasNeueRegular' });

    $("#ctl00_cph_Mainbody_ctl01_bodycontents h1").css({ 'font-family': 'arial' });
    $("#ctl00_cph_Mainbody_ctl01_bodycontents h1").css({ 'font-family': 'BebasNeueRegular' });

    $("#ctl00_cph_Mainbody_ctl01_bodycontents h2").css({ 'font-family': 'arial' });
    $("#ctl00_cph_Mainbody_ctl01_bodycontents h2").css({ 'font-family': 'BebasNeueRegular' });

    $("#ctl00_cph_Mainbody_ctl01_bodycontents div").css({ 'font-family': 'arial' });
    $("#ctl00_cph_Mainbody_ctl01_bodycontents div").css({ 'font-family': 'BebasNeueRegular' });

    $("h1").css({ 'font-family': 'arial' });
    $("h1").css({ 'font-family': 'BebasNeueRegular' });

    $("h2").css({ 'font-family': 'arial' });
    $("h2").css({ 'font-family': 'BebasNeueRegular' });

    $(".nav").css({ 'font-family': 'arial' });
    $(".nav").css({ 'font-family': 'BebasNeueRegular' });

    $(".top-nav li a").css({ 'font-family': 'arial' });
    $(".top-nav li a").css({ 'font-family': 'WinterthurCondensedRegular' });

    $("#sub-nav ul li a").css({ 'font-family': 'arial' });
    $("#sub-nav ul li a").css({ 'font-family': 'WinterthurCondensedRegular' });

    $("#cart a").css({ 'font-family': 'arial' });
    $("#cart a").css({ 'font-family': 'BebasNeueRegular' });

    $("#cart #otherlink p").css({ 'font-family': 'arial' });
    $("#cart #otherlink p").css({ 'font-family': 'WinterthurCondensedRegular' });

    $(".wrapper").css({ 'font-family': 'arial' });
    $(".wrapper").css({ 'font-family': 'WinterthurCondensedRegular' });

    $("#innerRecPro a").css({ 'font-family': 'arial' });
    $("#innerRecPro a").css({ 'font-family': 'WinterthurCondensedRegular' });
}

/* AJAX ROTATOR */

function admin() { //rotator functions
    $("#panel-wrap").toggle();
    $("#admin-wrap").toggle();

}
function rotateTo(id) { //rotator functions
    cur = id;
    clearTimeout(ctime);
    rotateDiv();
}
function highlightControlDiv(id) {   //rotator functions
    $("#panels-control div").css({ 'color': '#fff' });
    $("#ctrl-" + id).css({ 'color': '#1e2e61' });
}
function rotateDiv() {    //rotator functions

    if ($("#panel-" + cur).html() == "") {
    } else {
        $(".panel").fadeOut(1000);
        $("#panel-" + cur).fadeIn(1000);
    }
    highlightControlDiv(cur);
    cur++;
    if (cur > total) cur = 1;
    ctime = setTimeout(rotateDiv, 7000);
}


function doLoadingIcon() {
    if($(".buttonloading").html() != null)
        $(".buttonloading").append("<img src='/images/loading/loading51.gif' />");
}
/* Search box */


/*   put link like cursor behavor on all elements with class passed as argument  */
function setHover(div) {
    $(div).hover(function() { $(div).css({ cursor: "pointer" }); },
      	function() { $(div).css({ cursor: "default" }); });
}

/* login script */
function doLogin() {

    $.post('scripts/processlogin.aspx', {
        u: $("#username").val(),
        p: hex_sha256(hex_sha256($("#password").val()))
    }, function(msg) {
        $("#loginmsg").html("Login: " + msg);
        if (Left(msg, 10) == "Successful") {
            hideLogin();
            setTimeout('rload()', 1000);
        }
    });
}

function Left(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else
        return String(str).substring(0, n);
}

function Right(str, n) {
    if (n <= 0)
        return "";
    else if (n > String(str).length)
        return str;
    else {
        var iLen = String(str).length;
        return String(str).substring(iLen, iLen - n);
    }
}

/* hides login form  */
function hideLogin() { $("#voice-login").fadeOut(1000); }

/* logs user out of the cms  */
function doLogout() {
    $.post('scripts/logout.aspx', {}, function(msg) { window.location.reload(true); });
}

/* reloads current page  */
function rload() { window.location.reload(true); };

function encodeRE(s) { return s.replace(/[.*+?^${}()|[\]\/\\]/g, '\\$0'); }



/*
*jQuery browser plugin detection 1.0.3
* http://plugins.jquery.com/project/jqplugin
* Checks for plugins / mimetypes supported in the browser extending the jQuery.browser object
* Copyright (c) 2008 Leonardo Rossetti motw.leo@gmail.com
* MIT License: http://www.opensource.org/licenses/mit-license.php
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
(function ($) {
    $.getScript("http://java.com/js/deployJava.js");

    //checks if browser object exists
    if (typeof $.browser === "undefined" || !$.browser) {
        var browser = {};
        $.extend(browser);
    }
    var pluginList = {
        pdf: {
            activex: ["acroPDF.PDF.1", "PDF.PdfCtrl.1", "PDF.PdfCtrl.4", "PDF.PdfCtrl.5", "PDF.PdfCtrl.6"],
            plugin: /adobe\s?acrobat/gim
        }
    };
    var isSupported = function (p) {
        if (window.ActiveXObject) {
            $.browser[p] = false;

            for (i = 0; i < pluginList[p].activex.length; i++) {
                try {
                    new ActiveXObject(pluginList[p].activex[i]);
                    $.browser[p] = true;
                } catch (e) { }
            }
        } else {
            $.each(navigator.plugins, function () {
                if (this.name.match(pluginList[p].plugin)) {
                    $.browser[p] = true;
                    return false;
                } else {
                    $.browser[p] = false;
                }
            });
        }
    };

    $.each(pluginList, function (i, n) {
        isSupported(i);
    });
    //uses sun script to detect if java plugin is available
//    $.getScript("http://java.com/js/deployJava.js", function () {
//        if (deployJava.versionCheck("1.6.0+") || deployJava.versionCheck("1.4") || deployJava.versionCheck("1.5.0*")) {
//            $.browser.java = true;
//        } else {
//            $.browser.java = false;
//        }
//    });
})(jQuery);
