/*
 * simplyscroll for EG
 
 customize version
 
 2010.02.24 es
 */

(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: 1, //No of pixels per frame
	horizontal: false,//true
	autoMode: 'loop', //disables buttons 'off','bounce','loop'
	pauseOnHover: true,
	startOnLoad: false, //use this if having rendering problems (safari 3 + Mac OSX?)
	localJsonSource: '', //format [{"src":"images/pic.jpg","title":"title","link":"http://"},{etc..}]
	flickrFeed: null,
	jsonImgWidth: 232,
	jsonImgHeight: 139
};
	
$.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>');
	
	if (!this.o.auto) { //button placeholders
		this.$list.parent().parent()
		.prepend('<div class="simply-scroll-forward"></div>')
		.prepend('<div class="simply-scroll-back"></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();
		
		if (!this.o.horizontal) {
			this.itemMax = this.$items.eq(0).outerHeight(true); 
			this.clipMax = this.$clip.height();
			this.dimension = 'height';			
			this.moveBackClass = 'simply-scroll-btn-up';
			this.moveForwardClass = 'simply-scroll-btn-down';
		} else {
			this.itemMax = this.$items.eq(0).outerWidth(true);
			this.clipMax = this.$clip.width();			
			this.dimension = 'width';
			this.moveBackClass = 'simply-scroll-btn-left';
			this.moveForwardClass = 'simply-scroll-btn-right';
		}
		
		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;	
		this.intervalDelay = Math.floor(1000 / this.o.frameRate);
		
		//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.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.funcMoveBack2 = function() { self.trigger=this;self.moveFastUp(); };
		this.funcMoveForward2 = function() { self.trigger=this;self.moveFastDw(); };
		this.funcMoveResume2 = function() { self.moveResume2(); };
		
		if (this.auto) {
			if (this.o.pauseOnHover) {
				this.$clip.hover(this.funcMoveStop,this.funcMoveResume);
			}
			
			//----------------------------------
			// initialize of evergradsTV
			
			//moveFastUp
			$("#up").hover(this.funcMoveBack2,this.funcMoveResume2);//this.moveFastUp()
			$("#dw").hover(this.funcMoveForward2,this.funcMoveResume2);//this.moveFastUp()
			//$("#dw").mousedown(this.funcMoveForward2).mouseup(this.funcMoveResume2).mouseout(this.funcMoveResume2);
			//$("#up").mousedown(this.funcMoveBack2).mouseup(this.funcMoveResume2).mouseout(this.funcMoveResume2);
			
			/*
			$("#youtube-player-container").html('<iframe id="ytplayer1" width="540" height="334" src="http://www.youtube.com/embed/XpVc5R5Qgwo?rel=0&amp;hd=1&amp;enablejsapi=1&amp;playerapiid=ytplayer1&amp;autoplay=1" frameborder="0" allowfullscreen></iframe>');
			*/
			//--play startup movie ---
			var params = { allowScriptAccess: "always" };
			var atts = { id: "myytplayer1" };
			swfobject.embedSWF("http://www.youtube.com/v/4PlHK-HdnV4&enablejsapi=1&playerapiid=ytplayer1&autoplay=1&rel=0&hd=1&showinfo=0",
				 "youtube-player-container", "540", "330", "8", null, null, params, atts);
			//$("#defImg").hide();
			
			//-- evergradsTV carousel action ---------------
		  $(".movThums_upper").click(function(){//start video
			  //$("#defImg").hide("slow");
			  $(".movThums_upper").show("slow");
			  $(this).hide("slow");
				
		  	//-- put movie
				$(".box02").empty().append("<div id=\"youtube-player-container\"></div>");
				var params = { allowScriptAccess: "always" };
				var atts = { id: "myytplayer1" };
				swfobject.embedSWF("http://www.youtube.com/v/"+ $(this).attr("videoId") +"&enablejsapi=1&playerapiid=ytplayer1&autoplay=1&rel=0&showinfo=0",
					 "youtube-player-container", "540", "330", "8", null, null, params, atts);
		  });
			
			//-- playerのステータス監視 ---------------
			onChangeValue( jQuery , "EGYTstatus", function(){
				//---playerのステータス取得　　　未開始(-1)､終了(0)､再生中(1)､一時停止中(2)､ﾊﾞｯﾌｧﾘﾝｸﾞ中(3)､頭出し済み(5)
				if ( $.EGYTstatus  == -1 ) {// 未開始
				}
				else if ( $.EGYTstatus  == 0 ) {//終了
		      	self.playing = false;
						self.moveResume2();
						$(".box02").empty().append("<div id=\"youtube-player-container\" style='visibility:visible;'></div>");
		      	//$("#youtube-player-container").hide().append("<img id=\"defImg\" src=\"2011img01/eg01.jpg\" style=\"\" />").show();
		      	$(".movThums_upper").show("slow");
				}
				else if ( $.EGYTstatus  == 1 ) {//再生中
		      	self.playing = true;
						self.moveStop();
				}
				else if ( $.EGYTstatus  == 2 ) {//一時停止中
				}
				else if ( $.EGYTstatus  == 3 ) {//ﾊﾞｯﾌｧﾘﾝｸﾞ中
		      	self.playing = true;
		      	self.moveStop();
		      	//$("#youtube-player-container).show()
				}
				else if ( $.EGYTstatus  == 4 ) {//?
				}
				else if ( $.EGYTstatus  == 5 ) {//頭出し済み
				}
				return true;
			}, 100);
			
			$(".movThums_upper").css("visibility","visible");
			//--default move changed to up-to-down
			//this.moveForward();//default
			this.moveBack();
		}
	},
	
	moveForward: function() {
		var self = this;
		this.movement = 'forward';
		if (this.trigger !== null) {
			this.$btnBack.removeClass('disabled');
		}
		self.interval = setInterval(function() {
			if (!self.o.horizontal && self.$clip[0].scrollTop < (self.posMax-self.clipMax)) {
				self.$clip[0].scrollTop += self.o.speed;
			} else if (self.o.autoMode=='loop') {
				self.resetPos();
			} else {
				self.moveStop(self.movement);
			}
		},self.intervalDelay);
	},
	moveBack: function() {
		var self = this;
		this.movement = 'back';
		if (this.trigger !== null) {
			this.$btnForward.removeClass('disabled');
		}
		self.interval = setInterval(function() {
			if (!self.o.horizontal && self.$clip[0].scrollTop>0) {
				self.$clip[0].scrollTop -= self.o.speed;
			} else if (self.o.autoMode=='loop') {
				self.resetPos();
			} else {
				self.moveStop(self.movement);
			}
		},self.intervalDelay);
	},
	moveForwardFast: function() {
		var self = this;
		this.movement = 'forward';
		if (this.trigger !== null) {
			this.$btnBack.removeClass('disabled');
		}
		self.interval = setInterval(function() {
			if (!self.o.horizontal && self.$clip[0].scrollTop < (self.posMax-self.clipMax)) {
				self.$clip[0].scrollTop += self.o.speed + 7;
			} else if (self.o.autoMode=='loop') {
				self.resetPos2();
			} else {
				self.moveStop(self.movement);
			}
		},self.intervalDelay);
	},
	moveBackFast: function() {
		var self = this;
		this.movement = 'back';
		if (this.trigger !== null) {
			this.$btnForward.removeClass('disabled');
		}
		self.interval = setInterval(function() {
			if (!self.o.horizontal && self.$clip[0].scrollTop>0) {
				self.$clip[0].scrollTop -= self.o.speed + 7;
			} else if (self.o.autoMode=='loop') {
				self.resetPos();
			} else {
				self.moveStop(self.movement);
			}
		},self.intervalDelay);
	},
	moveFastUp: function(moveDir) {
		clearInterval(this.interval);	
		this.trigger = null;
		if (this.auto) {
			//this.movement=='forward' ? this.moveForwardFast() : this.moveBackFast();
			this.moveForwardFast();
		}
	},
	moveFastDw: function(moveDir) {
		clearInterval(this.interval);	
		this.trigger = null;
		if (this.auto) {
			//this.movement=='forward' ? this.moveBackFast() : this.moveForwardFast();
			this.moveBackFast();
		}
	},
	moveStop: function(moveDir) {
		clearInterval(this.interval);	
		if (this.trigger!==null) {
			if (typeof moveDir != "undefined") {
				$(this.trigger).addClass('disabled');
			}
			this.trigger = null;
		}
		if (this.auto) {
			if (this.o.autoMode=='bounce') {
				moveDir == 'forward' ? this.moveBack() : this.moveForward();
			}
		}
	},
	moveResume: function() {
		
		if(!this.playing){
		this.movement=='forward' ? this.moveForward() : this.moveBack();
		}
	},
	moveResume2: function() {//for speedup
		clearInterval(this.interval);	
		//this.movement=='forward' ? this.moveForward() : this.moveBack();
		if(!this.playing){
			this.moveBack();
		}
	},
	resetPos: function() {
		//this.$clip[0].scrollTop = 0;
		//this.$clip[0].scrollTop = this.itemMax + this.clipMax;
		this.$clip[0].scrollTop = this.itemMax * this.$items.length;
	},
	resetPos2: function() {//for speedup
		this.$clip[0].scrollTop = 0;
	}
		
});
		  
})(jQuery);
