/*
 * jQuery tsign rotator
 */
(function($) {
$.widget('tsign.backgroundslider', {

	_init: function() {
		var self = this;
		this._items = this.options.items;
		this._current = 0;
		$.each(this._items, function(i){
			var obj = this;
			var img = new Image();
			obj.loaded = false;
	    	$(img).load(function () {
	            //$(this).css('display', 'none'); // .hide() doesn't work in Safari when the element isn't on the DOM already
	            $(this).hide();
	            $('#loader').removeClass('loading').append(this);
	            obj.image = img;
		    	$(self.options.imageContainerTrigger).append(img);
		    	if(i == 0) {
		    		$(this).fadeIn();
		    		
		    	} else {
		    		$(this).css('left', '-2000px');
		    		$(this).show();
		    	}
		    	obj.loaded = true;
	        }).error(function () {
	            // notify the user that the image could not be loaded
	        	obj.image = null;
	        }).attr('src', this.bg);
	    	
	    	if(i > 0) {
	    		$(this.trigger).css('left', '-469px').removeClass('hide');
	    		$(this.trigger + '_info').addClass('hide');	
		    }
	    	
	    	var links = $(this.trigger).find(self.options.paginationTrigger).children().each(function(){
	    		$(this).click(function(){
	    			var trigger = parseInt($(this).find('span').html()) -1;
	    			if(trigger != self._current && self._items[trigger].loaded) {
	    				self._set(trigger);
	    			}
	    			return false;
	    		});
	    	});
	    });
	},
	
	_set: function(i) {
		var self = this;
		$(this.options.paginationTrigger).each(function(){
			var items = $(this).children();
			$(items[self._current]).removeClass('active');
			$(items[i]).addClass('active');
		});
		$('.slide_info').addClass('hide');
		$(this._items[i].trigger + '_info').removeClass('hide');
		
		this._animate(this._current, i);
		this._current = i;
	},
	
	_setNext: function() {
		var self = this;
	},
	
	_setPrev: function() {
		var self = this;
	},
	
	_animate :function(old, current) {
		var self = this;
		var oldImage = $(this._items[old].image);
		var oldBox = $(this._items[old].trigger);
		var currentImage = $(this._items[current].image);
		var currentBox = $(this._items[current].trigger);
		
		oldImage.animate({left: 2000}, self.options.speed);
		currentImage.css('left', -2000);
		currentImage.animate({left: 0}, self.options.speed);
		oldBox.animate({left: -469}, self.options.speed);
		currentBox.animate({left: 0}, self.options.speed);
	}
	
	
});
icanslider = $.extend($.tsign.backgroundslider, {
	version: '1.0.0',
	defaults: {
		imageContainerTrigger : '#bg_photos',
		paginationTrigger: '.pagination',
		speed: 500
	}
});

})(jQuery);

