(function($) {

$.fn.simplyScroll = function(o) {
	return this.each(function() {
		new $.simplyScroll(this,o);
	});
};

var defaults = {
	className: 'simply-scroll',
	frameRate: 24, //No of movements per second
	speed: 2, //No of pixels per frame
	horizontal: true,
	autoMode: 'off', //disables buttons 'loop','bounce'
	pauseOnHover: true,
	startOnLoad: true, //use this if having rendering problems (safari 3 + Mac OSX?)
	localJsonSource: '', //format [{"src":"images/pic.jpg","title":"title","link":"http://"},{etc..}]
	flickrFeed: '',
	jsonImgWidth: 240,
	jsonImgHeight: 180
};
	
$.simplyScroll = function(el,o) {
	
	var self = this;
	
	this.o = $.extend({}, defaults, o || {});
	this.auto = this.o.autoMode!=="off" ? true : false;
	
	//called on ul/ol/div etc
	this.$list = $(el);
	
	//generate extra markup
	this.$list.addClass('simply-scroll-list')
		.wrap('<div class="simply-scroll-clip"></div>')
		.parent().wrap('<div class="' + this.o.className + ' simply-scroll-container"></div>');
	

		this.$list.parent().parent()
		.prepend('<div class="simply-scroll-forward"><image src="./images/Gradient_right.png" height="198"  width="25" onload="fixPNG(this);"></div>')
		.prepend('<div class="simply-scroll-back"><image src="./images/Gradient.png" height="198"  width="25" onload="fixPNG(this);"></div>');
	if (!this.o.startOnLoad) {
			this.init();
		} else {
			//wait for load before completing setup
			$(window).load(function() { self.init();  });
		}

	
			
};
	
$.simplyScroll.fn = $.simplyScroll.prototype = {};

$.simplyScroll.fn.extend = $.simplyScroll.extend = $.extend;

$.simplyScroll.fn.extend({
	init: function() {
		//shortcuts
	
		this.$items = this.$list.children();
		this.$clip = this.$list.parent();
		this.$container = this.$clip.parent();
		this.itemMax = this.$items[0].offsetWidth;
		this.clipMax = this.$clip.width();			
		this.dimension = 'width';
	
		this.posMin = 0;
		this.posMax = this.$items.length * this.itemMax;
		this.$list.css(this.dimension,this.posMax +'px');
	
		
		if (this.o.autoMode=='loop') {
			var addItems = Math.ceil(this.clipMax / this.itemMax);	
			this.$items.slice(0,addItems).clone(true).appendTo(this.$list);
			this.posMax += (this.clipMax - this.o.speed);
			this.$list.css(this.dimension,this.posMax+(this.itemMax*addItems) +'px');
		}
		
		this.interval = null;	
	
		
		//ensure that speed is divisible by item width
		while (this.itemMax % this.o.speed !== 0) {
			this.o.speed--;
			if (this.o.speed===0) {
				this.o.speed=1; break;	
			}
		}
		
		var self = this;
		this.trigger = null;
		this.$timer = null;
		
		this.intervalDelay = Math.floor(1000 / this.o.frameRate);
		this.funcMoveBack = function() { self.trigger=this;self.moveBack(); };
		this.funcMoveForward = function() { self.trigger=this;self.moveForward(); };
		this.funcMoveStop = function() { self.moveStop(); };
		this.funcMoveResume = function() { self.moveResume(); };
		this.moveBackClass = 'simply-scroll-btn-left';
		this.moveForwardClass = 'simply-scroll-btn-right';
		this.$btnBack = $('.simply-scroll-back',this.$container)
		.addClass('simply-scroll-btn' + ' ' + this.moveBackClass);
		this.$btnForward = $('.simply-scroll-forward',this.$container)
		.addClass('simply-scroll-btn' + ' ' + this.moveForwardClass);
		this.$is_moving = 0;
		
		
		this.$container.mouseout(function(e)
		{
			
			var x = e.pageX - this.offsetLeft;
			var y = e.pageY - this.offsetTop;
			this.$timer = null;
			this.is_moving = 0;
			clearInterval(this.timer);
			//$('#divCoord').html(x +', '+ y + ' ' + $is_moving + ' Timer = ' + this.$timer + ' OUT');
			self.o.speed = 6.5;
		}
		
		);
		
		
		
		this.$container.mousemove(function(e){
				var x = e.pageX - this.offsetLeft;
				if (x >= 877 || x <= 100)
					self.o.speed = 10;
				else if (x > 388 && x < 588)
					clearInterval(this.timer);
				else
					self.o.speed = 6.5;
										   
		   });
										   
	
		this.$container.mouseover(function(e){

				var x = e.pageX - this.offsetLeft;
				var y = e.pageY - this.offsetTop;

				//$('#divCoord').html(x +', '+ y + ' ' + $is_moving + ' Timer = ' + this.$timer);	
		
				if (x >= (588))
				{
					
					//$('#divCoord').html(x +', '+ y + ' ' + $is_moving + ' Timer = ' + this.$timer);	
					if (this.is_moving == 0)
					{
						this.is_moving = 1;
						this.timer = setInterval(function()
						 {
					 
							if (self.$clip[0].scrollLeft < (self.posMax - self.clipMax))
							{
								self.$clip[0].scrollLeft += self.o.speed; 
							
							}
							else
							{
								clearInterval(this.timer);
								
							}
						},self.intervalDelay);
					}
				}
				else if ( x <= 388)
				{
	
					//$('#divCoord').html(x +', '+ y + ' ' + $is_moving + ' Timer = ' + this.$timer);	
					if (this.is_moving == 0)
					{
					this.is_moving = 1;
					this.timer = setInterval(function() {
					if (self.$clip[0].scrollLeft>0) 
					{
						
						self.$clip[0].scrollLeft -= self.o.speed;
					} 
					else
					{
						clearInterval(this.timer);
					}
				
					},self.intervalDelay);
					}

					
				}
				else
				{
						this.is_moving = 0;
						self.o.speed = 6.5;
						clearInterval(this.timer);

				//$('#divCoord').html(x +', '+ y + ' ' + $is_moving + ' Timer = ' + this.$timer);	
					
				}

			   }
		   );
		  
	},
	


	
	moveForward: function() {
		
	},
	
	moveBack: function() {
		
	},
	
	
	moveStop: function(moveDir) {
		$is_moving='stop';
		//clearInterval(this.this.$timer);
		},
		
		
	moveResume: function() {
		this.movement=='forward' ? this.moveForward() : this.moveBack();
	},

	resetPos: function() {
		if (!this.o.horizontal) {
			this.$clip[0].scrollTop = 0;
		} else {
			this.$clip[0].scrollLeft = 0;
		}
	},
	renderData: function(json) {
		if (json.length>0) { //render json data
			var self = this;
			$.each(json, function(i,item) {
				$("<img/>").attr({
					src: item.src,
					title: item.title,
					alt: item.title,
					width: self.o.jsonImgWidth,
					height: self.o.jsonImgHeight
				}).appendTo(self.$list);
			});
			this.init();
		}
	}
});
		  
})(jQuery);
