/* 
	From DHTML to DOM example script
	written by Christian Heilmann (http://www.wait-till-i.com) for SitePoint.
*/

fp={
// CSS classes
	dynamicClass:'fp',
	showClass:'show',
	currentClass:'current',
	hideClass:'hide',
// Page section IDs
	boundaryId:'boundary',
	detailsContainer:'flatdetails',
	detailsNav:'detailnav',
	popImageContainer:'flatimage',
	slideShowContainer:'flatshots',
	slides:'photos',
// ID of generated popunder
	popunderId:'popunder',
// slide show links 
	slideBack:'&raquo;',	
	slideFwd:'&laquo;',	
// popup closing link
	closeLabel:'close',
// global parameters
	currentLink:null,
	currentSection:null,
	photoCount:null,
	curPhoto:null,

/* Initialise functionality */
	init:function(){
		if(!document.getElementById || !document.createTextNode){return;}
		var container=document.getElementById(fp.boundaryId);
		if(!container){return;}
		fp.cssjs('add',container,fp.dynamicClass);
		fp.initTabs();
		fp.initWin();
		fp.initPhotos();
	},
	initTabs:function(){
		var nav=document.getElementById(fp.detailsNav);
		if(nav && nav.getElementsByTagName('a')[0]){
			var links=nav.getElementsByTagName('a');
			var firstSection=links[0].href.toString().match(/#(.*)/)[1];
			firstSection=document.getElementById(firstSection);
			if(firstSection){
				var parentDIV=firstSection.parentNode.parentNode;
				fp.cssjs('add',parentDIV,fp.showClass);
				fp.currentSection=firstSection;
				fp.cssjs('add',links[0],fp.currentClass);
				fp.currentLink=links[0];
				for(var i=0;i<links.length;i++){
					fp.addEvent(links[i],'click',fp.showTab,false);
					fp.fixSafari(links[i]);
				}
			}
		}
	},
	showTab:function(e){
		var section=fp.getTarget(e);
		var toshow=section.getAttribute('href').toString().match(/#(.*)/)[1];
		if(fp.currentSection && fp.currentLink){
			fp.cssjs('remove',fp.currentSection.parentNode.parentNode,fp.showClass);
			fp.cssjs('remove',fp.currentLink,fp.currentClass);
		}
		if(document.getElementById(toshow)){
			toshow=document.getElementById(toshow);
			fp.cssjs('add',toshow.parentNode.parentNode,fp.showClass);
			fp.cssjs('add',section,fp.currentClass);
			fp.currentSection=toshow;
			fp.currentLink=section;
		}
		fp.cancelClick(e);
	},
/* pop-under functionality */
	initWin:function(){
		var container=document.getElementById(fp.popImageContainer);
		if(!container){return;}
		fp.popunder=document.createElement('div');
		fp.popunder.id=fp.popunderId;
		fp.cssjs('add',fp.popunder,fp.hideClass);
		document.getElementById(fp.boundaryId).appendChild(fp.popunder);
		var closeLink=document.createElement('a');
		closeLink.setAttribute('href','#');
		closeLink.innerHTML=fp.closeLabel;
		fp.addEvent(closeLink,'click',fp.closeWin,false);
		fp.fixSafari(closeLink)
		fp.popunder.appendChild(closeLink);
		var links=container.getElementsByTagName('a');
		for(var i=0;i<links.length;i++){
			fp.addEvent(links[i],'click',fp.openWin,false);
			fp.fixSafari(links[i]);
		}
	},
	openWin:function(e){
		// if there is no image in popunder yet
		if(!fp.popunder.getElementsByTagName('img')[0]){
			// create image and grab link location as src
			var shot=document.createElement('img');
			var bigpic=fp.getTarget(e);
			shot.setAttribute('src',bigpic.href);
			fp.popunder.appendChild(shot);
			// apply close functionality to image
			fp.addEvent(shot,'click',fp.closeWin,false);
			fp.fixSafari(shot);
		}
		// show popunder div
		fp.cssjs('remove',fp.popunder,fp.hideClass);
		fp.cancelClick(e);
	},
	closeWin:function(e){
		// hide popunder div and don't follow link
		fp.cssjs('add',fp.popunder,fp.hideClass);
		fp.cancelClick(e);
	},
/* Photo slideshow */
	initPhotos:function(){
		var s=document.getElementById(fp.slideShowContainer);
		var p=document.getElementById(fp.slides);
		if(!s || !p){return;}
		// get all li elements and store length and current photo properties
		fp.photos=s.getElementsByTagName('li');
		fp.photoCount=fp.photos.length;
		fp.curPhoto=0;
		// create new paragraph
		var newp=document.createElement('p');
		// create next link
		fp.nextLink=document.createElement('a');
		fp.nextLink.innerHTML=fp.slideBack;
		fp.nextLink.setAttribute('href','#');
		// create previous link
		fp.prevLink=document.createElement('a');
		fp.prevLink.setAttribute('href','#');
		fp.prevLink.innerHTML=fp.slideFwd;
		// create counter display x of y
		fp.photoCountDisp=document.createElement('span');
		fp.photoCountDisp.appendChild(document.createTextNode((fp.curPhoto+1)+' of '+fp.photoCount));
		// append all to new paragraph
		newp.appendChild(fp.prevLink);
		newp.appendChild(fp.photoCountDisp);
		newp.appendChild(fp.nextLink);
		// insert paragraph before list
		p.insertBefore(newp,p.getElementsByTagName('ul')[0]);
		// set handlers
		fp.addEvent(fp.prevLink,'click',fp.showPhoto,false);
		fp.fixSafari(fp.prevLink);
		fp.addEvent(fp.nextLink,'click',fp.showPhoto,false);
		fp.fixSafari(fp.nextLink);
		// show first photo
		fp.showPhoto(fp.curPhoto)
	},
	showPhoto:function(e){
		var photo = fp.getTarget(e);
		// hide old photo
		fp.cssjs('remove',fp.photos[fp.curPhoto],fp.showClass);
		// increase, decrease or keep counter value
		var add=(photo==fp.prevLink)?-1:+1;
		if(photo!=fp.prevLink && photo !=fp.nextLink){add=0;}
		fp.curPhoto=fp.curPhoto+add;
		// show new image
		fp.cssjs('add',fp.photos[fp.curPhoto],fp.showClass);
		// show or hide previous or next link
		fp.cssjs(fp.curPhoto>0?'remove':'add',fp.prevLink,fp.hideClass);
		fp.cssjs(fp.curPhoto<(fp.photoCount-1)?'remove':'add',fp.nextLink,fp.hideClass);
		// increase or decrease counter display
		var disp=fp.photoCountDisp.firstChild;
		disp.nodeValue=disp.nodeValue.replace(/^\d/,(fp.curPhoto+1));	
		fp.cancelClick(e);
	},
/* helper methods */
	fixSafari:function(node){
//		node.onclick = function() { return false; }; // Safari
	},
	getTarget:function(e){
		var target = window.event ? window.event.srcElement : e ? e.target : null;
		if (!target){return false;}
		if (target.nodeName.toLowerCase() != 'a'){target = target.parentNode;}
		return target;
	},
	cancelClick:function(e){
		if (window.event){
			window.event.cancelBubble = true;
			window.event.returnValue = false;
			return;
		}
		if (e){
			e.stopPropagation();
			e.preventDefault();
		}
	},
	addEvent: function(elm, evType, fn, useCapture){
		if (elm.addEventListener) 
		{
			elm.addEventListener(evType, fn, useCapture);
			return true;
		} else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		} else {
			elm['on' + evType] = fn;
		}
	},
	// cssjs tests 
	cssjs:function(a,o,c1,c2){
		switch (a){
			case 'swap':
				o.className=!fp.cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
			break;
			case 'add':
				if(!fp.cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
			break;
			case 'remove':
				var rep=o.className.match(' '+c1)?' '+c1:c1;
				o.className=o.className.replace(rep,'');
			break;
			case 'check':
				return new RegExp("(^|\\s)" + c1 + "(\\s|$)").test(o.className)
			break;
		}
	}
}
// start the show.
fp.addEvent(window, 'load', fp.init, false);
