var holder;
var scrollbar=null;
var to=null;
var Photos=null;
var CH=null;
var scrollingTO=null;

function PageStart(){
	$('fore').hide();
	LoadMenu();
	LoadPage(startPage);
	Cufon('#copyright');
	scrollbar = new Control.ScrollBar('main','scrollbar_track');
	setTimeout("CheckHash();",250);
}

function LoadMenu(){
	var request=new Ajax.Request('./jax.php', {
		method: 'get',
		parameters: {},
		onSuccess: function(transport, json){
			if (transport['responseText']){
				var items=transport['responseText'].evalJSON();
			}
			else{
				var items={"list":[{"id":1,"name":"Home","lType":0},{"id":2,"name":"News","lType":0},{"id":3,"name":"Shows","lType":0},{"id":4,"name":"About","lType":0},{"id":5,"name":"Media","lType":1},{"id":6,"name":"Releases","lType":1},{"id":7,"name":"Photos","lType":1},{"id":8,"name":"Contact","lType":0},{"id":9,"name":"Links","lType":0}]};
			}
			KillChildren('menu');
			var total=items.list.length;
			var i=0;
			while (i<total)
			{
				var tmp=new Element('a',{href:'?p='+items['list'][i]['id'],id:'link'+items['list'][i]['id']}).update(items['list'][i]['name']);
				tmp['curVal']=new Object();
				tmp['curVal']['p']=items['list'][i]['id'];
				tmp['curVal']['t']=items['list'][i]['lType'];
				tmp['curVal']['n']=items['list'][i]['name'];
				tmp['onClick']=function(){LoadPage(this['curVal']);return false;};
				tmp['onclick']=function(){LoadPage(this['curVal']);return false;};
				$('menu').appendChild(tmp);
				var tmp=new Element('br');
				$('menu').appendChild(tmp);
				i++;
			}
			Cufon('#menu');

		}
	});
}

