
var tabManager=null;

function init(){
	var n;
	tabManager=new TabManager('activeContent','/media/site/images/front-end/homepage');
	var tabColor=new Array('#94c955','#43b4cf','#c26db2','#f1aa42');
	for(n=0;n<4;n++) {
		tabManager.addTab('tab_'+n+'_0.gif','tab_'+n+'_1.gif','tab_'+n+'_2.gif',tabColor[n]);
	}
	var overlay = document.getElementById('activeContent');
	//alert("manager loaded");
	//overlay.style.display = "absolute";
	overlay.style.position = "absolute";
	var tabCookie = getCookie('tabCookie');
//	if(tabCookie != null){
//		tabManager.tabClicked(tabCookie);
//		return false;	
//	}
}


function TabManager(obId,imgDir){
	var n,tdi;
	this.btnsMoving = false;
	this.ob=document.getElementById(obId);
	this.tabSize=new Array(74,179,40);
	this.wiper=document.getElementById('wiperDiv');
	this.wiperWidth=0;
	this.scrollImagesOk=false;
	this.wiperEndWidth=document.getElementById('rightContent').offsetWidth;
	this.scrollOb=document.getElementById('scrollerTable');
	var sttr=this.scrollOb.getElementsByTagName('tr')[0];
	var tds=sttr.getElementsByTagName('td');
	var len=tds.length;
	for(n=0;n<len;n++){
		tdi=tds[n].innerHTML;
		var newTD=document.createElement('td');
		newTD.innerHTML=tdi;
		sttr.appendChild(newTD);
	}
	this.tabWidth=237;
	this.panelHeight=302;
	this.currentTab=-1;
	this.imgDir=imgDir;
	this.tab=new Array();
	this.ix=0;
	this.timer=false;
	this.scrollTimer=false;
	this.scrollSpeed=0;
	this.scrollX=0;
	this.scrollWidth=0;
	this.moving=false;
	var d=document.getElementById('leftContent');

	this.topLine=document.createElement('div');
	this.topLine.className="whiteLine";
	d.appendChild(this.topLine);

	this.bottomLine=document.createElement('div');
	this.bottomLine.className="whiteLine";
	d.appendChild(this.bottomLine);

	var d=document.getElementById('rightContent');
	this.hint=document.createElement('img');
	this.hint.id="hintImg";
	this.hint.src="/media/site/images/front-end/homepage/scroll_hint.gif";
	this.hintOn=true;
	this.hintOpacity=0;
	this.hintOpacityDir=0.05;
	d.appendChild(this.hint);

	this.imageContainer=document.createElement('div');
	this.imageContainer.id="imageContainerDiv";
	d.appendChild(this.imageContainer);

	this.bottomBackBtn=document.createElement('div');
	this.bottomBackBtn.id="bottomBackBtnDiv";
	this.bottomBackBtn.innerHTML='<img onclick="tabManager.backToSlideshow();return false" src="/media/site/images/front-end/homepage/btn_back_to_slideshow.png" />';
	d.appendChild(this.bottomBackBtn);

	this.topBackBtn=document.createElement('div');
	this.topBackBtn.id="topBackBtnDiv";
	d.appendChild(this.topBackBtn);

	this.altTxtBox=document.createElement('div');
	this.altTxtBox.id="altTxtBoxDiv";
	d.appendChild(this.altTxtBox);

	this.prevBtn=document.createElement('img');
	this.prevBtn.src="/media/site/images/front-end/homepage/btn_prev.gif";
	this.prevBtn.className="prevNextBtn";
	this.prevBtn.onclick=function(){tabManager.showPrevImage()};
	d.appendChild(this.prevBtn);

	this.nextBtn=document.createElement('img');
	this.nextBtn.src="/media/site/images/front-end/homepage/btn_next.gif";
	this.nextBtn.className="prevNextBtn";
	this.nextBtn.onclick=function(){tabManager.showNextImage()};
	d.appendChild(this.nextBtn);

	this.oldIE=false;
	this.isIE=false;
	this.mouseX=0;
	this.mouseY=0;
	this.mouseOverGallery=false;
	this.zooming=false;
	this.imageShowing=false;
	this.zoomStep=0;
	this.totalZoomSteps=16;
	this.pickedImage=0;

	if(document.all){
		this.isIE=true;
		if( (document.all) && (navigator.appVersion.indexOf("MSIE 7.")==-1) )
			this.oldIE=true;
		document.onmousemove=this.mmIE;
	}else{
		document.onmousemove=this.mmDom2;
	}

	this.cachedBigImages=new Array();
	for(var n=0;n<bIArray.length;n++){
		this.cachedBigImages[n]=new Image();
		this.cachedBigImages[n].src=bIArray[n];
	}

	this.setGap(302,0);
	setTimeout('tabManager.startScroller()',50);
}

