var isTextOnly = false;
var textSize;

function init(domain) {
	$(document).ready(function(){
		textSize = Number($("body").css("font-size").replace(/px/, ''));
		switchStyle("set");
		setTextSize();
		togAccessability("");
		
		if (!isTextOnly) {
			// fix pngs
			//$('ELEMENT(s) HERE').ifixpng();
			
			/* for superfish with first level images*/
			/*
			$(".nmc").hover(
				function () {
					if (typeof($)=="function") {
						$(this).addClass("hover");
					}
				},
				function () {
					if (typeof($)=="function") {
						$(this).removeClass("hover");
					}
				}
			);
			*/
			
			$("#superfish").superfish({
				animation : {opacity:"show",height:"show"},
				delay : 1000,
				disableHI	: true
			});
			
			$("#superfish").show();
		}
		/* 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;
		});
		
		$("#access-buttons a").click(function(){
			if ($(this).text().substr(0,4) == "hide") {
				hideAccessability("slow");
			} else {
				showAccessability("slow");
			}
			this.blur();
			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();
	}
}

function showAccessability(spd) {
	$.cookie('accessabilityToggle', "on");
	togAccessability(spd);
}

function hideAccessability(spd) {
	$.cookie('accessabilityToggle', "off");
	togAccessability(spd);
}

function togAccessability(spd) {
	var value = $.cookie('accessabilityToggle');
	if (spd == "") spd = 0;
	if (value == "on") {
		$("#accessibility").slideDown(spd, function(){$("#access-buttons a").text($("#access-buttons a").text().replace(/show /, 'hide '));});
	} else {
		$("#accessibility").slideUp(spd, function(){$("#access-buttons a").text($("#access-buttons a").text().replace(/hide /, 'show '));});
	}
}

// from other script.js file.
function mmLoadMenus() {  // removed as it was giving issues in the menu.
/*  if (window.mm_menu_0000_0) return;
  window.mm_menu_0000_0 = new Menu("root",200,18,"Verdana, Helvetica, sans-serif",12,"#d4a464","#E3C6A1","#A13B2F","#700517","left","middle",10,0,200,-5,27,true,true,true,0,true,true);
   mm_menu_0000_0.addMenuItem("Organisational&nbsp;Chart&nbsp;","location='../organisational.asp'");
   mm_menu_0000_0.addMenuItem("Memorandum&nbsp;","location='../memorandum.asp'");
   mm_menu_0000_0.addMenuItem("Cittaslow&nbsp;Poem&nbsp;","location='../poem.asp'");
   mm_menu_0000_0.hideOnMouseOut=true;
   mm_menu_0000_0.bgColor='#d8d8c2' alpha=70%;
   mm_menu_0000_0.menuBorder=1;
   mm_menu_0000_0.menuLiteBgColor='#d8d8c2';
   mm_menu_0000_0.menuBorderBgColor='#a03c03';
   mm_menu_0000_0.writeMenus();  */
}
// mmLoadMenus()