function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function popup(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function switch_d(div_id) {
		if (document.getElementById(div_id).style.display=="block")
		{
			document.getElementById(div_id).style.display="none";
		}
		else 
		{
			document.getElementById(div_id).style.display="block";
		}
}

function flip(div_id) {
	if (document.getElementById(div_id)) 
	{
		if (document.getElementById(div_id).style.display=="inline")
		{
				document.getElementById(div_id).style.display="none";
		}
		else 
		{
			document.getElementById(div_id).style.display="inline";
		}
	}
}

function close_d(div_id) {
document.getElementById(div_id).style.display="block";
}

function previewPopup(template,pg_id)
{
	window.open('../'+template+'?pg_id='+pg_id,'page_preview');
}

function setBackground(to_opacity,speed,wait) {
	wait_time=0;
	duration_time=0.2;
	if (wait>0) { wait_time=wait; }
	if (speed>0) { duration_time=speed; }
	obj=document.getElementById('background_panel');
	if (to_opacity==1) { to_opacity=0.99; }
	new Effect.Opacity(obj, { delay: wait_time, duration: duration_time, to: to_opacity });
}

function set_div(div_id,html) {
	document.getElementById(div_id).innerHTML=html;
}

function clr_div(div_id) {
	document.getElementById(div_id).innerHTML='';
}

function open_site(zone){
	window.open('http://www.jamespfaff.com/site.php?zone='+zone+'&size='+screenSize,'jamespfaff'+zone,'toolbar=no,menubar=no,scrollbars=no,location=no,width='+screenW+',height='+screenH+',left=0,top=0');  
}

var sub_open=0;
var parent_open=0;
var sub_parent=0;

function sub_click(id,parent) {
	if (sub_open!=0) {
		if (document.getElementById('menu_text'+sub_open)) {
			document.getElementById('menu_text'+sub_open).style.display='none';
		}
	}
	if (document.getElementById('menu_text'+id)) {
		document.getElementById('menu_text'+id).style.display='block';
		if (document.getElementById('menu_text'+parent)) {
			document.getElementById('menu_text'+parent).style.display='none';
		}
	}
	sub_open=id;
	sub_parent=parent;
}

function parent_click(id) {
	if (parent_open!=0) {
		document.getElementById('menu_text'+parent_open).style.display='none';
	}
	if (id!=sub_parent) {
		if (document.getElementById('menu_text'+id)) {
			document.getElementById('menu_text'+id).style.display='block';
		}
	}
	parent_open=id;
	if (sub_parent!=parent_open) {
		if (document.getElementById('menu_text'+sub_open)) {
			document.getElementById('menu_text'+sub_open).style.display='none';
			sub_open=0;
		}
	}
}

//NAVIGATION
var allPageTags = new Array(); 

function showSub(nav_id,sub_n) {

	var allPageTags=document.getElementsByTagName("*");
	//Cycle through the tags using a for loop
	for (i=0; i<allPageTags.length; i++) {
	//Pick out the tags with our class name
		if ((allPageTags[i].id.indexOf('menu_sub')!=-1) && (allPageTags[i].id!='menu_sub'+nav_id)) {
		//Manipulate this in whatever way you want
			Effect.BlindUp(allPageTags[i].id, {duration: 0.2}, {queue: 'end'});
			up_id=allPageTags[i].id.replace('menu_sub','');
			nav[up_id]='up';
		}
	}

	//open new details
	if (sub_n>0) {
		if (document.getElementById('menu_sub'+nav_id).style.display=='none') {
			Effect.BlindDown('menu_sub'+nav_id, {duration: 0.2*sub_n}, {queue: 'end'});
			nav[nav_id]='down';
		} else {
			Effect.BlindUp('menu_sub'+nav_id, {duration: 0.2*sub_n}, {queue: 'end'});
			nav[nav_id]='up';
		}
	}
}


function setNav(pg_id,value) {
	nav[pg_id]=value;
}

function showNavStatus() {
	msg='';
	for (i=1; i<navstate.length; i++) {
		msg=msg+i+':'+navstate[i]+' ';
	}
	alert (msg);
}

var navstate = new Array();
var navparent = new Array();
var navsub = new Array();
var navid = new Array();
var navaccess = new Array();
var navtemplate = new Array();
var navbg= new Array();
var navcollapse= new Array();
var thispage = 0;
var subopen = 0;
var wasopen = -1;
var blindspeed = 0.2;
var showinitialbtns=1;

function navInit(pg_id) {
	thispage=pg_id;
	navstate[pg_id]='down';
	if (navparent[pg_id]!=0) {
		navstate[navparent[pg_id]]='down';
	}
	if ((navparent[pg_id]==0) && (navsub[pg_id]>0)) { subopen=pg_id; }
	if (navsub[navparent[pg_id]]>0) { subopen=navparent[pg_id]; }
}

function navOver(pg_id) {

	if (navparent[pg_id]==0) { level='top'; } else { level='sub'; }
	newclass='over';
	if (navstate[pg_id]=='down') {
		newclass='down_over';
	}
	navstate[pg_id]=newclass;
	newclass="menu_link_"+level+"_"+newclass;
	div_id='menu_'+level+pg_id;
	document.getElementById(div_id).className=newclass;
	//top.frames['nav'].document.getElementById(div_id).className=newclass;

	showStates();
}

function navOut(pg_id) {
	if (navparent[pg_id]==0) { level='top'; } else { level='sub'; }
	newclass='up';
	if ((navstate[pg_id]=='down_over') || (navstate[pg_id]=='down')) {
		newclass='down';
	}
	navstate[pg_id]=newclass;
	newclass="menu_link_"+level+"_"+newclass;
	div_id='menu_'+level+pg_id;
	document.getElementById(div_id).className=newclass;

	showStates();
}

function navClick(pg_id,parent,move,show,size) {
	
	if (showinitialbtns==1) {
		hideInitialBtns();
	}
	
	allUp(pg_id);
	
	if (navparent[pg_id]==0) {
		//top level
		if (pg_id!=subopen) {
			closeSub();
			subopen=0;
		}

		if ((navsub[pg_id]>0) && (show==1) && (wasopen!=pg_id)) {
			openSub(pg_id);
		}
		document.getElementById('menu_top'+pg_id).className="menu_link_top_down";
		
		parent_text='';sub_text='';
		if (document.getElementById('menu_text'+pg_id)) {
			parent_text=document.getElementById('menu_text'+pg_id).innerHTML;
		}
		if (parent_text=='') {
			parent_text='&nbsp;';
		}
		document.getElementById('inside_menu_text').innerHTML=parent_text;

	} else {
		//sub
		document.getElementById('menu_sub'+pg_id).className="menu_link_sub_down";
		if (navparent[pg_id]!=subopen) {
			openSub(navparent[pg_id]);
		}
		document.getElementById('menu_top'+navparent[pg_id]).className="menu_link_top_down";
		navstate[navparent[pg_id]]='down';											 
		
		parent_text=''; sub_text='';
		if (document.getElementById('menu_text'+parent)) {
			parent_text=document.getElementById('menu_text'+parent).innerHTML;
		}
		if (document.getElementById('menu_text'+pg_id)) {
			sub_text=document.getElementById('menu_text'+pg_id).innerHTML;
		}
		
		if (sub_text!='') {
			document.getElementById('inside_menu_text').innerHTML=sub_text;
		} else {
			if (parent_text=='') {
				parent_text='&nbsp;';
			}
			document.getElementById('inside_menu_text').innerHTML=parent_text;
		}

	}

	navstate[pg_id]='down';
		
	if (move==1) {
		//switch pages
		if (navaccess[pg_id]==1) {
			top.frames['mainFrame'].location.href=navtemplate[pg_id]+'?pg_id='+pg_id+'&size='+size+'&screen='+screenWidth+'-'+screenHeight;
		} else {
			if (navbg[pg_id]!='') {
				bg_srcs=navbg[pg_id].split(',');
				bg_n=bg_srcs.length;
				bg_src=bg_srcs[Math.floor(Math.random() * (bg_n))];
				top.frames['mainFrame'].location.href='image_new.php?size='+size+'&screen='+screenWidth+'-'+screenHeight+'&bg_src='+bg_src;
			}
		}
	}

	if (navcollapse[pg_id]==1) {
		collapse(pg_id);
	}
	
showStates(pg_id);
}

function closeSub() {
if ((subopen!=0)) {
		Effect.BlindUp('submenu'+subopen, {duration: blindspeed}, {queue: 'end'});
	}
	wasopen=subopen;
}

function openSub(pg_id) {
	if (subopen!=pg_id) {
		Effect.BlindDown('submenu'+pg_id, {duration: blindspeed*2}, {queue: 'end'});
		wasopen=subopen;
		subopen=pg_id;
	}
}

function allUp(pg_id) {
	for (i=0; i<navstate.length; i++) { 
		if ((navid[i]>0) && (navparent[pg_id]!=i)) {
			if (navparent[i]==0) { level='top'; } else { level='sub'; }
			document.getElementById('menu_'+level+i).className='menu_link_'+level+'_up';
			navstate[i]="up"; 
		}
	}
}

function collapse(pg_id) {
	closeSub();
	subopen=0;
}

function navSet(pg_id,parent,sub_n,access,template,bg_src,collapse) {
	navstate[pg_id]='up';
	navparent[pg_id]=parent;
	navsub[pg_id]=sub_n;
	navid[pg_id]=pg_id;
	navaccess[pg_id]=access;
	navtemplate[pg_id]=template;
	navbg[pg_id]=bg_src;
	navcollapse[pg_id]=collapse;
}

function navUpdate(pg_id) {
	if (parent.nav) {
		parent.nav.navClick(pg_id,0,0);
	} else { //alert ('nav not loaded yet'); 
	}
	if (document.getElementById('search')) { document.getElementById('search').focus(); }
}

function goTo(pg_id) {
	parent.nav.navClick(pg_id,1,0);
}

function showStates(pg_id) {
return;
for (i=0; i<navstate.length; i++) { 
		if (document.getElementById('state'+i)) {
			document.getElementById('state'+i).innerHTML='['+i+':'+navstate[i]+'] ';
		}
	}
	document.getElementById('subopen').innerHTML='[subopen:'+subopen+'] [wasopen:'+wasopen+'] [pg_id:'+pg_id+']';
}

function hideInitialBtns() {
	showinitialbtns=0;
	document.getElementById('index_btn_container').style.display='none';
	document.getElementById('home_footer_container').style.display='none';
}

function show_birds_eye(src,img_w,img_h) {
	if (img_w>img_h) {
		w=screenWidth-80;
		h=(w/img_w)*img_h;
		if (h>720) {
			h=720;
			w=(h/img_h)*img_w;
		}
		img='<img src="'+src+'" width="'+w+'">';
	} else {
		h=screenHeight-80;
		if (h>720) {
			h=720;
		}
		img='<img src="'+src+'" height="'+h+'">';
	}
	document.getElementById('birds_eye_image_container').innerHTML='<table cellspacing="0" cellpadding="0" border="0" width="100%" height="100%"><tr valign="middle"><td align="center">'+img+'</td></tr></table>';
	document.getElementById('birds_eye_image_container').style.display='block';
}

function clr_birds_eye() {
	document.getElementById('birds_eye_image_container').style.display='none';
	document.getElementById('birds_eye_image_container').innerHTML='&nbsp;';
}

function pfaff_start(index) {
	if (index==1) {
		document.getElementById('birds_eye_container').style.height=(screenHeight-80)+'px';
		document.getElementById('birds_eye_wrapper').style.display='block';
	}
}

function pfaff_open(zone) {
	if (zone=='home') {
		top.location.href='http://www.jamespfaff.com/index.php';
	} else {
		top.location.href='http://www.jamespfaff.com/inside.php?zone='+zone+'&size='+screenSize;  
	}
}

function pfaff_maximise() {
   window.resizeTo(screen.availWidth,screen.availHeight);
   window.moveTo(0,0);
}

function show_hidden(target) {
	target.style.visibility='visible';
}

function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	if (windowHeight==0) {
		windowHeight=screen.availHeight;
	}
	if (windowHeight==0) {
		windowHeight=screen.height;
	}
	return windowHeight;
}

