﻿function showPage(){
	var XMLHTTP;
	
	this.pageCount = 0;					//当前信息列表页面总数
	this.currentPage = 0;				//当前面页页码
	this.pageContent;				//AJAX返回内容
	this.time;						//setTimeOut对像
	this.limit = 5000;				//信息刷新时间间隔
	this.nextPage;					//下一页页码
	this.Previous;					//上一页页码
	this.status = document.getElementById("status");
	this.showContent = document.getElementById("currentPageContent");
	this.previousPage = document.getElementById("previousPage");
	this.playOff = true;
	this.playPic = true;
	this.histroy = new Array();			//历史记录信息储存Array
	this.histroyLength = 5;				//保存历史记录条数
	this.keyWord = "";					//关键词
	this.cartList = new Array();
	this.companyList = new Array();		//公司列表
	
	this.cPlay = true;					//公司列表信息循环
	this.cTime;							//公司列表信息循环对象
	this.cPage = 1;
	
	this.createXMLHttpRequest = function(){
		if(window.ActiveXObject){
			XMLHTTP = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else if(window.XMLHttpRequest) {
			XMLHTTP = new XMLHttpRequest();
		}
	}
	
	this.CallXMLHTTP = function(URL,Str){		
		this.status(true);
		this.createXMLHttpRequest();
		XMLHTTP.open("post",URL,true);
	
		XMLHTTP.setRequestHeader('Content-type','application/x-www-form-urlencoded');
		XMLHTTP.onreadystatechange = this.XMLHTTPRequest;
		XMLHTTP.send(Str);
	
	}
	
	this.XMLHTTPRequest = function(){
		
		if(XMLHTTP.readyState == 1) {
			//正在连接
		}else if(XMLHTTP.readyState == 2){
			//正在连接
		}else if(XMLHTTP.readyState == 4){
			//alert(XMLHTTP.status);
			if(XMLHTTP.status == 200){
				
				//alert(XMLHTTP.responseText);
				clearTimeout(this.time);
				//执行获取数据前,清空setTimeout,防止多线程请求,导致数据混乱
				
				
				
				var backContent = XMLHTTP.responseText;
				
				if (backContent == "0"){
					showPage.status(false);
					alert("The data you want was not found! ");
					return;
				} else if (backContent == "1"){
					showPage.status(false);
					alert("The data you want was not found! ");
					return;
				} else {
					var backContentArray = eval(backContent);
					
					if (typeof(backContentArray[backContentArray.length - 1].currentPage) == "undefined"){
						
						showPage.companyList = backContentArray;
						showPage.showCompany();
					} else {
						showPage.pageContent = backContent;
						showPage.saveHistroy();
					}
					
					
					
				}
				
			}else{
				//恶意攻击本站者，本站一律追究其法律责任
				
			}
		}
	}
	
	
	this.show = function (content){
		
		//if(this.showContent.innerHTML != ""){
//			//this.previousPage.innerHTML = this.showContent.innerHTML;
//		}
		
		if (typeof(content) == "undefined" ){
			return;
		}
		
		this.showContent.innerHTML = "";
		var str = "";
		
		for (var i=0; i< content.length-1; i++){
			str = "Product Name : "+ content[i].name 
			str += "<a href=# onclick=showPage.addInCart(" + content[i].pid + ");return false; class=tis><img src=images/h_13.gif  border=0><span>Add to chart</span></a><br />"
			str += "Model : " + content[i].model
			str += "<br />FOB : " + content[i].fob
			str += "<br />Factory Price : " + content[i].price
			str += "<br />Company : " + content[i].companyName  //<a href=# class=text>纽曼制造业有限公司出品</a>
			
			if (i == 0){
				showPage.showMessage(str,content[i].pic);
			}
			
			
			this.showContent.innerHTML += "<li><a href=\"#\" class=\"tooltips\"><img onclick=\"showPage.showMessage('"+ str +"',this.src)\" src=\""+ content[i].pic +"\" width=\"100\" height=\"85\" border=\"0\" title=" + content[i].name + "></a></li>\n"
			
		}
		
		this.currentPage = content[content.length-1].currentPage;
		this.pageCount = content[content.length-1].pageCount;
		this.nextPage = parseInt(this.currentPage) + 1;
		
		this.getPage();
		this.status(false);
		this.createTimer();
		this.pageSlider();
		this.limitSlider();
	}
	
	this.showPrevious = function(content){
		
		if (typeof(content) == "undefined" ){
			return;
		}
		
		this.previousPage.innerHTML = "";
		
		for (var i=0; i< content.length-1; i++){
			str = "Product Name : "+ content[i].name 
			str += "<a href=# onclick=showPage.addInCart(" + content[i].pid + ");return false; class=tis><img src=images/h_13.gif  border=0><span>Add to chart</span></a><br />"
			str += "Model : " + content[i].model
			str += "<br />FOB : " + content[i].fob
			str += "<br />Factory Price : " + content[i].price
			str += "<br />Company : " + content[i].companyName  //<a href=# class=text>纽曼制造业有限公司出品</a>
			
			this.previousPage.innerHTML += "<li><a href=\"#\" class=\"tooltips\"><img onclick=\"showPage.showMessage('"+ str +"',this.src)\" src=\""+ content[i].pic +"\" width=\"100\" height=\"85\" border=\"0\" title=" + content[i].name + "></a></li>\n"
			
		}
		
		
		this.pageSlider();
		this.limitSlider();
	}
	
	this.goTo = function(page,nType) {
		clearTimeout(this.time);
		
		var url = "data.asp?page=" + page;
		
		if (this.keyWord != ""){
			url += "&keyword=" + escape(this.keyWord);
		}
		if (nType == 2){
			url += "&nType=" + nType;
		}
		
		this.CallXMLHTTP(url,this.limit);
	}
	

	this.getPage = function() {
		document.getElementById("currentPageId").innerHTML = this.currentPage;
		document.getElementById("pageCountId").innerHTML = this.pageCount;
	}
	
	this.createTimer = function(){		//创建定时执行页面跳转
		clearTimeout(this.time);
		if (this.playOff){
			
			if (this.limit < 1000){
				this.limit = 5000;
			}
			
			var url;
		
			if (parseInt(this.currentPage) < parseInt(this.pageCount)){
				url = "data.asp?page="+ this.nextPage;
			}else{
				url = "data.asp?page=1"
			}
			if (this.keyWord != ""){
				url += "&keyword=" + escape(this.keyWord);
			}
			this.time = setTimeout("showPage.CallXMLHTTP('" + url + "','')",this.limit);
		}
	}
	
	this.saveHistroy = function(){
		
		if(this.histroy.length >= 1){
			this.showPrevious(this.histroy[this.histroy.length-1]);
		}
		
		if(this.histroy.length == 0){
			this.histroy.push(eval(this.pageContent));
		}else{
			if (this.histroy.length >= this.histroyLength){
				this.histroy.shift();
			}
			
			this.histroy.push(eval(this.pageContent));
		}
		
		this.show(eval(this.pageContent));
		
		
		//测试数组
		/*
		document.getElementById("array").innerHTML = "";
		
		
		for (var i=0; i<this.histroyLength; i++){
			var content = this.histroy[i];
			document.getElementById("array").innerHTML += i + ":" + content[content.length-1].currentPage + ",";
		}
		*/
		
	}
	
	this.play = function (playValue){
		if (playValue == "-1"){
			this.playOff = false;
		} else {
			this.playOff = !this.playOff;
		}
		if(this.playOff){
			
			document.getElementById("play").src = "images/h_10.gif"
			document.getElementById("play").title = "stop"
			this.createTimer();
		}else{
			clearTimeout(this.time);
			document.getElementById("play").src = "images/h_5.jpg"
			document.getElementById("play").title = "play"
		}
	}
	
	this.histroyGo = function (value){
		this.play(-1);
		for (var i=0; i<this.histroyLength; i++){
			
			var content = this.histroy[i];
			if (this.currentPage == content[content.length-1].currentPage){
				
				if (value == "-1"){
					if((i-2) >= 0){
						this.showPrevious(this.histroy[i-2]);
						this.show(this.histroy[i-1]);
						document.getElementById("previous").src = "images/h_4.jpg";
					} else {
						document.getElementById("previous").src = "images/h_4_1.jpg";
					}
					document.getElementById("next").src = "images/h_6.jpg";
				} else {
					
					if((i+1) < this.histroy.length){
						this.showPrevious(this.histroy[i]);
						this.show(this.histroy[i+1]);
						document.getElementById("next").src = "images/h_6.jpg";
					} else {
						//document.getElementById("next").src = "images/h_6_1.jpg";
						this.goTo(parseInt(this.currentPage)+1,this.limit,1);
					}
					document.getElementById("previous").src = "images/h_4.jpg";
				}
				
				break;
			}
		}
	}
	
	this.showMessage = function(str,pic){
		document.getElementById("photo").innerHTML ="<img src=\""+pic.replace("_X.",".")+"\" onload=\"showPage.loadImage(475,330,this)\" border=\"0\"/>" ;
		document.getElementById("showList").innerHTML = str;
		
		this.createTimer();
	}
	
	
	this.showCompany = function (){
		clearTimeout(this.cTime);
		
		var clDIV = document.getElementById("companyList");
		if(clDIV){
			clDIV.innerHTML = ""
			
			var k = (this.cPage-1) * 6;
			var j = this.cPage * 6;
			
			if(k >= this.companyList.length){
				return;
			} else if(k != 0){
				//k -= 1;
			}
			
			if(j >= this.companyList.length){
				j = this.companyList.length;
			}
			
			
			for(var i=k; i<j; i++){
				clDIV.innerHTML += "<li class=\"footright1 lefttitle\"><a href=\"" + this.companyList[i].HTML + "\" class=\"text2\"  target=\"_blank\">"+ this.companyList[i].name +"</a></li>"
			}
		}
		showPage.status(false);
		if(this.cPlay){
			if (this.limit < 1000) {
				this.limit = 1000;
			}
			this.cTime = setTimeout("showPage.companyPlay()",this.limit);
		}
	}
	
	this.companyPlay = function (){
		
		var cPageCount = 0;
		
		if(this.companyList.length % 6 == 0){
			cPageCount = this.companyList.length/6;
		} else {
			cPageCount = this.companyList.length/6 + 1;
		}
		
		if (this.cPage < cPageCount){
			this.cPage = parseInt(this.cPage) + 1;
		} else {
			this.cPage = 1;
		}
		
		this.showCompany();
		
	}
	
	
	this.pageSlider = function (){
		var pageSlider = new neverModules.modules.slider({
			targetId: "slider1",
			sliderCss: "imageSlider1",
			barCss: "imageBar1",
			min: 1,
			max: this.pageCount,
			hints: "move the slider"
		});
		
		getElementById(pageSlider.targetId).innerHTML = "";		//主要是用作去掉剪头边色
		
		pageSlider.onstart  = function () {}
		
		pageSlider.onchange = function () {
			getElementById("currentPageId").innerHTML = pageSlider.getValue();
			this.currentPage = pageSlider.getValue();
		}
		
		pageSlider.onend = function () {
			getElementById("currentPageId").innerHTML = this.currentPage;
		}
		pageSlider.create();
		pageSlider.setValue(this.currentPage);
	}
	
	this.limitSlider = function (){
		var limitSlider = new neverModules.modules.slider({
			targetId: "slider2",
			sliderCss: "imageSlider2",
			barCss: "imageBar2",
			min: 0,
			max: 10000,
			hints: "move the slider"
		});
		
		getElementById(limitSlider.targetId).innerHTML = "";		//主要是用作去掉剪头边色
		
		limitSlider.onstart  = function () {}
		
		limitSlider.onchange = limitSlider.onend = function () {
			
			showPage.limit = limitSlider.getValue();
			getElementById("time").innerHTML = parseInt(limitSlider.getValue()/1000);
		}
		
		limitSlider.create();
		limitSlider.setValue(this.limit);
	}
	
	
	this.Search = function (keyword){
		this.keyWord = keyword;
		this.histroy = new Array();
		this.goTo(1,1);
		this.play(-1);
		this.time = setTimeout('showPage.goTo(1,2)',5000);
	}
	
	this.openCart = function(){
		
		this.play(-1);
		var coverDiv = document.getElementById("cover_div");
		if (coverDiv) {
			coverDiv.style.display = '';
		}else{
			coverDiv = document.createElement('div');
			document.body.appendChild(coverDiv);
			coverDiv.id = 'cover_div';
		}
		
		var bodySize = this.getBodySize();
		with(coverDiv.style) {
			position = 'absolute';
			background = '#CCCCCC';
			left = '0px';
			top = '0px';
			width = '100%';
			height = bodySize[1] + 'px';
			zIndex = '98';
			if (this.isIE()) {
				filter = "Alpha(Opacity=90)";
			}else{
				opacity = 0.9;
			}
		}
		
		this.cart();
	}
	
	this.isIE = function(){
		return (document.all && window.ActiveXObject && !window.opera) ? true : false;
	}
	
	this.getBodySize = function(){
		var bodySize = [];
		with(document.documentElement) {
			bodySize[0] = (scrollWidth>clientWidth)?scrollWidth:clientWidth;
			bodySize[1] = (scrollHeight>clientHeight)?scrollHeight:clientHeight;
		}
		return bodySize;
	}
	
	
	this.cart = function(){
		var loginDivWidth = 660;    //弹出窗宽度
		var sign_in_flow ;		//弹出窗口内容
		var replyid;
		var etitle,econtent;
		
		sign_in_flow =' <div class="liebiao1">'
		+ '		<div class="liebiao2">Chart</div>'
		+ '		<div class="liebiao3"><a style="cursor:pointer;" onClick="showPage.closeCart()"><img src="images/h_14.gif" width="13" height="13" border="0"></a></div>'
		+ '	</div>'
		
		+ '	<div class="liebiao4" id="cartShow">'
		
		+ ' </div>'

		
		var signDiv = document.getElementById("plane");
		
		if(signDiv){
			signDiv.style.display = '';
		}else{
			signDiv = document.createElement('div');
			document.body.appendChild(signDiv);
			signDiv.id = 'plane';
			signDiv.align = "center";
			signDiv.className = "liebiao";
		}
		
		with (signDiv.style) {
			position = 'absolute';
			left = (document.documentElement.clientWidth - loginDivWidth)/2 + 'px';
			top = document.documentElement.scrollTop+(document.documentElement.clientHeight-450)/2  + 'px';
			width = loginDivWidth + 'px';
			zIndex = 99;
			
		}
		
		document.getElementById("plane").innerHTML = sign_in_flow;
		
		this.cartShow();
	}
	
	this.closeCart = function(){
		
		if(document.getElementById("cover_div")){
			document.getElementById("cover_div").style.display = "none";
			document.getElementById("plane").style.display = "none";
		}
	}
	
	
	/*
		加入购物车
	*/
	this.addInCart = function(pId){
		var exist = false;
		var pName
		
		if(this.histroy.length > 0){
			for(var i=0; i<this.histroy.length; i++){
				var content = this.histroy[i];
				if (content.length > 0){
					for (var j=0; j<content.length; j++){
						if (content[j].pid == pId){
							
							pName = content[j].name
							HTML = content[j].HTML
							PIC = content[j].pic
							model = content[j].model
							fob = content[j].fob
							price = content[j].price
							//companyName
							//HTML,name,pic,model,fob,price
							break;
						}
					}
				}
			}
		}
		
		if (this.cartList.length > 0){
			for(var i=0; i<this.cartList.length; i++){
			
				if(this.cartList[i][0] == pId){
					exist = true;
					break;
				}
				
			}
		}
		
		
		if (!exist){
			this.cartList[this.cartList.length] =  new Array(pId,pName,HTML,PIC,model,fob,price);
		}else{
			//alert("该数据已经加入购物车");
		}
		
		this.openCart();
		
	}
	
	/*
		显示购物车列表
	*/
	
	this.cartShow = function(){
		var cartDiv = document.getElementById("cartShow")
		cartDiv.innerHTML = '';
		cartDiv.innerHTML = '<div class="liebiao5">'
		+'	<ul>'
		+'		<li class="liebiao5_1">Product Picture</li>'
		+'		<li class="liebiao5_2">Prodct Name</li>'
		+'		<li class="liebiao5_3">Factory Price</li>'
		+'		<li class="liebiao5_3">FOB</li>'
		+'		<li class="liebiao5_3">Model</li>'
		+'	</ul>'
		+'</div>';
		
		
		var Str="";
		if (cartDiv && this.cartList.length > 0){
			
			
			for (var i=0; i<this.cartList.length; i++){
				//cartDiv.innerHTML = "<li>" + this.cartList[i][1] + "&nbsp;&nbsp;<span onclick=showPage.deleteInCart("+i+")>删除</span></li>" + cartDiv.innerHTML
				//Array(pId,pName,HTML,PIC,model,fob,price);
				
				Str = '<div class="liebiao6">'
				+'	<ul>'
				+'		<li class="liebiao6_1"><a href="' + this.cartList[i][2] + '" target="_blank"><img src="' + this.cartList[i][3] + '" alt="" width="100" height="85" border="0"></a></li>'
				+'		<li class="liebiao5_2">'
				+'			<span class="liebiao6_3 lefttitle"><a href="' + this.cartList[i][2] + '" class="text4" target="_blank"><span class="text4">●</span>'+this.cartList[i][1]+'</a></span>'
				+'			<span class="liebiao6_4"><a href="#" onclick="showPage.deleteInCart('+ i +');return false;" class="text5">delete</a></span></li>'
				+'		<li class="liebiao6_2 lefttitle">' + this.cartList[i][6] + '</li>'
				+'		<li class="liebiao6_2 lefttitle">' + this.cartList[i][5] + '</li>'
				+'		<li class="liebiao6_2 lefttitle">' + this.cartList[i][4] + '</li>'
				+'	</ul>'
				+'</div>'
				+ Str
				
			}
			
		}
		
		cartDiv.innerHTML += Str;
	}
	
	this.deleteInCart = function(initID){
		
		if(this.cartList.length > initID){
			this.cartList.splice(initID,1);
		}
		this.cartShow();
	}
	
	this.status = function(boolea){
		if(document.getElementById("showStatus")){
			if(boolea){
				document.getElementById("showStatus").className = "showStatus";
			}else{
				document.getElementById("showStatus").className = "";
			}
		}
	}
	
	this.playPicture = function (content){
		if(content.length>0){
			for(var i=0; i<content.length; i++){
				
			}
		}
	}
	
	this.loadImage = function(WIDTH,HEIGHT,obj){
		percent = HEIGHT / WIDTH;
	
		if(obj.width > obj.height / percent){
			if(obj.width > WIDTH){
				obj.width = WIDTH;
			}
		}else{
			if(obj.height > HEIGHT){
				obj.height = HEIGHT;
			}
		}
		
	}
}















