// Copyright 2010 htmldrive.net Inc.
/**
 * @projectHomepage http://www.htmldrive.net/go/to/simple-images-scroller
 * @projectDescription Simple images scroller with jquery plugin
 * @author htmldrive.net
 * More script and css style : htmldrive.net
 * @version 1.0
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */
(function(a){
    a.fn.scroller_roll=function(p){
        var p=p||{};

        var g=p&&p.time_interval?p.time_interval:"100";
        var h=p&&p.title_show?p.title_show:"enable";
        var i=p&&p.window_background_color?p.window_background_color:"white";
        var j=p&&p.window_padding?p.window_padding:"5";
        var k=p&&p.border_size?p.border_size:"1";
        var l=p&&p.border_color?p.border_color:"black";
        var m=p&&p.images_width?p.images_width:"45";
        var n=p&&p.images_height?p.images_height:"70";
        var o=p&&p.title_size?p.title_size:"12";
        var q=p&&p.title_color?p.title_color:"blue";
        var r=p&&p.show_count?p.show_count:"5";
        var i_m = p&&p.images_margin?p.images_margin:"3";
		var K_direction = p&&p.K_direction?p.K_direction:"left";
		var divwidth = p&&p.divwidth?p.divwidth:"0";
        j += "px";
        k += "px";
        m += "px";
        n += "px";
        o += "px";
        var s;
        var t=0;
        var u;
        var v;
        var w;
        var x;
        var y=a(this);
        var z=y.children("ul").children("li").length;
        var A=Math.ceil(z/r);		
		
		var original_speed = g;
		
        if(y.find("ul").length==0||a("li").length==0){
            dom.append("Require content");
            return null
            }
            y.children("ul").children("li").children("a").children("img").css("width",m).css("height",n);
        if(h=='enable'){
            y.children("ul").children("li").children("a").each(function(){
                $(this).append('<br/>'+$(this).attr("title"))
                })
            }
            s_s_ul(y,j,k,l,i);
        m=parseInt(m);
        y.children("ul").children("li").css("width",m+"px");
        y.children("ul").children("li").css("margin-left",i_m+"px");
        y.children("ul").children("li").children("a").css("color",q);
        y.children("ul").children("li").children("a").css("font-size",o);
        y.hover(function(){
            //stop()
        },function(){
            //play()
        });
		
		y.mousemove(function(e){
		  //var clientCoords = "( " + (e.clientX - y.offset().left) + ", " + (e.clientY - y.offset().top) + " )";
		  //console.log();
		  mouseX = e.clientX - y.offset().left;
		  mouseY = e.clientY - y.offset().top;
		  
		  total_width = m*r+i_m*r;
		  
		  if (mouseX > total_width/2){
			  if (K_direction == 'left'){
				  changeDirection('right');
			  }
			  
			  g = original_speed-parseInt(original_speed/total_width*(mouseX-total_width/2))*2;
			  if (g < 5 ){
				g = 5;  
			  }
		  } else {
			  if (K_direction == 'right'){
			  	changeDirection('left');
			  }
			  g = original_speed-parseInt(original_speed/total_width*(total_width/2-mouseX))*2;
			  if (g < 5 ){
				g = 5;  
			  }
		  }
		  
		});

        begin();
        play();
        function begin(){
            //x=y.children("ul").width();
			x = m*z + i_m*z;
            y.children("ul").children("li").hide();
            y.children("ul").children("li").slice(0,r).show();
			if (divwidth == 0){
            	u=y.children("ul").outerWidth();
			} else {
				u = divwidth;	
			}
            v=y.children("ul").outerHeight();
            y.children("ul").width(x);
            y.width(u);
            y.height(v);
            y.children("ul").children("li").show();
            y.css("position","relative");
            y.children("ul").css("position","absolute");
            y.children("ul").clone().prependTo(y);
            j=parseInt(j);
            y.children("ul").css("left","0px");
            y.find("ul:last").css("left",x);
            w=x-i_m;
			y.css("display","block");
            }
            function play(){
				if (K_direction == "left"){
					if(parseInt(y.find("ul").eq(t).css("left"))<=-(x)){
						var a=x;
						y.find("ul").eq(t).css("left",a);
						if(t==0){
							t=1
						}else{
							t=0
						}
					}
				
					y.find("ul").each(function(){
						$(this).css("left",(parseInt($(this).css("left"))-1)+"px");
					});
					s=setTimeout(play,g)
				} else {
					if(parseInt(y.find("ul").eq(t).css("left"))>=0){
						var a=-x;
						if(t==0){
							t=1
						}else{
							t=0
						}
						y.find("ul").eq(t).css("left",a);	
					}
				
					y.find("ul").each(function(){
						$(this).css("left",(parseInt($(this).css("left"))+1)+"px");
					});
					s=setTimeout(play,g)
				}
    	    }
			
			function changeDirection(direct){
				K_direction = direct;	
			}
        function stop(){
        clearTimeout(s)
        }
        function s_s_ul(a,b,c,d,e){
        b=parseInt(b);
        c=parseInt(c);
        var f="border: "+d+" solid "+" "+c+"px; padding:"+b+"px; background-color:"+e;
        a.attr("style",f)
        }
    }
})(jQuery);
