fp={
// CSS classes
	dynamicClass:'fp',
	showClass:'show',
	currentClass:'current',
	hideClass:'hide',
// Page section IDs
	boundaryId:'boundary',
	slideShowContainer:'flatshots',
	slides:'photos',
// ID of generated popunder
	popunderId:'popunder',
// slide show links 
        space:'&nbsp;',
	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.initPhotos();
	},
/* Photo slideshow */
	initPhotos:function()
        {
		var s = document.getElementById(fp.slideShowContainer);
		var p = document.getElementById(fp.slides);
                var parentTag = document.getElementById('boundary');
                var x = parentTag.offsetLeft;
                var y = parentTag.offsetTop + parentTag.offsetHeight;
                var parent = parentTag;
                
                while (parent.offsetParent) 
                {
                    parent = parent.offsetParent;
                    x += parent.offsetLeft;
                    y += parent.offsetTop;
                }

		if(!s || !p)
                {
                    return;
                }
		
                //alert("We have s AND p");

                // 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');
                
                if(fp.photoCount != 0)    
                {
                // create next link
		fp.nextLink=document.createElement('a');
                fp.nextLink.innerHTML=fp.space;
		
                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 = photos 
                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);
