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

/**
 * Modal window control
 *
 */
var modalWindow = newObject({
	mWin       : null,
	voile      : null,
	mContent   : null,
	mLoad      : null,
	bigImg     : null,
	url        : null,
	pager      : null,
	p_link     : null,
	pages      : [],
	
	id           : null,
	aPhotos      : [],
	cnt          : null,
	flag_url     : null,
	
	qttStartEnd : null,
	qttMiddle   : null,
	nMidlHalf   : null,
	
	lnk  : [],
	prev : null,
    next : null,
    close  : null,
	
    timer   : null,
    loader  : null,
    current : 0,
	
	init : function (id, aPhotos, cnt) {
        this.id = id;
        this.cnt = cnt;
        this.aPhotos = aPhotos;
        
        // 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.voile    = this.$("#modal_window #voile");
        this.mContent = this.$("#modal_window #modal_content");
        this.mContent.addListener(this, "onclick", "click_stop");
        this.$w0.addListener(this, "onclick", "click_close");
        
        this.mLoad    = this.$("#modal_window #modal_load");
        this.bigImg   = this.$("#modal_window #bigImg").getChildren()[0];
        this.url      = this.$("#modal_window #url");
        this.url.addListener(this, "onclick", "click_url");
        this.pager    = this.$("#modal_window #pager");

        this.lnk = this.$$("#photo_list a#photo_*");
        for(i = 0; i < this.lnk.length; i++) {
           this.lnk[i].addListener(this, "onclick").n = i;
        }
        
        this.prev = this.$("a#prev");
        this.prev.addListener(this, "onclick", "click_prev").p = (this.current - 1);
        this.next = this.$("a#next");
        this.next.addListener(this, "onclick", "click_next").p = (this.current + 1);
        
        this.close = this.$("#modal_window a#close");
        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);
        
        width = this.$w0.getDocumentWidth() > this.$w0.getWindowWidth() ? this.$w0.getDocumentWidth() : this.$w0.getWindowWidth();
        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";
        
        this.mWin.show();
    },
    
    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;	
    	}
    },
    
    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});
        
    	this.bigImg.elm.src = "/file.php?id=" + this.id;
		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;
    },
    
    config : {
        "loader"      : "/portfolio/site_load.php",
        "delay"       : 1500,
        "qttStartEnd" : 1,
        "qttMiddle"   : 7
    }
});
