/**
	Update: 8/02/2009
	//backup -> use js/func.js
*/
/* PAGELOCATOR */
		function PageLocator(id, defaultPage) {
			this.id = id;
			this.iframeid = "locator_frame_" + id;
			this.defaultQS = defaultPage;
			this.page_data = Array();
			
			this.onAfterPageLoad = function () { };
			this.onBeforePageLoad = function () { };
			//Startup("locator_" + id + ".Start()");		// Start page locator - name defined in datagarid.tpl.htm
		}
		PageLocator.prototype.getLocation = function() {			
			return eval(this.propertyToUse);
		}
		
		PageLocator.prototype.getHash = function() {
			var url = this.getLocation();
			
			if(url.indexOf(this.dividingCharacter)>-1) {
				var url_elements = url.split(this.dividingCharacter);
				return url_elements[url_elements.length-1];
			} else {
				return this.defaultQS;
			}
		}
		PageLocator.prototype.getHref = function() {
			var url = this.getLocation();
			var url_elements = url.split(this.dividingCharacter)
			return url_elements[0];
		}
		PageLocator.prototype.makeNewLocation = function(new_qs) {
			return this.getHref() + this.dividingCharacter + new_qs;
		}
		
		PageLocator.prototype.changePage = function (page) {
		
			if (this.iframesfix) {
				//document.getElementById(this.iframeid).contentWindow.location.href = "actions/set_page.php?page=" + this.locator.getHash();								
				document.getElementById(this.iframeid).setAttribute('src', ROOT_URL + "/modules.php?page=" + page);
				
			}else window.location = "#page=" + page;
						
		}
		
		PageLocator.prototype.pageChanged = function (page) {
			
			this.onBeforePageLoad();
			
			if (typeof this.page_data[page] != 'undefined' && this.page_data[page].length) {
				document.getElementById(this.id + "_grid").innerHTML = this.page_data[page];
				this.onAfterPageLoad();
			}else {
				$ele(this.id + "_grid").style.opacity = 9.5;
				$ele(this.id + "_grid").style.filter  = "alpha(opacity=50)";

				var self = this;
				var aj = new AJAX("classes/aj_grid.class.php?aj_grid_ajax=1&aj_grid_id=" + this.id + "&" + this.id + "_op=view_page&" + this.id + "_page=" + page);
				aj.element = this.id + "_grid";								
				aj.execute = "optional";
				aj.inform = true;
				aj.onFinish = function() {
									self.page_data[page] = aj.response;
									self.onAfterPageLoad();
									
									$ele(this.id + "_grid").style.opacity = 1;
									$ele(this.id + "_grid").style.filter  = "alpha(opacity=100)";

								};
				aj.run();
			}
		}

		PageLocator.prototype.checkWhetherChanged = function(location){
		
			if(this.getHash() != location) {
			//	alert(this.getHash());	
			//	alert(location);
				Move_to_page(this.id, this.getHash());	
			//	eval("var curr = " + this.id + "_curr_page;");
			//	alert(curr);
				this.pageChanged(this.getHash());	
								
			}
			
			setTimeout("locator_" + this.id + ".checkWhetherChanged('" + this.getHash() + "');", 200);
		}		
		
		PageLocator.prototype.Start = function() {
			
			var detect = navigator.userAgent.toLowerCase();
			if(detect.indexOf("msie")>-1) this.iframesfix = true;
				
			if (this.iframesfix) {
				if (!$ele(this.iframeid)) {
					var ele = document.createElement("IFRAME");
					ele.src = ROOT_URL + "modules.php?page=" + this.defaultQS ;
					ele.style.display = 'none';
					ele.width = "200px";
					ele.height = "500px";
					ele.id = this.iframeid;
					document.body.appendChild(ele);
					//document.write("<iframe id=\"" + this.iframeid + "\" name=\"" + this.iframeid + "\" src=\"" + ROOT_URL + "modules.php?page=" + this.defaultQS + "\" width=\"200\" height=\"50\" style=\"display:none\" frameborder=1></iframe>");
				}
				
				this.propertyToUse = "document.getElementById('" + this.iframeid + "').contentWindow.location.href";				
				this.dividingCharacter = "?page=";				
			}else {
				this.propertyToUse = "window.location.href";				
				this.dividingCharacter = "#page=";				
			}			
			
			this.checkWhetherChanged(this.defaultQS);						
		}		
		
		
