/**
 * Photo gallery conctructor (itialisation here)
 */
var photo_gallery = function(path, prefixId, imageArray, winWr)
{
    this.path       = path;
    this.prefixId   = prefixId;
    this.imageArray = imageArray;
    this.winWr      = winWr ? winWr : _wrapper;
    for(var ind=0; ind<this.imageArray.length; ind++) {
        this.imageArray[ind].status = 0;
    }

    this.winWr.setOnloadListener(this);
	this.winWr.setOnUnloadListener(this, "close_popup");
}

/**
 * Event processors
 */
photo_gallery.prototype = {
    currentIndex : -1,    // index of current image for this.imageArray
    isSlideShow  : false, // slide show flag
    bl           : {},    // popup html-blocks exclude Img-tag
    timerID      : null,  // timer ID

/* ============= main window functions ============= */
    onload : function(evtWr)
    {
        var linkId, links, url, name, ind, lsn, show, re, i;
        this.winWr.removeOnloadListener(this);

        this.photos = {};
        re = /([^\/]+)\.\w{3}$/i;
        links = this.winWr.doc.getElementsByTagName("a");
        for(i=0; i<links.length; i++) {
            linkId = links[i].getAttribute("id");
            if(linkId && linkId.substring(0, this.prefixId.length) == this.prefixId) {
                url = links[i].getAttribute("href");
                if(re.test(url)) {
                    name = re.exec(url)[1];
                    for(ind=0; ind<this.imageArray.length; ind++) {
                    	if(this.imageArray[ind].name == name) {
                            lsn = this.winWr.getElmWrapper(links[i]).addListener(this, "onclick", "mainPageRequest");
                            lsn.url = url;
                            lsn.slsh = false;
                            lsn.imgIndex = ind;
                            break;
                    	}
                    }
                }
            }
        }
        show = this.winWr.getElement("sShow_" + this.prefixId, 0);
        if(show.elm) {
            var shLsn = show.addListener(this, "onclick", "mainPageRequest");
            shLsn.url = show.elm.href;
            shLsn.slsh= true;
            shLsn.imgIndex = 0;
        }
    },
    mainPageRequest : function(evtWr, par)
    {
    	evtWr.eventDrop();
        this.isSlideShow = par.slsh || (this.isSlideShow && this.popupWr);
        if(this.popupWr && this.popupWr.win) {
        	try {
        		this.popupWr.win.focus();
                if (par.slsh) this.startSlideshow();
                else this.requestImage(par.imgIndex);
        	} catch (e) {
        		this.onUnload_popup();
        	}
        }
        if(!this.popupWr) {
            this.firstIndex = par.imgIndex;
            open(par.url + "?js=1", "gallery", "width=" + this.config.popup_width + ",height=" + this.config.popup_height + ",top=" + (window.screen.availHeight - this.config.popup_height)/2 + ",left=" + (window.screen.availWidth - this.config.popup_width)/2);
        }
    },

/* ============= Pop-Up window functions ============= */

/* ---- Pop-Up init/destrucr ---- */
    popup_init : function(popupWin)
	{
		this.popupWr = new winWrapper(popupWin);
		this.popupWr.setOnloadListener(this, "onload_popup");
		this.popupWr.setOnUnloadListener(this, "onUnload_popup");
	},
	onload_popup : function()
	{
		var pWr = this.popupWr
		var bl = this.bl;
		/* Navigation */
		bl.signature  = pWr.getElement("signature");

		bl.first     = pWr.getElement("first");
		bl.previous  = pWr.getElement("previous");
	    bl.next      = pWr.getElement("next");
		bl.last      = pWr.getElement("last");
		bl.slideshow = pWr.getElement("slideshow");
		bl.stopshow  = pWr.getElement("stopshow");

		bl.first.addListener(this, "onclick", "first_photo");
		bl.previous.addListener(this, "onclick", "previous_photo");
	    bl.next.addListener(this, "onclick", "next_photo");
		bl.last.addListener(this, "onclick", "last_photo");
		bl.slideshow.addListener(this, "onclick", "startSlideshow");
		bl.stopshow.addListener(this, "onclick", "stopSlideshow");

		/* Content */
		bl.gallery   = pWr.getElement("gallery");
		bl.caption   = pWr.getElement("caption");
		bl.error_msg = pWr.getElement("error_msg");

        if(this.isSlideShow) {
    		this.bl.slideshow.hide();
            this.bl.stopshow.show();
        }
		this.chargeImage(this.firstIndex);

		this.popupWr.win.focus();
	},
    close_popup : function()
	{
	    try {
	      if (this.popupWr && this.popupWr.win) this.popupWr.win.close();
	    } catch (e) {}
		this.onUnload_popup();
	},
	onUnload_popup : function()
	{
	    this.popupWr = null;
	    this.clearTimer();
        this.isSlideShow = false;
        this.currentIndex = -1;
        for(var ind=0; ind<this.imageArray.length; ind++) {
            this.imageArray[ind].status = 0;
            this.imageArray[ind].imgWr = null;
        }
	},

/* ---- Image request/charge/switch ---- */
	requestNextImage : function() // <-- Thir request from Slide Show only!
	{
	    this.nextShowImg = this.currentIndex<this.imageArray.length-1 ? this.currentIndex+1 : 0;
		if(!this.imageArray[this.nextShowImg].status) {
			this.chargeImage(this.nextShowImg);
		}
	},
	requestImage : function(imgIndex, evtWr, hideCurrent) // <-- Thir request from Navigation only!
    {
        if (evtWr) evtWr.eventDrop();

        if(this.imageArray[imgIndex].status == 2) {
        	this.switchImage(imgIndex);
        	if(this.isSlideShow) {
        		this.showGo();
        	}
        }
        else if(!this.imageArray[imgIndex].status) {
        	if(hideCurrent && !this.isSlideShow) {
        	    var bl = this.bl;
    	    	bl.caption.show();
    	    	bl.error_msg.hide();
    		    bl.signature.write("");
    	    	if(this.currentIndex >= 0) this.imageArray[this.currentIndex].imgWr.hide();
    	    	this.popupWr.doc.title = "Please wait"
			}
			this.chargeImage(imgIndex);
        }
    },
	chargeImage : function(imgIndex)
    {
    	var imgAr = this.imageArray[imgIndex];
    	if(imgAr.status) return false;
    	imgAr.status = 1;

     	var img = this.popupWr.doc.createElement("img");
		this.bl.gallery.elm.appendChild(img);
		var imgWr = this.popupWr.getElmWrapper(img);
		imgAr.imgWr = imgWr;
		imgWr.addListener(this, "onload", "onloadImage").imgIndex = imgIndex;
		imgWr.addListener(this, "onerror", "onErrorImage").imgIndex = imgIndex;
		imgWr.elm.src = this.path + imgAr.name + ".jpg";
		imgWr.elm.alt = imgAr.main_alt;
        return true;
    },
	switchImage : function(imgIndex)
	{
	    var bl, sh, img, h;
	    bl = this.bl;

	    bl.error_msg.hide();
	    if(this.currentIndex < 0) bl.caption.hide();
	    else this.imageArray[this.currentIndex].imgWr.hide();

	    this.currentIndex = imgIndex;

        img = this.imageArray[imgIndex];
	    img.imgWr.show();
	    bl.signature.write(img.main_alt);
	    this.popupWr.doc.title = img.main_alt

		this.popupWr.win.focus();

		// -- Show/hide navigation -- \\
		if(!imgIndex) sh = [false,true];
		else if (imgIndex == this.imageArray.length-1) sh = [true,false];
		else sh = [true,true];

		bl.first.setDisplay(sh[0]);
		bl.previous.setDisplay(sh[0])
	    bl.next.setDisplay(sh[1]);
		bl.last.setDisplay(sh[1]);
	},

/* ---- Navigation events ---- */
	next_photo : function(evtWr)
	{
        if(this.currentIndex<this.imageArray.length-1) this.requestImage(this.currentIndex+1, evtWr);
	},
	first_photo : function(evtWr)
	{
        this.requestImage(0, evtWr);
	},
	previous_photo : function(evtWr)
	{
	    if(this.currentIndex>0) this.requestImage(this.currentIndex-1, evtWr);
	},
	last_photo : function(evtWr)
	{
        this.requestImage(this.imageArray.length-1, evtWr);
	},

/* ---- Slide Show / Timer functions ---- */
	startSlideshow : function(evtWr)
	{
        if(evtWr) evtWr.eventDrop();
        this.isSlideShow = true;
        this.bl.slideshow.hide();
        this.bl.stopshow.show();
        this.showGo();
	},
	stopSlideshow : function(evtWr)
	{
        if(evtWr) evtWr.eventDrop();
        this.isSlideShow = false;
        this.bl.stopshow.hide();
        this.bl.slideshow.show();
	    this.clearTimer();
	},

	showGo : function () {
        this.clearTimer();
	    var func2 = (function(o) {
            return function(){
				try {
            		o.onTimer.call(o);
				} catch (e) {
					o.clearTimer();
				}
            }
        })(this);
        this.timerID = setInterval(func2, this.config.show_period);
        this.requestNextImage();
	},
	onTimer : function () {
	    if(this.imageArray[this.nextShowImg].status == 2) {
	        this.switchImage(this.nextShowImg);
	        this.requestNextImage();
	    } else {
	        this.clearTimer();
	    }
	},
 	clearTimer : function () {
        if (this.timerID) clearInterval(this.timerID);
        this.timerID = null;
	},

/* ---- Image Loader ---- */
	onloadImage : function(evtWr, par)
	{
		var img = this.imageArray[par.imgIndex];
		img.status = 2;

		var imgWr = img.imgWr;
		imgWr.style.marginTop = "-2000px";
		imgWr.show();

		var w = imgWr.getWidth();
		var h = imgWr.getHeight();
	    imgWr.hide();

	    if(w<=500 && w>=10) imgWr.style.marginLeft = (this.config.popup_area_width - w)/2 + "px";
	    if(h<=500 && h>=10) imgWr.style.marginTop = (this.config.popup_area_height - h)/2 + "px";

	    if(!this.isSlideShow) {
	        this.switchImage(par.imgIndex);
	    } else if(this.timerID == null) {
            this.switchImage(par.imgIndex);
            this.showGo();
	    }
	},

	onErrorImage : function(evtWr, par)
	{
	    var bl = this.bl;
	    bl.caption.hide();
	    if(this.currentIndex >= 0) this.imageArray[this.currentIndex].imgWr.hide();
	    this.stopSlideshow();
	    bl.error_msg.show();
	    bl.signature.write("");
		this.popupWr.win.focus();
		this.popupWr.doc.title = "Error"
	},

/* ============= Configuration parameters ============= */
    config : {
        popup_width       : 530,
        popup_height      : 610,
        popup_area_width  : 500,
        popup_area_height : 500,
        show_period       : 3500
    }
}