function getWindowWidth() {
	var windowWidth = 0;
	if (typeof(window.innerWidth) == 'number') {
		windowWidth = window.innerWidth;
	}
	else {
		if (document.documentElement && document.documentElement.clientWidth) {
			windowWidth = document.documentElement.clientWidth;
		}
		else {
			if (document.body && document.body.clientWidth) {
				windowWidth = document.body.clientWidth;
			}
		}
	}
	if (windowWidth==0) {
		windowWidth=screen.availWidth;
	}
	if (windowWidth==0) {
		windowWidth=screen.width;
	}
	return windowWidth;
}

function pageWidth() {return window.innerWidth != null? window.innerWidth: document.body != null? document.body.clientWidth:null;}
function pageHeight() {return window.innerHeight != null? window.innerHeight: document.body != null? document.body.clientHeight:null;}


function gallery_focus(id) {
	$("#thumbnail_image_"+id).attr('class', 'thumbnail_image_over');
}

function gallery_blur(id) {
	if (id!=thumb_current) {
		$("#thumbnail_image_"+id).attr('class', 'thumbnail_image');
	}
}

function gallery_prev() {
	id=thumb_current-1;
	if (id<0) {
		id=thumb_n-1;
	}
	prev=1;
	gallery_goto(id);
}

function gallery_next() {
	id=thumb_current+1;
	if (id>(thumb_n-1)) {
		id=0;
	}
	gallery_goto(id);
}