/* END PAGELOCATOR */

function Next_page(id) {
	eval("var curr_page = " + id + "_curr_page + 1;");
	return Move_to_page(id, curr_page);
}

function Prev_page(id) {
	eval("var curr_page = " + id + "_curr_page - 1;");
	return Move_to_page(id, curr_page);
}

function First_page(id) {	
	return Move_to_page(id, 1);
}

function Last_page(id) {
	eval("var total_page = " + id + "_total_page;");
	return Move_to_page(id, total_page);
}	

function Move_to_page(id, page_num) {
	eval("var curr_page = " + id + "_curr_page" + ";");
	
	if (page_num == curr_page) 			// k0 chuyen trang nao
		return false;
		
	eval("var useAjax = " + id + "_useAjax;");
	eval("var total_page = " + id + "_total_page;");
	eval(id + "_curr_page = " + page_num + ";");
	
	//eval("alert(" + id + "_curr_page); ");
	
	if (page_num < 1) page_num = 1;
	if (page_num > total_page) page_num = total_page;
	
		
	if (useAjax)	{
			
		if (page_num >= total_page) {	
			document.getElementById(id + '_next_ctrl_page1').style.display = 'none';
			document.getElementById(id + '_next_ctrl_page2').style.display = 'none';
		}else {	
			document.getElementById(id + '_next_ctrl_page1').style.display = 'block';		
			document.getElementById(id + '_next_ctrl_page2').style.display = 'block';			
		}
		
		if (page_num <= 1) {
			document.getElementById(id + '_prev_ctrl_page1').style.display = 'none';
			document.getElementById(id + '_prev_ctrl_page2').style.display = 'none';
		}else {
			document.getElementById(id + '_prev_ctrl_page1').style.display = 'block';
			document.getElementById(id + '_prev_ctrl_page2').style.display = 'block';
		}
		
		eval("locator_" + id + ".changePage(page_num);");
		
		return true;
		
	}else {		
		eval("window.location = " + id + "_url + '" + "&" + id + "_page=" + page_num + "';");		
		return false;
	}	
}

function Show_sort(id, e) {

	menuobj = document.getElementById(id + "_sort");
	menuobj.style.display = 'block';
		
	menuobj.contentwidth=menuobj.offsetWidth;
	menuobj.contentheight=menuobj.offsetHeight;
	
	eventX=(window.event)? event.clientX : e.clientX;
	eventY=(window.event)? event.clientY : e.clientY;
	
	//Find out how close the mouse is to the corner of the window
	var rightedge=ie5? document.body.clientWidth-eventX : window.innerWidth-eventX;
	var bottomedge=ie5? document.body.clientHeight-eventY : window.innerHeight-eventY;
	//if the horizontal distance isn't enough to accomodate the width of the context menu
	if (rightedge<menuobj.contentwidth)
	//move the horizontal position of the menu to the left by it's width
		menuobj.style.left=(window.event)? document.body.scrollLeft+eventX-menuobj.contentwidth+"px" : window.pageXOffset+eventX-menuobj.contentwidth+"px";
	else
	//position the horizontal position of the menu where the mouse was clicked
		menuobj.style.left=(window.event)? document.body.scrollLeft+eventX+"px" : window.pageXOffset+eventX+"px";
	//same concept with the vertical position
	if (bottomedge<menuobj.contentheight)
		menuobj.style.top=(window.event)? document.body.scrollTop+eventY-menuobj.contentheight+"px" : window.pageYOffset+eventY-menuobj.contentheight+"px";
	else
		menuobj.style.top=(window.event)? document.body.scrollTop+event.clientY+"px" : window.pageYOffset+eventY+"px";
	
	menuobj.style.visibility="visible";
	
	Clear_hide_sort(id)
}

function Hide_sort(id) {	
	eval(id + "_delayhide = setTimeout(\"document.getElementById('" +  id + "_sort').style.display = \'none\';\", 500);");
}

function Clear_hide_sort(id) {
	eval("var delayhide = " + id + "_delayhide;");
	
	clearTimeout(delayhide);
}
