/**include
//_javascript/js-wrapper.js;
*/

/**
 * Modal window control
 */

var galleryPhoto = newObject({
    bigImg : null,
    //nameImg : null,
    suff   : null,
    tmp    :[],
    id     : null,
    width  : null,
    height : null,

	prev : null,
    next : null,

    lnk   : [],

    current : -1,
    //loader  : null,

	init : function (id) {
        this.id = id;
        
        // 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.bigImg = this.$("#bigImg").getChildren()[0];
        //this.nameImg = this.$("#nameImg");

        this.prev = this.$("a#prev");
        this.prev.addListener(this, "onclick", "click_prev");
        this.next = this.$("a#next");
        this.next.addListener(this, "onclick", "click_next");

        this.lnk = this.$$("table#photo_list a#photo_*");
        for(i = 0; i < this.lnk.length; i++) {
			this.lnk[i].addListener(this, "onclick").n = i;
			this.suff = this.lnk[i].elm.id.substr(6);
			this.temp = this.suff.split('_');
			if(this.temp[0] == this.id){
			   this.current = i;
			}
        }
        if(this.current >= 0){
            this.lnk[this.current].addClass("current");
        }
	},

    onclick : function(evtWr, dt)
    {
        evtWr.eventDrop();
        evtWr.elmWr.elm.blur();

        this.suff = evtWr.elmWr.elm.id.substr(6);
		this.temp = this.suff.split('_');
        
        this.bigImg.elm.src = "/file.php?id=" + this.temp[0];
        this.bigImg.elm.width = this.temp[1];
    	this.bigImg.elm.height = this.temp[2];
        //this.loader.send({"idFileData" : evtWr.elmWr.elm.id.substr(6)});

        if(this.current != -1) {
            this.lnk[this.current].removeClass("current");
            this.prev.removeClass("hide_p");
            this.next.removeClass("hide_n");
        }

        if(dt.n == 0) {
            this.prev.addClass("hide_p");
        }
        if(dt.n == this.lnk.length-1) {
            this.next.addClass("hide_n");
        }

        evtWr.elmWr.addClass("current");
        this.current = dt.n;
    },

    click_prev : function(evtWr, dt)
    {
        evtWr.eventDrop();
        evtWr.elmWr.elm.blur();
        this.next.removeClass("hide_n");

        if(this.current > 0) {
            this.lnk[this.current].removeClass("current");
            this.current--;
            this.lnk[this.current].addClass("current");
            
            this.suff = this.lnk[this.current].elm.id.substr(6);
			this.temp = this.suff.split('_');
	        
	        this.bigImg.elm.src = "/file.php?id=" + this.temp[0];
	        this.bigImg.elm.width = this.temp[1];
	    	this.bigImg.elm.height = this.temp[2];
            //this.loader.send({"idFileData" : this.lnk[this.current].elm.id.substr(6)});
        }

        if(this.current == 0) {
            evtWr.elmWr.addClass("hide_p");
        }
    },

    click_next : function(evtWr, dt)
    {
        evtWr.eventDrop();
        evtWr.elmWr.elm.blur();
        this.prev.removeClass("hide_p");

        if(this.current < this.lnk.length-1) {
            this.lnk[this.current].removeClass("current");
            this.current++;
            this.lnk[this.current].addClass("current");
            
            this.suff = this.lnk[this.current].elm.id.substr(6);
			this.temp = this.suff.split('_');
	        
	        this.bigImg.elm.src = "/file.php?id=" + this.temp[0];
	        this.bigImg.elm.width = this.temp[1];
	    	this.bigImg.elm.height = this.temp[2];
            //this.loader.send({"idFileData" : this.lnk[this.current].elm.id.substr(6)});
        }

        if(this.current == this.lnk.length-1) {
            evtWr.elmWr.addClass("hide_n");
        }
    },
    
    /* ----- Loader events ----- */
    onDataLoad : function(json, dom, text)
    {
    	//this.nameImg.write(json);
    	this.bigImg.elm.width = json.width;
    	this.bigImg.elm.height = json.height;
    },
    
    onDataError : function()
    {
        alert("Error!");
    },

    config : {
        "loader" : "/gallery/photo_name_load.php"
    }
});
