_pageContainers = new Object();

isGoogleChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
//if(isGoogleChrome) alert("chrome");

function show(divid, pageid, Transition)
{
	if (Transition==null)Transition = TransitionBase;
    if (divid in _pageContainers)
        _pageContainers[divid].show(pageid, Transition);
}

function close_last(divid, pageid) {
    if (divid in _pageContainers)
        _pageContainers[divid].close_last();
}

function close_prev(divid, pageid) {
    if (divid in _pageContainers)
        _pageContainers[divid].close_prev();
}


function PageContainer(divid, dummygif, pages_arr) {
    //alert("PageContainer");
    _pageContainers[divid] = this;
    this.div = document.getElementById(divid);

    this.imgdiv = document.createElement("div");
    this.div.appendChild(this.imgdiv);
    this.imgdiv.className = "layerdiv";
    this.imgdiv.style.zIndex = 2;

    this.imgdiv2 = document.createElement("div");
    this.div.appendChild(this.imgdiv2);
    this.imgdiv2.className = "layerdiv";
    this.imgdiv2.style.zIndex = 3;

    this.linkdiv = document.createElement("div");
    this.div.appendChild(this.linkdiv);
    this.linkdiv.className = "layerdiv";
    this.linkdiv.style.zIndex = 4;

    this.linkimg = document.createElement("img");
    this.linkdiv.appendChild(this.linkimg);
    this.linkimg.src = dummygif;
    this.linkimg.className = "imgclass1";
    this.linkimg.useMap = "";

    this.pages = new Object();
    for (var i = 0; i < pages_arr.length ;i++) {
        var p = pages_arr[i];
        this.pages[p[0]] = new Page(this, p);
    }

    this.open_pages = new Array();
    this.topPage = function(param) {
        if (param==null) return this.open_pages[this.open_pages.length - 1];
        else this.open_pages[this.open_pages.length-1] = param;
    }

    this.inTransition = false;

    this.show = function(pageid, Transition) {
        if (this.inTransition) return;
        this.inTransition = true;
        this.transition = new Transition();
        var page = this.pages[pageid];
        if (this.open_pages.length>0) {
            if (page != this.topPage()) {
            	this.topPage().active(false);
                this.open_pages.push(page);
                this.topPage().img.style.zIndex = this.open_pages.length;
                this.topPage().show();
                return;
            }
        }
        else {
            this.open_pages.push(page);
            this.topPage().img.style.zIndex = 1;
            this.topPage().show();
            return;
        }
        this.inTransition = false;
    }
	this.show2 = function() {
		this.transition.end();
		this.inTransition = false;
	}
    this.close_last = function() {
		if(this.open_pages.length > 0){
        	this.topPage().hide();
        	this.open_pages.pop();
		}
        if(this.open_pages.length > 0)this.topPage().active(true);
    }
    this.close_prev = function() {
		if(this.open_pages.length > 1){
	        var toppage = this.open_pages.pop();
	        this.topPage().hide();
	        this.topPage(toppage);
		}
    }
}

function Page(parent, p) {
    this.parent = parent;

    this.img = document.createElement("img");
    this.parent.imgdiv.appendChild(this.img);
    this.img.className = "imgclass1";
    this.img.style.visibility = "hidden";

    this.imgurl = p[1];
    this.links = new PageLinks(p[2],parent);

    this.loaded = false;

    this.show = function() {
        if (!this.loaded) {
			if(!isGoogleChrome)setTimeout('document.body.style.cursor = "wait";',1);
            var _this = this;
            onLoadHandler = function() { _this.loaded = true; setTimeout('document.body.style.cursor = "default";',1); _this.show2(); };
            if (this.img.addEventListener)
                this.img.addEventListener("load", onLoadHandler, false);
            else if (this.img.attachEvent)
                this.img.attachEvent("onload", onLoadHandler);
            else
                this.img.onload = onLoadHandler;
            this.img.src = this.imgurl;
            return;
        } else this.show2();
    }
    this.show2 = function() {
        this.parent.transition.start(this);
    }
    this.show3 = function() {
        this.active(true);
        this.parent.show2();
    }
    this.hide = function() {
        this.active(false);
        this.img.style.visibility = "hidden";
    }
    this.active = function(a){
        this.links.active(a);
    }
    this.html_element = function() {
        return this.img;
    }
}

