/*****************************************************************************
 * base
 * @author		$Author: nicolasrudas $
 * @copy		(c) Copyright 2010 Octavodia Ltd. All Rights Reserved.
 * 
 *****************************************************************************/

$(document).ready(function() {
	$('body').addClass('js');
	$('#slideshow').slideshow({
		container: '.slideshow-inner',
		items: '.item',
		beforeInit: function(){
			$(this).show();
		},
		init: function(slideshow){
			slideshow.looped_times = 0;
			var max_loops = 3 * 2; // 3 items, 2 times
			slideshow.timer = setInterval(function() {
				slideshow.looped_times++;
				if(slideshow.looped_times==max_loops) {
					clearInterval(slideshow.timer);
					delete slideshow.timer;
				}
				slideshow.next();
			},5000);
		}
	});
	$('#login form').hide_form_label();
});

jQuery.fn.hide_form_label = function() {
	var copy_styles = ['font','color','background'];
	
	return this.each(function() {
		if(this.tagName !== 'FORM'){
			throw new Error('Need a form element for hide_form_element');}
		
		var $this = $(this),
			labels = $this.find('label[for]');
		
		labels.each(function() {
			var id = $(this).attr('for'),
				text = $(this).text(),
				corresponding_input = $this.find('#'+id);
			
			if(!corresponding_input.length
					|| !text){return;}

			$(this).hide();
				
			corresponding_input[0].__label = text;
			corresponding_input[0].__label_style = $(this).getCSS(copy_styles);
			corresponding_input[0].__original_style = corresponding_input.getCSS(copy_styles);

			corresponding_input
				.attr('data-hide-form-label',true)
				.unbind('focus.hide_form_label')
				.unbind('blur.hide_form_label')
				.bind('focus.hide_form_label',function() {
					var val = $(this).val(), ns = null;
					val === this.__label && $(this).val('').css(this.__original_style);
				})
				.bind('blur.hide_form_label',function() {
					var val = $(this).val();
					(val === '' || val == this.__label)
						&& $(this).css(this.__label_style).val(this.__label);						
				});
			
			setTimeout(function() {
				corresponding_input.trigger('blur.hide_form_label');
			}, 0);
		});
		
		$this
			.unbind('submit.hide_form_label')
			.bind('submit.hide_form_label',function() {
				$(this).find('[data-hide-form-label]').each(function(){$(this).triggerHandler('focus.hide_form_label');});
			});
	});
};
$.fn.getCSS = function( style ){
	var self = $(this),
		r = {};
	if( !$.isArray( style ) ) {style=style.split(' ');}
	$.each(style,function(i,name){
		r[name] = self.css(name);
	});		
	return r;
}
/*
return;
for(var s in this.__label_style) {
	console.info(s);
	if(s in this.__original_style) {
		ns = this.__original_style[s];
	}
	console.log(ns);
	$(this).css(s,ns);							
}			

$.fn.copyCSS = function( style, toNode ){
	return this.each(function() {
		var self = $(this);
		if( !$.isArray( style ) ) {style=style.split(' ');}
		$.each( style, function( i, name ){ toNode.css( name, self.css(name) ) } );		
	});
}
*/