TabManager.prototype.pickImage=function(imgId,resizeContainer){
	var ix=parseInt(imgId.substring(9,imgId.length));
	var n,t=tabManager;
	var i=document.getElementById('scrollImg'+ix);
	if(!resizeContainer){
		t.startWidth=i.offsetWidth;
		t.startHeight=i.offsetHeight;
		t.startX=t.scrollX;
		for(n=0;n<ix;n++)
			t.startX+=document.getElementById('scrollImg'+n).offsetWidth;
		t.startY=74;
		t.totalZoomSteps=14;
		t.resizingContainer=false;
	}else{
		t.startWidth=t.endWidth;
		t.startHeight=t.endHeight;
		t.startX=t.endX;
		t.startY=t.endY;
		t.totalZoomSteps=5;
		t.resizingContainer=true;
	}
	t.pickedImage=(ix>=t.cachedBigImages.length?ix-t.cachedBigImages.length:ix);

	t.setOpacity(t.scrollOb,0.5);

	var at=t.altTxt[t.pickedImage].split('|');
	if(at.length==3){
		t.setOpacity(t.altTxtBox,0);
		t.altTxtBox.style.display="block";
		t.altTxtBox.innerHTML="<p><strong>Title:</strong> "+at[0]+"<br /><strong>Taken by:</strong> "+at[1]+"<br /><strong>Using a:</strong> "+at[2]+"</p>";
	}

	t.imageContainer.innerHTML='&nbsp;';
	t.setOpacity(t.imageContainer,(resizeContainer?1:0));

	t.endHeight=233;
	t.endWidth=Math.round((t.endHeight/i.offsetHeight)*i.offsetWidth);
	t.endY=30;
	t.endX=Math.round((t.wiperEndWidth-t.endWidth)/2);
	if(t.endX+t.endWidth>490){
		t.endX-=((t.endX+t.endWidth)-490);
	}
	t.zooming=true;
	t.zoomStep=0;
	if(!t.imageShowing){
		t.bottomOpacity=0;
		t.setOpacity(t.bottomBackBtn,0);
		t.bottomBackBtn.style.display='block';
		t.setOpacity(t.topBackBtn,0);
		t.topBackBtn.style.display='block';
		t.reLoad=false;
	}else{
		t.setOpacity(t.altTxtBox,1);
		t.reLoad=true;
	}
	t.prevBtn.style.display='none';
	t.nextBtn.style.display='none';
	t.buttonSteps=0;
	t.btnsMoving=0;
}

TabManager.prototype.showPrevImage=function(){
	var t=tabManager;
	var ix=t.pickedImage-1;
	if(ix<0)
		ix=t.cachedBigImages.length-1;
	t.pickImage('scrollImg'+ix,true);
}

TabManager.prototype.showNextImage=function(){
	var t=tabManager;
	var ix=t.pickedImage+1;
	while(ix>=t.cachedBigImages.length)
		ix-=t.cachedBigImages.length;
	if(ix<0)
		ix=0;
	t.pickImage('scrollImg'+ix,true);
}


