/*
JavaScript Document
Created: March 31, 2009
Author: M Haris Munir
Author Email: haris.multi@gmail.com

Example:
	var ajs = new hr_ajax();
	ajs.url = "ajax.php";
	ajs.loading('contentArea'); // element id where loading show, etc: <div id='contentArea'></div>
	ajs.load(function (retData){document.getElementById('contentArea').innerHTML});
*/
function hr_ajax(){
	this.hr_http = null;
	this.HttpObject = function (){
		var HttpRequest=null;	
		try
		 {
		 HttpRequest=new XMLHttpRequest();
		 }
		catch (e)
		 {
		 try
		  {
		  HttpRequest=new ActiveXObject("Msxml2.XMLHTTP");
		  }
		 catch (e)
		  {
		  HttpRequest=new ActiveXObject("Microsoft.XMLHTTP");
		  }
		 }
		return HttpRequest;

	}
	this.url = '';
	this.ret = function(retdata){}
	this.loadingid = null;
	this.loadinginner = null;
	this.method = 'GET';
	this.load = function (postback){
		this.hr_http = this.HttpObject();
		this.ret = postback;
		var thishttp = this;//Closure
		this.hr_http.onreadystatechange=function(){
				var hr_http = thishttp.hr_http;
				var result = "";
				if(hr_http.readyState==4){
					thishttp.removeLoading();
					if(thishttp.ret) 
						thishttp.ret(hr_http.responseText);
				}
			};
		this.hr_http.open(this.method,this.url,true);
		this.hr_http.send(null);
	}
	this.loading = function(loadid){
		this.loadingid = loadid;
		var loadobj = document.getElementById(this.loadingid);
		loadobj.innerHTML = '';
		var loadingElement = document.createElement('div');
		loadobj.appendChild(loadingElement);
		loadingElement.id = 'hr_ajax_loading_' + Math.ceil(Math.random()*1999);
		this.loadinginner = loadingElement.id;
		loadingElement.style.display = 'inline';
		loadingElement.style.backgroundColor = '#FF9';
		loadingElement.style.color = '#000';
		loadingElement.innerHTML = 'loading...';
	}
	this.removeLoading = function (){
		if(this.loadingid == null ) return;
		var loadobj = document.getElementById(this.loadingid);
		try{loadobj.removeChild(document.getElementById(this.loadinginner));}catch(e){}
	}
	this.get = function (){
		alert(this.hr_http.readyState);
	}
	
	this.form = function (f){
		return {
			varstr: function (){
				var ret = '';
				if(!hr.trim){
					alert('required hr_default.js');
					return false;
				}
				if(f.length > 0){
					for(var i=0; i < f.length; i++){
						ret += f[i].name + '='+ f[i].value + '&';
					}
					ret = hr.trim(ret,'&');
				}
				return ret;
			}
		}
	}
}
var ajs_gallery = new hr_ajax();
var hrpagi = {
	preLink: false,
	presort: false,
	page:false,
	glink: function (url){
		this.loading();
		var goingUrl = url.replace(/&hrj=1/gi,'') + "&hrj=1";
		ajs_gallery.url = goingUrl;
		this.preLink = goingUrl;
		ajs_gallery.load(function (retData){
			hr.$('hrdg_gridin').innerHTML = retData
			hrpagi.loading(1);
		});
		return false;
	},
	loading: function (r){
		var mid = hr.$('hrdg_grid');
		var lid = hr.$('hrvcload');
		if(r){
			lid.style.display = 'none';
			lid.style.height = '1px';
			return false;
		}
		var midpoint = Math.ceil((mid.offsetHeight/2))-25;
		hr.$('hrvcloadimg').style.marginTop = midpoint + "px";
		lid.style.height = mid.offsetHeight + "px";
		lid.style.display = 'block';
	}
}
