﻿// -- test for ie
var uaTest = function(string){
    var pattern = string.split("/")[1],
    mods = string.split("/")[2],
    regEx = new RegExp(pattern,mods);
    return regEx.test(navigator.userAgent);
};

var ie = uaTest("/MSIE/g"),
ie7 = uaTest("/MSIE 7.0/g"),
ie6 = uaTest("/MSIE 6.0/g");

// -- shorthand document.getElementById

var _get = function(str){
    return document.getElementById(str);
};

// -- placeholder attributes for those without

(function(placeholder) {
	if(!placeholder) {
		$(document.forms).find('input').each(function() {
			var me = $(this),
			text = me.attr("placeholder");
			if(!!text) {
				me.val(text).css({"color":"#999"});
				me.focus(function(){
					if(me.val() == text) me.val('').removeAttr("style");
				});
				me.blur(function(){
					if(me.val() == '') me.val(text).css({"color":"#999"});
				});
			};
		});
	};
})(Modernizr.input.placeholder);

// -- font size adjustment

(function(links){
    var x = links.length;
    while(x--) {
        links[x].clickIdx = x;
        links[x].onclick = function(){
            setStylesheet(this.clickIdx);
        };
    };
    function setStylesheet(x) {
        var hrefStr = "";
        switch(x) {
            case 0: hrefStr = "/styles/fonts-normal.css"; break;
            case 1: hrefStr = "/styles/fonts-large.css"; break;
            case 2: hrefStr = "/styles/fonts-largest.css"; break;
        };
        $("#fontStylesheet").attr("href",hrefStr);
        setPersistentFontSize(hrefStr);
    };
})($(".fontsize"));


function setPersistentFontSize(str) {
    $.ajax({
        type: "POST",
        url: "/services/wsd.asmx/SetFontSize",
        data: "{ fs: '" + str + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json"
    });
};

(function getPersistentFontSize() {
    $.ajax({
        type: "POST",
        url: "/services/wsd.asmx/GetFontSize",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            $("#fontStylesheet").attr("href",msg.d);
        }
    });
})();

// -- right nav hover animation

(function(subNav){
    if(typeof subNav !== "undefined") {
        var elms = subNav.children,
        x = elms.length;      
        
        
        if(ie) {
            var bgObj0 = {backgroundPositionX: "0px"},
            bgObj1 = {backgroundPositionX: "-225px"};
        } else {
            var bgObj0 = {backgroundPosition: "0px 0px"},
            bgObj1 = {backgroundPosition: "-225px 0px"};
        };
        
        while(x--) {           
            var link = elms[x].children[0],
            subLinks = elms[x].children[1].children,
            i = subLinks.length,
            linkColor = "";
            switch(x) {
                case 0: linkBG = "url('/images/orangeRollover.gif') -225px 0 no-repeat"; break;
                case 1: linkBG = "url('/images/greenRollover.gif') -225px 0 no-repeat"; break;
                case 2: linkBG = "url('/images/blueRollover.gif') -225px 0 no-repeat"; break;
            };
            link.onmouseover = function(){
                this.style.backgroundImage = "url(/images/greyRollover.gif)";
                $(this).stop().animate(bgObj0);
            };
            link.onmouseout = function(){
                $(this).stop().animate(bgObj1,function(){
                     this.removeAttribute("style");
                }); 
            };
            while(i--) {
                var subLink = subLinks[i].children[0];
                subLink.linkBG = linkBG;
                subLink.onmouseover = function(){
                    this.style.background = this.linkBG;
                    $(this).stop().animate(bgObj0,function(){
                        this.style.color = "white"
                   });
                };
                subLink.onmouseout = function(){
                    this.style.color = "#666"
                    $(this).stop().animate(bgObj1,function(){
                       this.removeAttribute("style");
                    });
                };
            };
        };
    };    
})(document.getElementById("subNav") || undefined);