TabManager.prototype.backToSlideshow=function(){
	var t=tabManager;
	t.totalZoomSteps=14;
	t.resizingContainer=false;
	t.startHeight=t.endHeight;
	t.startWidth=t.endWidth;
	t.startX=t.endX;
	t.startY=t.endY;
	t.endHeight=72;
	t.endWidth=Math.round((t.endHeight/t.startHeight)*t.startWidth);
	t.endY=72;
	t.endX=Math.round((t.wiperEndWidth-t.endWidth)/2);
	t.imageContainer.innerHTML='&nbsp;';
	t.zooming=true;
	t.zoomStep=0;
	t.reLoad=false;
	t.setOpacity(t.scrollOb,1);
}

TabManager.prototype.setImageContainer=function(x,y,w,h){
	this.icW=w;
	this.icH=h;
	this.icX=x;
	this.icY=y;
	this.imageContainer.style.display="block";
	this.imageContainer.style.left=Math.round(this.icX)+'px';
	this.imageContainer.style.top=Math.round(this.icY)+'px';
	this.imageContainer.style.width=Math.round(this.icW)+'px';
	this.imageContainer.style.height=Math.round(this.icH)+'px';
}

TabManager.prototype.hideImageContainer=function(){
	this.altTxtBox.style.display="none";
	this.imageContainer.style.display="none";
	this.prevBtn.style.display="none";
	this.nextBtn.style.display="none";
	this.zooming=false;
	this.imageShowing=false;
}

TabManager.prototype.mmDom2=function(event){
	var t=tabManager;
	t.mouseX=event.clientX+window.scrollX;
	t.mouseY=event.clientY+window.scrollY;
	t.handleMouseMove();
}

TabManager.prototype.mmIE=function(){
	var t=tabManager;
	t.mouseX=window.event.clientX+getScrollX();
	t.mouseY=window.event.clientY+getScrollY();
	t.handleMouseMove();
}

function getScrollY(){
	return(document.documentElement && document.documentElement.scrollTop) || window.pageYOffset || self.pageYOffset || document.body.scrollTop;
}

function getScrollX(){
	return (document.documentElement && document.documentElement.scrollLeft) || window.pageXOffset || self.pageXOffset || document.body.scrollLeft;
}

TabManager.prototype.handleMouseMove=function(){
	if(this.currentTab==-1){
		var mX=this.mouseX;
		var mY=this.mouseY;
		this.mouseOverGallery=false;
		if(
			(mY>178 && mY<322 && mX>259 && mX<947)
		){
			if(this.hintOn){
				this.hintOn=false;
				this.hint.style.display='none';
			}
			this.mouseOverGallery=true;
			if(mX>571 && mX<591)
				this.scrollSpeed=0;
			else
				this.scrollSpeed=(581-mX)/30;
		}
	}
}

TabManager.prototype.resetScrollClicks=function(){
	var n;
	var imgs=this.scrollOb.getElementsByTagName('img');
	this.altTxt=new Array();
	for(n=0;n<imgs.length;n++){
		imgs[n].id="scrollImg"+n;
		imgs[n].onclick=function(){tabManager.pickImage(this.id);return false};
		this.altTxt[n]=imgs[n].alt;
	}
}

TabManager.prototype.attachDomEvent=function(ob,eType,f,cap){
        if(ob.addEventListener)
                ob.addEventListener(eType,f,cap);
        else if(ob.attachEvent)
                ob.attachEvent("on"+eType,f);
        else if(navigator.userAgent.toLowerCase().indexOf('mac')!=-1||navigator.userAgent.toLowerCase().indexOf('apple')!=-1)
                eval('ob.on'+eType+'='+f);
        else
                alert("Handler could not be added");
}

TabManager.prototype.startScroller=function(){
	if(!this.scrollImagesOk){
		this.scrollWidth=this.scrollOb.offsetWidth;
		this.resetScrollClicks();
		this.scrollImagesOk=true;
	}
	if(this.scrollTimer)
		return;
	this.scrollSpeed=-15;
	this.scrollTimer=setInterval('tabManager.scrollMove()',40);
}

TabManager.prototype.stopScroller=function(){
	if(!this.scrollTimer)
		return;
	clearInterval(this.scrollTimer);
	this.scrollTimer=false;
}

