// Used with ajax in shopping type to browse product
	var changePage = false;
	var wintitle = document.title;
	var shoppingObj ;

/* PAGELOCATOR */
		function PageLocator(propertyToUse, dividingCharacter, defaultPage) {
			this.propertyToUse = propertyToUse;
			this.defaultQS = defaultPage;
			this.dividingCharacter = dividingCharacter;
		}
		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;
		}
/* END PAGELOCATOR */
					
function ShoppingNavigator(total_page, curr_page, page_type, element) {
	this.total_page = total_page;
	
	if (!curr_page) curr_page = 1;
	this.curr_page = curr_page;
	
	if (!page_type) page_type = 1;
	this.page_type = page_type;
	
	this.loading_page = curr_page;
	this.pageObjs = new Array();
	
	this.element = element;
	this.elementObj = null;

	this.iframeid = "frame_url";
	
	this.initData = function () {			
						var i;
						for(i = 1;i <= this.total_page; i++) {
							this.pageObjs[i] = new pageObj(i, curr_type);	
						}	
						
						this.ajaxObj = ajaxCreate();
						
						this.up_page = 1;
						this.down_page = this.total_page;
						this.elementObj = document.getElementById(this.element);
																											
						this.onAfterLoad = function () { };
						this.onBeforeLoad = function () { };
					};
					
	this.loadData = function(ajaxObj)	{						
						shoppingObj.pageObjs[shoppingObj.loading_page].result = ajaxObj.responseText;
						shoppingObj.pageObjs[shoppingObj.loading_page].status = 'loaded';
						
						if (shoppingObj.curr_page == shoppingObj.loading_page) {
							shoppingObj.elementObj.innerHTML = ajaxObj.responseText;
							
							shoppingObj.onAfterLoad();
						}	
					}
					
	this.loadPage = function (page) {
						try {
							this.loading_page = page;						
							
												
							if (this.pageObjs[this.loading_page].status == 'loaded') {
								
								if (this.pageObjs[this.loading_page].result=="") { // reLoad if without result - maybe error
									this.pageObjs[this.loading_page].status = 'none';
									this.pageObjs[this.loading_page].ajaxObj.abort();
									
									this.loadPage(page);
									
								}else {							
									if (this.loading_page == this.curr_page) {							
										//alert(this.pageObjs[this.loading_page].result );
										//shoppingObj.elementObj.innerHTML = this.pageObjs[this.loading_page].result;
										this.elementObj.innerHTML = this.pageObjs[this.loading_page].result;
										this.onAfterLoad();
									}
									
									//this.loadUp();
									//this.loadDown();
								}
								
							}else if (this.pageObjs[this.loading_page].status == 'none') {
								this.pageObjs[this.loading_page].status = 'loading';				
								//alert(this.loading_page);		
								
								path = "shopping.php?op=sub&page=" + this.loading_page + "&type=" + this.page_type;
								var ajaxObj = ajaxCreate();
								
								//path = "modules.php?module=shopping&file=shopping&page=" + this.loading_page + "&type=" + this.page_type;
								
								ajaxObj.onreadystatechange = function() { 
									if (ajaxObj.readyState == 4) {	// success
										shoppingObj.loadData(ajaxObj); 
			
										//shoppingObj.loadUp();
										//shoppingObj.loadDown();
										
									}else {
										//alert("Opps, Can not connect to the server! Please refresh your browser!");
									}
								}
								
								ajaxObj.open('GET', path, true) //get page asynchronously 
								ajaxObj.send(null)						
								
								this.pageObjs[this.loading_page].ajaxObj = ajaxObj;
							}
						}catch(e) {
							alert(page);
							this.onAfterLoad();
							alert("Opps, Can not connect to the server! Please refresh your browser!");
						}
						
					};
					
	this.loadUp = function() {		
						//alert("load up:");
						//if no page is running				
						if (this.pageObjs[this.loading_page].status != 'loading' ) {
							up_page = this.loading_page + 1;
						
							while(up_page <= this.total_page && this.pageObjs[up_page].status == 'loaded') {
								up_page++;
							}
							
							if ( up_page <= this.total_page && this.pageObjs[up_page].status == 'none') {
								//alert("load up:"  + up_page);
								this.loadPage(up_page);										
							}
						}
					};
					
	this.loadDown = function() {
						//alert("load down:");
						//if no page is running
						if (this.pageObjs[this.loading_page].status != 'loading' ) {
							
							down_page = this.loading_page - 1;
							
							while(down_page >= 1 && this.pageObjs[down_page].status == 'loaded') {
								down_page--;
							}
							
							if ( down_page >= 1 && this.pageObjs[down_page].status == 'none') {
							//	alert("load down:"  + down_page);
								this.loadPage(down_page);										
							}
						}							
					};
	
	this.changePage = function (page) {
		
						if (document.iframesfix) {
							//document.getElementById(this.iframeid).contentWindow.location.href = "actions/set_page.php?page=" + this.locator.getHash();								
							document.getElementById(this.iframeid).setAttribute('src', ROOT_URL + "actions/set_page.php?page=" + page);
						}else window.location = "#page=" + page;
							
					};
					
	this.pageChanged = function (page) {
						if (isFinite(page) && page <= this.total_page) {
							this.onBeforeLoad();
							
							// if change to another page and loading_page is running
							for(i = 1;i <= this.total_page; i++) {
								if (i != page && this.pageObjs[i].status == 'loading') {
									
									this.pageObjs[i].ajaxObj.abort();
									this.pageObjs[i].status = 'none';
								}
							}
									
							this.curr_page = page;
													
							this.loadPage(page);
						}
					};
	
	this.checkWhetherChanged = function(location){
		
			if(this.locator.getHash() != location) {
				this.pageChanged(this.locator.getHash());
				
				if (document.iframesfix) {					
					 window.location = "#page=" +  this.locator.getHash();					
				}else ajaxRead("actions/set_page.php?page=" + this.locator.getHash());					
			}
			
			setTimeout("eval('shoppingObj.checkWhetherChanged(" + this.locator.getHash() + ")')", 200);
		}
		
	this.Start = function() { 
		var detect = navigator.userAgent.toLowerCase();
		if(detect.indexOf("msie")>-1) document.iframesfix = true;

		
		if (document.iframesfix) {
			document.write("<iframe id=\"" + this.iframeid + "\" name=\"" + this.iframeid + "\" src=\"" + ROOT_URL + "actions/set_page.php?page=" + this.curr_page + "\" width=\"200\" height=\"50\" style=\"display:none\" frameborder=1></iframe>");
			
			this.locator = new PageLocator("document.getElementById('" + this.iframeid + "').contentWindow.location.href", "?page=", this.curr_page);
		}else {
			this.locator = new PageLocator("window.location.href", "#page=", this.curr_page);			
		}		
		
		document.write("<div id=\"status\" style=\"position: absolute; top:200; left:" + (screen.width - 150) + "; display:none;width:150; height: 20; text-align:center; vertical-align:middle; background-color:#BD2421; color: #FFFFFF;\" >Loading...</div>");
	
		if (this.total_page) {
			this.pageObjs[this.curr_page].result = this.elementObj.innerHTML;
			this.pageObjs[this.curr_page].status = 'loaded';
		}
		
		this.checkWhetherChanged(this.curr_page);
		
		//this.loadUp();
		
	};				
	this.initData();
}
	
