/** A AJAXAdaptor class  */
function AJAXAdaptor(name) {	
	this.className = 'AJAXAdaptor';
	//alert(this.className + ' ' + name);
	
	/** Default construtor
	 *
	 * name - div name
	 */
	{	
		this.name = name;
	}

	this.xmlhttpPost = function (strURL, functionObj) {
	    var xmlHttpReq = false;
	    var self = this;
	    // Mozilla/Safari
		if (window.XMLHttpRequest) {
			self.xmlHttpReq = new XMLHttpRequest();
			if (self.xmlHttpReq.overrideMimeType) {
				self.xmlHttpReq.overrideMimeType('text/xml');
				// See note below about this line
			}
		// IE
		} else if (window.ActiveXObject) { // IE
			try {
				self.xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
			}
		}
		if (!self.xmlHttpReq) {
			alert('ERROR AJAX:( Cannot create an XMLHTTP instance');
			return false;
		}	
		self.xmlHttpReq.open('GET', strURL, true);
	    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	    self.xmlHttpReq.onreadystatechange = function() { _alertContents(self.xmlHttpReq, functionObj); };
	    self.xmlHttpReq.send("");
	}
	
	_alertContents = function (http_request, functionObj) {
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				alert(http_request.responseText);
				functionObj.callBackFunction(http_request.responseText);
			} else {
				functionObj.callBackFunction(http_request.responseText);
				//alert('ERROR: AJAX request status = ' + http_request.status);
			}
		}
	}
}

function AFrame(name, url) {
	
	this.className = 'AFrame';
	//alert(this.className + ' ' + name);
	
	/** Default construtor
	 *
	 * name - div name
	 */
	{	
		this.name = name;
		this.url = url;
	    this.genericDiv = new GenericDiv(this.name);
	    this.frameAContent = new ATemplate('frameAContent' + name , 'templates/aframe.html');
	}

	this.init = function() {
		this.genericDiv.setDivText(this.url);
		this.ajaxAdaptor = new AJAXAdaptor(this.name);
		this.frameAContent.init();
		this.frameAContent.refresh();
	}
		
	this.refresh = function() {
		this.ajaxAdaptor.xmlhttpPost(this.url, this);	
	}
	
	this.draw = function(str) {
		var acontent = this.frameAContent.getContent();
		var newcontent = acontent.replace("%content", str);
		this.genericDiv.setDivText(newcontent);
	}	
	
	this.callBackFunction = function(str) {
		var newcontent1 = str.replace("(.*)<body>", "");
		var newcontent2 = newcontent1.replace("</body>(.*)", "");	
		alert(newcontent2);
		this.draw(newcontent2);
	}
		
	
	this.isColapsed = function () {
		//return this.colapsed;
	}	
}

/** A ATemplate class */
function AlertTemplate() {	
	this.className = 'AlertTemplate';
	
	/** Call Back Function - called by AJAXAdaptor
	 *
	 * str - string from XMLHttpRequest
	 */	
	this.callBackFunction = function(str) {
		alert(str)
	}	
}

/** A ATemplate class is a html template adapter and conteiner */
function ATemplate(name, url) {
	
	this.className = 'AFrame';
	//alert(this.className + ' ' + name);
	
	/** Default construtor
	 *
	 * name - div name
	 */
	{	
		this.name = name;
		this.url = url;
		this.content = '';
	}
	
	this.init = function() {
		this.content = '';
		this.ajaxAdaptor = new AJAXAdaptor(this.name);
	}

	this.refresh = function() {
		this.ajaxAdaptor.xmlhttpPost(this.url, this);	
	}
		
	this.callBackFunction = function(str) {
		var newcontent1 = str.replace("(.*)<body>", "");
		var newcontent2 = newcontent1.replace("</body>(.*)", "");
		//alert(newcontent2);
		this.content = newcontent2;
	}	
	
	this.getContent = function() {
		return this.content;	
	}
}