TabManager.prototype.scrollMove=function(){
	var t=tabManager;

	// Since the zoomer only appears in the gallery scrolling panel, I might 
	// as well add it here...
	if(t.zooming){
		++t.zoomStep;
		if(t.zoomStep==t.totalZoomSteps){
			t.zooming=false;
			if(t.imageShowing && !t.reLoad){
				t.hideImageContainer();
			}else{
				t.setOpacity(t.imageContainer,1);
				t.setImageContainer(t.endX,t.endY,t.endWidth,t.endHeight);
				t.prevBtn.style.display='block';
				t.nextBtn.style.display='block';
				t.prevBtnX=t.endX;
				t.nextBtnX=t.endX+t.endWidth-13;
				t.btnsMoving=1;
				t.imageShowing=true;
				t.imageContainer.innerHTML='<img src="'+t.cachedBigImages[t.pickedImage].src+'" />';
			}
			// Need to show the buttons here...
		}else{
			if(!t.resizingContainer)
				t.setOpacity(t.imageContainer,(t.imageShowing&&!t.reLoad?1-(t.zoomStep/t.totalZoomSteps):t.zoomStep/t.totalZoomSteps));
			var xChange=((t.endX-t.startX)/t.totalZoomSteps)*t.zoomStep;
			var yChange=((t.endY-t.startY)/t.totalZoomSteps)*t.zoomStep;
			var wChange=((t.endWidth-t.startWidth)/t.totalZoomSteps)*t.zoomStep;
			var hChange=((t.endHeight-t.startHeight)/t.totalZoomSteps)*t.zoomStep;
			t.setImageContainer(t.startX+xChange,t.startY+yChange,t.startWidth+wChange,t.startHeight+hChange);
			if(!t.reLoad){
				if(t.imageShowing){
					if(t.bottomOpacity>0){
						t.bottomOpacity-=0.2;
						t.setOpacity(t.bottomBackBtn,t.bottomOpacity);
						t.setOpacity(t.topBackBtn,t.bottomOpacity);
						t.setOpacity(t.altTxtBox,t.bottomOpacity);
					}else{
						t.bottomBackBtn.style.display="none";
					}
				}else{
					if(t.bottomOpacity<1){
						t.bottomOpacity+=0.2;
						t.setOpacity(t.bottomBackBtn,t.bottomOpacity);
						t.setOpacity(t.topBackBtn,t.bottomOpacity);
						t.setOpacity(t.altTxtBox,t.bottomOpacity);
					}
				}
			}
		}
	}

	if(t.btnsMoving>0 && t.btnsMoving<11){
		t.prevBtnX-=(10/t.btnsMoving);
		t.nextBtnX+=(10/t.btnsMoving);
		t.setOpacity(t.prevBtn,t.btnsMoving/10);
		t.setOpacity(t.nextBtn,t.btnsMoving/10);
		t.prevBtn.style.left=Math.round(t.prevBtnX)+"px";
		t.nextBtn.style.left=Math.round(t.nextBtnX)+"px";
		++t.btnsMoving;
	}

	if(!t.mouseOverGallery){
		if(t.scrollSpeed>0){
			t.scrollSpeed-=0.2
			if(t.scrollSpeed<0)
				t.scrollSpeed=0;
		}else if(t.scrollSpeed<0){
			t.scrollSpeed+=0.2
			if(t.scrollSpeed>0)
				t.scrollSpeed=0;
		}
	}
	t.scrollX+=t.scrollSpeed;
	var maxDist=(t.scrollWidth/2);
	if(t.scrollX<-maxDist)
		t.scrollX+=maxDist;
	else if(t.scrollX>0)
		t.scrollX-=maxDist;
	t.scrollOb.style.left=Math.round(t.scrollX)+'px';
	if(t.hintOn){
		t.hintOpacity+=t.hintOpacityDir;
		if(t.hintOpacity>1.5){
			t.hintOpacity=1.5;
			t.hintOpacityDir=-0.05;
		}else if(t.hintOpacity<0.1){
			t.hintOpacity=0.1;
			t.hintOpacityDir=+0.05;
		}
		t.setOpacity(t.hint,t.hintOpacity);
	}
}

