;jQuery.itemSlider || (function($) {
	// make this work more than once on different objects
	$.itemSlider = {
			_object: {},
			_params : {
				itemFadeTime:600,
				shown: 'all items',
				currentTab: 0,
				shownItmes: '',
				rows: 0,
				obj: {},
				width: 0,
				height: 0,
				itemCount: 0,
				tabCount:0,
				itemsPerTabInRow:0,
				itemsPerRow:0,
				item: {
					layoutHeight:0,
					layoutWidth:0,
					height:0,
					widht: 0,
					'margin-top':0,
					'margin-left':0,
					'margin-right':0,
					'margin-bottom':0
				},
				pagination: {
					selector: '',
					prev:{},
					next:{},
					item:{},
					seperator: {}
				}
			},
			start: function( obj ,options ){
				var self = this;
				$.extend(self,obj);
				self._params.obj = self;
				if( typeof( options ) != 'undefined' ){
					( typeof( options.width ) != 'undefined' ) ? self._params.width = options.width : self._params.width = self.width();
					( typeof( options.height ) != 'undefined' ) ? self._params.height = options.height : self._params.height = self.height();
					if ( typeof( options.itemFadeTime ) != 'undefined' ){ 
						self._params.itemFadeTime = options.itemFadeTime ;
					}
					if( typeof( options.item ) != 'undefined' ){
						( typeof( options.item.width ) != 'undefined' ) ? self._params.item.width = options.item.width : self._params.item.width = self.find(":first").find(":first").width();
						( typeof( options.item.height ) != 'undefined' ) ? self._params.item.height = options.item.height : self._params.item.height = self.find(":first").find(":first").height();
						( typeof( options.item['margin-top'] ) != 'undefined' ) ? self._params.item['margin-top'] = options.item['margin-top'] : self._params.item['margin-top'] = parseInt(self.find(":first").find(":first").css('margin-top'));
						( typeof( options.item['margin-left'] ) != 'undefined' ) ? self._params.item['margin-left'] = options.item['margin-left'] : self._params.item['margin-left'] = parseInt(self.find(":first").find(":first").css('margin-left'));
						( typeof( options.item['margin-right'] ) != 'undefined' ) ? self._params.item['margin-right'] = options.item['margin-right'] : self._params.item['margin-right'] = parseInt(self.find(":first").find(":first").css('margin-right'));
						( typeof( options.item['margin-bottom'] ) != 'undefined' ) ? self._params.item['margin-bottom'] = options.item['margin-bottom'] : self._params.item['margin-bottom'] = parseInt(self.find(":first").find(":first").css('margin-bottom'));
					} else {
						self._params.item.width = 				$( self ).find(":first").find(":first").width();
						self._params.item.height = 				$(self).find(":first").find(":first").height();
						self._params.item['margin-top'] = 		parseInt($(self).find(":first").find(":first").css('margin-top'));
						self._params.item['margin-left'] = 		parseInt($(self).find(":first").find(":first").css('margin-left'));
						self._params.item['margin-right'] = 	parseInt($(self).find(":first").find(":first").css('margin-right'));
						self._params.item['margin-bottom'] = 	parseInt($(self).find(":first").find(":first").css('margin-bottom'));		
					}
					// if rows undefined in options then calculate depending on item height and wraper height
					self._params.item.layoutHeight = self._params.item.height + self._params.item['margin-top'] + self._params.item['margin-bottom'];
					//console.debug('layoutHeight = ' + self._params.item.layoutHeight);
					if( ( self._params.height % self._params.item.layoutHeight ) < self._params.item.height ){
						( typeof( options.rows ) != 'undefined' ) ? self._params.rows = options.rows : self._params.rows = parseInt( self._params.height / self._params.item.layoutHeight );
					} else {
						( typeof( options.rows ) != 'undefined' ) ? self._params.rows = options.rows : self._params.rows = parseInt( self._params.height / self._params.item.layoutHeight ) + 1;
					}
				} else {
					self._params.width = 					$(self).width();
					self._params.height = 					$(self).height();
					self._params.item.width = 				$(self).find(":first").find(":first").width();
					self._params.item.height = 				$(self).find(":first").find(":first").height();
					self._params.item['margin-top'] = 		parseInt($(self).find(":first").find(":first").css('margin-top'));
					self._params.item['margin-left'] = 		parseInt($(self).find(":first").find(":first").css('margin-left'));
					self._params.item['margin-right'] = 	parseInt($(self).find(":first").find(":first").css('margin-right'));
					self._params.item['margin-bottom'] = 	parseInt($(self).find(":first").find(":first").css('margin-bottom'));
					self._params.item.layoutHeight = 		self._params.item.height + self._params.item['margin-top'] + self._params.item['margin-bottom'];
					if( ( self._params.height % self._params.item.layoutHeight ) < self._params.item.height ){
						self._params.rows = 					parseInt( self._params.height / self._params.item.layoutHeight );
					} else {
						self._params.rows = 					parseInt( ( self._params.height / self._params.item.layoutHeight ) + 1 );
					}
				}
				//TODO: apply margins to item
				self._params.itemHolder = $(self).find(':first');
				AAA = self._params.itemHolder;
				$( self._params.obj ).css( {'overflow':'hidden', 'position':'relative'});
				$( self._params.itemHolder ).css({'width':'9999px','overflow-y':'hidden','position':'relative','height': self._params.height});
				self._params.itemCount = $( self ).find(":first").children().length;
				// seting itemsPerTabInRow and seting tabCount				
				self.calculateTabCount();
				$( self ).find(":first").children().addClass('item').css('position','absolute');
				self.arangeItems( true );
				// if pagination required
				if( typeof( options.pagination ) != 'undefined' ){
					self._params.pagination.prev = $( options.pagination ).children(":first").clone().click(function(){ $.itemSlider.prevItems(); });
					self._params.pagination.next = $( options.pagination ).children(":last").clone().click(function(){ $.itemSlider.nextItems(); });		
					self._params.pagination.item = $( options.pagination ).children().eq(1).clone();
					self._params.pagination.seperator = $( options.pagination ).find(".seperator").clone();
					self._params.pagination.selector = options.pagination;
					self.makePagination();
				}
				
			},
			makePagination: function(){
				$( $.itemSlider._params.pagination.selector ).empty();
				if( $.itemSlider._params.tabCount < 2 ){
					$( $.itemSlider._params.pagination.selector ).css('display','none');
				} else {
					$( $.itemSlider._params.pagination.selector ).css('display','');
					$( $.itemSlider._params.pagination.selector ).append( $( $.itemSlider._params.pagination.prev ) );
					$( $.itemSlider._params.pagination.selector ).append( $( document.createElement('span') ).addClass('span_pagination_item_holder') );
					$.itemSlider.createPaginationItems();
					$( $.itemSlider._params.pagination.selector ).append( $( $.itemSlider._params.pagination.next ) );
				}
			},
			createPaginationItems: function(){
				$( $.itemSlider._params.pagination.selector ).find('.span_pagination_item_holder').empty();
				for( var i = 0; i < $.itemSlider._params.tabCount; i++ ){
					(function(){
						var temp = i;
					$( $.itemSlider._params.pagination.selector ).find('.span_pagination_item_holder').append(
							$( $.itemSlider._params.pagination.seperator ).clone()
					).append( 
							$( $.itemSlider._params.pagination.item ).clone().addClass( ( temp == $.itemSlider._params.currentTab )? 'active':'' )
							.addClass( 'pagination_item_' + temp )
							.click( function(){ $.itemSlider.sliderGoTo( temp ); } )
					);
					})();
				}	
				$( $.itemSlider._params.pagination.selector ).find('.span_pagination_item_holder').append( $( $.itemSlider._params.pagination.seperator ).clone() );
			},
			arangeItems : function( firstTime ){
				var visible = '';
//				if ( firstTime !== true ){
//					visable = ':visible';
//				}
				if (this._params.shown != 'all items')
					visible = '.'+this._params.shown;
				var cols = this._params.itemsPerTabInRow;
				var rows = this._params.rows;
				var itemLayoutWidth = this._params.item.layoutWidth;
				var itemlayoutHeight = this._params.item.layoutHeight;
				this._params.itemHolder.children( '.item' + visible ).each(
					function(index, item){
						$( this ).animate( { top : ( ( parseInt( index / cols ) % rows ) * itemlayoutHeight ) + 'px',
										 left : ( ( ( index % cols ) + ( parseInt( index / ( cols * rows ) ) * cols  ) ) * itemLayoutWidth ) + 'px'
									},1000 );
					}	
				);
			},
			calculateTabCount : function(){
				this._params.item.layoutWidth = this._params.item.width + this._params.item['margin-left'] + this._params.item['margin-right'];
				if( this._params.width % this._params.item.layoutWidth != 0 ){
					this._params.itemsPerTabInRow = parseInt( this._params.width / this._params.item.layoutWidth ) + 1;
				} else {
					this._params.itemsPerTabInRow = parseInt( this._params.width / this._params.item.layoutWidth );
				}
//				console.debug( $.itemSlider._params.itemCount + ' ' + $.itemSlider._params.rows + ' ' + $.itemSlider._params.itemsPerTabInRow);
				if( ( $.itemSlider._params.itemCount / $.itemSlider._params.rows ) % $.itemSlider._params.itemsPerTabInRow != 0 ){
					$.itemSlider._params.tabCount = parseInt( ( $.itemSlider._params.itemCount / $.itemSlider._params.rows ) / $.itemSlider._params.itemsPerTabInRow ) + 1;
				} else {
					$.itemSlider._params.tabCount = parseInt( ( $.itemSlider._params.itemCount / $.itemSlider._params.rows ) / $.itemSlider._params.itemsPerTabInRow );
				}				
			},
			pagActivateItem: function( index ){
				$( this._params.pagination.selector ).find('.span_pagination_item_holder .item.active').removeClass('active');
				$( this._params.pagination.selector ).find('.span_pagination_item_holder .pagination_item_' + index).addClass('active');
			},
			nextItems: function(  ){
				if( $.itemSlider._params.currentTab + 1 < $.itemSlider._params.tabCount ){
					$.itemSlider.sliderGoTo( parseInt( $.itemSlider._params.currentTab ) + 1 );
				}
			},
			prevItems: function(  ){
				if( $.itemSlider._params.currentTab != 0 ){
					$.itemSlider.sliderGoTo( parseInt( $.itemSlider._params.currentTab ) - 1 );
				}
			},
			recalculate: function(){
				var visible = '';
				if (this._params.shown != 'all items')
					visible = '.'+this._params.shown;
				$.itemSlider._params.itemCount = $.itemSlider._params.itemHolder.find('.item' + visible).length;
				if( $.itemSlider._params.itemCount % $.itemSlider._params.rows != 0 ){
					$.itemSlider._params.tabCount = ( $.itemSlider._params.itemCount / $.itemSlider._params.rows ) + 1;
				} else {
					$.itemSlider._params.tabCount = $.itemSlider._params.itemCount / $.itemSlider._params.rows;
				}
				$.itemSlider._params.itemHolder.css( 'margin-left','0');
				$.itemSlider._params.currentTab = 0;
				$.itemSlider.calculateTabCount();
				$.itemSlider.createPaginationItems();
				$.itemSlider.arangeItems();
			},
			sliderGoTo: function( tabIndex ){
				this._params.itemHolder.stop();
				this._params.itemHolder.animate( {'margin-left' : - ( tabIndex ) * this._params.width }, 1000 );
				this._params.currentTab = tabIndex;
				this.pagActivateItem( tabIndex );
			},
			showAll: function(element){
				$.itemSlider._params.itemHolder.stop();
				if ( this._params.shown != 'all items' ){
					this._params.itemHolder.children().stop(true,true);
					this._params.shown = 'all items';
					$(element).addClass('active').siblings().removeClass('active');
					this._params.itemHolder.children().fadeIn($.itemSlider._params.itemFadeTime ,function(){$(this).show().css({filter:''})});
					$.itemSlider.recalculate();
				}
			},			
			showItemsByClass: function( className, element ){
				$( $.itemSlider._params.itemHolder ).stop();
				if (this._params.shown != className){
					this._params.itemHolder.children().stop(true,true);
					this._params.shown = className
					$(element).addClass('active').siblings().removeClass('active');
					this._params.itemHolder.children().not( '.' + className ).fadeOut($.itemSlider._params.itemFadeTime ,function(){$(this).hide().css({filter:''});});
					this._params.itemHolder.find( '.' + className ).fadeIn($.itemSlider._params.itemFadeTime, function(){$(this).show().css({filter:''});});
					$.itemSlider.recalculate();
					//this.recalculate();
				}
			}
			
	};
	$.fn.extend({
		createSlider: function( options ){
			if( typeof( options ) != 'undefined' ){
				$.itemSlider.start( this, options );
			} else {
				$.itemSlider.start( this );
			}
		},
		sliderNext: function(){
			$.itemSlider.nextItems();
		},
		sliderPrev: function(){
			$.itemSlider.prevItems();
		}
	});
})(jQuery);
