/* 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
//font size vars
var fontsize = 'NORMAL'; //NORMAL,LARGER, SMALLER
var COOKIE_NAME = "Simple-Fontresizer-CSUC"; //Maybe give this the name of your site.
var contentresize = "#ctl00_cph_Breadcrumbs_pCrumbs *, #ctl00_cph_Mainbody_ctl00_bodycontents * "; //content IDs, classes to rezise
//rotator functions
var cur = 2;
var total;
var ctime;
var binc = true;


(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)


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 () {
    fixFontFace(); // in title.js

    window.onresize = resize;
    /* SEARCH FUNCTIONS */
    $('#searchkeybtn').click(function (e) {
        if (document.getElementById('searchkey').value != "") {
            window.location = '?Content=Search+' + $('#searchkey').val();
			return false;
        }
    });

    $('#searchkey').keydown(function (e) {
        //alert(e.keyCode);
        if (e.keyCode == 13) {
            if (document.getElementById('searchkey').value != "" && document.getElementById('searchkey').value != "Search...") {
                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...");
        }
    });

    $("#right_btn").click(function () {
        rotateDivRight();
    });
    $("#left_btn").click(function () {
        rotateDivLeft();
    });

    //set initial searc box color
    $('#searchkey').css({ 'color': '#666666' });

    //text resize function
    $("#text-resize").show();
    // if cookie exists set font size to saved value, otherwise create cookie
    //    if ($.cookie(COOKIE_NAME)) {
    //        fontsize = $.cookie(COOKIE_NAME);
    //        //set initial font size for this page view:
    //        $("body").css("font-size", fontsize + "px");
    //        //set up appropriate class on font resize link:
    //    } else {
    //        $.cookie(COOKIE_NAME, fontsize);
    //    }

    $("#font_Larger").bind("click", function () {
        $("#ctl00_cph_Mainbody_ctl01_bodycontents div").removeClass("divsmaller");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents div").addClass("divlarger");

        $(".col").removeClass("divsmaller");
        $(".col").addClass("divlarger");

        $("#home-events .eventcol h3 em").removeClass("divsmaller");
        $("#home-events .eventcol h3 em").addClass("divlarger");

        $("#ctl00_cph_Mainbody_ctl01_bodycontents h1").addClass("h1larger");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents h1").removeClass("h1smaller");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents h2").addClass("h2larger");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents h2").removeClass("h2smaller");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents h3").addClass("h3larger");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents h3").removeClass("h3smaller");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents h4").addClass("h4larger");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents h4").removeClass("h4smaller");
    });

    // small font-size link:
    $("#font_Smaller").bind("click", function () {
        $("#ctl00_cph_Mainbody_ctl01_bodycontents div").removeClass("divlarger");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents div").addClass("divsmaller");

        $(".col").removeClass("divlarger");
        $(".col").addClass("divsmaller");

        $("#home-events .eventcol h3 em").removeClass("divlarger");
        $("#home-events .eventcol h3 em").addClass("divsmaller");

        $("#ctl00_cph_Mainbody_ctl01_bodycontents h1").addClass("h1smaller");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents h1").removeClass("h1larger");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents h2").addClass("h2smaller");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents h2").removeClass("h2larger");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents h3").addClass("h3smaller");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents h3").removeClass("h3larger");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents h4").addClass("h4smaller");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents h4").removeClass("h4larger");
    });
    // normal font-size link:
    $("#font_Normal").bind("click", function () {
        $("#ctl00_cph_Mainbody_ctl01_bodycontents div, .col, em").removeClass("divsmaller");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents div, .col, em").removeClass("divlarger");

        $("#ctl00_cph_Mainbody_ctl01_bodycontents h1").removeClass("h1smaller");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents h1").removeClass("h1larger");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents h2").removeClass("h2smaller");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents h2").removeClass("h2larger");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents h3").removeClass("h3smaller");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents h3").removeClass("h3larger");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents h4").removeClass("h4smaller");
        $("#ctl00_cph_Mainbody_ctl01_bodycontents h4").removeClass("h4larger");
    });


    //rotator functions
    $("#ctl00_RotatorWrap").load('/scripts/widgets/rotator/gethtml.aspx', function () {
        total = $(".panel").length;
        ctime = setTimeout(rotateDiv, 6000);
        highlightControlDiv(1);
    });

    // Track mousemouse co-ords for login box popup
    $(document).mousemove(function (e) {
        px = e.pageX;
        py = e.pageY;
    });

    setHover(".hover");

    $().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
    });

    /* 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><br /></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(); }
        });
    });

    $(".overlay").each(function () {
        var id = $(this).attr("id");
        $(this).attr("href", "javascript:;");
        $(this).mousedown(function () {
            $("#wrapper").append("<div class='ol-body'><div class='ol-left'></div><div class='ol-right'><img src='images/ol-close.jpg' alt='Close' title='Close' /><h3></h3><p>Loading...</p></div></div>");
            $.getJSON('scripts/admin/overlay/getjson.aspx', { "uid": id }, function (data) {
                $(".ol-body .ol-right h3").html(data.title);
                $(".ol-body .ol-right p").html(data.desc);
                $(".ol-body .ol-right img").click(function () { $(".ol-body").remove(); });
                $(".ol-body").css({ "position": "fixed", "top": "45%", "left": (($(document).width() / 2) - 210) + "px", "display": "block" });
                if (data.image.length > 2) {
                    $(".ol-body .ol-left").html("<img src='" + data.image + "' alt='" + data.title + "' />");
                    $(".ol-body .ol-right").css("width", "345px");
                } else {
                    $(".ol-body .ol-left").remove();
                }
            });

        });
    });

    //    $("a").each(function () {
    //        $this = $(this);
    //        if (Right($this.attr("href"), 4) == ".PDF" || Right($this.attr("href"), 4) == ".pdf") {
    //            $this.attr("target", "_blank");
    //            if (userid <= 0) {
    //                $("<img>").attr("alt", "PDF").attr("src", "/images/pdf.png").attr("class", "pdficon").appendTo($this);
    //            }
    //        }
    //    });


    $("a[href*='popup.aspx']").fancybox({
        'width': 820,
        'height': 700,
        'autoDimensions': false,
        'type': 'ajax',
        onComplete: function (data) { }
    });


});

// fix for @FONTFACE style flash in CMS
function fixFontFace() {
//    $("#nav-sub ul li a").css({ 'font-family': 'arial' });
//    $("#nav-sub ul li a").css({ 'font-family': 'TeXGyreHerosCnRegular' });
//    $("#nav-main").css({ 'font-family': 'arial' });
//    $("#nav-main").css({ 'font-family': 'TeXGyreHerosCnRegular' });
//    $("#secondary-nav ul li a").css({ 'font-family': 'arial' });
//    $("#secondary-nav ul li a").css({ 'font-family': 'TeXGyreHerosCnRegular' });
//    //secondary styles
//    $("#nav-top ul li a").css({ 'font-family': 'arial' });
//    $("#nav-top ul li a").css({ 'font-family': 'TeXGyreHerosCnRegular' });

//    $(".crumb ul li a, .crumb li").css({ 'font-family': 'arial' });
//    $(".crumb ul li a, .crumb li").css({ 'font-family': 'TeXGyreHerosCnRegular' });

//    $("#ctl00_cph_Mainbody_ctl01_bodycontents h1").css({ 'font-family': 'arial' });
//    $("#ctl00_cph_Mainbody_ctl01_bodycontents h1").css({ 'font-family': 'TeXGyreHerosCnRegular' });

//    $("#ctl00_cph_Mainbody_ctl01_bodycontents h2").css({ 'font-family': 'arial' });
//    $("#ctl00_cph_Mainbody_ctl01_bodycontents h2").css({ 'font-family': 'TeXGyreHerosCnRegular' });

//    $(".yearnav").css({ 'font-family': 'arial' });
//    $(".yearnav").css({ 'font-family': 'TeXGyreHerosCnRegular' });

//    $("#ctl00_cph_Mainbody_ctl01_bodycontents div").css({ 'font-family': 'arial' });
//    $("#ctl00_cph_Mainbody_ctl01_bodycontents div").css({ 'font-family': 'TeXGyreHerosCnRegular' });
}

/* 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
//alert(cur);
    if ($("#panel-" + cur).html() == "") {
    } else {
        $(".panel").fadeOut(1000);
        $("#panel-" + cur).fadeIn(1000);
    }
    //highlightControlDiv(cur);
    if (binc) cur++;
    if (cur > total) cur = 1;
    ctime = setTimeout(rotateDiv, 7000);
}
function rotateDivRight() {    //rotator functions
    //cur = cur + 1;
    if (cur > total) { cur = 1; }
    clearTimeout(ctime);
    rotateDiv();
}
function rotateDivLeft() {    //rotator functions
     //cur = cur -2;
    if (cur == 1) {
        cur = total - 1;
    } else if (cur == 2) {
        cur = total;
    } else { cur = cur - 2; }


    clearTimeout(ctime);
    rotateDiv();
    
}

function doLoadingIcon() {
    if ($(".buttonloading").html() != null)
        $(".buttonloading").append("<img src='/images/loading/loading51.gif' />");
}

/*   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'); }

function email(sender, receiver, message) { }
