var imageviewer = function(){
	imageviewer.prototype.styles = function(){}
	imageviewer.prototype.styles.layer = function(){}
	imageviewer.prototype.styles.frame = function(){}
	imageviewer.prototype.styles.title = function(){}

	/* ****************************** STYLE ****************************** */
	imageviewer.prototype.spinnerimage = '/content/images/imageviewer/imageviewer_spinner.gif';
	imageviewer.prototype.previmage = '/content/images/imageviewer/imageviewer_prev.gif';
	imageviewer.prototype.nextimage = '/content/images/imageviewer/imageviewer_next.gif';
	imageviewer.prototype.closeimage = '/content/images/imageviewer/imageviewer_close.gif';
	//imageviewer.prototype.saveimage = 'images/imageviewer/imageviewer_save.gif';
	
	imageviewer.prototype.styles.layer.opacity = 0.95;
	imageviewer.prototype.styles.layer.bgcolor = '#0b0e19';
	imageviewer.prototype.styles.frame.margin = 10;
	imageviewer.prototype.styles.frame.padding = 10;
	imageviewer.prototype.styles.frame.border = '3px solid #222';
	imageviewer.prototype.styles.frame.bgcolor = '#FFF';
	imageviewer.prototype.styles.title.fontFamily = 'Tahoma';
	imageviewer.prototype.styles.title.fontSize = 12;
	imageviewer.prototype.styles.title.fontColor = '#000';
	
	/* ****************************** ****************************** */
	
	imageviewer.prototype.list = new Array();
	imageviewer.prototype.titles = new Array();
	imageviewer.prototype.download = new Array();
	imageviewer.prototype.title_margin = 40;
	imageviewer.prototype.img_close;
	imageviewer.prototype.area;
	imageviewer.prototype.selected;
	imageviewer.prototype.allowclose = true;
	imageviewer.prototype.dimm;
	imageviewer.prototype.contener;
	imageviewer.prototype.div;
	imageviewer.prototype.img;
	imageviewer.prototype.map;
	imageviewer.prototype.prev;
	imageviewer.prototype.next;
	imageviewer.prototype.st;
	
	imageviewer.prototype.open = function(){
		
		this.dimm = stardust.createTag('div', {zIndex:2100,background:this.styles.layer.bgcolor,opacity:0,position:'absolute',top:'0px',left:'0px',width:'100%',height:stardust.bodysize()[1]+'px'});
		document.body.appendChild(this.dimm);
		this.contener = stardust.createTag('div', {zIndex:2101,position:'absolute',top:'0px',left:'0px',paddingTop:this.styles.frame.margin+'px',width:'99%',textAlign:'center'});
		this.div = stardust.createTag('div', {position:'relative',backgroundColor:this.styles.frame.bgcolor,backgroundImage:'url('+this.spinnerimage+')',backgroundPosition:'center',backgroundRepeat:'no-repeat',margin:'0px auto',width:'300px',height:'300px',border:this.styles.frame.border,overflow:'hidden'});
		
		//this.debug = stardust.createTag('div', {zIndex:102,background:'#FFF',position:'absolute',top:'300px',left:'0px'});
		
		this.map = stardust.createTag('map', {id:'prev_next_map',name:'prev_next_map'});
		this.prev = stardust.createTag('area', {href:'#prev',coords:'0,0,0,0'});
		this.next = stardust.createTag('area', {href:'#next',coords:'0,0,0,0'});
		this.map.appendChild(this.prev);
		this.map.appendChild(this.next);
		this.div.appendChild(this.map);
		
		this.prev_img = stardust.createTag('img', {src:this.previmage,border:'none'});
		this.prev_a = stardust.createTag('a', {href:'#prev',alt:'<<< prevoius',opacity:0.15,display:'block',zIndex:102,position:'absolute',left:this.styles.frame.padding+'px',top:'140px'});
		this.prev_a.appendChild(this.prev_img);
		this.div.appendChild(this.prev_a);
		
		this.next_img = stardust.createTag('img', {src:this.nextimage,border:'none'});
		this.next_a = stardust.createTag('a', {href:'#next',alt:'next >>>',opacity:0.15,display:'block',zIndex:102,position:'absolute',right:this.styles.frame.padding+'px',top:'140px'});
		this.next_a.appendChild(this.next_img);
		this.div.appendChild(this.next_a);
		
		this.img_close = stardust.createTag('img', {src:this.closeimage,border:'none'});
		this.img_close.onclick = function(){ imageviewer.allowclose = true; return false; }
		this.close_link = stardust.createTag('a', {href:'#',position:'absolute',right:this.styles.frame.padding+'px',top:'0px'});
		
		//this.img_save = stardust.createTag('img', {src:this.saveimage,border:'none'});
		//this.save_link = stardust.createTag('a', {href:'#',position:'absolute',right:this.styles.frame.padding+90+'px',top:'0px'});
		
		this.div_title_1 = stardust.createTag('div', {opacity:0,position:'relative',width:'300px',height:'40px'});
		this.div_title_2 = stardust.createTag('div', {position:'absolute',left:this.styles.frame.padding+'px',top:'0px',width:stardust.width(this.div)-40-(this.styles.frame.padding*3)+'px',color:this.styles.title.fontColor,fontFamily:this.styles.title.fontFamily,fontSize:this.styles.title.fontSize+'px',textAlign:'left'});
		this.close_link.appendChild(this.img_close);
		this.div_title_1.appendChild(this.close_link);
		//this.save_link.appendChild(this.img_save);
		//this.div_title_1.appendChild(this.save_link);
		this.div_title_1.appendChild(this.div_title_2);
		this.div.appendChild(this.div_title_1);

		this.dimm.onclick = function(){ imageviewer.close(); }
		this.contener.onclick = function(){ imageviewer.close(); }
		this.div.onmouseover = function(){ imageviewer.allowclose = false; }
		this.div.onmouseout = function(){ imageviewer.allowclose = true; imageviewer.links(0,0); }
		
		this.fade(this.dimm, 0, this.styles.layer.opacity, 0.125, 'imageviewer.show("opened")' );
		
		document.body.appendChild(this.contener);
		this.contener.appendChild(this.div);
		return false;
	}
	
	imageviewer.prototype.buttons = function(state){
		var w = state? stardust.width(this.div)-(this.styles.frame.padding*2):0;
		var h = state? stardust.height(this.div)-(this.styles.frame.padding*2):0;

		this.prev.setAttribute('coords', '0,0,'+(w/2)+','+h);
		this.next.setAttribute('coords', ''+(w/2)+',0,'+w+','+h);
		this.prev.onclick = function(){ imageviewer.show('change', imageviewer.selected-1); this.blur(); return false; }
		this.next.onclick = function(){ imageviewer.show('change', imageviewer.selected+1); this.blur(); return false; }
		this.prev_a.onclick = function(){ imageviewer.show('change', imageviewer.selected-1); this.blur(); return false; }
		this.next_a.onclick = function(){ imageviewer.show('change', imageviewer.selected+1); this.blur(); return false; }
		
		if(state)
			stardust.addEvent(document.body, 'keypress', imageviewer.bindkeys, false);
		else
			stardust.removeEvent(document.body, 'keypress', imageviewer.bindkeys );
		
		this.prev.onmouseover = function(){ imageviewer.links(1,0); }
		this.prev.onmousemove = function(){ imageviewer.links(1,0); }
		this.prev.onmouseout = function(){ imageviewer.links(0,null); }
		this.prev_a.onmouseover = function(){ imageviewer.links(1,0); }
		
		this.next.onmouseover = function(){ imageviewer.links(0,1); }
		this.next.onmousemove = function(){ imageviewer.links(0,1); }
		this.next.onmouseout = function(){ imageviewer.links(null,0); }
		this.next_a.onmouseover = function(){ imageviewer.links(0,1); }
		
	}
	
	imageviewer.prototype.bindkeys = function(e)
	{
		var key = stardust.keyboardEvent(e);
		if( key=='p' ) imageviewer.show('change', imageviewer.selected-1);
		else if( key=='n' ) imageviewer.show('change', imageviewer.selected+1);
		else if( key=='z' ){
			imageviewer.allowclose = true;
			imageviewer.close();
		}
		return false;
	}
	
	imageviewer.prototype.links = function(p, n)
	{
		if(p!=null) stardust.opacity(imageviewer.prev_a, p? 1:0.15);
		if(n!=null) stardust.opacity(imageviewer.next_a, n? 1:0.15);
	}
	
	imageviewer.prototype.spinner = function(state)
	{
		this.div.style.backgroundImage = state? 'url('+this.spinnerimage+')':'';
	}
	
	imageviewer.prototype.title = function(value)
	{
		//this.div_title_2
		stardust.width(this.div_title_1, stardust.width(this.div));
		stardust.width(this.div_title_2, stardust.width(this.div)-150);
		this.div_title_2.innerHTML = '<strong>'+(1+this.selected) + '/' + this.list[this.area].length + '</strong>'+ ((value)? (' - ' + value):'');
		stardust.opacity(this.div_title_1, 1);
		this.div_title_1.style.display = '';
		
		//this.img_save.onclick = function(){ imageviewer.allowclose = false; }
		/*
		this.save_link.onclick = function(){
			imageviewer.allowclose = false;
			window.location.href = imageviewer.download[imageviewer.list[imageviewer.area][imageviewer.selected].getAttribute('href')];
			return false;
			}
		*/
		imageviewer.show("titled");
	}

	imageviewer.prototype.fade = function( o, p, m, dir, func){
		
		//stardust.$('bodydebug').innerHTML = stardust.$('bodydebug').innerHTML + p + '-' + m + '<br>';
		if( (p+dir)>=0 & ((p+dir<m & p<m) || (p+dir>m & p>m)) ){
			p += dir;
			stardust.opacity(o, p);
			this.st = window.setTimeout( function(){ imageviewer.fade(o, p, m, dir, func) }, 0);
		}else{
			stardust.opacity(o, m);
			eval(''+func+'');
			//this.st = window.setTimeout( function(){ imageviewer.fade(o, p, m, dir, func) }, 0);
		}
	}
	
	imageviewer.prototype.resize = function( o, w, h, step, func){
		this.spinner(0);
		var cur_w = stardust.width(o);
		var cur_h = stardust.height(o);
		var step_w = Math.round((w-cur_w)/step);
		var step_h = Math.round((h-cur_h)/step);
		var exit = true;
		
		if( (cur_w+step_w)>=0 & ((cur_w+step_w<w & cur_w<w) || (cur_w+step_w>w & cur_w>w)) ){
			stardust.width(o, cur_w + step_w );
			exit = false;
		}
		if( (cur_h+step_h)>=0 & ((cur_h+step_h<h & cur_h<h) || (cur_h+step_h>h & cur_h>h)) ){
			stardust.height(o, cur_h + step_h );
			exit = false;
		}
		if( !exit ){
			this.move();
			this.st = window.setTimeout( function(){ imageviewer.resize(o, w, h, step-1, func) }, 0);
		}else{
			stardust.width(o, w );
			stardust.height(o, h );
			this.move();
			eval(''+func+'');
		}
	}
	
	imageviewer.prototype.close = function()
	{
		if( !this.allowclose || !this.div || !this.contener ) return false;
		window.clearTimeout(this.st);
		this.buttons(0);
		this.contener.removeChild(this.div, true);
		document.body.removeChild(this.contener, true);
		this.contener = undefined;
		this.div = undefined;
		this.img = undefined;
		this.fade(this.dimm, 1, 0, -0.3, 'imageviewer.remove()' );
	}
	
	imageviewer.prototype.remove = function()
	{
		document.body.removeChild(this.dimm, true);
		window.clearTimeout(this.st);
		this.dimm = undefined;
	}
	
	imageviewer.prototype.load = function()
	{
		this.links(0,0);
		this.spinner(1);
		this.img = stardust.createTag('img', {margin:this.styles.frame.padding+'px',opacity:0,border:'none',usemap:'#prev_next_map'});
		this.img.onload = function(){ imageviewer.resize(imageviewer.div, imageviewer.img.width+(imageviewer.styles.frame.padding*2), imageviewer.img.height+(imageviewer.styles.frame.padding*2), 6, 'imageviewer.show("loaded")'); }
		this.img.src = this.list[this.area][this.selected];
	}
	
	imageviewer.prototype.show = function(action, i)
	{
		if( !this.dimm ) this.open();
		switch( action )
		{
			case 'change':
				if(isNaN(i)) break;
				this.buttons(0);
				this.selected = (i<0)? this.list[this.area].length-1:(i>=this.list[this.area].length)?0:i;
				this.show('start', this.list[this.area][this.selected]);
				break;
			case 'start':
				if( !stardust.isObject(i) ) break;
				if(i.getAttribute('rel')) this.area = i.getAttribute('rel');
				for(k in this.list[this.area]) if(this.list[this.area][k]==i) this.selected = parseInt(k);
				if(this.div_title_1) this.div_title_1.style.display = 'none';
				if( this.img  ){
					this.div.removeChild(this.img, true );
					this.img = false;
					this.buttons(0);
					this.show('opened');
				}
				break;
			case 'opened':
				this.load();
				break;
			case 'loaded':
				this.div.insertBefore(this.img, this.div.childNodes[0]);
				this.fade(this.img, 0, 1, 0.125, 'imageviewer.show("showed")' );
				break;
			case 'showed':
				this.buttons(1);
				//alert( this.list[this.area][this.selected].getAttribute('href') );
				this.title( this.titles[this.list[this.area][this.selected].getAttribute('href')] );
				break;
			case 'titled':
				this.resize(imageviewer.div, imageviewer.img.width+(imageviewer.styles.frame.padding*2), imageviewer.img.height+(imageviewer.styles.frame.padding*2)+this.title_margin, 3, '');
				break;
		}
		return false;
	}

	imageviewer.prototype.move = function()
	{
		if(!this.div) return;
		var old_top = parseInt(this.contener.style.paddingTop);
		var div_height = stardust.height(this.div) + this.styles.frame.margin + this.styles.frame.margin;
		var view_height = stardust.viewsize()[1];
		if( div_height < view_height ){
			var div_top = old_top + (( old_top<(this.styles.frame.margin + stardust.scrollY()) )? 10:-10);
			if( Math.abs(old_top-(this.styles.frame.margin + stardust.scrollY()))<10 ){
				return;
				//div_top = old_top;
			}
		}else{
			var diff_top = stardust.elementPosition(this.div)[1] - this.styles.frame.margin - stardust.scrollY();
			var diff_bottom = ( stardust.scrollY() + view_height ) - (stardust.elementPosition(this.div)[1] + div_height -  this.styles.frame.margin);
			if( diff_top>0 ) var div_top = this.styles.frame.margin + stardust.scrollY();
			if( diff_bottom>0 ) var div_top = stardust.scrollY() + view_height - div_height;
		}
		if( div_top + div_height >= this.body_height ) div_top = this.body_height - div_height - this.styles.frame.margin;
		stardust.height(this.dimm, this.body_height);
		stardust.padding(this.contener, new Array(div_top,null,null,null));
	}
	
	imageviewer.prototype.init = function()
	{
		var tmp_list = stardust.getElementsByTagName('a', document);
		for(k in tmp_list){
			if(!tmp_list[k].getAttribute('rel')) continue;
			var rel = tmp_list[k].getAttribute('rel');
			if( rel.substr(0,11)=='imageviewer' ){
				if( this.list[rel]==undefined ) this.list[rel] = new Array();
				this.list[rel].push( tmp_list[k] );
				this.titles[ tmp_list[k].getAttribute('href') ] = tmp_list[k].getAttribute('title');
				this.download[ tmp_list[k].getAttribute('href') ] = tmp_list[k].getAttribute('alt');
				tmp_list[k].onclick = function(){
					imageviewer.show('start', this);
					return false;
				}
			}
		}
		this.body_height = stardust.getBodyDimensions()[1];
		setInterval( function(){ imageviewer.move()	}, 10);
	}
	
	this.init();
}

var imageviewer;
stardust.addLoadEvent( function(){ imageviewer = new imageviewer(); } );
