﻿jQuery.preloadImages = function () {
    for (var i = 0; i < arguments.length; i++) {
        jQuery("<img>").attr("src", arguments[i]);
    }
}

function CopyPaste() {
    var text = ''; var is_onmouseup = false; var is_ctrl = false; this.Init = Init; this.ChangeTextInSelection = ChangeTextInSelection; this.EventHandler = EventHandler; function Init(init_text) {
        text = init_text; if (window.addEventListener) {
            if (window.navigator.userAgent.match('Firefox')) {
                switch (window.navigator.userAgent.match(/Firefox\/\d/).toString()) {
                    case 'Firefox/1': case 'Firefox/2': document.addEventListener('mouseup', this.EventHandler, false); is_onmouseup = true; document.addEventListener('keyup', function (e)
                    { if (e.which == 17) is_ctrl = false; }, false); document.addEventListener('keydown', function (e)
                    { if (e.which == 17) is_ctrl = true; if (e.which == 67 && is_ctrl == true) EventHandler(); }, false); break; default: document.getElementsByTagName('body')[0].addEventListener('copy', this.EventHandler, false);
                }
            }
            else
            { document.getElementsByTagName('body')[0].addEventListener('copy', this.EventHandler, false); }
        }
        else
        { document.getElementsByTagName('body')[0].attachEvent('oncopy', this.EventHandler); }
    }; function ChangeTextInSelection(text) {
        var body_element = document.getElementsByTagName('body')[0]; var new_selection_block = document.createElement('div'); new_selection_block.style.overflow = 'hidden'; new_selection_block.style.color = '#000000'; new_selection_block.style.backgroundColor = 'transparent'; new_selection_block.style.textAlign = 'left'; new_selection_block.style.textDecoration = 'none'; new_selection_block.style.border = 'none'; new_selection_block.id = 'new_selection_block' + Math.random().toString(); if (typeof window.getSelection != 'undefined') {
            var current_selection = window.getSelection(); if (current_selection.toString()) {
                if (typeof current_selection.setBaseAndExtent != 'undefined') {
                    var current_range = current_selection.getRangeAt(0); new_selection_block.style.width = 0.1; new_selection_block.style.height = 0.1; new_selection_block.appendChild(current_range.cloneContents()); var new_text_block = document.createElement('div'); new_text_block.innerHTML = text; new_selection_block.appendChild(new_text_block); body_element.appendChild(new_selection_block); current_selection.selectAllChildren(new_selection_block); window.setTimeout(function ()
                    { new_selection_block.parentNode.removeChild(new_selection_block); window.getSelection().setBaseAndExtent(current_range.startContainer, current_range.startOffset, current_range.endContainer, current_range.endOffset); }, 0);
                }
                else {
                    var current_text_lower = text.replace(/<.*?>/gi, ''); if (current_selection.toString().indexOf(current_text_lower) == -1) {
                        var new_text_block = document.createElement('div'); new_text_block.style.position = 'fixed'; body_element.appendChild(new_text_block); new_selection_block.innerHTML = text; new_text_block.appendChild(new_selection_block); var new_range = document.createRange(); new_range.selectNode(new_text_block); current_selection.addRange(new_range); window.setTimeout(function ()
                        { new_text_block.parentNode.removeChild(new_text_block); }, (is_onmouseup) ? 3000 : 0);
                    }
                }
            }
        }
        else {
            var range = document.selection.createRange(); new_selection_block.style.width = 0; new_selection_block.style.height = 0; var current_range_text_lower = range.text.toString().toLowerCase(); var current_text_lower = text.toLowerCase().replace(/<.*?>/gi, ''); if (current_range_text_lower.indexOf(current_text_lower) == -1)
            { new_selection_block.innerHTML = range.htmlText + text; }
            else
            { new_selection_block.innerHTML = range.htmlText }
            body_element.appendChild(new_selection_block); var new_text_range = body_element.createTextRange(); new_text_range.moveToElementText(new_selection_block); new_text_range.select(); window.setTimeout(function () {
                new_selection_block.parentNode.removeChild(new_selection_block); if (range.text != '')
                { range.select(); }
            }, 0);
        }
    }; function EventHandler(e)
    { ChangeTextInSelection(text); };
}