jQuery.fn.slideshow = function(o) {
	var options = $.extend({
		container: 'ol.album', // will have a large width
		items: 'li.project',
		class_names: {
			container: 'slideshow-container',
			item: 'slideshow-item',
			item_active: 'slideshow-item-active',
			wrapper: 'slideshow-wrapper'
		},
		controls: {
			next: '.controls .next',
			previous: '.controls .previous'
		},
		scrollOptions: { duration: 1500 },
		onItemClick: function() { // return false to scroll to item, true to do what browser would do
			return true;
		},
		beforeInit: function(slideshow,options) {},
		init: function(slideshow){},
		beforeChange: function(slideshow,current_item,next_item){},
		change: function(slideshow,current_item,next_item){}
	},o||{});
	
	function Slideshow(element,options){
		this.options = {};
		for(var o in options){
			this.options[o] = options[o]; }		

		this.id = Math.floor(Math.random()*10000) + new Date().valueOf();
		this.current_item = -1;
		this.element = element;
		this.$element = $(element);
		this.items = $([]);
		this.container = $([]);
		this.wrapper = $([]);
		
		if(this.__method__('beforeInit',this,this.options) === false){
			return this;}
		
		return this.init();
	}
	
	Slideshow.prototype.__method__ = function(name) {
		if ($.isFunction(this.options[name])){
			return this.options[name].apply(this.element,[this].concat(Array.prototype.slice.call(arguments,1)));
		}
		return false;
	};
	
	Slideshow.prototype.init = function() {
		var self = this,
			options = this.options,
			controls = options.controls,
			$el = this.$element,
			items = this.items = $el.find(options.items)
									.addClass(options.class_names.item),
			container = this.container = $el.find(options.container)
											.addClass(options.class_names.container),		
			wrapper = this.wrapper = $('<div class="'+options.class_names.wrapper+'"></div>')
								.css({'overflow': 'hidden'})
								.insertBefore(container);
								
		container
			.css('width',items.length * container.outerWidth() + (items.length * 15)  )
			.appendTo(wrapper);

		this.options.controls.next = $(controls.next,$el)
			.die('click.Slideshow')
			.live('click.Slideshow',function(e) {
				self.next();
				return false;			
			})
			.live('mousedown.Slideshow',function() { $(this).addClass('focus'); })
			.live('mouseup.Slideshow',function() { $(this).removeClass('focus'); });

		this.options.controls.previous = $(controls.previous,$el)
			.die('click.Slideshow')
			.live('click.Slideshow',function(e) {
				self.previous();
				return false;
			})
			.live('mousedown',function() { $(this).addClass('focus'); })
			.live('mouseup',function() { $(this).removeClass('focus'); });

		items
			.die('click.Slideshow')
			.live('click.Slideshow',function(e) {
				var onItemClick = self.__method__('onItemClick');
			
				if(onItemClick === true) {
					return onItemClick; }
			
				var	class_name = self.options.class_names.item,
					item = $(e.target).closest('.'+class_name),
					item_index = item.length && self.items.index(item[0]);			

				if (item_index !== false) {
					self.go_to( item_index ); }
			
				return onItemClick;
			});

				
		if(this.__method__('init',this) === false){
			return this;}
					
		return this.go_to(0);
	};
	
	Slideshow.prototype.next = function() {
		this.go_to(this.current_item + 1);		
		return this;
	};
	
	Slideshow.prototype.previous = function() {
		this.go_to(this.current_item - 1);	
		return this;
	};
	
	Slideshow.prototype.go_to = function(item_index) {
		var	self = this,
			options = this.options,
			controls = options.controls,
			scrollOptions = $.extend({},this.options.scrollOptions),
			sci = this.current_item;
		
		if( item_index === this.current_item || this.__method__('beforeChange',self,sci,item_index) === false ) {
			return false;}
	
		if(item_index >= this.items.length){
			item_index = 0; }			
					
		if(item_index < 0){
			item_index = this.items.length - 1; }	
	
		var next = controls.next.removeClass('inactive'),
			previous = controls.previous.removeClass('inactive'),
			new_item = this.items.eq(item_index).addClass(options.class_names.item_active),
			previous_item = this.items.eq(this.current_item).removeClass(options.class_names.item_active);
		
		item_index === 0 && previous.addClass('inactive');
		item_index === this.items.length - 1 && next.addClass('inactive');
		
		if(options.change) {
			scrollOptions.onAfter = function() {
				return self.__method__('change',self,sci,item_index);
			}
		}
		
		this.wrapper.scrollTo(new_item, scrollOptions);			

		this.current_item = item_index;		
		
		return this;
	};
	
	return this.each(function() {
		this.slideshow = new Slideshow(this,options);
	});
};

/*! jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2009 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 5/25/2009
 * @author Ariel Flesler
 * @version 1.4.2
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(d){var k=d.scrollTo=function(a,i,e){d(window).scrollTo(a,i,e)};k.defaults={axis:'xy',duration:parseFloat(d.fn.jquery)>=1.3?0:1};k.window=function(a){return d(window)._scrollable()};d.fn._scrollable=function(){return this.map(function(){var a=this,i=!a.nodeName||d.inArray(a.nodeName.toLowerCase(),['iframe','#document','html','body'])!=-1;if(!i)return a;var e=(a.contentWindow||a).document||a.ownerDocument||a;return d.browser.safari||e.compatMode=='BackCompat'?e.body:e.documentElement})};d.fn.scrollTo=function(n,j,b){if(typeof j=='object'){b=j;j=0}if(typeof b=='function')b={onAfter:b};if(n=='max')n=9e9;b=d.extend({},k.defaults,b);j=j||b.speed||b.duration;b.queue=b.queue&&b.axis.length>1;if(b.queue)j/=2;b.offset=p(b.offset);b.over=p(b.over);return this._scrollable().each(function(){var q=this,r=d(q),f=n,s,g={},u=r.is('html,body');switch(typeof f){case'number':case'string':if(/^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(f)){f=p(f);break}f=d(f,this);case'object':if(f.is||f.style)s=(f=d(f)).offset()}d.each(b.axis.split(''),function(a,i){var e=i=='x'?'Left':'Top',h=e.toLowerCase(),c='scroll'+e,l=q[c],m=k.max(q,i);if(s){g[c]=s[h]+(u?0:l-r.offset()[h]);if(b.margin){g[c]-=parseInt(f.css('margin'+e))||0;g[c]-=parseInt(f.css('border'+e+'Width'))||0}g[c]+=b.offset[h]||0;if(b.over[h])g[c]+=f[i=='x'?'width':'height']()*b.over[h]}else{var o=f[h];g[c]=o.slice&&o.slice(-1)=='%'?parseFloat(o)/100*m:o}if(/^\d+$/.test(g[c]))g[c]=g[c]<=0?0:Math.min(g[c],m);if(!a&&b.queue){if(l!=g[c])t(b.onAfterFirst);delete g[c]}});t(b.onAfter);function t(a){r.animate(g,j,b.easing,a&&function(){a.call(this,n,b)})}}).end()};k.max=function(a,i){var e=i=='x'?'Width':'Height',h='scroll'+e;if(!d(a).is('html,body'))return a[h]-d(a)[e.toLowerCase()]();var c='client'+e,l=a.ownerDocument.documentElement,m=a.ownerDocument.body;return Math.max(l[h],m[h])-Math.min(l[c],m[c])};function p(a){return typeof a=='object'?a:{top:a,left:a}}})(jQuery);

