var isTextOnly = false;
var textSize;

function init(domain) {
	$(document).ready(function(){
		textSize = Number($("body").css("font-size").replace(/px/, ''));
		switchStyle("set");
		setTextSize();
		
		if (!isTextOnly) {
			// fix pngs
			//$('ELEMENT(s) HERE').ifixpng();
			$("ul.nav li").hover(
				function () {
					if (typeof($)=="function") {
						//$(this).addClass("hover");
						$(this).css("background-image", $(this).css("background-image").replace(/\.jpg/, '_over.jpg'));
					}
				},
				function () {
					if (typeof($)=="function") {
						//$(this).removeClass("hover");
						$(this).css("background-image", $(this).css("background-image").replace(/_over\.jpg/, '.jpg'));
					}
				}
			);
		}
		/* for superfish with first level images*/
		/*
		$(".nmc").each(function(){
			$(this).click(function(){
				document.location.href=$(this).attr("rel");
			}).attr("rel", $(this).text()).text("");	
		});
		*/
		
		$('#styletoggle').click(function(){
			switchStyle("change");
			return false;
		});
		
		$('#textSizeDn').click(function(){
			textSizeDn();
			return false;
		});
		
		$('#textSizeUp').click(function(){
			textSizeUp();
			return false;
		});
		
		$('#textSizeNorm').click(function(){
			textSizeNorm();
			return false;
		});
	});
		
}
	
var newWin;
function popUp(page,name,details){
	newWin=window.open(page,name,details);
	newWin.focus();
	return false;
}

function initLB() {
	if (self.innerWidth) {
		frameWidth = self.innerWidth;
		frameHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientWidth) {
		frameWidth = document.documentElement.clientWidth;
		frameHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		frameWidth = document.body.clientWidth;
		frameHeight = document.body.clientHeight;
	}
	
	var imageResizeWidth = frameWidth - 20;
	//var imageResizeHeight = frameHeight - 80;
	var imageResizeHeight = Math.floor((frameHeight - 80) * 0.85);
	var imageResizeLoaded = false;
	
	for ( i = 0; i < document.links.length; i++ ) {
		if ( document.links[i].href.slice(-15) == '{IMAGERESIZEQS}' ) {
			var href = document.links[i].href
			href = href.slice(0, href.length - 15);
			document.links[i].href = href + "&width="+imageResizeWidth + "&height="+imageResizeHeight;
		}
	}
	imageResizeLoaded = true;
	
	var lightboxes = new Object();
	$('a[rel^="lightbox"]').each(function() {
		lightboxes[$(this).attr("rel")] = 1;
	});
	
	for (var key in lightboxes) {
		$('a[rel="'+key+'"]').lightBox({
			overlayBgColor: '#0A1A30',
			containerResizeSpeed: 600,
			disabled: isTextOnly
		});
	}
}

// ********** Supporting Functions **********
		
function textSizeUp(){
	var ts = Number($.cookie('textSize'))+1;
	if (ts > 3) {ts = 3}
	$.cookie('textSize', String(ts))
	setTextSize(ts);
}

function textSizeDn(){
	var ts = Number($.cookie('textSize'))-1;
	if (ts < -3) {ts = -3}
	$.cookie('textSize', String(ts))
	setTextSize(ts);
}

function textSizeNorm(){
	$.cookie('textSize', "0")
	setTextSize(0);
}

function setTextSize() {
	var ts = $.cookie('textSize');
	if (ts == "") {ts = "0"}
	ts = Number(ts)
	$("body").css("font-size", String((ts*2)+textSize)+"px");
}

function switchStyle(what) {
	var c = $.cookie('styletoggle');
	
	c = (c == "" || c == null) ? c = "styled" : c;
	
	if (what == "change") {
		c = c == "plain" ? "styled" : "plain";
	}
	
	$.cookie('styletoggle', c);
	
	if (what == "change") {
		document.location.href = document.location.href;
	} else {
		var plain = c == "plain";
		
		if (plain) {
			// nav menu
			var TO_menu = $("ul.nav-abs-top").clone().attr("id", "menu_TO").attr("style","").attr("class","").prepend("<li><a href='"+domain+"'>Home</a></li>");
			TO_menu.find("div.nmc").remove();
			TO_menu.find("ul").each(function(){
				$(this).attr("style","").attr("class","");
			});
			TO_menu.prependTo("div#header");
			$("ul.nav-abs-top").hide();
			$("#styletoggle").text("turn off "+$("#styletoggle").text());
			$("head").append('<link href="css/textonly.css" rel="stylesheet" type="text/css" />');
		} else {
			// nav menu
			$("#menu_TO").remove();
			$("ul.nav-abs-top").show();
			$("#styletoggle").text($("#styletoggle").text().replace(/turn off /, ''));
			$('link[@href$="textonly.css"]').remove();
		}
		
		isTextOnly = plain;
		
		initLB();
	}
}