/* 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 () {

    if (document.getElementById('secondary-nav')) {
        $('#page_title').removeClass('page_head_in');
        $('#page_title').addClass('text_padding');
        $('#ctl00_cph_Mainbody_pBody').css('width', '60%');
    }

    //rotator functions
    $("#ctl00_RotatorWrap").load('/scripts/widgets/rotator/gethtml.aspx', function () {
        total = $(".panel").length;
        ctime = setTimeout(rotateDiv, 6000);
        highlightControlDiv(1);
    });

    $("#torbay_rotate").load('/scripts/widgets/torbay_rotate/gethtml.aspx', function () {
        total = $(".panel").length;
        ctime = setTimeout(rotateDiv, 6000);
        highlightControlDiv(1);
    });

    $("#life_rotate").load('/scripts/widgets/life_rotate/gethtml.aspx', function () {
        total = $(".panel").length;
        ctime = setTimeout(rotateDiv, 6000);
        highlightControlDiv(1);
    });

    $("#opp_rotate").load('/scripts/widgets/opp_rotate/gethtml.aspx', function () {
        total = $(".panel").length;
        ctime = setTimeout(rotateDiv, 6000);
        highlightControlDiv(1);
    });

    $("#place_rotate").load('/scripts/widgets/place_rotate/gethtml.aspx', function () {
        total = $(".panel").length;
        ctime = setTimeout(rotateDiv, 6000);
        highlightControlDiv(1);
    });

    //submenu functions
    $('.togglemenu0').click(function (e) {
        $('.activeul0').show();
        $('.activeul1').hide();
        $('.activeul2').hide();
        $('.activeul3').hide();
        $('.activeul4').hide();
        $('.activeul5').hide();
    });
    $('.togglemenu1').click(function (e) {
        $('.activeul0').hide();
        $('.activeul1').show();
        $('.activeul2').hide();
        $('.activeul3').hide();
        $('.activeul4').hide();
        $('.activeul5').hide();
    });
    $('.togglemenu2').click(function (e) {
        $('.activeul0').hide();
        $('.activeul1').hide();
        $('.activeul2').show();
        $('.activeul3').hide();
        $('.activeul4').hide();
        $('.activeul5').hide();
    });
    $('.togglemenu3').click(function (e) {
        $('.activeul0').hide();
        $('.activeul1').hide();
        $('.activeul2').hide();
        $('.activeul3').show();
        $('.activeul4').hide();
        $('.activeul5').hide();
    });
    $('.togglemenu4').click(function (e) {
        $('.activeul0').hide();
        $('.activeul1').hide();
        $('.activeul2').hide();
        $('.activeul3').hide();
        $('.activeul4').show();
        $('.activeul5').hide();
    });
    $('.togglemenu5').click(function (e) {
        $('.activeul0').hide();
        $('.activeul1').hide();
        $('.activeul2').hide();
        $('.activeul3').hide();
        $('.activeul4').hide();
        $('.activeul5').show();
    });

    // Track mousemouse co-ords for login box popup
    $(document).mousemove(function (e) {
        px = e.pageX;
        py = e.pageY;
    });

    setHover(".hover");

    $('#searchkeybtn').click(function (e) {
        window.location = '?Content=Life/Search+' + $('#searchkey').val();
    });

    $('#searchkey').keydown(function (e) {
        //alert(e.keyCode);
        if (e.keyCode == 13) {
            window.location = '?Content=Life/Search+' + $('#searchkey').val();
            return false;
        }
    });

    $("#searchkeybtn").bind('click', function (e) { window.location = '?Content=Life/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
    });


});

/* 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'); }