function PageLinks(mapid, parent) {
    this.map = document.getElementById(mapid);
    this.imgs = new Array();
    this.areas = new Array();

	this.parent = parent;
	var j=0;
	for(var i=0; i<this.map.childNodes.length; i++)
	{
		var area = this.map.childNodes[i];
		if(area.tagName == null) continue;
		if(area.tagName.toLowerCase() != "area") continue;
		if(isGoogleChrome) area.href = "#";
		this.areas[j] = area;
		this.imgs [j] = document.createElement("img");
		this.parent.imgdiv2.appendChild(this.imgs[j]);
		this.imgs[j].style.visibility = "hidden";
		this.imgs[j].src = this.areas[j].alt;
		this.areas[j].alt = "";
		this.imgs[j].className = "imgclass1 pages_rolover";
		var _this = this;
		//var jj = j;
		this.areas[j].onmouseover = bind1st(function(jj){if(!isGoogleChrome)document.body.style.cursor = "pointer"; _this.imgs[jj].style.visibility = "visible";},j);
		this.areas[j].onmouseout = bind1st(function(jj){if(!isGoogleChrome)document.body.style.cursor = "default"; _this.imgs[jj].style.visibility = "hidden";},j);
		j++;
	}

    this.active = function(a) {
        if (a) {
            if(this.parent.linkimg.useMap != "") alert("napaka v PageLinks.active! " + this.parent.linkimg.useMap);
            else this.parent.linkimg.useMap = "#" + this.map.name;
        }
        else {
            if(this.parent.linkimg.useMap == ("#" + this.map.name) ) this.parent.linkimg.useMap = "";
            //alert(this.imgs[0]);
            document.body.style.cursor = "default"
            for(var i=0; i<this.imgs.length; i++)this.imgs[i].style.visibility = "hidden";
        }
    }
}

function showElement(element) {
    element.style.visibility = "visible";
}

function hideElement(element) {
    element.style.visibility = "hidden";
}

function bind1st(fnc, param) {
    return function() {
        fnc(param);
    }
}

/*function Slide() {
    //alert(p);
    //this.x = parseInt(this.page.html_element().style.left);
    //this.y = parseInt(this.page.html_element().style.top);
    this.deltax = 0;
    this.deltay = 0;
    this.n_move = 0;
    this.dt = 10;
    this.timer = 0;
    this.move = function() {
        this.tmpx += this.deltax / this.n_move;
        this.tmpy += this.deltay / this.n_move;
        this.i_move++;
        if (this.i_move < this.n_move) {
            this.page.html_element().style.left = Math.round(this.tmpx) + "px";
            this.page.html_element().style.top = Math.round(this.tmpy) + "px";
        }
        else {
            clearInterval(this.timer);
            this.timer = 0;
            this.tmpx = 0;
            this.tmpy = 0;
            this.page.html_element().style.left = Math.round(this.tmpx) + "px";
            this.page.html_element().style.top = Math.round(this.tmpy) + "px";
            this.page.show3();
        }
    };
    this.start = function(p) {
		this.page = p;
        this.tmpx = -this.deltax;
        this.tmpy = -this.deltay;
        this.i_move = 0;
        this.page.html_element().style.left = Math.round(this.tmpx) + "px";
        this.page.html_element().style.top = Math.round(this.tmpy) + "px";
        this.page.html_element().style.visibility = "visible";
        var _this = this;
        this.timer = setInterval(function() { _this.move() }, this.dt);
    };
    this.end = function(){}
}*/

function Linear() {
    //alert(p);
    //this.x = parseInt(this.page.html_element().style.left);
    //this.y = parseInt(this.page.html_element().style.top);
    this.n_move = 0;
    this.dt = 10;
    this.timer = 0;
    this.move = function() {
        this.u += this.du;
        this.i_move++;
        if (this.i_move < this.n_move) {
            this.step();
        }
        else {
            clearInterval(this.timer);
            this.timer = 0;
            this.u = 1;
            this.step();
            this.page.show3();
        }
    };
    this.start = function(p) {
		this.page = p;
		this.u = 0;
		this.du = 1/this.n_move;
        this.i_move = 0;
        this.step();
        this.page.html_element().style.visibility = "visible";
        var _this = this;
        this.timer = setInterval(function() { _this.move() }, this.dt);
    };
    this.end = function(){}
    this.step = function(){}
}

function Slide()
{
	Linear.apply(this,new Array());
	this.deltax = 0;
	this.deltay = 0;
	this.step = function(){
		this.page.html_element().style.left = Math.round((this.u-1)*this.deltax) + "px";
        this.page.html_element().style.top = Math.round((this.u-1)*this.deltay) + "px";
	}
}

function Fade()
{
	Linear.apply(this,new Array());
	this.step = function(){
		if(this.u==1)
			this.page.html_element().style.filter = null;
		else
			this.page.html_element().style.filter = "alpha(opacity=" + Math.round(this.u*100) + ")";
        this.page.html_element().style.opacity = this.u;
	}
}

function TransitionBase() {
	this.start = function(p) {
    	this.page = p;
    	this.page.html_element().style.visibility = "visible";
    	this.page.show3();
	}
	this.end = function(){}
}

function CloseLast()
{
	this.end = function(){this.page.parent.close_prev();}
}

function TransitionCL()
{
	TransitionBase.apply(this,new Array());
	CloseLast.apply(this,new Array());
}