TabManager.prototype.setGap=function(y,h){
	this.topLine.style.height=y+'px';
	this.bottomLine.style.top=(y+h)+'px';
	this.bottomLine.style.height=(this.panelHeight-(y+h))+"px";
}

TabManager.prototype.addTab=function(img0,img1,img2,bgColor){
	this.tab[this.ix]=new Tab(this.ix,this.imgDir+'/'+img0,this.imgDir+'/'+img1,this.imgDir+'/'+img2,bgColor);
	++this.ix;
}


TabManager.prototype.tabClicked=function(ix){
	if(ix==this.currentTab || this.moving)
		return;
	this.currentTab=ix;
	var n;
	for(n=0;n<this.tab.length;n++){
		if(ix>-1)
			this.tab[n].setState(n==ix?1:2);
		else
			this.tab[n].setState(0);
	}
	this.resetWiper(ix);
	this.startMoving();
	setCookie('tabCookie', this.currentTab, 1);
}


TabManager.prototype.startMoving=function(){
	if(this.moving)
		return;
	this.moving=true;
	this.timer=setInterval(tabManager.move,20);
}


TabManager.prototype.move=function(){
	var moving=false;
	var n,tb,td,reqHeight,nonActiveHeight,t=tabManager;
	
	// Move the "wiper" across the right panel, switch underlying content and fade wiper out...
	if(document.all){ // IE hack as for some reason it doesn't get the widths right so never runs the conditional statement.
		t.wiperEndWidth=700;
	}
	if(t.wiperWidth<t.wiperEndWidth){
		t.wiperWidth+=40;
		moving=true;
		if(t.currentTab==-1){
			t.wiper.style.width=(t.wiperWidth+80)+'px';
			t.wiper.style.left=(t.wiperEndWidth-t.wiperWidth)+'px';
		}else
			t.wiper.style.width=t.wiperWidth+'px';
		if(t.wiperWidth>=t.wiperEndWidth){
			t.wiperWidth=t.wiperEndWidth;
			if(t.currentTab==-1){
				t.wiper.style.width=(t.wiperWidth+40)+'px';
				t.wiper.style.left=(t.wiperEndWidth-t.wiperWidth)+'px';
			}else{
				t.wiper.style.width=t.wiperWidth+'px';
			}
			document.getElementById('tabContentDefault').style.display='none';
			for(n=0;n<t.tab.length;n++)
				t.tab[n].ob.style.display='none';
			if(t.currentTab==-1){
				document.getElementById('tabContentDefault').style.display='block';
			}else{
				t.tab[t.currentTab].ob.style.display='block';
				document.getElementById('tabContentDefault').style.display='none';
			}
		}
	}else if(t.wiperOpacity>0){
		t.wiper.style.left=0;
		t.wiperOpacity-=0.1;
		if(t.wiperOpacity<=0){
			t.wiper.style.display='none';
		}
		t.setOpacity(t.wiper,t.wiperOpacity);
		moving=true;
	}

	// Resize the button tabs and move them about a bit. We'll do everything here, as we need to calculate space
	// and make sure it doesn't look wierd as buttons grow and shrink - harder to do if the button tabs were all
	// running around independently. Easiest way is to work out how much the non-active tabs need to change by,
	// in each iteration, then add up all their heights, subtract that from the height of the panel, and make the
	// active tab that height - you know, sometimes I even amaze myself ;-)
	nonActiveHeight=0;
	for(n=0;n<t.tab.length;n++){
		if(n!=t.currentTab){
			tb=t.tab[n];
			reqHeight=t.tabSize[tb.targetState];
			if(tb.height!=reqHeight){
				moving=true;
				if(tb.height<reqHeight){
					tb.height+=Math.ceil((reqHeight-tb.height)/4);
					if(tb.height>reqHeight)
						tb.height=reqHeight;
				}else{
					tb.height-=Math.ceil((tb.height-reqHeight)/4);
					if(tb.height<reqHeight)
						tb.height=reqHeight;
				}
				tb.setHeight(tb.height);
			}
			nonActiveHeight+=(tb.height+2);
		}
	}

	// Now we have the heights of the non-active tabs, we need to work out the height for the active tab,
	// then reposition everything in the vertical axis...
	if(t.currentTab>-1){
		tb=t.tab[t.currentTab];
		tb.height=t.panelHeight-nonActiveHeight;
		tb.setHeight(tb.height);
		if(tb.cdivOpacity<1){
			moving=true;
			tb.cdivOpacity+=0.03;
			t.setOpacity(tb.tabCDiv,tb.cdivOpacity);
		}
	}

	// Finally, reposition the tab buttons...
	nonActiveHeight=0;
	for(n=0;n<t.tab.length;n++){
		t.tab[n].moveTo(0,nonActiveHeight);
		if(n==t.currentTab)
			t.setGap(nonActiveHeight,t.tab[n].height);
		nonActiveHeight+=(t.tab[n].height+2);
	}

	// If nothing's moving, stop and clear the timer
	if(!moving){
		clearInterval(t.timer);
		t.timer=false;
		t.moving=false;
	}
}