/** A GenericDiv class is a div conteiner */
function GenericDiv(name) {
	//alert('GenericDiv' + name);
	
	this.className = 'GenericDiv';
	
	/** Default construtor
	 *
	 * name - div name
	 */
	{	
		this.name = name;
		
		if (NS4) {
			this.obj = Zorya.divFactory().getDivForN4(document,name);
			this.style = this.obj;
			this.width = parseInt(this.obj.width);
			this.x = parseInt(this.style.left);
			this.y = parseInt(this.style.top);
		} else if (IE4) {
			this.obj = document.getElementById(name);
			this.style = document.getElementById(name).style;	
			this.width = parseInt(this.style.pixelWidth);
			this.x = parseInt(this.style.pixelLeft);
			this.y = parseInt(this.style.pixelTop);	
		} else if (NS6) {
			this.obj = document.getElementById(name);
			this.style = document.getElementById(name).style;	
			this.x = parseInt(this.style.left);
			this.y = parseInt(this.style.top);
		} else {
			alert('Browser not supported');
		} 
		/*else if (document.all)
			this.obj = document.all[name];
			this.style = document.all[name].style;	
			this.x = parseInt(this.style.left);
			this.y = parseInt(this.style.top);
		  }*/
	}
		
	/** Template like function declaration where browser type 
	 * is like class type
	 */
	if (NS4) {
		/** The method returns y field */
		this.getX = function(){
			return parseInt(this.style.left);
		}	
		/** The method returns y field */
		this.getY = function(){
			return parseInt(this.style.top);
		}

		/** The method returns width field */
		this.getWidth = function(){
			return parseInt(this.obj.offsetWidth);
		}

		/** The method returns height field */
		this.getHeight = function(){
			return parseInt(this.obj.offsetHeight);
		}		
	} else if (IE4) {
		/** The method returns y field */
		this.getX = function(){
			return parseInt(this.style.pixelLeft);
		}	
		/** The method returns y field */
		this.getY = function(){
			return parseInt(this.style.pixelTop);
		}

		/** The method returns width field */
		this.getWidth = function(){
			return parseInt(this.obj.offsetWidth);
		}

		/** The method returns height field */
		this.getHeight = function(){
			return parseInt(this.obj.offsetHeight);
		}		
	} else if (NS6) {
		//alert('declare getX');
		/** The method returns y field */
		this.getX = function(){
			return parseInt(this.style.left);
		}	
		/** The method returns y field */
		this.getY = function(){
			return parseInt(this.style.top);
		}

		/** The method returns y field */
		this.getY = function(){
			return parseInt(this.style.top);
		}

		/** The method returns width field */
		this.getWidth = function(){
			return parseInt(this.obj.offsetWidth);
		}

		/** The method returns height field */
		this.getHeight = function(){
			return parseInt(this.obj.offsetHeight);
		}
		
		/** The method sets the text in the div */
		this.setDivText = function(text){
		    var rng = document.createRange();
		    rng.setStartBefore(this.obj);
		    var htmlFrag = rng.createContextualFragment(text);
		
		    while (this.obj.hasChildNodes()) {
		      this.obj.removeChild(this.obj.lastChild);
		    }
		    this.obj.appendChild(htmlFrag);
    	}
	} 	
	
	/** The method returns y field */
	this.getCenterX = function(){
		return this.getX() + this.getWidth()/2;
	}
	
	/** The method returns y field */
	this.getCenterY = function(){
		return this.getY() + this.getHeight()/2;
	}
			
	this.paint = function(content){

	}
	
	this.getStyle = function(content){
		return this.style;
	}
	/** 
	 * The method append to the div content
	 * TODO if the method is coled many times refactor
	 * to template like implementation
	 */
	this.appendContent = function(content) {
		Zorya.divFactory().appendToDiv(content, this.name);		
	}
	
	//alert('[' + this.name + ']');
}