function LoadPage(page){
	Params=new Object();
	Params['m']=page['p'];
	if (page['g'])
	{
		Params['g']=page['g'];
	}
	if (page['o'])
	{
		Params['o']=page['o'];
	}
	if (page['f'])
	{
		Focus=page['f'];
	}
	var request=new Ajax.Request('./jax.php',{
		method: 'get',
		parameters: Params,
		onSuccess: function(transport, json){
			
			var tmp=transport['responseText'].evalJSON();
			KillChildren('main');			
			if (tmp['links'])
			{
				var links=tmp['links'];
				var total=links.length;
				var i=0;
				/*while (i<total)
				{
					if (links[i]['more'])
					{
						var m=new Element('a',{id:'More'+page}).update("More");
						//m['onClick']=Loa
					}
					i++;
				}*/
			}
			
			var items=tmp['items'];
			var total=items.length;
			var head="h1"
			if (tmp['title'])
			{
				var title=new Element("h1",{id:'H1'+page['p']}).update(tmp['title']);
				$('main').appendChild(title);
				head="h2";
			}
			if (page['t']==2)
			{
				if (head=="h1")
				{
					var title=new Element("h1",{id:'H1'+page['p']}).update(page['n']);
					$('main').appendChild(title);
					if (tmp['description']){
						var Desc=new Element("div",{id:'DESC'+page['p']}).update(tmp['description']);
						$('main').appendChild(Desc);
					}
					title.curVal=new Object();
					title.curVal['p']=page['p'];
					title.curVal['t']=1;
					//alert($('link'+page['p'])
					title.curVal['n']=$('link'+page['p']).curVal['n'];
					title.onclick=function(){LoadPage(this.curVal);};
					title.style.cursor="pointer";
					head="h3";
				}
				var i=0;
				Photos=new Array();
				while (i<total)
				{
					var D=new Element('div',{id:'DIV'+items[i]['id']});
					var H=new Element(head,{id:'H'+items[i]['id']}).update(items[i]['title']);
					var I=new Element('img',{id:'IMG'+items[i]['id'],src:items[i]['img']});
					if (items[i]['full'])
					{
						Photos[i]=new Object();
						Photos[i]['full']=items[i]['full'];
						Photos[i]['img']=items[i]['img'];
						Photos[i]['title']=items[i]['title'];
						Photos[i]['id']=items[i]['id'];
						
						D['curVal']=i;
						
						D.onclick=function(){
							ShowPhoto(this['curVal']);
						}
					}
					D['class']='media';
					D.className='media';
					D.appendChild(H);
					D.appendChild(I);
					$('main').appendChild(D);
					i++;
				}				
		
			}
			else if (page['t']==1)
			{
				if (head=="h1")
				{
					var title=new Element("h1",{id:'H1'+page['p']}).update(page['n']);
					$('main').appendChild(title);
					head="h3";
				}
				var i=0;
				while (i<total)
				{
					var D=new Element('div',{id:'DIV'+items[i]['id']});
					var H=new Element(head,{id:'H'+items[i]['id']}).update(items[i]['title']);
					var P=new Element('div',{id:'P'+items[i]['id']}).update(items[i]['body']);
					D.curVal=new Object();
					if (items[i]['embed'])
					{
						D.curVal['embed']=items[i]['embed'];
						D.curVal['title']=items[i]['title'];
						D.curVal['id']=items[i]['id'];
						D.onclick=function(){
							var D=new Element('div',{id:'FULL'+this['curVal']['id'],'class':'mediaFull'});
							var T=new Element('h1',{id:'HEAD'+this['curVal']['id']}).update(this['curVal']['title']);
							var I=new Element('div',{id:'EMB'+this['curVal']['id']}).update(this['curVal']['embed']);
							D.appendChild(T);
							D.appendChild(I);
							LoadFront(D);
						}
					}
					else
					{
						D.curVal['p']=page['p'];
						D.curVal['t']=2;
						D.curVal['n']=page['n']+": "+items[i]['title'];
						D.curVal['g']=items[i]['id'];
						D.onclick=function(){LoadPage(this.curVal);};
					}
					D['class']='media';
					D.className='media';
					D.appendChild(H);
					D.appendChild(P);
					$('main').appendChild(D);
					i++;
				}				
			}
			else
			{
				var i=0;
				while (i<total)
				{
					
					var D=new Element('div',{id:'DIV'+items[i]['id']});
					var H=new Element(head,{id:'H'+items[i]['id']}).update(items[i]['title']);
					holder=items[i];
					var P=new Element('span',{id:'SPAN'+items[i]['id']}).update(holder['bHtml']);//This is breaking IE.
					D.appendChild(H);
					D.appendChild(P);
					if (items[i]['date']!="")
					{
						var P2=new Element('p',{id:'C'+items[i]['id']}).update();
						if (items[i]['name'])
						{
							P2.update("Last Updated : "+items[i]['date']+" by "+items[i]['name']+".");
						}
						else
						{
							P2.update("Last Updated : "+items[i]['date']+".");
						}
						P2['class']='date';
						P2.className='date';
						D.appendChild(P2);
					}					
					$('main').appendChild(D);
	/*				$('menu').appendChild(tmp);
					var tmp=new Element('br');
					$('menu').appendChild(tmp);*/
					i++;
				}
				if (tmp['links'])
				{
					var total=tmp['links'].length;
					var i=0;
					while (i<total)
					{
						if (tmp['links'][i]['more'])
						{
							var M=new Element('div',{id:'MORE'}).update('Older');
							M['curVal']=new Object();
							M['curVal']['p']=page['p'];
							M['curVal']['o']=tmp['links'][i]['more'];
							M['onclick']=function(){LoadPage(this['curVal']);};
							$('main').appendChild(M);
						}
						if (tmp['links'][i]['less']!=undefined)
						{
							var M=new Element('div',{id:'LESS'}).update('Newer');
							M['curVal']=new Object();
							M['curVal']['p']=page['p'];
							M['curVal']['o']=tmp['links'][i]['less'];
							M['onclick']=function(){LoadPage(this['curVal']);};
							$('main').appendChild(M);
						}
						i++;
					}
				}
			}
			Cufon('h1,h2,h3');
			FixScroll(scrollbar);
			FixScroll(scrollbar);
			HideScroll();
			if (Focus!=null)
			{
				setTimeout("Focusser()",100);
			}
		}
	});
}

function Focusser(){
	if (Focus!=null)
	{
		if ($('DIV'+Focus))
		{
			scrollbar.scrollTo($('DIV'+Focus).offsetTop);
		}
		Focus=null;
	}
}

function KillChildren(ToRemove){
	if (typeof(ToRemove) != 'object')
	{
		if ($(ToRemove))
		{
			CurObj=$(ToRemove);
		}
		else
		{
			return 0;
		}
	}
	else
	{
		CurObj=ToRemove;
	}
	if (CurObj && CurObj.hasChildNodes && CurObj.removeChild) {
		while (CurObj.hasChildNodes()) {
			CurObj.removeChild(CurObj.firstChild);
		}
	}
	return CurObj;
}

function FixScroll(s)
{
	if (s)
	{
		s.reset();
		s.recalculateLayout();
		s.scrollBy(0);
	}
}

function ShowScroll(){
	if (scrollbar!=null)
	{
		if (scrollbar.enabled)
		{
			if (to!=null)
			{
				clearTimeout(to);
			}
			$('scrollbar_track').show();
			setTimeout("$('scrollbar_track').show()",150);
		}
	}
}

function HideScroll(){
	if (scrollbar.enabled)
	{
		if (to!=null)
		{
			clearTimeout(to);
		}
		to=setTimeout("$('scrollbar_track').fade({duration:0.15})",750);
	}
}

