/*
	name			: ClassBehaviours, the javascript framework based on class-name parsing
	update			: 9.11.9
	author			: Maurice van Creij
	dependencies	: jquery.classbehaviours.js
	info			: http://www.classbehaviours.com/

    This file is part of jQuery.classBehaviours.

    ClassBehaviours is a javascript framework based on class-name parsing.
    Copyright (C) 2008  Maurice van Creij

    ClassBehaviours is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    ClassBehaviours is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with ClassBehaviours. If not, see http://www.gnu.org/licenses/gpl.html.
*/

	// create the jQuery object if it doesn't already exist
	if(typeof(jQuery)=='undefined') jQuery = function(){};

	// create the root classbehaviours object if it doesn't already exist
	if(typeof(jQuery.classBehaviours)=='undefined') jQuery.classBehaviours = function(){};

	// create the handlers child object if it doesn't already exist
	if(typeof(jQuery.classBehaviours.handlers)=='undefined') jQuery.classBehaviours.handlers = function(){}

	// Update a slide based on clicks on thumbnails
	jQuery.classBehaviours.handlers.thumbnailToPhoto = {
		// properties
		name: 'thumbnailToPhoto',
		thumbnails: new Array(),
		stackTop: 1,
		// methods
		start: function(node){
			// add the thumbnail to the index
			this.thumbnails[this.thumbnails.length] = node;
			// set the click event
			node.onclick = this.loadPhoto;
		},
		// events
		loadPhoto: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var ttp = jQuery.classBehaviours.handlers.thumbnailToPhoto;
			// if this is not an active thumbnail
			if(objNode.className.indexOf('active')<0){
				// get the target of this thumbnail
				targetId = jQuery.classBehaviours.utilities.getClassParameter(objNode, 'id', 'thumbnailTarget');
				// for all thumbnails
				allThumbnails = ttp.thumbnails;
				for(var a=0; a<allThumbnails.length; a++){
					// deactivate the thumbnails with the same target
					thumbnailTargetId = jQuery.classBehaviours.utilities.getClassParameter(allThumbnails[a], 'id', 'thumbnailTarget')
					if(thumbnailTargetId==targetId) allThumbnails[a].className = allThumbnails[a].className.replace(' active','');
				}
				// activate this thumbnail
				objNode.className += ' active';
				// find the image in the existing target stack
				targetStack = document.getElementById(targetId);
				targetImages = targetStack.getElementsByTagName('img');
				targetImage = null;
				for(var a=0; a<targetImages.length; a++) if(targetImages[a].src==objNode.href) targetImage = targetImages[a];
				// if the image isn't in the stack allready
				if(targetImage==null){
					// create a new image
					clonedImage = targetImages[0].parentNode.cloneNode(true);
					// place it at the bottom of the stack
					targetStack.insertBefore(clonedImage, targetStack.firstChild);
					// set the properties of the new image
					clonedImage = targetStack.firstChild;
					clonedImage.getElementsByTagName('img')[0].id = null;
					clonedImage.getElementsByTagName('img')[0].onload = ttp.showPhoto;
					clonedImage.getElementsByTagName('img')[0].src = objNode.href;
					clonedImage.getElementsByTagName('img')[0].title = objNode.getElementsByTagName('img')[0].title;
					clonedImage.getElementsByTagName('img')[0].alt = objNode.getElementsByTagName('img')[0].alt;
				// else
				}else{
					// show the photo
					ttp.showPhoto(targetImage);
				}
			}
			// cancel the click
			return false;
		},
		showPhoto: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var ttp = jQuery.classBehaviours.handlers.thumbnailToPhoto;
			// get the target node
			targetNode = objNode.parentNode
			// fade it out
			jQuery.classBehaviours.fader.setFade(targetNode, 0);
			// put it at the top of the stack
			ttp.stackTop += 1;
			targetNode.style.zIndex = ttp.stackTop;
			// fade it in
			targetNode.id = (targetNode.id==null) ? targetNode.id : ttp.name + ttp.stackTop ;
			jQuery.classBehaviours.fader.fadeIn(targetNode.id, 10, 50, null);
		}
	}

	jQuery.classBehaviours.handlers.nextThumbnailToPhoto = {
		// properties
		name: 'nextThumbnailToPhoto',
		// methods
		start: function(node){
			// set the click event
			node.onclick = this.nextPhoto;
		},
		// events
		nextPhoto: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var nttp = jQuery.classBehaviours.handlers.nextThumbnailToPhoto;
			var ttp = jQuery.classBehaviours.handlers.thumbnailToPhoto;
			// for all thumbnails in the list
			targetId = jQuery.classBehaviours.utilities.getClassParameter(objNode, 'id', 'thumbnailTarget');
			allThumbnails = document.getElementById(targetId).getElementsByTagName('A');
			nextIndex = 0;
			for(var a=0; a<allThumbnails.length-1; a++){
				// find the current active thumbnail in the list
				if(allThumbnails[a].className.indexOf('active')>-1){
					// pick the next one
					nextIndex = a+1;
				}
			}
			// activate the first one is none was found
			ttp.loadPhoto(allThumbnails[nextIndex]);
		}
	}

	jQuery.classBehaviours.handlers.previousThumbnailToPhoto = {
		// properties
		name: 'previousThumbnailToPhoto',
		// methods
		start: function(node){
			// set the click event
			node.onclick = this.previousPhoto;
		},
		// events
		previousPhoto: function(that){
			var objNode = (typeof(this.nodeName)=='undefined') ? that : this ;
			var pttp = jQuery.classBehaviours.handlers.previousThumbnailToPhoto;
			var ttp = jQuery.classBehaviours.handlers.thumbnailToPhoto;
			// for all thumbnails in the list
			targetId = jQuery.classBehaviours.utilities.getClassParameter(objNode, 'id', 'thumbnailTarget');
			allThumbnails = document.getElementById(targetId).getElementsByTagName('A');
			previousIndex = allThumbnails.length-1;
			for(var a=1; a<allThumbnails.length; a++){
				// find the current active thumbnail in the list
				if(allThumbnails[a].className.indexOf('active')>-1){
					// pick the previous one and activate it
					previousIndex = a-1;
				}
			}
			// activate the last one is none was found
			ttp.loadPhoto(allThumbnails[previousIndex]);
		}
	}

	// add this addon to the jQuery object
	if(typeof(jQuery.fn)!='undefined'){
		// extend jQuery with this method
		jQuery.fn.thumbnailToPhoto = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.thumbnailToPhoto.start(this);
				}
			);
		};
		jQuery.fn.nextThumbnailToPhoto = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.nextThumbnailToPhoto.start(this);
				}
			);
		};
		jQuery.fn.previousThumbnailToPhoto = function(){
			return this.each(
				function(){
					jQuery.classBehaviours.handlers.previousThumbnailToPhoto.start(this);
				}
			);
		};
		// set the event handler for this jQuery method
		$(document).ready(
			function(){
				$(".thumbnailToPhoto").thumbnailToPhoto();
				$(".nextThumbnailToPhoto").nextThumbnailToPhoto();
				$(".previousThumbnailToPhoto").previousThumbnailToPhoto();
			}
		);
	}

