var qmeCms = qmeCms || {};
qmeCms.MinImgsBox = {
    intImgId: 0,
    arrImgs: [],
    objMaxImgBoxObj: null,
    objMinImgBoxObj: null, 
    objMinImgObj: null,
    objPlayRateObj: null,
    Init: function(fctArrImgs) {
        this.arrImgs = fctArrImgs;
        this.objMaxImgBoxObj = $("divPyzyImgBoxContent");
        this.objMinImgBoxObj = $("divPyzyMinImgs");
        this.objPlayRateObj = $("divPyzyImgBoxRate");
        var tmpMinImgsBoxHTML = [];
        for (var intI = 0; intI < this.arrImgs.length; intI++) {
            tmpMinImgsBoxHTML[tmpMinImgsBoxHTML.length] = "<img id='imgMinItem" + intI + "' onClick='qmeCms.MinImgsBox.Show(" + intI + ")' alt='" + this.arrImgs[intI][0] + "' src='" + this.arrImgs[intI][2] + "' />"
        }
        this.objMinImgBoxObj.innerHTML = tmpMinImgsBoxHTML.join("");
		var _self = this;
		var listenTimer = 0;
		var listenMousewheel = function(e){
			var e = e || window.event;
			var delta = e.wheelDelta ? (e.wheelDelta / 120) : ( 0 - e.detail / 3);
			_self.Show(_self.intImgId - delta);
			window.scrollTo(0, fetchOffset(_self.objMaxImgBoxObj).top);
			return false;
		};
		$$E.addEvent(this.objMaxImgBoxObj, $$B.firefox ? 'DOMMouseScroll' : 'mousewheel', function(e){
			var e = e || window.event;
			$$E.fixEvent(e).stopPropagation();
			$$E.fixEvent(e).preventDefault();
			clearTimeout(listenTimer);
			listenTimer = setTimeout(function(){
				listenMousewheel(e);
			},200);
		});
		$$E.addEvent(document, 'keydown', function(e){
			var e = e || window.event;
			if(e.keyCode == 37){
				_self.Show(_self.intImgId - 1);
			}else if(e.keyCode == 39){
				_self.Show(_self.intImgId + 1);
			}
			return true;
		});
    },
    Show: function(fctId) {
        if (this.arrImgs.length == 0) {
            return;
        }
        fctId = (fctId < 0 ? this.arrImgs.length - 1 : (fctId >= this.arrImgs.length ? 0 : fctId));
        this.objPlayRateObj.style.width = ((fctId + 1) / this.arrImgs.length * this.objPlayRateObj.parentNode.offsetWidth) + "px";
        this.objMinImgObj = $("imgMinItem" + fctId)
        qmeCms.MinImgsScroll.Init(
            this.objMinImgBoxObj,
            (
                (   
                    this.objMinImgObj.offsetLeft -
                    (navigator.appVersion.indexOf("MSIE") == -1 ? this.objMinImgObj.parentNode.offsetLeft:0)
                ) -  
                this.objMinImgBoxObj.scrollLeft
                -
                (this.objMinImgBoxObj.offsetWidth / 2)
             )
        );
        this.objMaxImgBoxObj.innerHTML = "";
        $("imgMinItem" + this.intImgId).className = "";
        this.objMinImgObj.className = "Sel";
        this.intImgId = fctId;
		this.loadImage(this.arrImgs[fctId][1]);
    },
	loadImage:function(url){
		var o = this;
		var obj = new Image();
		obj.onload = function(){
			if(obj.src != o.arrImgs[o.intImgId][1]) return;
			var w = this.width;
			var h = this.height;
			var ow = o.objMaxImgBoxObj.offsetWidth;
			var oh = o.objMaxImgBoxObj.offsetHeight;
			var r = w / h;
			if(w > ow || h > oh){
				if(w / h >= ow / oh){
					w = ow;
					h = (1 / r) * w;
				}else{
					h = oh;
					w = r * h;
				}
			}
			var top = (oh - h) / 2 ;
			o.objMaxImgBoxObj.innerHTML = "<img src='"+url+"' width='"+w+"' height='"+h+"' style='margin-top:"+top+"px;opacity:0;filter:Alpha(Opacity=0)' />";
			o.fadeMark(o.objMaxImgBoxObj.getElementsByTagName('img')[0]);
		};
		obj.src = url;
	},
	fadeMark:function(obj, method){
		var b=0,c=100-b;
		var d=20;
		var method = (typeof method == 'string' && method) ? method : 'easeInOutCubic';
		var t=0,ttl=20;
		var o = this;
		(function (){
			clearInterval(o.objMaxImgBoxObj.timer);
			o.objMaxImgBoxObj.timer=setInterval(function (){
				var m = Math.ceil(Tweener[method](t,b,c,d)) ;
				obj.style.opacity = m / 100;
				obj.style.filter = "Alpha(Opacity="+m+")";
				if(t<d) t++;
				else clearInterval(o.objMaxImgBoxObj.timer);
			},ttl)
		})()
	}
};
qmeCms.MinImgsScroll = {
    intAllScrollSeep: 0,
    intScrollCount: 0,
    objScroll: null,
    boolLeft: true, 
    Init: function(fctObj, fctSeep, fctTop) {
        this.objScroll = fctObj;
        this.boolLeft = (fctTop == null);
        this.intAllScrollSeep = (fctSeep == null ? (this.boolLeft ? this.objScroll.offsetWidth : this.objScroll.offsetHeight) : fctSeep);
        this.intSeep = ((this.intAllScrollSeep < 0 && this.intSeep > 0) || (this.intAllScrollSeep > 0 && this.intSeep < 0) ? -1 : 1) * this.intSeep;
        this.intScrollCount = 0; 
        this.AutoSeep();
    },
    intSeep: 5,
    AutoSeep: function() {
        if (Math.abs(this.intAllScrollSeep) > Math.abs(this.intScrollCount)) {
            this.intScrollCount += this.intSeep;
            if (this.boolLeft) { this.objScroll.scrollLeft += this.intSeep; }
            else { this.objScroll.scrollTop += this.intSeep; }
            window.setTimeout("qmeCms.MinImgsScroll.AutoSeep();", 10);
        }
    }
};