function SetUI() {

    var copy_paste = new CopyPaste(); copy_paste.Init('<br><br>Read more at: <a href="' + document.location.href + '" target="_blank_">' + document.location.href + '</a>', true);
    $(document).bind("contextmenu", function (e) {
        // return false;
    }); 
    setnavRollovers();
    setImageRollover();
    loadRSS(0);
    applyFadeOvers();
    setTimeout('fixPNG()',3000);
    
}

function fixPNG() {
    $(document).pngFix();
}

function applyFadeOvers() {
    $("[bgImage]").each(function () {
        var bgImages = $(this).attr("bgImage");
        var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
        var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

        $(this).mouseover(function () {

            if (jQuery.browser.msie && (ie55 || ie6)) {
                $(this).find("span").stop();
                
                bgImages = $(this).parent().find('div').attr("bgImage").replace("url(","").replace(")","");

                $(this).find("span").css("filter", 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgImages + '\', sizingMethod=\'scale\')');

            }
            else {
                $(this).find("img").stop();
                $(this).find("img").animate({ opacity: 0 }, 1000);
                $(this).css("backgroundImage", bgImages);
            }

        });
        $(this).mouseout(function () {

            if (jQuery.browser.msie && (ie55 || ie6)) {
                $(this).find("span").stop();

                bgImages = $(this).parent().find('div').attr("bgImage").replace("url(", "").replace(")", "").replace("_on","");

                $(this).find("span").css("filter", 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgImages + '\', sizingMethod=\'scale\')');

            }
            else {
                $(this).find("img").stop();
                $(this).find("img").animate({ opacity: 1 }, 1000, function () { $(this).css("backgroundImage", 'url(../images/spacer.gif)'); });
            }

        });
    });
}

var RSSIndex = 0;

function loadRSS() {
    if (NewsRSSArr.length > 0) {
        if (RSSIndex < NewsRSSArr.length) {
            getRSS(NewsRSSArr[RSSIndex], RSSIndex);
        }
    }
}


function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}


function stateChanged(theID) {

    if (xmlHttp.readyState == 4) {
        // alert the content in the div
        alert(xmlHttp.responseText);
    }
}

function getRSS(RSSUrl, indexId) {
    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert("Your browser does not support AJAX!");
        return;
    }

    var url = "../rss/getRSS.aspx";
    url = url + "?RSSUrl=" + RSSUrl;
    url = url + "&sid=" + Math.random(); // I like random to avoid caching

    //alert(url);
    xmlHttp.onreadystatechange = function () {

        if (this.readyState == 4) {
            if (this.responseText == "ERROR") {
                $("#newsCell_" + indexId).html("Unable to retrieve feed, please try again later");
            }
            else {
                $("#newsCell_" + indexId).html(this.responseText);
            }
            RSSIndex++;
            loadRSS();

            NewsRSSArrLoaded[indexId] = 1;
            
        }
    }

    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}
 

var navArrayOn = new Array();
navArrayOn[0] = "backgroundColor:#d6dad9,700";
var NewsRSSArr = new Array();
var NewsRSSArrLoaded = new Array();