function LoadFront(showMe){
	$('fore').style.zIndex='2';
	$('fore').show();
	KillChildren('fore');
	
	var BD=new Element('div',{id:'Btm'});
	var Cl=new Element('a',{id:'Cl',href:'#',style:"color:white;"}).update('<h3>Close</h3>');
	Cl['onclick']=function(){HideFore();return false;};
	BD.appendChild(Cl);	
	
	showMe.appendChild(BD);
	//$('player').hide();
	$('fore').appendChild(showMe);
	$('close')['style']['left']=(showMe.offsetLeft)+"px";
	$('close')['style']['top']=(showMe.offsetTop)+"px";
	$('close').show();
	setTimeout('FixPix();',100);
	Cufon('h1,h3,h4');
}

function PageSized(){
	FixScroll(scrollbar);
	FixClose();
	FixPix();
}

function FixPix(){
	var d=$('fore').getElementsByTagName('img');
	var i=0;
	while (i<d.length)
	{
		if (d[i].offsetHeight>($('fore').offsetHeight-300))
		{
			d[i]['style']['height']=($('fore').offsetHeight-300)+"px";
		}
		i++;
	}
}

function HideFore(){
	/*$('fore').fade({duration:0.25});*/
	$('close').hide();
	$('fore').hide();
	//$('player').show();
}

function ShowPhoto(curVal){
	Photo=Photos[curVal];
	var D=new Element('div',{id:'FULL'+Photo['id'],'class':'mediaFull'});

	if ((curVal+1)<(Photos.length))
	{
		NP=Photos[curVal+1];
		var NL=new Element('div',{id:'NL'+NP['id'],'class':'mediaSM','style':'float:right;'});
		NL['curVal']=curVal+1;
		NL['onclick']=function(){
		ShowPhoto(this['curVal']);
		}
		var NH=new Element('h4',{id:'NLH'+NP['id']}).update(NP['title']);
		var NI=new Element('img',{id:'NLIMG'+NP['id'],src:NP['img']});	
		NL.appendChild(NH);
		NL.appendChild(NI);
	}
	else
	{
		var NL=new Element('div',{id:'NL_','class':'spacerSM','style':'float:right;'});
	}
	D.appendChild(NL);
	
	if ((curVal-1)>-1)
	{
		LP=Photos[curVal-1];
		var LL=new Element('div',{id:'LL'+LP['id'],'class':'mediaSM','style':'float:left;'});
		LL['curVal']=curVal-1;
		LL['onclick']=function(){
		ShowPhoto(this['curVal']);
		}
		var LH=new Element('h4',{id:'LLH'+LP['id']}).update(LP['title']);
		var LI=new Element('img',{id:'LLIMG'+LP['id'],src:LP['img']});	
		LL.appendChild(LH);
		LL.appendChild(LI);
	}
	else
	{
		var LL=new Element('div',{id:'LL_','class':'spacerSM','style':'float:left;'});
	}
	D.appendChild(LL);
	
	var T=new Element('h1',{id:'HEAD'+Photo['id']}).update(Photo['title']);
	var I=new Element('img',{id:'FIMG'+Photo['id'],src:Photo['full']});
	I['onclick']=function(){HideFore();return false;};
	D.appendChild(T);
	D.appendChild(I);
	LoadFront(D);
}

function IconGrow(obj)
{
	new Effect.Morph(obj, {
	  style: 'width:60px;height:60px;',
	  duration: 0.15
	});
}

function IconShrink(obj)
{
	new Effect.Morph(obj, {
	  style: 'width:30px;height:30px;',
	  duration: 0.15
	});
}

function FixClose(){
	var tmp=$('fore').getElementsByTagName('div');
	if (tmp.length>0)
	{
		var cur=tmp['0'];
		$('close')['style']['left']=(cur.offsetLeft)+"px";
		$('close')['style']['top']=(cur.offsetTop)+"px";
	}
}

function ParseHash(){
	HideFore();
	var hash=window.location.hash.replace("#","");
	if (hash!="")
	{
		var arr=hash.split(',');
		var tmp=new Object();
		tmp['p']=arr['0'];
		if (arr.length>3)
		{
			tmp['t']=arr['3'];
			tmp['g']=arr['1'];
		}
		else
		{
			if (arr.length>1)
			{
				tmp['f']=arr['1'];
			}
			if (arr.length>2)
			{
				tmp['o']=arr['2'];
			}
		}
		if ($('link'+tmp['p']))
		{
			tmp['n']=$('link'+tmp['p'])['curVal']['n'];
		}
		LoadPage(tmp);
		
	}
}

function CheckHash(){
	if (CH==null)
	{
		CH=window.location.hash;
		if (CH!="")
		{
			ParseHash();
		}
	}
	else
	{
		if (CH!=window.location.hash)
		{
			CH=window.location.hash;
			ParseHash();
		}
	}
	setTimeout("CheckHash()",250);
}

function ClickScroll(amt){
	scrollingTO=setTimeout("ClickScroll("+amt+");",100);
	scrollbar.scrollBy(amt);
}

function ClickScrollOver(){
	if (scrollingTO!=null)
	{
		clearTimeout(scrollingTO)
		scrollingTO=null;
	}
}