TabManager.prototype.resetWiper=function(ix){
	var w=this.wiper.style;
	w.width="10px";
	w.left=(ix>-1?0:this.wiperEndWidth-1)+'px';
	this.setOpacity(this.wiper,1);
	if(ix>-1){
		w.backgroundColor=this.tab[ix].bgColor;
		this.stopScroller();
	}else{
		w.backgroundColor='#000000';
		this.setGap(302,0);
		this.startScroller();
	}
	w.display="block";
	this.wiperWidth=10;
	this.wiperOpacity=1;
	if(this.hintOn){
		this.hintOn=false;
		this.hint.style.display='none';
	}
	this.hideImageContainer();
	this.setOpacity(this.topBackBtn,0);
	this.topBackBtn.style.display="none";
	this.setOpacity(this.bottomBackBtn,0);
	this.bottomBackBtn.style.display="none";
	this.zooming=false;
	this.imageShowing=false;
	this.setOpacity(this.scrollOb,1);
}

TabManager.prototype.setOpacity=function(ob,op){
	if(op>1)
		op=1;
	else if(op<0)
		op=0;
	if(document.all){
		ob.style.filter='Alpha(opacity='+Math.round(op*100)+')';
	}else{
		ob.style.opacity=op;
	}
}

function Tab(ix,img0,img1,img2,bgColor){
	var n,i;
	this.ix=ix;
	this.ob=document.getElementById('tabContent'+ix);
	this.buttonImg=document.images['tabImg'+ix];
	this.tabDiv=document.getElementById('tabDiv'+ix);
	this.tabCDiv=document.getElementById('tabCDiv'+ix);
	this.height=(tabManager.tabSize[0]+2);
	this.y=ix*this.height;
	this.state=0;
	this.targetState=0;
	this.img=new Array();
	i=new Array(img0,img1,img2);
	for(n=0;n<3;n++){
		this.img[n]=new Image(tabManager.tabWidth,this.height);
		this.img[n].src=i[n];
	}
	this.bgColor=bgColor;
	this.cdivOpacity=0;
}


Tab.prototype.setState=function(st){
	this.targetState=st;
	this.buttonImg.src=this.img[st].src;
	this.buttonImg.style.cursor=(st==1?"default":"pointer");
	if(st==1){
		this.cdivOpacity=0;
		tabManager.setOpacity(this.tabCDiv,0);
		this.tabCDiv.style.display='block';
	}else if(st==0){
		this.tabCDiv.style.display='none';
	}
}

Tab.prototype.setHeight=function(h){
	this.tabDiv.style.clip="rect(0 237px "+h+"px 0)";
	this.tabDiv.style.height=h+'px';
}

Tab.prototype.moveTo=function(x,y){
	this.tabDiv.style.left=x+'px';
	this.tabDiv.style.top=y+'px';
}


/**
 * Open a form submission in a new window - also define in main.js
 */
var formWin = null;

function submitToPopup(fm) {
	formWin = window.open('', 'form', 'width=505,height=600,toolbar=no,scrollbars,status,resizable=yes');

	if(formWin && !formWin.closed) {
		fm.target = 'form';
		formWin.focus();
	}

	return true;
}


function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+"="+escape( value ) +
		( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}