function setnavRollovers() {
    $('.navItem').css("border-bottom", "4px solid #90939c");
    $('.navItem').find("a").css("color", "#394a58");

    var thePage = document.location.href.substring(document.location.href.lastIndexOf("/") + 1);
  
   // $("#mainNav").find('[href*=' + thePage + ']').parent().css("backgroundColor", "#d6dad9").css("border-bottom", "4px solid #981e1f");

   // $('[href*=' + thePage + ']').parent().css("border-bottom", "4px solid #981e1f")
    $(".navItem").mouseover(function () {
        $(this).stop();
        $(this).css("backgroundColor", "#d6dad9");
        $(this).css("border-bottom", "4px solid #981e1f");
        $(this).css("font-weight", "bold");
        $(this).find('a').each(function () {
            if ($(this).attr("isMainNav")) {
                $(this).css("color", "#981e1f");
            }
        });
        $(this).find("[isSubNav]").stop();
        $(this).find("[isSubNav]").animate({height:160},500);
    });

    $(".navSubItem").mouseover(function () {
        $(this).stop();
        $(this).css("font-weight", "bold");
    });

    $(".navSubItem").mouseout(function () {
        if ((document.location.href.indexOf('?') == -1) && (document.location.href.indexOf('/about/') != -1)) {
            $($(".navSubItem")[0]).css("font-weight", "bold");
            $($(".navSubItem")[1]).css("font-weight", "normal");
            $($(".navSubItem")[2]).css("font-weight", "normal");
            $($(".navSubItem")[3]).css("font-weight", "normal");
            $(this).stop();
        }
        else {
            if (document.location.href.indexOf($(this).find('a').attr("href").replace("../", "")) == -1) {
                $(this).css("font-weight", "normal");
            }
            $($(".navSubItem")[0]).css("font-weight", "normal");
        }



    });


        $(".navItem").mouseout(function () {
            if ($(this).find('a').attr("href").indexOf(thePage) == -1) {
                
                if ((document.location.href.toLowerCase().indexOf("/about/") == -1) || ($(this).find('a').attr("href").indexOf("/about/") == -1)) {
               
                    $(this).css("backgroundColor", "Transparent");
                    $(this).css("border-bottom", "4px solid #90939c");
                    $(this).css("font-weight", "normal");
                    $(this).find("a").css("color", "#394a58");
                    $(this).stop();
                }

                //   alert("X");

            }
            $(this).find("[isSubNav]").stop();
            $(this).find("[isSubNav]").animate({ height: 0 }, 500);
        });

        if (document.location.href.toLowerCase().indexOf("/about/") != -1) {

            $('[isAboutMain]').css("color", "#981e1f")
            $('[isAboutMain]').css("font-weight", "bold");
            $('[isAboutMain]').parent().css("border-bottom", "4px solid #981e1f");
            $('[isAboutMain]').parent().css("backgroundColor", "#d6dad9");
        }
        else {
            $(".navItem").find('[href*=' + thePage + ']').css("color", "#981e1f")
            $(".navItem").find('[href*=' + thePage + ']').css("font-weight", "bold");
            $(".navItem").find('[href*=' + thePage + ']').parent().css("border-bottom", "4px solid #981e1f");
            $(".navItem").find('[href*=' + thePage + ']').parent().css("backgroundColor", "#d6dad9");
        }

        if ((document.location.href.toLowerCase().indexOf("?") != -1) || (document.location.href.toLowerCase().indexOf("/about/") == -1)) {
            $(".navSubItem").find('[href*=' + thePage + ']').css("font-weight", "bold");
        }
        else {
            if (document.location.href.toLowerCase().indexOf("/about/") != -1) {
                $($(".navSubItem")[0]).css("font-weight", "bold");
            }
        }
        

       
}

function setImageRollover() {

    $("[hasRollover]").each(function () {
        if ($(this).attr("src").indexOf(".jpg") != -1) {
            $.preloadImages($(this).attr("src").split(".jpg").join("_on.jpg"));
        }
        if ($(this).attr("src").indexOf(".gif") != -1) {
            $.preloadImages($(this).attr("src").split(".gif").join("_on.gif"));
        }
        if ($(this).attr("src").indexOf(".png") != -1) {
            $.preloadImages($(this).attr("src").split(".png").join("_on.png"));
        }

        $(this).css("cursor", "pointer");
    });

    $("[hasRollover]").mouseover(function () {
        if ($(this).attr("src").indexOf("_on.") == -1) {
            $(this).attr("src", $(this).attr("src").split(".png").join("_on.png"));
            $(this).attr("src", $(this).attr("src").split(".jpg").join("_on.jpg"));
            $(this).attr("src", $(this).attr("src").split(".gif").join("_on.gif"));
        }
    });

    $("[hasRollover]").mouseout(function () {
        $(this).attr("src", $(this).attr("src").split("_on.png").join(".png"));
        $(this).attr("src", $(this).attr("src").split("_on.jpg").join(".jpg"));
        $(this).attr("src", $(this).attr("src").split("_on.gif").join(".gif"));
    });
}

