﻿//////////////////////////////////////////////////////////////////////////////////////////////
// Un-minified.  Edit this file but Only link to common.min.js.
// Use http://compressorrater.thruhere.net/ to minify.  Un-check "Packer" and "gzip"
// Options: YUI Compressor 2.3.5 nomunge, preserveAllSemi, disableOpt
//////////////////////////////////////////////////////////////////////////////////////////////

//****************** Global vars **************
var g_titleUriEnc = escape(document.title).replace(/%20/g, "+");
var g_urlUriEnc = escape(document.location).replace(/\//g, "%2f");

//****************** Common ******************
//setup StringBuilder
function StringBuilder() { this._buffer = []; };
StringBuilder.prototype.Append = function(str) { this._buffer[this._buffer.length] = str; };
StringBuilder.prototype.ToString = function() { return this._buffer.join(''); };

function OpenWindow(url, val, height, width, ShowHeader) {
    docwidth = width;
    docheight = height;
    leftVal = (screen.width - docwidth) / 2;
    topVal = (screen.height - docheight) / 2;
    var args = 'toolbar=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1';
    if (ShowHeader != true) {
        args = 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1';
    }
    var newWin = window.open(url + val, "view", "height=" + docheight + ",width=" + docwidth + ",left=" + leftVal + ",top=" + topVal + "toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes");
    if (newWin != null) newWin.focus();
}

function PrintDoc(id, l) {
    var docwidth = 717, docheight = 550;
    var top = (screen.height - docheight) / 2;
    var left = (screen.width - docwidth) / 2;
    var printWin = window.open("/PrintContent.aspx?id=" + id + "&l=" + l, "printWin", "height=" + docheight + ",width=" + docwidth + ",top=" + top + ", left=" + left + ",toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no");
    if (printWin != null) printWin.focus();
}

function ViewResource(id) {
    var docwidth = 500, docheight = 350;
    var top = (screen.height - docheight) / 2;
    var left = (screen.width - docwidth) / 2;
    var viewWin = window.open("/ViewResource.aspx?id=" + id, "viewWin", "height=" + docheight + ",width=" + docwidth + ",top=" + top + ", left=" + left + ",toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no");
    if (viewWin != null) viewWin.focus();
}

function CharCountDisplay(txtBox, divCountDisplay, maxLength) {
    var charCount = GetTextCount(txtBox);
    var divC = document.getElementById(divCountDisplay);
    var remainingC = maxLength - charCount;
    var outText = '';
    if (remainingC >= 0) {
        outText = remainingC + ' character';
        if (remainingC != 1) {
            outText = outText + 's';
    }
    outText = outText + ' remaining';
    divC.className = 'CharNormal';
    } else {
        outText = 'You are over the character limit (' + maxLength + ')';
        divC.className = 'CharError';
    }
    divC.innerHTML = outText;
}

function GetTextCount(txt) {
    try {
        var txtDisplay = document.getElementById(txt);
        return txtDisplay.value.length;
    } catch(err) { 
        alert(err.message); 
    }
}

function SetUniqueRadioButton(nameregex, current) {
    // Use this when you need to put radio buttons in a repeater or gridview 
    // nameregex: id of repeater and groupname of radio button in the following format: 'repeaterId.*groupname'
    // current: radio button object (this)
    //
    // put a version of the following line inside your itemdatabound or equivalent
    // CType(e.Item.FindControl("rbSelect"), RadioButton).Attributes.Add("onclick", "SetUniqueRadioButton('rptStores.*stores',this)")
    // replace 'rptStores.*stores' with the repeater id and radio button groupname like this 'repeaterId.*groupname'

    re = new RegExp(nameregex);
    for (i = 0; i < document.forms[0].elements.length; i++) {
        elm = document.forms[0].elements[i]
        if (elm.type == 'radio') {
            if (re.test(elm.name)) {
                elm.checked = false;
            }
        }
    }
    current.checked = true;
}

function HtmlDecode(str) {
    var x = str.replace(/\&lt\;/g, '\<');
    x = x.replace(/\&gt\;/g, '\>');
    x = x.replace(/\&amp\;/g, '\&');
    x = x.replace(/\&quot\;/g, '\'');
    x = x.replace(/\&\#39\;/g, '\'');
    return x;
}

function GetEventKeyNum(e) {
    var keynum;
    // IE
    if (window.event) keynum = e.keyCode;
    // Netscape/Firefox/Opera
    else if (e.which) keynum = e.which;
    return keynum;
}

function audio(audioNbr) {
    var leftVal = (screen.width - 220) / 2;
    var topVal = (screen.height - 100) / 2;
    var url = '/Audio/AudioPlayer.htm?id=' + audioNbr;
    window.open(url, 'Audio', 'width=220,height=100,menubar=0,toolbar=0,status=0,scrollbars=0,resizable=0,top=' + topVal + ',left=' + leftVal);
}

function video(videoNbr) {
    var url = '/Video/VideoPlayer.htm?id=' + videoNbr;
    window.open(url, 'Video', 'width=400,height=336,menubar=0,toolbar=0,status=0,scrollbars=0,resizable=0');
}

// IE 6 image caching fix
try {
    document.execCommand("BackgroundImageCache", false, true);
} catch (err) { }

// f(x) for passing pagename & site section to swf file for videos
// return comma delimited string --> "pagename,sitesection"
function GetOmnitureVarsForSWF() {
    try {
        return s.pageName + "," + s.channel;
    } catch (err) {
        return "None,None";
    }
}

//****************** Search engine ******************

function RunSearch(sTerm) {
    var searchTerm = escape(sTerm);
    searchTerm = searchTerm.replace("<", "").replace("&#", "").replace("%20", "+");
    if (searchTerm == "Search" || searchTerm == "") {
        location.href = "/Search/";
    } else {
        location.href = "/Search/?q=" + searchTerm;
    }
}
function goSearch() {
    RunSearch(document.getElementById('txtSearch').value);
}
function submitSearchBox(e) {
    var keynum = GetEventKeyNum(e);

    // don't allow '<' in textbox
    if (keynum == 60) return false;

    // don't allow '&#' in textbox
    // check and see if current character is a '#'
    if (keynum == 35) {
        // check and see if previous character is a '&'
        var temp = document.getElementById('txtSearch').value;
        var ampTest = temp.endsWith("&");
        if (ampTest === true) return false;
    }

    if (keynum == 13 || keynum == 10) {
        goSearch();
        return false;
    }
}
function clearSearchText(txtSearch) {
    if (txtSearch.value == 'Search') txtSearch.value = "";
}
function resetSearchText(txtSearch) {
    var trimmedText = txtSearch.value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
    if (trimmedText == '') txtSearch.value = 'Search';
}

//****************** Controls ******************

//* Types
function FeaturedVideo(iId, sTitle, sDescr) {
    this.Id = iId;
    this.Title = sTitle;
    this.Descr = sDescr;
}

//* Featured Video
function ShowFeaturedVideo(featVid) {
    document.getElementById("dFeaturedVideo").innerHTML = "";

    so = new SWFObject("/video/flash/featuredVideo.swf", "eFeaturedVideo", "195px", "175px", "9", "");
    so.addParam("allowScriptAccess", "sameDomain");
    so.addParam("wmode", "Opaque");
    so.addParam("quality", "High");
    so.addParam("play", "True");
    so.addParam("loop", "False");
    so.addParam("menu", "False");
    so.addParam("scale", "Showall");
    so.addVariable("xmlpath", "http%3a%2f%2f" + window.location.hostname + "%2fVideo%2fStreamXml.aspx%3fembed%3dtrue%26id%3d" + featVid.Id);
    so.write("dFeaturedVideo");

    document.getElementById("spnVideoTitle").innerHTML = featVid.Title;
    document.getElementById("spnVideoDescription").innerHTML = featVid.Descr;
}

function LoadDivFeaturedVideo() {
    if (g_featVids.length == 0) return;

    ShowFeaturedVideo(g_featVids[g_featVidIndex]);
    g_featVidIndex++;
    if (g_featVidIndex >= g_featVids.length) g_featVidIndex = 0;
}

//* Thoughts
function LoadDivFact() {
    document.getElementById("divFact").innerHTML = g_facts[g_factIndex];
    g_factIndex++;
    if (g_factIndex >= g_facts.length) g_factIndex = 0;
}

function GeatFeaturedVideoIndex() {
    var index = 0
    if (g_featVids.length > 1) {
        if (g_featVidsRandom) {
            //* randomize if needed
            index = Math.floor(Math.random() * g_featVids.length - 1);
            if (index < 0) {
                index = 0;  // fix when index = -1
            }
        }
    }
    return index;
}

//* Share This
function ShowShareThis() {
    $('#dShareThisModal').modal();
    $('#aDelicious').attr("href", "http://del.icio.us/post?url=" + g_urlUriEnc + "&title=" + g_titleUriEnc);
    $('#aDigg').attr("href", "http://digg.com/submit?phase=2&url=" + g_urlUriEnc + "&title=" + g_titleUriEnc);
    $('#aStumbleUpon').attr("href", "http://www.stumbleupon.com/submit?url=" + g_urlUriEnc + "&title=" + g_titleUriEnc);
    $('#aFurl').attr("href", "http://furl.net/storeIt.jsp?u=" + g_urlUriEnc + "&t=" + g_titleUriEnc);
    $('#aNetscape').attr("href", "http://www.netscape.com/submit/?U=" + g_urlUriEnc + "&T=" + g_titleUriEnc);
    $('#aYahoo').attr("href", "http://myweb2.search.yahoo.com/myresults/bookmarklet?u=" + g_urlUriEnc + "&t=" + g_titleUriEnc);
    $('#aTechnorati').attr("href", "http://www.technorati.com/faves?add=" + g_urlUriEnc);
    $('#aGoogle').attr("href", "http://www.google.com/bookmarks/mark?op=edit&bkmk=" + g_urlUriEnc + "&title=" + g_titleUriEnc);
    $('#aNewsvine').attr("href", "http://www.newsvine.com/_wine/save?u=" + g_urlUriEnc + "&h=" + g_titleUriEnc);
    $('#aBlinkList').attr("href", "http://blinklist.com/index.php?Action=Blink/addblink.php&Url=" + g_urlUriEnc + "&Title=" + g_titleUriEnc);
    $('#aReddit').attr("href", "http://reddit.com/submit?url=" + g_urlUriEnc + "&title=" + g_titleUriEnc);
    $('#aBlogmarks').attr("href", "http://blogmarks.net/my/new.php?mini=1&url=" + g_urlUriEnc + "&title=" + g_titleUriEnc);
    $('#aWindowsLive').attr("href", "http://favorites.live.com/quickadd.aspx?marklet=1&mkt=en-us&url=" + g_urlUriEnc + "&title=" + g_titleUriEnc + "&top=1");
    $('#aTailrank').attr("href", "http://tailrank.com/share/?link_href=" + g_urlUriEnc + "&title=" + g_titleUriEnc);
    $('#aSlashdot').attr("href", "http://slashdot.org/bookmark.pl?url=" + g_urlUriEnc + "&title=" + g_titleUriEnc);
    $('#aFacebook').attr("href", "http://www.facebook.com/share.php?u=" + g_urlUriEnc);
}

//* Email This
function ShowEmailThis() {
    OpenWindow("/P/EmailContent.aspx?title=" + g_titleUriEnc + "&url=" + g_urlUriEnc, "", "630", "420", "False"); return false;
}

// ****************** JSCookies ******************

// gets a cookie from the existing document and returns its value
function get_text_cookie(cookie_name) {
    var results = document.cookie.match(cookie_name + '=(.*?)(;|$)');

    if (results) {
        return (unescape(results[1]));
    }
    else { return null; }
}

// creates a cookie with the given parameters
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    } else {
        var expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

// locates and reads the value of a cookie with a specified name
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

// deletes a cookie with a specified name
function eraseCookie(name) {
    createCookie(name, "", -1);
}

// -------------------------------------------------------------------------------------
// New Methods from http://techpatterns.com/downloads/javascript_cookies.php
// -------------------------------------------------------------------------------------

function Set_Cookie(name, value, expires, path, domain, secure) {
    // set time (milliseconds)
    var today = new Date();
    today.setTime(today.getTime());

    /*
    if the expires variable is set, make the correct 
    expires time, the current script below will set 
    it for x number of days, to make it for hours, 
    delete * 24, for minutes, delete * 60 * 24
    */
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + "=" + escape(value) +
        ((expires) ? ";expires=" + expires_date.toGMTString() : "") +
        ((path) ? ";path=" + path : "") +
        ((domain) ? ";domain=" + domain : "") +
        ((secure) ? ";secure" : "");
}

function Get_Cookie(check_name) {
    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (i = 0; i < a_all_cookies.length; i++) {
        // now we'll split apart each name=value pair
        a_temp_cookie = a_all_cookies[i].split('=');

        // and trim left/right whitespace while we're at it
        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        // if the extracted name matches passed check_name
        if (cookie_name == check_name) {
            b_cookie_found = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            // note that in cases where cookie is initialized but no value, null is returned
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}

function Get_Int_Cookie(check_name) {
    var intVal = null;
    if (Get_Cookie(check_name)) {
        intVal = parseInt(Get_Cookie(check_name));
    }
    return intVal;
}

// this deletes the cookie when called
function Delete_Cookie(name, path, domain) {
    if (Get_Cookie(name)) document.cookie = name + "=" +
        ((path) ? ";path=" + path : "") +
        ((domain) ? ";domain=" + domain : "") +
        ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

// gets the current domain, removing a leading www.
function GetDomain() {
    var dm = window.location.hostname;
    dm = dm.replace(/www./i, '');
    return dm;
}

//****************** Font-Sizes ******************
// dependency on JSCookies.js

// text resizing
var currentTextSize = null;
var currentLineHeight = null;
// check for cookie
if (get_text_cookie("ihtfontsize")) {
    currentTextSize = get_text_cookie("ihtfontsize");
    if (currentTextSize == .85) {
        currentLineHeight = 1.45;
    }
    else if ((currentTextSize == 1) || (currentTextSize == 1.15)) {
        currentLineHeight = 1.4;
    }
    else if (currentTextSize == 1.35) {
        currentLineHeight = 1.3;
    }
} else {
    currentTextSize = 1;
    currentLineHeight = 1.4;
    createCookie("ihtfontsize", currentTextSize, 90);
}

function textSize(dir) {
    if (dir == 'up') {
        if (currentTextSize == 1.15) {
            currentTextSize = 1.35;
            currentLineHeight = 1.3;
        }
        else if (currentTextSize == 1) {
            currentTextSize = 1.15;
            currentLineHeight = 1.4;
        }
        else if (currentTextSize == .85) {
            currentTextSize = 1;
            currentLineHeight = 1.4;
        }
    } else if (dir == 'down') {
        if (currentTextSize == 1) {
            currentTextSize = .85;
            currentLineHeight = 1.45;
        }
        else if (currentTextSize == 1.15) {
            currentTextSize = 1;
            currentLineHeight = 1.4;
        }
        else if (currentTextSize == 1.35) {
            currentTextSize = 1.15;
            currentLineHeight = 1.4;
        }
    }
    //    currentLineHeight = currentTextSize + .3;

    SetContentStyles();
    // write/rewrite cookie
    createCookie("ihtfontsize", currentTextSize, 90);
}

function SetContentStyles() {
    var div = document.getElementById('divContent');
    if (div != null) {
        div.style.fontSize = currentTextSize + 'em';
        div.style.lineHeight = currentLineHeight + 'em';
    }
}

function SavePdf(id, l) {
    var printWin;

    if (id) {
        if (l) {
            printWin = window.open("/SaveContent.aspx?id=" + id + "&l=" + l);
        } else {
            printWin = window.open("/SaveContent.aspx?id=" + id);
        }
    } else {
        if (window.location.href.indexOf("?") > 0) {
            printWin = window.open("/SaveContent.aspx?url=" + escape(window.location + "&print=true"));
        } else {
            printWin = window.open("/SaveContent.aspx?url=" + escape(window.location + "?print=true"));
        }
    }
    if (printWin != null) printWin.focus();
}

function PrintPage(id, l) {

    var docwidth = 717, docheight = 550;
    var top = (screen.height - docheight) / 2;
    var left = (screen.width - docwidth) / 2;
    var printWin;

    if (id) {
        if (l) {
            printWin = window.open("/PrintContent.aspx?id=" + id, "printWin", "height=" + docheight + ",width=" + docwidth + ",top=" + top + ", left=" + left + ",toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no");
        } else {
            printWin = window.open("/PrintContent.aspx?id=" + id + "&l=" + l, "printWin", "height=" + docheight + ",width=" + docwidth + ",top=" + top + ", left=" + left + ",toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no");
        }
    } else {
        if (window.location.href.indexOf("?") > 0) {
            printWin = window.open(window.location + "&print=true", "printWin", "height=" + docheight + ",width=" + docwidth + ",top=" + top + ", left=" + left + ",toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no");
        } else {
            printWin = window.open(window.location + "?print=true", "printWin", "height=" + docheight + ",width=" + docwidth + ",top=" + top + ", left=" + left + ",toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no");
        }
    }
    if (printWin != null) printWin.focus();
}

// parses the value of the cookie
// returns an array of the different values
//function parseCookie(cookievalues){
//	if (cookieValues.length > 0){
//		var cookieValuesArray = new Array(3);
//		var CookiesArray = cookievalues.split("&");
//		for (var i = 0; i < CookiesArray.length; i++){
//			var v = CookiesArray[i].split(":");
//			cookieValuesArray[i] = v[1];
//		}
//		return cookieValuesArray;
//	} else {
//		return null;
//	}
//}
