/**
* Neo-CMS Random Product view Script
* @author Yoshiaki Sugimoto <neo.yoshiakil.sugimoto@gmail.com>
* @create 2010/0216
*
* @note need jQuery
*
*/

$(function() {

	// 編集モードの時は動作しないようにする
	if ($('div.ccm-block').size() > 0) { return;}

	var ImageAnimator = {}, IA = ImageAnimator; // alias
	var win = window, doc = document;

	var items = doc.getElementById('products_random').getElementsByTagName('li'),
		pointer = 0, current = 0, first = true, cFirst = true, jqItem = $('ul#products_random li');
	var base = $('ul#products_random'), len = items.length;

	// Timerクラス - タイマー管理クラス
	var Timer = function(fn) {
		this.fn = this.__fnToReg(fn);
		this.timerId = CoreTimer.pushStack(this);
		return this;
	};
	Timer.prototype = {
		stop : function() { CoreTimer.stop(this.timerId); },
		start : function() { CoreTimer.start(); },
		exec : function() { this.fn(); },
		__fnToReg : function(fn) {
			var f, that = this;
			f = fn;
			f.prototype.stop = that.stop;
			return f;
		}
	};

	// CoreTimer - 内部処理タイマーオブジェクト
	var CoreTimer = {count : 0, stack : [], play : false, globalTimerId : null};
	CoreTimer.pushStack = function(obj /* Timer instance */) {
		var cn = this.count;
		this.stack[cn] = [obj, cn, false];
		this.count++;
		return cn;
	};
	CoreTimer.isPlaying = function() {
		return this.play;
	};
	CoreTimer.start = function() {
		if (this.isPlaying() === true) return;
		this.play = true;
		CoreTimer.globalTimerId = win.setInterval(function() {
			var len = CoreTimer.stack.length, t;
			for (var i = 0; i < len; i++) {
				t = CoreTimer.stack[i];
				if (t && t[2] === false)t[0].exec();
			};
		}, 15);
	};
	CoreTimer.stop = function(id) {
		if(!this.stack[id])return;
		this.stack[id][2] = true;
		var len = this.stack.length, flag = true;
		for (var i = 0; i < len; i++) {
			if (this.stack[i][2] === true)continue;
			else flag = false;
		};
		if (flag === true) this.clear();
	};
	CoreTimer.clear = function() {
		win.clearInterval(CoreTimer.globalTimerId);
		CoreTimer.play = false;
		CoreTimer.stack = [];
		CoreTimer.count = 0;
	};

	var w = 184;

	function fade() {
		var li = $(items[0]);
		var fn = function() {
			var op = li.css('opacity'), mop = op - 0.05;
			if (mop < 0) { mop = 0;}
			li.css('opacity', mop);
			if (mop <= 0) { this.stop(); win.setTimeout(moveX, 500);}
		};
		new Timer(fn).start();
	}

	function appear() {
		base.get(0).appendChild(items[0]);
		base.css('marginLeft', '-40px');
		var li = $(items[2]);
		var fn = function() {
			var op = li.css('opacity'), mop = parseFloat(op) + 0.05;
			if (mop > 1) { mop = 1;}
			li.css('opacity', mop);
			if (mop >= 1) {
				this.stop();
				if (pointer + 2 === len) { win.setTimeout(initialize, 1000);}
				else { win.setTimeout(fade, 5000);}
			}
		};
		new Timer(fn).start();
	}

	var dist;


	function moveX() {
		dist = -(w + 42 * 2);
		base.animate({'marginLeft' : dist + 'px'}, 'slow', appear);
	}

	function initialize() {
		pointer = 0;
		jqItem.css('opacity', 1);
		base.animate({'marginLeft' : 0 + 'px'}, 'slow');
		win.setTimeout(fade, 5000);
	}


	win.setTimeout(fade, 5000);

});

