/**include
//_javascript/load-wrapper.js;
*/

/**
 * Modal window control
 *
 */
var oModalWindow = {
	mWin       : null,
	voile      : null,
	mContent   : null,
	mLoad      : null,
	bigImg     : null,
	url        : null,
	pager      : null,
	p_link     : null,
	pages      : [],
	
	id_goods     : null,
	// id image
	id           : null,
	// or file name
	file_name    : null,
	aPhotos      : [],
	cnt          : null,
	flag_url     : null,
	// id or file name? id=false, file_name=true
	src          : false,
	
	qttStartEnd : null,
	qttMiddle   : null,
	nMidlHalf   : null,
	
	lnk  : [],
	prev : null,
    next : null,
    close  : null,
	
    timer   : null,
    loader  : null,
    current : 0,
	
	init : function (id_goods, id, aPhotos, cnt, src) {
        this.id_goods = id_goods;
		this.id = id;
        this.cnt = cnt;
        this.aPhotos = aPhotos;
        if (src) { 
			this.file_name = id; 
			this.src = src; 
		}
		// alert([id, this.file_name,this.id, this.src])
		
        // Loader initialisation
        //this.loader = this.getLoadWrapper(this.config.loader, "get", false);
        //this.loader.addListener(this, "ondataload", "onDataLoad");
        //this.loader.addListener(this, "ondataerror", "onDataError");
	},
	
	onready : function () {
        this.mWin     = this.$("#modal_window_"+this.id_goods+"");
        this.voile    = this.$("#modal_window_"+this.id_goods+" #voile_"+this.id_goods+"");
        this.mContent = this.$("#modal_window_"+this.id_goods+" #modal_content_"+this.id_goods+"");
        this.mContent.addListener(this, "onclick", "click_stop");
        this.$w0.addListener(this, "onclick", "click_close");
		
		this.$w0.addListener(this, "onresize", "win_resize");
        
        this.mLoad    = this.$("#modal_window_"+this.id_goods+" #modal_load_"+this.id_goods+"");
        this.bigImg   = this.$("#modal_window_"+this.id_goods+" #bigImg_"+this.id_goods+"").getChildren()[0];
        // this.url      = this.$("#modal_window_"+this.id_goods+" #url");
		// this.url.addListener(this, "onclick", "click_url");
        this.pager    = this.$("#modal_window_"+this.id_goods+" #pager_"+this.id_goods+"");

        this.lnk = this.$$("#photo_list_"+this.id_goods+" a#photo_*");
        for(i = 0; i < this.lnk.length; i++) {
           this.lnk[i].addListener(this, "onclick").n = i;
        }
        
        this.prev = this.$("a#prev_"+this.id_goods+"");
        this.prev.addListener(this, "onclick", "click_prev").p = (this.current - 1);
        this.next = this.$("a#next_"+this.id_goods+"");
        this.next.addListener(this, "onclick", "click_next").p = (this.current + 1);
        
        this.close = this.$("#modal_window_"+this.id_goods+" a#close_"+this.id_goods+"");
        this.close.addListener(this, "onclick", "click_close");
        
        this.qttStartEnd = this.config.qttStartEnd,
		this.qttMiddle   = this.config.qttMiddle,
        this.nMidlHalf   = Math.floor(this.config.qttMiddle / 2);
	},
	
	onclick : function(evtWr, dt)
    {
        evtWr.eventDrop();
        evtWr.stopBubbling();
        evtWr.elmWr.elm.blur();

        var list = this.pager.elm.childNodes;
        var length = list.length;
		for (var i = length - 1; i >= 0; i--) {
			this.pager.elm.removeChild(list[i]);
		}
        
        //this.pages[this.current].removeClass("active");
        this.id = evtWr.elmWr.elm.id.substr(6);
       	for (var j in this.aPhotos) {
       		var value = this.aPhotos[j];
       		if (value == this.id) {
       			this.current = j * 1;
       			break;
       		}
       	}
       	if(dt.n == 0) {
            this.prev.addClass("hide");
        }
        if(dt.n == this.lnk.length-1) {
            this.next.addClass("hide");
        }
       	this.page_visible();
        this.photoLoad(true);
		
        // voile size
		this.set_voile();

        this.mWin.show();
    },
	
	win_resize : function()
	{
		// voile size
		this.set_voile();
	}, 
	
	set_voile : function()
	{
		// voile size
		var arr1   = this.___getPageSize();
		var arr2   = this.___getPageScroll();
		var width  = arr1[0];
		var height = arr1[1];
		
		/*var width = this.$w0.getDocumentWidth() > this.$w0.getWindowWidth() ? 
			this.$w0.getDocumentWidth() : this.$w0.getWindowWidth();
        var height = this.$w0.getDocumentHeight() > this.$w0.getWindowHeight() ? 
			this.$w0.getDocumentHeight() : this.$w0.getWindowHeight();*/
			
        this.voile.style.width  = width + "px";
        this.voile.style.height = height + "px";
		
		// move
		var y  = arr2[1];
		y += 269;     // margin top = -269
		y += 90
		y2 = y+600;   // height     = 600
		
		var delta = 0;
		if (y2>arr1[1]) delta = y2 - arr1[1];
		if (delta) {
			this.voile.style.height = (arr1[1] + delta) + "px";
		}
		if (arr2[0]) {
			this.voile.style.width  = width + arr2[0] + "px";
		}
		this.mContent.moveY(y);
	}, 
    
    click_page : function(evtWr, dt)
    {
        evtWr.eventDrop();
        evtWr.elmWr.elm.blur();
        
        this.pages[this.current].removeClass("active");
        this.current = dt.p;
		
		this.prev.removeClass("hide");
		this.next.removeClass("hide");
		if(this.current == 0) {
            this.prev.addClass("hide");
        }
        if(this.current == this.aPhotos.length-1) {
            this.next.addClass("hide");
        }
        
        var list = this.pager.elm.childNodes;
        var length = list.length;
		for (var i = length - 1; i >= 0; i--) {
			this.pager.elm.removeChild(list[i]);
		}
        this.page_visible();
		this.photoLoad(false);
    },
    
    click_stop : function(evtWr, dt)
    {
    	if (this.flag_url) {
    		this.flag_url = false;
    	} else {
        	evtWr.eventDrop();
        	evtWr.stopBubbling();
    	}
    },
    
    click_url : function(evtWr, dt)
    {
    	this.flag_url = true;
    },
    
    click_prev : function(evtWr, dt)
    {
        evtWr.eventDrop();
        evtWr.elmWr.elm.blur();
        this.next.removeClass("hide");

        this.pages[this.current].removeClass("active");
        if(this.current > 0) {
            this.current--;
        }
        if(this.current == 0) {
            evtWr.elmWr.addClass("hide");
        }
        var list = this.pager.elm.childNodes;
        var length = list.length;
		for (var i = length - 1; i >= 0; i--) {
			this.pager.elm.removeChild(list[i]);
		}
        this.page_visible();
        this.photoLoad(false);
    },
    
    click_next : function(evtWr, dt)
    {
        evtWr.eventDrop();
        evtWr.elmWr.elm.blur();
        this.prev.removeClass("hide");
        
        this.pages[this.current].removeClass("active");
        if(this.current < this.aPhotos.length-1) {
            this.current++;
        }
        if(this.current == this.aPhotos.length-1) {
            evtWr.elmWr.addClass("hide");
        }
        var list = this.pager.elm.childNodes;
        var length = list.length;
		for (var i = length - 1; i >= 0; i--) {
			this.pager.elm.removeChild(list[i]);
		}
        this.page_visible();
        this.photoLoad(false);
    },
    
    click_close : function(evtWr, dt)
    {
    	if(evtWr.elmW){    	
	        evtWr.eventDrop();
	        evtWr.elmW.elm.blur();
    	}
        this.mWin.hide();
    },
    
    page_visible : function(evtWr, dt)
    {
        if (this.cnt <= (this.qttStartEnd * 2 + this.qttMiddle)) {
        	for(i = 0; i < this.cnt; i++) {
        		this.p_link = this.winWr.makeElement("a", {"href" : "gmail.com"}, (i+1).toString());
        		this.p_link.addListener(this, "onclick", "click_page").p = i;
        		this.pager.elm.appendChild(this.p_link.elm);
        		this.pages[i] = this.p_link;
	        }
        } else if ((this.current + 1) <= (this.qttStartEnd + this.nMidlHalf + 1)) {
        	for(i = 0; i < this.qttMiddle; i++) {
        		this.p_link = this.winWr.makeElement("a", {"href" : "gmail.com"}, (i+1).toString());
        		this.p_link.addListener(this, "onclick", "click_page").p = i;
        		this.pager.elm.appendChild(this.p_link.elm);
        		this.pages[i] = this.p_link;
	        }
	        this.p_link = this.winWr.makeElement("span", {}, "...");
    		this.pager.elm.appendChild(this.p_link.elm);
	        for(i = (this.cnt - this.qttStartEnd); i < this.cnt; i++) {
        		this.p_link = this.winWr.makeElement("a", {"href" : "gmail.com"}, (i+1).toString());
        		this.p_link.addListener(this, "onclick", "click_page").p = i;
        		this.pager.elm.appendChild(this.p_link.elm);
        		this.pages[i] = this.p_link;
	        }
        } else if ((this.cnt - this.current + 1) <= (this.qttStartEnd + this.nMidlHalf + 2)) {
            for(i = 0; i < this.qttStartEnd; i++) {
        		this.p_link = this.winWr.makeElement("a", {"href" : "gmail.com"}, (i+1).toString());
        		this.p_link.addListener(this, "onclick", "click_page").p = i;
        		this.pager.elm.appendChild(this.p_link.elm);
        		this.pages[i] = this.p_link;
	        }
	        this.p_link = this.winWr.makeElement("span", {}, "...");
    		this.pager.elm.appendChild(this.p_link.elm);
	        for(i = (this.cnt - this.qttMiddle); i < this.cnt; i++) {
        		this.p_link = this.winWr.makeElement("a", {"href" : "gmail.com"}, (i+1).toString());
        		this.p_link.addListener(this, "onclick", "click_page").p = i;
        		this.pager.elm.appendChild(this.p_link.elm);
        		this.pages[i] = this.p_link;
	        }
        } else {
            for(i = 0; i < this.qttStartEnd; i++) {
        		this.p_link = this.winWr.makeElement("a", {"href" : "gmail.com"}, (i+1).toString());
        		this.p_link.addListener(this, "onclick", "click_page").p = i;
        		this.pager.elm.appendChild(this.p_link.elm);
        		this.pages[i] = this.p_link;
	        }
	        this.p_link = this.winWr.makeElement("span", {}, "...");
    		this.pager.elm.appendChild(this.p_link.elm);
	        for(i = (this.current - this.nMidlHalf); i < (this.current + 1 + this.nMidlHalf); i++) {
        		this.p_link = this.winWr.makeElement("a", {"href" : "gmail.com"}, (i+1).toString());
        		this.p_link.addListener(this, "onclick", "click_page").p = i;
        		this.pager.elm.appendChild(this.p_link.elm);
        		this.pages[i] = this.p_link;
	        }
	        this.p_link = this.winWr.makeElement("span", {}, "...");
    		this.pager.elm.appendChild(this.p_link.elm);
	        for(i = (this.cnt - this.qttStartEnd); i < this.cnt; i++) {
        		this.p_link = this.winWr.makeElement("a", {"href" : "gmail.com"}, (i+1).toString());
        		this.p_link.addListener(this, "onclick", "click_page").p = i;
        		this.pager.elm.appendChild(this.p_link.elm);
        		this.pages[i] = this.p_link;
	        }
        }
    },
    
    /* ----- Loader events ----- */
    onDataLoad : function(json, dom, txt)
    {
    	if (txt == "ok") {
    		this.url.write(json.url);
    		this.url.elm.href = json.url;	
    	}
        //this.nameImg.write(json.nm);
        //this.bigImg.elm.width  = json.w;
        //this.bigImg.elm.height = json.h;
    },
    
    onDataError : function()
    {
        alert("Error!");
    },
    
    photoLoad : function(click_main)
    {
    	this.setTimer(true);
    	this.pages[this.current].addClass("active");
    	if (!click_main) {
    		this.id = this.aPhotos[this.current];
    	}
    	// this.loader.send({"idFileData" : this.id});
        
		if (this.src) {
			this.bigImg.elm.src = this.config.images_path + this.file_name;
		}
		else {
			this.bigImg.elm.src = "/file.php?id=" + this.id;
		}	
		//alert([this.id, this.config.images_path, this.file_name, this.bigImg.elm.src])
		this.bigImg.hide();
        this.mLoad.show();
    },
    
    photoShow : function()
    {
        this.setTimer(false);
        this.bigImg.show();
        this.mLoad.hide();
    },
    
    setTimer : function(set)
    {
        if(this.timer) {
            clearTimeout(this.timer);
        }
		this.timer = set ? this.winWr.setTimeout(this.config.delay, this, "photoShow") : null;
    },
    
    /**
     / THIRD FUNCTION
     * getPageSize() by quirksmode.com
     *
     * @return Array Return an array with page width, height and window width, height
     */
    ___getPageSize : function() {
        var xScroll, yScroll;
        if (window.innerHeight && window.scrollMaxY) {  
            xScroll = window.innerWidth + window.scrollMaxX;
            yScroll = window.innerHeight + window.scrollMaxY;
        } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
            xScroll = document.body.scrollWidth;
            yScroll = document.body.scrollHeight;
        } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
            xScroll = document.body.offsetWidth;
            yScroll = document.body.offsetHeight;
        }
        var windowWidth, windowHeight;
        if (self.innerHeight) { // all except Explorer
            if(document.documentElement.clientWidth){
                windowWidth = document.documentElement.clientWidth; 
            } else {
                windowWidth = self.innerWidth;
            }
            windowHeight = self.innerHeight;
        } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
            windowWidth = document.documentElement.clientWidth;
            windowHeight = document.documentElement.clientHeight;
        } else if (document.body) { // other Explorers
            windowWidth = document.body.clientWidth;
            windowHeight = document.body.clientHeight;
        }   
        // for small pages with total height less then height of the viewport
        if(yScroll < windowHeight){
            pageHeight = windowHeight;
        } else { 
            pageHeight = yScroll;
        }
        // for small pages with total width less then width of the viewport
        if(xScroll < windowWidth){  
            pageWidth = xScroll;        
        } else {
            pageWidth = windowWidth;
        }
        arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
        return arrayPageSize;
    },
    
    /**
     / THIRD FUNCTION
     * getPageScroll() by quirksmode.com
     *
     * @return Array Return an array with x,y page scroll values.
     */
     ___getPageScroll : function() {
        var xScroll, yScroll;
        if (self.pageYOffset) {
            yScroll = self.pageYOffset;
            xScroll = self.pageXOffset;
        } else if (document.documentElement && document.documentElement.scrollTop) {     // Explorer 6 Strict
            yScroll = document.documentElement.scrollTop;
            xScroll = document.documentElement.scrollLeft;
        } else if (document.body) {// all other Explorers
            yScroll = document.body.scrollTop;
            xScroll = document.body.scrollLeft; 
        }
        arrayPageScroll = new Array(xScroll,yScroll);
        return arrayPageScroll;
    },
    
    config : {
        "loader"      : "/gallery_photo_load.php",
		"images_path" : "/global/images/",
        "delay"       : 1500,
        "qttStartEnd" : 1,
        "qttMiddle"   : 7
    }
}

var modalWindow = newClass(oModalWindow);
// var modalWindow = newObject(oModalWindow);
