/*function debug(str){
	if(document.getElementById('debug')!=null)
		document.getElementById('debug').innerHTML=str
	else{
		var deb_ = document.createElement('DIV');
		deb_.style.backgroundColor = '#FFFFFF';
		deb_.style.color = '#000000';
		deb_.id = 'debug';
		document.body.appendChild(deb_);
	}
}*/

function imgPopup(){

	this.isIE = (navigator.appName.indexOf('Microsoft Internet Explorer') != -1)? true : false;
	
	this.init = function(){
		this.cover = document.createElement('DIV');
		var c_ = this.cover;
		c_.style.backgroundColor = '#000000';
		c_.style.opacity = '0.5';
		c_.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=50)';
		c_.style.position = 'absolute';
		c_.style.left = '0px';
		c_.style.top = '0px';
		c_.style.zIndex = '97';
		this.container = document.createElement('DIV');
		var co_ = this.container;
		co_.style.backgroundColor = '#FFFFFF';
		co_.style.position = 'absolute';
		co_.style.left = '0px';
		co_.style.top = '0px';
		co_.style.zIndex = '98';
		var thisObj = this;
		co_.onclick = function(){ thisObj.hide(); }
		c_.onclick = function(){ thisObj.hide(); }
		co_.bindEvent = this.bindEvent;
		co_.listen = this.listen;
		this.ajaxURL = null;
		this.loader = document.createElement('IMG');
		var l_ = this.loader;
		l_.src = 'images/loading.gif';
		l_.style.position = 'absolute';
		l_.style.zIndex = '99';
		var thisObj = this;
		this.container.onContentChange = function(){
			thisObj.container.style.left = ((thisObj.viewport.windowX-thisObj.container.offsetWidth)/2)+'px';
			thisObj.container.style.top = ((thisObj.viewport.windowY-thisObj.container.offsetHeight)/2)+'px';
		}
		this.container.onContentChanged = function(){
			thisObj.container.style.visibility = 'visible';		
			thisObj.loader.style.visibility = 'hidden';		
		}
	}
	
	this.bindEvent = function(evtName,prop,nonStop,steps,lastEvtName){
		if(typeof nonStop != 'boolean')
			nonStop = false;
		if(typeof steps != 'number')
			steps = false;
		if(typeof lastEvtName != 'string')
			lastEvtName = false;
		this.listen(prop,this['on'+evtName.substr(0,1).toUpperCase()+evtName.substr(1,evtName.length-1)],nonStop,steps,	
					lastEvtName!=false?this['on'+lastEvtName.substr(0,1).toUpperCase()+lastEvtName.substr(1,lastEvtName.length-1)]:false);
	}
	
	this.listen = function(prop,func,nonStop,steps,lastFunc){
		var thisObj = this;
		this_prop = 'this';
		if(typeof prop == 'object')
			for(i in prop)	
				this_prop += "[\'"+prop[i]+"\']";
			else
				this_prop += "[\'"+prop+"\']";
		if(typeof this.propOld != 'undefined'){
			if(eval(this_prop)!=this.propOld){
				func();
				if(nonStop){
					this.propOld = eval(this_prop);
					setTimeout(function(){thisObj.listen(prop,func,nonStop)},0);
					return;
				}
				if(steps!=false && steps!=0){
					this.propOld = eval(this_prop);
					setTimeout(function(){thisObj.listen(prop,func,nonStop,--steps,lastFunc)},0);
					return;
				}
				if(typeof lastFunc=='function' && steps==0){
					lastFunc();
					return;
				}
			}else
				setTimeout(function(){thisObj.listen(prop,func,nonStop,steps,lastFunc)},0);
		}else{
			this.propOld = eval(this_prop);
			setTimeout(function(){thisObj.listen(prop,func,nonStop,steps,lastFunc)},0);
		}
		
	}
	
	
	this.request = function(img,add){ 
		this.container.innerHTML = '';
		var req_vars = '';
		if(typeof add == 'object')
			for(i in add)
				req_vars += '&'+add[i][0]+'='+add[i][1];
		if(this.ajaxURL!=null){
			//include ajax.js for this
			this.viewport=new Viewport();
			ajax.url=this.ajaxURL;
			ajax.post_vars='img='+img+'&windowx='+this.viewport.windowX+'&windowy='+this.viewport.windowY+req_vars;
			ajax.eval_string=null;
			ajax.node=this.container;
			ajax.makeRequest();
			this.container.bindEvent('contentChange','offsetHeight',false,2,'contentChanged');
			this.container.bindEvent('contentChange','offsetWidth',false,2,'contentChanged');
		}else{
			this.currentImg = document.createElement('IMG');
			this.currentImg.src = img;
			this.container.appendChild(this.currentImg);
			var thisObj = this;
			this.currentImg.onload = function(){
				thisObj.container.style.left = ((thisObj.viewport.windowX-thisObj.container.offsetWidth)/2)+'px';
				thisObj.container.style.top = ((thisObj.viewport.windowY-thisObj.container.offsetHeight)/2)+'px';
			}
		}
		this.show();
	}
	
	this.show = function(){ 
	
		this.viewport=new Viewport();
		this.cover.style.display = 'none';
		this.loader.style.visibility = 'hidden';
		this.container.style.visibility = 'hidden';
		document.body.appendChild(this.cover);
		document.body.appendChild(this.loader);
		document.body.appendChild(this.container);
	
		this.cover.style.width = (this.viewport.pageX>this.viewport.windowX?this.viewport.pageX:this.viewport.windowX)+'px';
		this.cover.style.height = (this.viewport.pageY>this.viewport.windowY?this.viewport.pageY:this.viewport.windowY)+'px';
		this.loader.style.left = ((this.viewport.windowX-this.loader.offsetWidth)/2)+'px';
		this.loader.style.top = ((this.viewport.windowY-this.loader.offsetHeight)/2)+'px';

		this.cover.style.display = '';
		this.loader.style.visibility = 'visible';
		
	}
	
	this.hide = function(){
		if(this.container) { this.container.innerHTML = ''; document.body.removeChild(this.container); }
		if(this.loader) document.body.removeChild(this.loader);
		if(this.cover) document.body.removeChild(this.cover);
	}
		
	this.init();
}