function gallery_goto(id) {
	$("#gallery_container").trigger("slideTo",id);
	if (thumb_current!=id) {
		$("#thumbnail_image_"+thumb_current).attr('class', 'thumbnail_image');
	}
	$("#thumbnail_image_"+id).attr('class', 'thumbnail_image_over');
	if (thumb_max<thumb_n) {
		if (id==thumb_last) {
			$("#thumbnails_container").trigger("next",1);
			thumb_first++;
		} else if ((id==thumb_first) && (moved==1)) {
			$("#thumbnails_container").trigger("prev",1);
			thumb_first--;
		} else if ((thumb_current==thumb_first) && (prev==1)) {
			$("#thumbnails_container").trigger("prev",1);
			thumb_first--;
		}
		
		if (thumb_first<0) {
			thumb_first=thumb_n-1;
		}
		if (thumb_first==thumb_n) {
			thumb_first=0;
		}
		thumb_last=thumb_first+thumb_max-1;
		if (thumb_last>(thumb_n-1)) {
			thumb_last=thumb_last-(thumb_n);
		}
	}
	if (thumb_current!=id) {
			moved=1;
	}
	thumb_current=id;
	prev=0;
//	alert ('clicked:'+id+' thumb_max:'+thumb_max+' thumb_n:'+thumb_n+' thumb_current:'+thumb_current+' thumb_first:'+thumb_first+' thumb_last:'+thumb_last);
//   	moved=1;
	return false; 
}


