$(document).ready(function() {
    /*Set thickbox Opacity Style*/
    $(".thickbox").click(function() {
        $("#TB_overlay").css("-moz-opacity", "0.50");
        $("#TB_overlay").css("opacity", "0.50");

        $("#TB_HideSelect").css("-moz-opacity", "0");
        $("#TB_HideSelect").css("opacity", "0");

        $("#TB_iframeContent").css("_margin-bottom", "1px");        
    });
    // Navigation :: Primary Nav
    /* 	if(window.XMLHttpRequest=='function(){return new ActiveXObject("Microsoft.XMLHTTP")}') {
    $("#menu li").hover(
    function() { $("ul", this).slideDown("fast"); }, 
    function() {  }
    );
    } else {
    $("#menu li ul").fadeTo("normal", 0);
    $("#menu li").hover(
    function() { $("ul", this).fadeTo(400, 1).css("display", "block"); }, 
    function() { $("ul", this).fadeTo(1, 0).css("display", "none"); }
    );
    } */

    if (window.XMLHttpRequest == 'function(){return new ActiveXObject("Microsoft.XMLHTTP")}') {
        $("#menu li").hover(
			function() {
			    $("ul", this).css("display", "none");
			    $("ul", this).slideDown("fast");
			},
			function() {
			}
		);
    } else {
        /* $("#menu li ul").fadeTo("normal", 0); */
        $("#menu li").hover(
			function() {
			    $("ul", this).css("display", "none");
			    $("ul", this).slideDown("fast");
			},
			function() {
			}
		);
    }

    if (document.all) {
        $("#menu li").hoverClass("hover");
    }

    // Accessible Target links
    $(".external-window").click(function(e) {
        // Abort if a modifier key is pressed
        if (e.shiftKey || e.altKey || e.ctrlKey || e.metaKey) {
            return true;
        } else {
            // Change "_blank" to something like "newWindow" to load all links in the same new window
            var newWindow = window.open(this.getAttribute('href'), '_blank');
            if (newWindow) {
                if (newWindow.focus) {
                    newWindow.focus();
                }
                return false;
            }
            return true;
        }

    });
    $(".external-window").attr("title", "Link opens in an external window");

    /*
    ** ClientName Specific Components
    */
    if (document.getElementById("infoCarousel")) {
        $('#infoCarousel').jcarousel({
            orientation: "vertical",
            itemScroll: 1,
            itemVisible: 1,
            autoScroll: 6,
            autoScrollStopOnMouseover: true,
            autoScrollResumeOnMouseout: true,
            noButtons: true,
            wrap: true,
            wrapPrev: true,
            initCallback: assignControlsBehaviour,
            scrolledCallback: scrolled
        });

        /* var carouselObj; */
        //resize all the div.img's
        $("div#infoCarousel li").each(
			function(i) {
			    w = parseInt($(this).find("img").attr("width")) + 2;
			    $(this).children(".img").width(w + "px");
			}
		);
    }

    function assignControlsBehaviour(publ) {
        carouselObj = publ;
        //build the controls list
        var code = "";
        var counter = 0;
        $("#infoCarousel ul li").each(
			function(i) {
			    num = i + 1;
			    counter++;
			    if (i == 0) {
			        code = code + "<li class='asdf first one'><a href='#'>" + num + "</a></li>";
			    }
			    else {
			        code = code + "<li class='asdf'><a href='#'>" + num + "</a></li>";
			    }
			}
		);
        $("div.controls ul").append(code);
        $(this).log(code);

        //assign behaviour to the controls list
        var num;

        $(".scroller div.controls li a").click(
			function() {
			    $(".scroller div.controls li").removeClass("selected");
			    num = parseInt($(this).text());

			    animateScrollerInfo(num, carouselObj);

			    $(this).parent().addClass("selected");
			    return false;
			}
		);
        //add scrolling behaviour for the numbers
        var sh = parseInt($("div.controls li").height());
        $(".scroller div.controls img.controls-next").click(
			function() {
			    max = (counter - 5) * sh * -1;
			    t = parseInt($("div.controls ul").css("top"));
			    nt = t - sh;

			    if (nt >= max) {
			        $("div.controls ul").animate({ top: nt });
			    }
			    //move the first class
			    el = $("div.scope ul li.first");
			    if (el.next()) {
			        el.next().addClass("first");
			        el.removeClass("first");
			    }

			    $('div.scope ul li').each(function() {
			        if ($(this).attr("class") == "asdf one selected" || $(this).attr("class") == "asdf selected") {
			            num = parseInt($(this).text());
			            num = num + 1;
			            if (num >= 6) {
			                num = 1;
			            }

			            $(".scroller div.controls li").removeClass("selected");
			            animateScrollerInfo(num, carouselObj);
			            $(this).parent().addClass("selected");
			            return false;
			        }
			    });
			    
			}
		);
        $(".scroller div.controls img.controls-prev").click(
			function() {

			    max = 0;
			    t = parseInt($("div.controls ul").css("top"));
			    nt = t + sh;

			    if (nt <= max) {
			        $("div.controls ul").animate({ top: nt });
			    }
			    el = $("div.scope ul li.first");
			    if (el.prev()) {
			        el.prev().addClass("first");
			        el.removeClass("first");
			    }

			    $('div.scope ul li').each(function() {			        
			        if ($(this).attr("class") == "asdf one selected" || $(this).attr("class") == "asdf selected") {			            
			            num = parseInt($(this).text());
			            num = num - 1;
			            if (num <= 0) {
			                num = 5;
			            }

			            $(".scroller div.controls li").removeClass("selected");
			            animateScrollerInfo(num, carouselObj);
			            $(this).parent().addClass("selected");
			            return false;
			        }
			    });
			}
		);
        //stop auto scrolling when hovering over controls
        $(".scroller div.controls").hover(
			function() {
			    carouselObj.stopAuto();
			},
			function() {
			    carouselObj.startAuto();
			}
		);
    };

    function animateScrollerInfo(num, publ) {
        $(this).log("scroll to: " + num);
        //stop the carousel auto scrolling
        publ.stopAuto();
        //scroll to the selected number
        publ.scroll(num);

    };

    function scrolled(num) {
        //find which info li is showing
        // == num
        //update the selected class to the corresponding control li
        $(".controls li").each(
			function(i) {
			    if (num == i + 1) {
			        $(this).addClass("selected");
			    }
			    else {
			        $(this).removeClass("selected");
			    }
			}
		);
        //if the li is not on the page, scroll the numbers
        var first = parseInt($(".controls ul li.first a").text());
        if (num < first || num > first + 4) {
            $(this).log("corresponding number not showing");
            //scroll to make the item show at bottom
            var h = parseInt($(".controls ul li").height());
            var curr = parseInt($(".controls ul").css("top"));
            var diff = (first + 4) - num;
            var scrollTo = h * diff + curr;
            $(this).log("st: " + scrollTo);
            if (num > first + 4) {
                $(".controls ul").animate({ top: scrollTo });
                $(".controls ul li.first").removeClass("first").next().addClass("first");
            }
            else //it'll scroll back to the first item
            {
                $(this).log("going up");
                $(".controls ul").animate({ top: 0 });
                $(".controls ul li.first").removeClass("first");
                $(".controls ul li.one").addClass("first");
            }

        }
        else {
            $(this).log("number is showing");
        }
    };

    /*expand list click*/
    $("ul.expand-list a").click(
		function(i) {
		    if (!($(this).parents("ul").attr("class").match("submenu"))) {
		        if ($(this).parent("li").attr("class")) {
		            $(this).siblings().slideUp("slow", function() { $(this).parent("li").toggleClass("selected"); });
		        }
		        else {
		            $(this).parent("li").toggleClass("selected");
		            $(this).siblings().css("display", "none");
		            $(this).siblings().slideDown("slow");
		        }
		        return false;
		    }
		}
	);

    /*swfir img replacement on content images*/
    /*content images*/
    var borders1 = new swfir();
    borders1.specify("border-radius", "3");
    borders1.specify("border-color", "#E8E8E8");
    borders1.specify("border-width", "1");
    borders1.specify("shadow-color", "#fff");
    borders1.specify("shadow-blur", "0");
    borders1.specify("shadow-offset", "0");
    borders1.specify("rotate", "0");
    borders1.swap("div.main-column img.swfir");

    $(".content .main-column h1").each(
	    function(i) {
	        if ($(this).parent().attr("class")) {
	            if (!($(this).parent().attr("class").match("page-info"))) {
	                $(this).addClass("sifr");
	            }
	        }
	    }
	);

    //sIFR.replaceElement("h1.sifr", named({ sFlashSrc: "basemedia/sifr/Myriad-Pro-light.swf", sColor: "#000000", sWmode: "transparent" }));

});

/*
	** Global functions
*/
$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover( 
			function() { $(this).addClass(c);  },
			function() { $(this).removeClass(c); }
		);
	});
};

jQuery.fn.vjustify=function() {
    var maxHeight=0;
    this.each(function(){
        if (this.offsetHeight>maxHeight) {maxHeight=this.offsetHeight;}
    });
    this.each(function(){
        $(this).height(maxHeight + "px");
        if (this.offsetHeight>maxHeight) {
            $(this).height((maxHeight-(this.offsetHeight-maxHeight))+"px");
        }
    });
};

jQuery.fn.log = function (msg) {
	/* console.log("%s: %o", msg, this);  */
	return this;
};

function enableJSClass()
{
	document.body.className = "jsEnabled";
}

function printPage() { 
	if (window.print) { 
		window.print() ; 
	} else { 
		var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>'; 
		document.body.insertAdjacentHTML('beforeEnd', WebBrowser); 
		WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box WebBrowser1.outerHTML = ""; 
	}
}


