/*
 * jQuery ticker
 *
 * jQuery required.
 * jQuery Easing Plugin extends this Plugin.
 *
 * Copyright 2011 (c) kamem
 * http://develo.org/
 * Licensed Under the MIT.
 *
 * Date: 2011.06.25 
*/

(function(){
// リストにつくid名
var tagId ="tickerId_"

var count = [];
var start = [];
var end = [];

var wrapArray = [];
var wrapId = [];

var wrapWidth = [];
var wrapHegith = [];
var ulHegith = [];

var beforeTag= [];
var tag= [];
var nextTag= [];

var info = {};

var btn;

//スマートフォンウィンドウサイズを変えたときに情報更新
window.onresize = function() {
	for(var i = 0;wrapId.length > i ;i++) {
			if(info[i].type == "move1" || info[i].type == "normal") {
				var userAgent = navigator.userAgent.toLowerCase();
				if(userAgent.search(/iphone/) >= 0 || userAgent.search(/ipad/) >= 0 || userAgent.search(/Android/) >= 0) {
					
				wrapWidth[i] = wrapArray[i].width();
				wrapHegith[i] = wrapArray[i].height();
				
				$("#" + wrapId[i] + " ul li").each(function(j){
					$(this).queue([]).stop()
					$(this).css("left",wrapWidth[i]);
				});
				ticker(
				i,
				info[i].target,
				info[i].delay,
				info[i].delay1,
				info[i].delay2,
				info[i].speed,
				info[i].speed1,
				info[i].speed2,
				info[i].easing,
				info[i].type
				);
				flag = false
				}
			}
	}
}

$(function(){
	$.fn.newsTicker = function(options){
		//if(this.length<=1) return false;
	
		//親取得
		wrapArray.push($(this).closest("ul").closest("div"));
		wrapId.push($(this).closest("ul").closest("div").attr("id"));

		//ページ内に複数Tikcrがある場合の番号付け
		var num = wrapArray.length -1;
		start[num] = count[num] = 0
		end[num] = this.length-1
		
		//親の幅
		wrapWidth[num] = wrapArray[num].width();
		wrapHegith[num] = wrapArray[num].height();
		ulHegith[num] = $("#" + wrapId[num] + " ul").height();
		
		var c = $.extend({
			target: this,
			delay: 2000,
			delay1: 1000,
			delay2: 500,
			speed: 50,
			speed1: 1000,
			speed2: 50,
			easing: "easeOutQuad",
			type: "normal",
			onresize: false
		},options);
		
		info[num] =  {
			target: c.target,
			delay: c.delay,
			delay1: c.delay1,
			delay2: c.delay2,
			speed: c.speed,
			speed1: c.speed1,
			speed2: c.speed2,
			easing: c.easing,
			type: c.type,
			onresize: c.onresize
		}

		//初期設定
		this.each(function(i){
			//idをつける
			$(this).attr("class",tagId+i);
			
			if(c.type == "fade") {
				//最初以外は消す
				if(i!=0) $(this).hide();
			}
			
			if(c.type == "move1") {
				$(this).css("left",$(this).closest("ul").closest("div").width());
			}
			
			if(c.type == "normal") {
				$(this).css("left",$(this).closest("ul").closest("div").width());
			}
			
			if(c.type == "longitudinally") {
				if(i == 0) {
					$(this).css("top",0);
				}
				else {
					$(this).css("top",ulHegith[num]);
				}
			}
		});

		$(window).bind("load",function(){
			ticker(
			num,
			c.target,
			c.delay,
			c.delay1,
			c.delay2,
			c.speed,
			c.speed1,
			c.speed2,
			c.easing,
			c.type
			);
		});
	}
});

function ticker(num,target,delay,delay1,delay2,speed,speed1,speed2,easing,type) {
			
	//今のタグと前のタグと次のタグ
	beforeTag[num] = "#" + target.closest("ul").closest("div").attr("id") + " ." +tagId+ (count[num] == 0 ? end[num] : count[num] -1);
	
	tag[num] = "#" + target.closest("ul").closest("div").attr("id") + " ." +tagId+count[num];
	
	++count[num]	
	if(count[num]>end[num]){
		start[num] = 1;
		count[num] = 0;
	}
	nextTag[num] = "#" + target.closest("ul").closest("div").attr("id") + " ." +tagId+ count[num];

	//それぞれ横幅
	var tagWidth =$(tag[num]).width();
	var nextTagWidth =$(nextTag[num]).width();

	switch (type) {
	
		//ノーマルティッカー
		case "normal":
			var time =  (tagWidth + wrapWidth[num]) / (speed / 1000);
			$(tag[num]).delay(0).animate({ left : - tagWidth}, time, 'linear',
				function(){$(tag[num]).delay(0).animate({ left: wrapWidth[num]}, 0, 'linear',
				function(){ticker(num,target,delay,delay1,delay2,speed,speed1,speed2,easing,type);});
				});
			break;
		
		//縦バージョン
		case "longitudinally":
			//最初のひとつだけは少し待ってから処理
			if(start[num] == 0 && tag[num].charAt(tag[num].length-1) == 0) {
				$(tag[num]).delay(delay).animate({top : -ulHegith[num],opacity : 0}, 1000, easing);
				$(nextTag[num]).delay(delay).animate({top : 0,opacity : 1}, 1000, easing,function(){$(tag[num]).delay(delay).animate({top: ulHegith[num],opacity : 0},0,
				function(){ticker(num,target,delay,delay1,delay2,speed,speed1,speed2,easing,type);});});
			}
			else{
				$(tag[num]).animate({top : -ulHegith[num],opacity : 0}, 1000, easing);
				$(nextTag[num]).animate({top : 0,opacity : 1}, 1000, easing,function(){$(tag[num]).delay(delay).animate({top: ulHegith[num],opacity : 0},0,
				function(){ticker(num,target,delay,delay1,delay2,speed,speed1,speed2,easing,type);});});
			}
			break;

		//フェイドイン、フェイドアウト
		case "fade":
			//最初のひとつだけは少し待ってから処理
			if(start[num] == 0 && tag[num].charAt(tag[num].length-1) == 0) {
				$(tag[num]).delay(delay).fadeOut(1000);
				$(nextTag[num]).delay(delay).fadeIn(1000,function(){$(tag[num]).delay(delay).animate({left: 10},0,
				function(){ticker(num,target,delay,delay1,delay2,speed,speed1,speed2,easing,type);});});
			}
			else{
				$(tag[num]).fadeOut(1000);
				$(nextTag[num]).fadeIn(1000,function(){$(tag[num]).delay(delay).animate({left: 10},0,
				function(){ticker(num,target,delay,delay1,delay2,speed,speed1,speed2,easing,type);});});
			}
			break;

		//すばやく出てきて、そのあとにティッカー
		case "move1":
			var time =  (tagWidth + 10) / (speed2 / 1000);
			$(tag[num]).animate({ left : 10}, speed1, easing,
				function(){$(tag[num]).delay(delay1).animate({ left : - tagWidth}, time, 'linear',
				function(){$(tag[num]).delay(delay2).animate({ left: wrapWidth[num]}, 0,
				function(){ticker(num,target,delay,delay1,delay2,speed,speed1,speed2,easing,type);});});
				});
			break;
	}
}
})();