function pageObj(page_num, page_type) {	
	this.page = page_num;							
	this.status = "none";
	this.result = '';		
	
	this.ajaxObj = ajaxCreate();
}	


// Inform message
function Update_status(status) {
	
	if (!status || !status.length) {
		document.getElementById("status").style.display = 'none';			
	}
	else {					
		document.getElementById("status").style.display = 'block';			
	}
}	

function Pre_page() {	
	page = parseInt(curr) - 1;
	if (page >=1) {		
		//alert(page);
		Change_page(page, curr_type);				
	}else {
		curr = 1;
		Update_page_navigator(curr);
	}
	
}

function Next_page() {	
	page = parseInt(curr) + 1;	
	if (page <= total_page) {	
		//alert(page);			
		Change_page(page, curr_type);				
	}else {
		curr = total_page;
		Update_page_navigator(curr);
	}
}


function Change_page(page, page_type, is_new) {
	//alert(page);
	//winlocator = new PageLocator("window.location.href","#page=", curr);
	//window.location = "#page=" + page; //winlocator.makeNewLocation(page);
	shoppingObj.changePage(page);
}


// Update page number in select box
function Update_page_navigator(page) {
	if (document.getElementById('page1'))
		document.getElementById('page1').selectedIndex = page - 1;
	if (document.getElementById('page2'))
		document.getElementById('page2').selectedIndex = page - 1;	
}

//if Change page from select box
function Select_change(pointer) {	
	
	page = pointer.options[pointer.selectedIndex].value;
	
	Change_page(page, curr_type);
	
	window.focus();
}

function positionit(){
	var ie=document.all
	var ieNOTopera=document.all&&navigator.userAgent.indexOf("Opera")==-1

	var topmsg_obj=ie? document.all.status : document.getElementById? document.getElementById("status") : document.status;
	
	var dsocleft=ie? document.body.scrollLeft : pageXOffset;
	var dsoctop=ie? document.body.scrollTop : pageYOffset;
	var window_width=ieNOTopera? document.body.clientWidth : window.innerWidth-20;
	var window_height=ieNOTopera? document.body.clientHeight : window.innerHeight;
	
	if (ie||document.getElementById){
		topmsg_obj.style.left=parseInt(dsocleft)+window_width/2-topmsg_obj.offsetWidth/2;
		topmsg_obj.style.top=parseInt(dsoctop)+parseInt(window_height)/2 -topmsg_obj.offsetHeight-4;
	}
	else if (document.layers){
		topmsg_obj.left=dsocleft+window_width/2-topmsg_obj.document.width/2;
		topmsg_obj.top=dsoctop+window_height/2-topmsg_obj.document.height-5;
	}
}

	try {	
		shoppingObj = new ShoppingNavigator(total_page, curr, curr_type, 'result');
		
		shoppingObj.onAfterLoad = function () {
									//	setTimeout("changePage = false;",1000);	// setTimeout to wait frame_url apply new address
										
										
										Update_page_navigator(shoppingObj.curr_page);
										
										curr = shoppingObj.curr_page;
																				
										document.getElementById('status').focus();	
										Update_status();
										
										document.title = wintitle;		
										
										clearInterval(nTime);
									};
									
		shoppingObj.onBeforeLoad = function () {
										nTime = setInterval("positionit()",100);
										wintitle = document.title ;
										Update_status('Loading...');
							
									};
									
		//shoppingObj.pageObjs[curr].result = shoppingObj.elementObj.innerHTML;
		//shoppingObj.pageObjs[curr].status = 'loaded';
		
		shoppingObj.Start();
		
		
		
	}catch(e) {
		alert(e.message);
		//alert("Opps, Can not connect to the server! Please refresh your browser!");
	}
		
 