///Slides v1.0


(function($){
$.bind = function(object, method){
	var args = Array.prototype.slice.call(arguments, 2);
	if(args.length){
		return function() {
			var args2 = [this].concat(args, $.makeArray( arguments ));
			return method.apply(object, args2);
		};
	} else {
		return function() {
			var args2 = [this].concat($.makeArray( arguments ));
			return method.apply(object, args2);
		};
	}
};
	
})(jQuery);

/*
 * jQuery Animate To Class
 * Copyright 2008 Igor Frias Vieira
 * http://igorvieira.com/blog/animate-to-class/
 *
 * Released under the MIT and GPL licenses.
 */

(function($)
{
	$.fn.extend({
		animateToClass : function(to, duration, easing, callback)
		{
			if(!to){ return this; }
			
			styles = selectStyle(to);
			
			if(!styles) return this;
			
			return this.animate(styles, duration, easing, callback);
		}
	});
	
	function selectStyle(sel)
	{
		if(sel.substr(0,1) != ".")
		{
			sel = "." + sel;
		}
		
		$(document.styleSheets).each(function(i,v)
		{
			if(attrClassTest = selectAttr(sel, v))
			{
				attrClass = attrClassTest;
			}
		});
		
		if(!attrClass)
		{
			attrClass = Array();
		}
		
		objStyle = {}
		
		if(attrClass == "")
		{
			return false;
		}
		
		if(attrClass.match(";"))
		{
			attrClass = attrClass.split(";");
		}
		else
		{
			attrClass = [attrClass];
		}
		
		$(attrClass).each(function(i,v){
			if(v != ""){
				v = v.split(":");
				v[0] = toCamelCase(v[0]);
				
				objStyle[v[0]] = $.trim(v[1]);
			}
		});
		return objStyle;
	}
	
	function selectAttr(sel, v)
	{
		attrClass = false;	
			
		if($.browser.msie)
		{
			if(v.rules.length > 0)
			{
				$(v.rules).each(function(i2,v2){
					if(sel == v2.selectorText)
					{
						attrClass = v2.style.cssText;
					}
				});
			}
			else if(v.imports.length > 0)
			{
				$(v.imports).each(function(i2,v2){
										   
					if(sel == v2.selectorText)
					{
						attrClass = v2.style.cssText;
					}
					else if(v2 == "[object]" || v2 == "[Object CSSStyleSheet]" || v2 == "[object CSSImportRule]")
					{
						return selectAttr(sel, v2);
					}
				});
			}
		}
		else
		{
			$(v.cssRules).each(function(i2,v2){
				if(sel == v2.selectorText)
				{
					attrClass = v2.style.cssText;
				}
				else if(v2 == "[object CSSImportRule]")
				{
					return selectAttr(sel, v2.styleSheet);
				}
			});
		}
		
		return attrClass;
	}
	
	function toCamelCase(str)
	{
		str = $.trim(str);
		str = str.replace(/-/g, " ");
		str = str.toLowerCase();
		
		strArr = str.split(" ");
		
		var nStr = "";
		$(strArr).each(function(i,v){
			if(i == 0){
				nStr += v;
			}else{
				/*
				v = v.split("");
				v[0] = v[0].toUpperCase();
				nStr += v.join();
				*/
				
				//There was a bug in older version, this correction was sugested by Simon Shepard.
				nStr += v.substr(0,1).toUpperCase();
				nStr += v.substr(1,v.length);
			}
		});
			
		return nStr;
	}
})(jQuery);

var Slides = function(scope,photos,options){
	
	///////////////////
	// class methods //
	///////////////////
	this.add = function(image,url){
		if(this.options.nav != ""){
			var num = this.photos.length;
			var tick = $("<span class=\"tick\" rel=\""+num+"\">"+this.options.nav+"</span>")
			tick.appendTo(this.nav);
			
			tick.bind("click",this,function(e){
					e.data.goto($(this).attr("rel"));
			});
		}
		this.photos.push({"image":image,"url":url, "tick":tick});
	}
	
	this.goto = function(num){
		
		var image = this.photos[num].image;
		var url = this.photos[num].url;
		var tick = this.photos[num].tick;
		if(url != ""){
			this.bottom.html("<a href=\""+url+"\" target=\""+this.options.target+"\"><image class=\"slidePhoto\" src=\""+image+"\" style=\"width:"+this.options.width+"px;height:"+this.options.height+"px\"/></a>");
		}else{
			this.bottom.html("<image class=\"slidePhoto\" src=\""+image+"\" style=\"width:"+this.options.width+"px;height:"+this.options.height+"px\"/>");
		}
		clearInterval(this.interval);
		this.photos[this.current].tick.removeClass("selected");
		tick.addClass("selected");
		
		this.top[this.options.effect](this.options.fade*1000,$.bind(this,function(){
			this.top.html(this.bottom.html());
			this.top.show();			
			this.startcycle(this.options.delay);
		}));
		
		this.current = num;
	}
	
	this.forward = function(){
		if(this.photos[this.current+1] != undefined){
			this.goto(this.current+1);
		}else{
			this.goto(0);		
		}
		this.startcycle(this.options.delay);
	}
	
	this.back = function(){
		if(this.photos[this.current-1] != undefined){
			this.goto(this.current-1);
		}else{
			this.goto(this.photos.length-1);
		}
		this.startcycle(this.options.delay);
	}
	
	this.startcycle = function(wait){
		clearInterval(this.interval);
		this.interval = setInterval($.bind(this,function(){
			clearInterval(this.interval);
			this.forward();
		}),wait*1000);
	}
	
	
	//////////////////////
	// class Properties //
	//////////////////////
	this.photos = [];
	this.current = 0;
	this.options = {
		"width":200,
		"height":200,
		"nav":"<span>&bull;</span>",
		"target":"",
		"delay":8,
		"fade":.5,
		"effect":"fadeOut"
	};
	for(index in options)this.options[index] = options[index];	
	
	// build show html
	this.scope = $(scope);
	this.wrapper = $("<div class=\"wrapper\"></div>");
	this.wrapper.appendTo(this.scope);
	this.scope.css({
		"position":"relative",
		"width":this.options.width,
		"height":this.options.height
	});//*/
	
	this.bottom = $("<div id=\"BottomSlide\" class=\"image bottom\"></div>");
	this.bottom.appendTo(this.wrapper);
	
	this.top = $("<div id=\"TopSlide\" class=\"image top\"></div>");
	this.top.appendTo(this.wrapper);
	
	this.top.css({
		"position":"absolute",
		"top":0,
		"left":0,
		"width":this.options.width,
		"height":this.options.height
	});
	
	this.bottom.css({
		"position":"absolute",
		"top":0,
		"left":0,
		"width":this.options.width,
		"height":this.options.height
	});
		
	//if(this.options.nav != ""){
		this.nav = $("<div class=\"nav\"></div>");
		this.nav.css({
			"position":"absolute",
			"bottom":0,
			"right":0
		});
		this.nav.appendTo(this.scope);
	//}
	
	// add photos
	for(var index in photos)this.add(index,photos[index]);
	this.goto(this.current);
	
	this.startcycle(this.options.delay);
	
}