var parameters = {
	ease: null,
	speed:0,
	height: 100,
	width: 140,
	titleOpacity : .60,
	direction : 'horizontal', // vertical horizontal diagonal
	num_item : 1,
	autoscroll: 'true'
};

jq.fn.jqGalScroll = function(options){
    var current = 0;
    
	return this.each(function(i){
		var el = this;
		el.curImage = 0;
		el.jqthis = jq(this).css({position:'relative'});
		el.jqchildren = el.jqthis.children();
		el.opts = jq.extend({}, parameters, options);
		el.index = i;
		el.totalChildren = el.jqchildren.size();
		el.total = el.totalChildren / num_item;
		var width,height;
		
		var num_item = el.opts.num_item;
		if (num_item > el.totalChildren) num_item = el.totalChildren;
		
		switch(el.opts.direction){
			case 'horizontal':
				width = el.totalChildren * el.opts.width;
				height = el.opts.height;
				break;
			case 'vertical':
				width = el.opts.width;
				height = el.totalChildren * el.opts.height;
				break;
			default:
				width = el.totalChildren * el.opts.width;
				height = el.totalChildren * el.opts.height;
				break;
		};
		
		el.container = jq('<div class="jqGSContainer">').css({position:'relative'});
		el.ImgContainer = jq('<div class="jqGSImgContainer" style="height:'+el.opts.height+'px;position:relative;overflow:hidden">')
							.css({height:el.opts.height,width:el.opts.width,position:'relative',overflow:'hidden'});
		el.jqthis.css({height:height,width:width});
		
		el.jqthis.wrap(el.container);
		el.jqthis.wrap(el.ImgContainer);
		
		var jqaPrev = jq('.portletSlidingPortlet .sliding-prev').click(function(){
		    current = el.index;
                if (current == 0){
                    current = el.totalChildren - 1;
                }
                else current = current - num_item;
                var params = {};
                if( el.opts.direction == 'diagonal'){
                 params = {right:(el.opts.width*current),bottom:(el.opts.height*current)}
                }
                else if( el.opts.direction == 'vertical'){
                 params = {bottom:(el.opts.height*current)}
                }
                else if( el.opts.direction == 'horizontal'){
                 params = {right: (el.opts.width / num_item * current)}
                };
                el.jqthis.stop().animate(params,el.opts.speed, el.opts.ease);
                if (el.opts.autoscroll == true){
                    clearTimeout(timeId);
                    timeId = setInterval(Next, 5000);
                }
                    el.index = current;
                return false;
        });

        function Next(){
            current = el.index;
            if(num_item < el.totalChildren){
                if (current  >= el.totalChildren - 1) 
                    current = 0;
                else 
                    current = current + num_item;
                var params = {};
                if( el.opts.direction == 'diagonal'){
                	params = {right:(el.opts.width / num_item * current),bottom:(el.opts.height / num_item * current)}
                }
                else if( el.opts.direction == 'vertical'){
                	params = {bottom:(el.opts.height / num_item * current)}
                }
                else if( el.opts.direction == 'horizontal'){
                	params = {right:(el.opts.width / num_item * current)}
                };

                el.jqthis.stop().animate(params,el.opts.speed, el.opts.ease);
                el.index = current;
            }
        }
		if (el.opts.autoscroll == true){
		    var timeId = setInterval(Next, 5000);
		}
		var jqaNext = jq('.portletSlidingPortlet .sliding-next').click(function(){
		    Next();
		    if (el.opts.autoscroll == true){
		        clearTimeout(timeId);
    		    timeId = setInterval(Next, 5000);
    		}
    		return false;
		});
		
		var pos = {x:0,y:0};
		el.jqchildren.each(function(j){
		    
		    var li_height = el.opts.height;
		    var li_width = el.opts.width;
			var selected = '';
			if( el.opts.direction == 'diagonal'){
			    li_width /= num_item;
			    li_height /= num_item;
				pos.x = j * li_width;
				pos.y = j * li_height;
			}
			else if( el.opts.direction == 'horizontal'){
			    li_width /= num_item;
				pos.x = j * li_width;
			}
			else if( el.opts.direction == 'vertical'){
			    li_height /= num_item;
				pos.y = j * li_height;
			};
            
			var jqchild = jq(this).css({height:li_height,width:li_width,position:'absolute',left:pos.x, top:pos.y});


		});
        
	}); // end : this.each(function()
};  // end : $.fn.jqGalScroll
