Object.extend = function(destination, source) {for (var property in source){destination[property] = source[property];};return destination;};

var Tweener = {easeInCubic: function(t, b, c, d) {return c*(t/=d)*t*t + b;},easeInOutCubic: function(t, b, c, d) {if((t/=d/2) < 1) return c/2*t*t*t + b;return c/2*((t-=2)*t*t + 2) + b;},easeOutCubic: function(t, b, c, d) {return c*((t=t/d-1)*t*t + 1) + b;},easeOutInCubic: function(t, b, c, d) {if(t < d/2) return Tweener.easeOutCubic(t*2, b, c/2, d);return Tweener.easeInCubic((t*2)-d, b+c/2, c/2, d);},easeNone: function(t, b, c, d) {return c*t/d + b;},easeInQuint: function(t, b, c, d) {return c*(t/=d)*t*t*t*t + b;},easeOutQuint: function(t, b, c, d) {return c*((t=t/d-1)*t*t*t*t + 1) + b;},easeInExpo: function(t, b, c, d) {return(t==0) ? b : c * Math.pow(2, 10 *(t/d - 1)) + b - c * 0.001;},easeOutBack: function(t, b, c, d, s) {if(s == undefined) s = 1.70158;return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;}};

var addClassName = function(obj,name){if(obj == null) return true;if(obj.className.indexOf(name) > -1) return true;var n=obj.className;obj.className=n+' '+name;};
var removeClassName = function(obj,name){var n=obj.className;obj.className = obj.className.replace(name, '');};

	var HFQ = new Object();
		HFQ.flash = {
			index:0,
			data:[],
			config:{timer:2000},
			obj:null,
			current:1,
			ta:0,
			img:{pre:0,next:0},
			init:function(option){
				var option = option || {};
				Object.extend(this.config, option);
				this.obj = $$('#flash_box');
				var o = this;
				$$('#flash_pre').onclick = function(){o.run(o.index-1, 'pre');};
				$$('#flash_pre').onmouseover = function(){addClassName(this, 'over')};
				$$('#flash_pre').onmouseout = function(){removeClassName(this, 'over')};
				$$('#flash_next').onclick = function(){o.current++;o.run(o.index+1, 'next');};
				$$('#flash_next').onmouseover = function(){addClassName(this, 'over')};
				$$('#flash_next').onmouseout = function(){removeClassName(this, 'over')};
				this.build();
				this.obj.getElementsByTagName('li')[1].className = 'current loading';
				var img = this.obj.getElementsByTagName('img');
				for(var i=0;i<Math.min(3,img.length);i++){
					this.loadimg(img[i]);
				}
				$$('#flash-info').innerHTML = "<span class='name'>"+this.data[0][2]+"</span> － "+this.data[0][3];
				$$('#flash-info-btn').href = 'play.php?vid='+this.data[0][1];//'/media/'+this.data[0][1]+'?img='+encodeURIComponent(this.data[0][0]);
				$$('#flash_description').style.visibility = 'visible';
				o.fade($$('#flash_description'), $$('#flash_description_background'));
			},
			run:function(i, type){
				this.close();
				var dir = 'marginLeft';
				var len = -694;
				var b=this.obj.parentNode.style[dir];
				b = isNaN(parseInt(b)) ? 0 : parseInt(b);
				this.index = i;
				this.buildItem(i, type, true);
				if(type == 'pre'){
					this.index++;
					this.obj.parentNode.style[dir] = b + len + 'px';
					var b=this.obj.parentNode.style[dir];
					b = isNaN(parseInt(b)) ? 0 : parseInt(b);
					var c = this.current * len - b ;
				}else{
					var c = this.current * len - b;
				}
				var d=50,t=0,ttl=500 /d;
				var img_up = {b:40, c:100};
				var img_down = {b:100, c:-60};
				var img_down_down = {b:0, c:40};
				var isIE = document.all ? true : false;
				var left,right;
				if(type == 'next'){
					left = -1;
					right = 1;
				}else{
					left = 1;
					right = -1;
				}
				(function (me){
					clearInterval(me.only);
					var lis = me.obj.getElementsByTagName('img');
					me.loadimg(lis[me.current+right]);
					var index = parseInt(lis[me.current].getAttribute('rel'));
					$$('#flash_description').style.visibility = 'hidden';
					$$('#flash-info').innerHTML = "<span class='name'>"+me.data[index][2]+"</span> － "+me.data[index][3];
					$$('#flash-info-btn').href = 'play.php?vid='+me.data[index][1];//'/media/'+me.data[index][1]+'?img='+encodeURIComponent(me.data[index][0]);
					me.only=setInterval(function (){
						me.obj.parentNode.style[dir] = Math.ceil(Tweener.easeOutQuint(t,b,c,d)) + 'px';
						if(isIE){
							lis[me.current].style.filter = "Alpha(Opacity=" + Math.ceil(Tweener.easeOutInCubic(t,img_up.b,img_up.c,d)).toString() + ")";
							lis[me.current+left].style.filter = "Alpha(Opacity=" + Math.ceil(Tweener.easeInOutCubic(t,img_down.b,img_down.c,d)).toString() + ")";
							lis[me.current+right].style.filter = "Alpha(Opacity=" + Math.ceil(Tweener.easeInOutCubic(t,img_down_down.b,img_down_down.c,d)).toString() + ")";
						}else{
							lis[me.current].style.opacity = Math.ceil(Tweener.easeOutInCubic(t,img_up.b,img_up.c,d)) / 100;
							lis[me.current+left].style.opacity = Math.ceil(Tweener.easeInOutCubic(t,img_down.b,img_down.c,d)) / 100;
							lis[me.current+right].style.opacity = Math.ceil(Tweener.easeInOutCubic(t,img_down_down.b,img_down_down.c,d)) / 100;
						}
						if(t<d) t++; 
						else{
							clearInterval(me.only);
							$$('#flash_description').style.visibility = 'visible';
							me.fade($$('#flash_description'), $$('#flash_description_background'));
						}
					},ttl)
				})(this)
			},
			build:function(autoIndex){
				this.buildItem(this.data.length-1);
				for(var i=0; i<this.data.length-1;i++){
					this.buildItem(i, 'next', false);
				}
				this.img.next = this.data.length-2;
				this.img.pre = this.data.length - 1;
			},
			buildItem:function(i, type, check){
				var type = type || 'next';
				var i = i % this.data.length;
				if(i < 0) i = i + this.data.length;
				if(typeof this.data[i] != 'object') alert(i);
				try{
					var li = document.createElement('li');
					if(check == true){
						if(type == 'next'){
							this.img['next']++; 
							var index = Math.abs(this.img['next']) % this.data.length;
						}else{
							this.img['pre']--; 
							var index = this.img['pre'] % this.data.length;
							if(index < 0) index += this.data.length;
						}
						li.innerHTML = '<a href="play.php?vid='+this.data[index][1] + '"><img onload="removeClassName(this.parentNode.parentNode, \'loading\');" rel="'+index+'" src="../blank.gif" width="690" height="300" /></a>';//"/media/'+this.data[index][1]+'?img='+encodeURIComponent(this.data[index][0])+'"
					}else{
						li.innerHTML = '<a href="play.php?vid='+this.data[i][1] + '"><img onload="removeClassName(this.parentNode.parentNode, \'loading\');"  rel="'+i+'"  src="../blank.gif" width="690" height="300" /></a>';
					}///media/'+this.data[i][1]+'?img='+encodeURIComponent(this.data[i][0])+'
					li.className = 'loading';
					if(type == 'next') this.obj.appendChild(li);
					else this.obj.insertBefore(li, this.obj.getElementsByTagName('li')[0]);
					return true;
				}finally{
					li = null;
				}
			},
			removeItem:function(i){
				this.obj.removeChild(this.obj.getElementsByTagName('li')[i]);
			},
			addItem:function(d){
				if(this.ta && d[4]) return false;
				this.data[this.data.length] = d;
			},
			close:function(){
				$$('#flash_description_background').style.display = 'none';
			},
			loadimg:function(obj){
				var i = obj.getAttribute('rel');
				obj.src = this.data[i][0];
			},
			fade:function(obj, obj_2, fun){
				var b=0,c=100;
				obj.style.opacity = 0;
				obj.style.filter = "Alpha(Opacity=0)";
				obj_2.style.opacity = 0;
				obj_2.style.filter = "Alpha(Opacity=0)";
				obj.style.display = 'block';
				obj_2.style.display = 'block';
				var d = d || 20,t=0,ttl=20;
				(function (me){
					clearInterval(me.only);//me.fadeOnly
					me.only=setInterval(function (){
						var m = Math.ceil(Tweener.easeNone(t,b,c,d)) ;
						if(m <= 40){obj_2.style.opacity = m / 100;obj_2.style.filter = "Alpha(Opacity="+m+")";}
						obj.style.opacity = m / 100;
						obj.style.filter = "Alpha(Opacity="+m+")";
						if(t<d) t++; 
						else{clearInterval(me.only);if(typeof fun == 'function') fun(obj);}
					},ttl)
				})(this)
			}
		}
