﻿var BASEURL = '';//'http://59.36.101.91/client2009/prosa/';//'http://192.168.1.10/client/nuoan/'
var API =  BASEURL+'ajax/api.php';
var subpage = '#subpage';
var containers = '#containers';
$(document).ready(function (){
	gopage('home');
	window.defaultStatus='NUOAN';
});
/*页面链接转发*/
var gopage = function(pagename,size){
	$.ajax({
		type: "post",
		dataType : "html",
		url: BASEURL+pagename+".php",
		beforeSend: function(){
			showloading();
		},
		success: function (response) {
			closeloading();	
			if($(containers).length){
			}
			html = '<div id="containers">'+response+'</div>';
			$(subpage).empty().html(html);
			try{
				eval(pagename+'.init();');
				//========================================
				if(pagename=='catalog'&&size!=undefined)
				{
					var sizestr={'98':0,'100-119':1,'120-135':2,'140-149':3,'150-160':4,'163-196':5,'200-220':6,'225':7};
					catalog.left_size(sizestr[size]);
				}
				//=========================================
			}catch(err){ 
				$(containers).slideDown(500);
			}
		} 
	});
};
function gotosize(size)
{
	gopage('catalog',size);
};
var home = {
	init:function(){
		$(containers).slideDown(500);
	}
};
var aboutus = {
	aboutDiv:'#aboutus'
	,init:function(){
		$(containers).slideDown(500);
		$('.about_more').click(function(){
			aboutus.showoption($(this).attr('tab'));
		});
		$('.about_close').click(function(){
			aboutus.closeoption();
		});
		$('.about_tab_map').click(function(){
			aboutus.showoption($(this).attr('tab'));
		});
		$(aboutus.aboutDiv).hide();
		$(aboutus.aboutDiv).slideDown(500);
	}
	/*显示子页面*/
	,showoption:function(tab){
		switch(tab){
			case 'tag_intr':
				$('#about_btn').attr('src',BASEURL+'user/images/about_btn1.jpg');
				break;
			case 'tag_cert':
				$('#about_btn').attr('src',BASEURL+'user/images/about_btn2.jpg');
				break;
			case 'tag_rese':
				$('#about_btn').attr('src',BASEURL+'user/images/about_btn3.jpg');
				break;
		}
		$(aboutus.aboutDiv).show();
		$('#tabbox').empty().html($('.'+tab).html());
		$('#tabbox').show();
		$(aboutus.aboutDiv).slideUp(400);
		setTimeout(aboutus.autoscroll,400);
	}
	/*关闭子页面*/
	,closeoption:function(){
		$(aboutus.aboutDiv).slideDown(500);
		$('#tabbox').empty();
	}
	/*滚动条绑定*/
	,autoscroll:function(){
		scroll('#tabbox .scroll-pane',1);
	}
	/*证书*/
	,cretshow:function(src){
		$('#cret_big').slideUp(100);
		$('#cret_big').attr('src',src);
		$('#cret_big').slideDown(200);
	}
};
var catalog = {
	init:function(){
		catalog.build_left();
		catalog.load_make();
	}
	/*装载make记录*/
	,load_make:function(){
		ajax('get_makes','','json',function(response){
			var html_1 = '';
			var html_2 = '';
			var html_3 = '';
			var regI=1;
			var preLetter = 'A';
			var tpl = $('#tpl_makeitems').html();
			tpl = tpl.replace(/\%7B/ig,'{');
			tpl = tpl.replace(/\%7D/ig,'}');
			var re = /(\{loop\}([^@]*)\{endloop\})/ig
			var loopstr = re.exec(tpl);
			if(loopstr==null){
				loopstr = re.exec(tpl);
			}	
			loopstr = loopstr[2];
			tpl = tpl.replace(/(\{loop\}([^@]*)\{endloop\})/ig,'{list}');
			var list = '';
			$.each(response,function(i,x){
				var letter = x.make.substring(0,1).toUpperCase();
				if(letter != preLetter){
					preLetter = letter;
					tmp = '';
					tmp = tpl.replace(/({letter})/g,String.fromCharCode(letter.charCodeAt()-1));
					tmp = tmp.replace(/\{list\}/g,list);
					if(regI < 7) html_1 += tmp;
						else if(regI < 15) html_2 += tmp;
							else html_3 += tmp;

					list = '';
					regI++;
				}
				tmp = '';
				tmp = loopstr.replace(/\{id\}/g,x.id);
				tmp = tmp.replace(/\{name\}/g,x.make);
				list += tmp;
			});
			$('#catalog_list').empty().html($('#tpl_make').html());
			$('#catalog_list #row1').empty().html(html_1);
			$('#catalog_list #row2').empty().html(html_2);
			$('#catalog_list #row3').empty().html(html_3);
			$(containers).slideDown(500);
			//$('#catalog_list #row1').show();
			//$('#catalog_list #row2').show();
			//$('#catalog_list #row3').show();
			/*
			$(containers).show(1,function(){
				$('#catalog_list #row1').show(200,function(){
					$('#catalog_list #row2').show(200,function(){
						$('#catalog_list #row3').show(200);
					});
				});
			});*/
			/*
			for(i='A'.charCodeAt();i<='Z'.charCodeAt();i++)
			{
				latter_div = '#letter_'+String.fromCharCode(i);
				$(latter_div).show(100+i*10);
			}*/
		});
	}
	/*make填冲模板*/
	,addmake:function(x,letter){
		var tpl = $('#tpl_makeitems').html();
		tpl = tpl.replace(/({letter})/ig,letter);
		tpl = tpl.replace(/({id})/ig,x.id);
		tpl = tpl.replace(/({item})/ig,x.item);
		tpl = tpl.replace(/({name})/ig,x.name);
		loopstr = tpl.replace(/(\{loop\}([^@]*)\{endloop\})/ig,'$2');
		tpl = tpl.replace(/(\{loop\}([^@]*)\{endloop\})/ig,x.name);
		return loopstr;
	}
	/*左侧事件绑定*/
	,build_left:function(){
		$('.btn_view_view').click(function(){
			catalog.left_size(-1);
			catalog.load_make();
		});
		$('#partnumber').focus(function(){
			this.select();
		});
		$('#SubmitNumber').click(function(){
			catalog.load_number($('#partnumber').val());
		});

		$.each($('#sizesearch ul li'),function(i,x){
			$(x).children('a').click(function(){
				catalog.left_size(i);
			});
		});
	}
	/*替换产品tpl tag*/
	,replaceTag:function(str, item){
		if(item.WVA.substring(0,1)=='@') item.WVA = '';
		str = str.replace(/\{td_id\}/ig, item.id);
		str = str.replace(/\{td_ypid\}/ig, item.ypid);
		str = str.replace(/\{td_MYNO\}/ig, item.MYNO);
		str = str.replace(/\{td_DON\}/ig, item.DON);
		str = str.replace(/\{td_BFMC\}/ig, item.BFMC);
		str = str.replace(/\{td_WVA\}/ig, item.WVA);
		str = str.replace(/\{td_Width\}/ig, item.Width);
		str = str.replace(/\{td_hole\}/ig, item.hole);
		str = str.replace(/\{td_note\}/ig, item.note);
		str = str.replace(/\{td_InnerLength\}/ig, item.InnerLength);
		str = str.replace(/\{td_MinThickness\}/ig, item.MinThickness);
		str = str.replace(/\{td_MaxThickness\}/ig, item.MaxThickness);
		str = str.replace(/\{td_DrumDiameter\}/ig, item.DrumDiameter);
		str = str.replace(/\{td_Numberofholes\}/ig, item.Numberofholes);
		str = str.replace(/\{td_note\}/ig, item.note);
		return str;
	}
	/*左侧尺寸变化*/
	,left_size:function(index){
		$.each($('#sizesearch ul li'),function(i,x){
			$(x).removeClass('active');
			html = $(x).children('a').children(':first-child').html();
			html = html.replace(/=\s/g,'');
			$(x).children('a').children(':first-child').html(html);
			if(i==index){
				$(x).addClass('active');
				$(x).children('a').children(':first-child').prepend("");
				var min =$(x).children('a').attr('min');
				var max =$(x).children('a').attr('max');
				sel_size_text = $(x).children('a').html();
				if($('#sel_size').html() == sel_size_text){
					$('#catalog_list').children(':first-child').slideDown("slow");
				}else{
					catalog.load_size(min,max,sel_size_text);
				}
			}
		});
	}
	/*装载尺寸记录*/
	,load_size:function(min,max,sel_size_text){
		var postdata = 'min='+min+'&max='+max;
		ajax('get_size',postdata,'json',function(response){
			var tpl = $('#tpl_size').html();
			tpl = tpl.replace(/\%7B/ig,'{');
			tpl = tpl.replace(/\%7D/ig,'}');
			tpl = tpl.replace(/({sel_size})/ig,sel_size_text);
			var re = /(\{loop\}([^@]*)\{endloop\})/ig
			var loopstr = re.exec(tpl);
			if(loopstr==null){
				loopstr = re.exec(tpl);
			}	
			loopstr  = loopstr[2];
			var loopstr2 = $('#tpl_size2').html();
			var regI=0;
			var list = '';
			$.each(response,function(i,x){
				regJ = 0;
				$.each(x,function(i2,item){
					tmp = '';
					if(regJ==0){
						tmp = catalog.replaceTag(loopstr,item);
						tree = (x.length>1) ? '<img class="size_img" id="size_img_'+regI+'" src="images/jia.gif" style="padding-left:4px" onclick="catalog.size_more('+regI+')" />' : '';
						tmp = tmp.replace(/\{td_tree\}/g,tree);
					}else{
						tmp = catalog.replaceTag('<div  class="noplay size_m size_m_'+regI+'">'+loopstr2+'</div>',item);
					}
					//$('#catalog_right #tpl_size_table tr:last').after(tmp);
					regJ++;
					list += tmp;
				});
				regI++;
			});
			tpl = tpl.replace(/(\{loop\}([^@]*)\{endloop\})/ig,list);
			$('#catalog_list').empty().html(tpl);
			$('#sel_size').show(800);
			scroll('#size_table .scroll-pane',1);
		});
	}
	,size_more:function(index){
		if($('#size_img_'+index).attr('src')=='images/jia.gif'){
			$('.size_img').attr('src','images/jia.gif');
			$('.size_m').slideUp(500);
			$('#size_img_'+index).attr('src','images/jian.gif');
			$('.size_m_'+index).slideDown(500);
		}else{
			$('#size_img_'+index).attr('src','images/jia.gif');
			$('.size_m_'+index).slideUp(500);
		}
	}
	/*装载号码查询记录*/
	,load_number:function(number){
		if(number=='Enter a Part No.' || number=='') return false;
		var postdata = 'number='+number;
		var tpl = $('#tpl_number').html();
		var re = /(\{loop\}([^@]*)\{endloop\})/ig
		var loopstr = re.exec(tpl);
		if(loopstr==null){
			loopstr = re.exec(tpl);
		}	
		loopstr  = loopstr[2];
		var re2 = /(\{loop2\}([^@]*)\{endloop2\})/ig
		var loopstr2 = re2.exec(tpl);
		if(loopstr2==null){
			loopstr2 = re2.exec(tpl);
		}	
		loopstr2  = loopstr2[2];
		ajax('get_number',postdata,'json',function(response){
			list = '';
			if(response.rows!=null){
				var plist=new Array();
				$.each(response.rows,function(i,x){
					var exists = false;
					for (k in plist)
					{
						if(plist[k]==x.ypid){
							exists = true;
						}
					}
					var tmp2 = loopstr2;
						tmp2 = tmp2.replace(/({factory})/ig,x.factory);
						tmp2 = tmp2.replace(/({number})/ig,x.number);
					if(!exists){ 
						plist[i] = x.ypid;
						var tmp = loopstr;
						tmp = tmp.replace(/({id})/ig,x.id);
						tmp = tmp.replace(/({td_ypid})/ig,x.ypid);
						tmp = tmp.replace(/({numbers})/ig,x.factory+': '+x.number);
						tmp = tmp.replace(/({myno})/ig,x.MYNO);
						tmp = tmp.replace(/({wva})/ig,x.WVA);
						tmp = tmp.replace(/({Width})/ig,x.Width);
						tmp = tmp.replace(/({Hole})/ig,x.hole);
						tmp = tmp.replace(/({InnerLength})/ig,x.InnerLength);
						tmp = tmp.replace(/({DrumDiameter})/ig,x.DrumDiameter);
						list += tmp + tmp2;
					}else{
						list += tmp2;
					}
				});
			}else{
				list = '<tr><td colspan="8" class="part_title part_title1">"<strong style="color:yellow">'+number+'</strong>" is not found</td></tr>';
			}
			tpl = tpl.replace(/(\{loop\}([^@]*)\{endloop\})/ig,list);
			tpl = tpl.replace(/(\{loop2\}([^@]*)\{endloop2\})/ig,'');
			$('#catalog_right').hide();
			$('#catalog_list').empty().html(tpl);
			$('#catalog_right').slideDown("slow",function(){
				//scroll('#search_list_box',2);
			});
		});
	}
	,showpro:function(id,ypid,numbers){
		numbers = (typeof(numbers) == "undefined") ? '' : numbers;
		var tpl = $('#tpl_showpro').html();
		tpl = tpl.replace(/\%7B/ig,'{');
		tpl = tpl.replace(/\%7D/ig,'}');
		var re = /(\{loop\}([^@]*)\{endloop\})/ig
		var loopstr = re.exec(tpl);
		if(loopstr==null){
			loopstr = re.exec(tpl);
		}	
		loopstr  = loopstr[2];
		var re2 = /(\{loop2\}([^@]*)\{endloop2\})/ig
		var loopstr2 = re2.exec(tpl);
		if(loopstr2==null){
			loopstr2 = re2.exec(tpl);
		}	
		loopstr2  = loopstr2[2];
		var postdata = 'id='+id+'&pid='+ypid;
		ajax('get_pro',postdata,'json',function(response){
			var pic = '';
			var rows = '';
			var models = '';
			$.each(response.rows,function(i,x){
				if(pic=='') pic = x.pic;
				rows += catalog.replaceTag(loopstr,x);
			});
			if(response.models != null){
				$.each(response.models,function(i,x){
					var tmp = loopstr2;
					tmp = tmp.replace(/({Make})/ig,x.makename);
					tmp = tmp.replace(/({Model})/ig,x.modelname);
					tmp = tmp.replace(/({Application})/ig,x.engineername);
					models += tmp;
				});
			}
			if(pic=='' || pic==null)  pic = 'noimg.gif';
			tpl = tpl.replace(/\{pic\}/ig,pic);
			tpl = tpl.replace(/\{picbig\}/ig,pic);
			tpl = tpl.replace(/\{what\}/ig,numbers);
			tpl = tpl.replace(/(\{loop\}([^@]*)\{endloop\})/ig,rows);
			tpl = tpl.replace(/(\{loop2\}([^@]*)\{endloop2\})/ig,models);
			$('#catalog_pro').empty().html(tpl);
			$('#catalog_pro').show(1,function(){
				scroll('#promdlist',2);
			});

			$('#catalog_list').children(':first-child').hide(1,function(){
				$('#catalog_pro').slideDown(300);
			});

			$('.pro_back').click(function(){
				$('#catalog_list').children(':first-child').show();
				$('#catalog_pro').hide();
			});
			$('.pro_showbig').click(function(){
				$('#showpro_box').slideUp(300,function(){
					$('#pro_pic_big').slideDown(300);
				});
			});
			$('#pro_pic_big').click(function(){
				$('#showpro_box').slideDown(300);
			});
			//if(response.models.length>8)
				;//scroll('#mdlist',2);
		});
	}
	,model:function(makeid,makename){
		var postdata = 'id='+makeid;
		var tpl = $('#tpl_models').html();
		$('#catalog_list').empty().html(tpl);
		showloading();
		$('#catalog_list').hide().slideDown(1,function(){
			catalog.modelgrid(makeid,makename);
		});
		/*
		ajax('get_model',postdata,'json',function(response){
			$('#catalog_list').empty().html(tpl);
			$('#catalog_list').hide().slideDown(500);
			var modelstr = '<div id="md_model"><ul>';
			if(response.rows != null)
			$.each(response.rows,function(i,x){
				modelstr += '<li id="mdmli_'+x.id+'"><a href="javascript:catalog.engineer('+makeid+','+x.id+');">'+x.model+'</a></li>';
			});
			modelstr += '</ul></div>';
			tpl = tpl.replace(/\{md_make\}/ig,makename);
			tpl = tpl.replace(/\{md_model\}/ig,modelstr);
			$('#catalog_list').empty().html(tpl);
			$('#catalog_list').hide().slideDown(500);
		});
		*/
	}
	,modelgrid:function(makeid,makename){
		jQuery("#grid_list").jqGrid({
			//treeGrid: true,
			//treeGridModel : 'adjacency',
			//ExpandColClick : true,
			//ExpandColumn : 'model',
			url:API+'?method=get_model_tree&makeid='+makeid,
			datatype: "json",
			colNames:['id','check','Model', 'Application', 'Year','F/R','Prosa No.','WVA'],
			colModel:[
				{name:'id',index:'id',hidden:true,key:true},
				{name:'check',index:'check',hidden:true},
				{name:'model',index:'model', width:120},
				{name:'app',index:'app', width:220},
				{name:'year',index:'year', width:50, align:"center"},
				{name:'fr',index:'fr', width:20, align:"center"},
				{name:'myno',index:'myno', width:60, align:"left"},	
				{name:'wva',index:'wva', width:100, align:"center"}	
			],
		    width:730,
		    height:450,
			rowNum:100,
			pager: '#grid_pager',
			sortname: 'id',
			viewrecords: true,
			sortorder: "desc",
			//forceFit : true,
			multiselect: true,
			multiselectWidth:1,
			caption: makename
			,beforeRequest:function(){
				showloading();
			}
			,gridComplete: function() { 
				closeloading();
				var ids = jQuery("#grid_list").jqGrid('getDataIDs');
				for(var i=0;i<ids.length;i++){
					var cl = ids[i];
					rowData = jQuery("#grid_list").jqGrid('getRowData',cl);
					if(rowData.check==1){
						jQuery("#grid_list").jqGrid('setSelection',cl);
						//document.getElementById(cl).style.backgroundColor="#E3F0F9"; 
					}
				}
			}
		});
		jQuery("#grid_list").jqGrid('navGrid','#grid_pager',{edit:false,add:false,del:false});
		jQuery("#grid_list").jqGrid('hideCol','cb');

	}
	,engineer:function(makeid,modelid){
		$.each($('#md_model ul li'),function(i,x){
			$(x).removeClass('sel');
		});
		$('#mdmli_'+modelid).addClass('sel');
		var postdata = 'id='+modelid+'&makeid='+makeid;
		ajax('get_app',postdata,'json',function(response){
			var appstr = '<ul>';
			$.each(response.rows,function(i,x){
				appstr += '<li class="model_cur21" id="mdpli_'+x.id+'"><a href="javascript:catalog.modelpro('+makeid+','+modelid+','+x.id+');">'+x.engineer+'</a></li>';
			});
			appstr += '</ul>';
			$('#md_app').empty().html(appstr);
			$('#md_app').hide().slideDown(500,function(){
			});
			catalog.modelpro(makeid,modelid,'');
		});
	}
	,modelpro:function(makeid,modelid,engineid){
		if(engineid!=''){
			$.each($('#md_app ul li'),function(i,x){
				$(x).removeClass('model_cur2');
			});
			$('#mdpli_'+engineid).addClass('model_cur2');
		}
		var postdata = 'makeid='+makeid+'&modelid='+modelid+'&engineid='+engineid;
		ajax('get_modelpro',postdata,'json',function(response){
			var prostr = '';
			$.each(response.rows,function(i,x){
				 var tmp = '<div class="md_plist"><ul>';
				 tmp += '<li style="width:100px"><a href="javascript:catalog.showpro('+x.id+','+x.ypid+')">'+x.MYNO+'</a></li>';
				 //tmp += '<li style="width:100px"><a href="javascript:catalog.showpro('+x.id+')">'+x.BFMC+'</a></li>';
				 //tmp += '<li style="width:85px"><a href="javascript:catalog.showpro('+x.id+')">'+x.WVA+'</a></li>';
				 tmp += '</ul></div><div class="mdline"></div>';
				 prostr += tmp;
			});
			$('#md_plist').empty().html(prostr);
			$('#md_plist').hide();
			$('#md_plist').slideDown(500,function(){
				var ht = $('#md_plist').height();
				if(ht<456) $('#md_plist').append('<div style="height:'+(460-ht)+'px">&nbsp</div>');
				scroll('#md_plist_scroll-pane',1);
			});
		});
	}
};
var news={
	init:function(){
		news.build_left(1);
		news.shownews(0);
		$(containers).slideDown(500);
	},
	build_left:function(page){
		postdata='l=en&page='+page;
		ajax('get_newslist',postdata,'json',function(response){
			var tpl = $('#newslisttpl').html();
			var re = /(\{loop\}([^@]*)\{endloop\})/ig
			var loopstr = re.exec(tpl);
			if(loopstr==null){
				loopstr = re.exec(tpl);
			}	
			loopstr  = loopstr[2];
			list='';
			if (response.rows.id!=0)
			{
				$.each(response.rows,function(i,x){
					var tmp = loopstr;
					tmp = tmp.replace(/({title})/ig,x.title);
					tmp = tmp.replace(/({msg})/ig,x.msg);
					tmp = tmp.replace(/({tdate})/ig,x.tdate);
					tmp = tmp.replace(/({id})/ig,x.id);
					list+=tmp;
				});
				//输出分页
				list+='<div class="pagenav">';
				if (response.page.pre!=undefined)
				{
					list+='<a href="javascript:news.build_left('+response.page.pre+')">Prev</a>';	
				}
				if (response.page.cur!=undefined)
				{
					list+='<span class="curpage">'+response.page.cur+'</span>';	
				}
				if (response.page.next!=undefined)
				{
					list+='<a href="javascript:news.build_left('+response.page.next+')">Next</a>';	
				}
				list+='</div>';
				tpl = tpl.replace(/(\{loop\}([^@]*)\{endloop\})/ig,list);
			}
			else
			{
				tpl='no news!';
			}
			$('#news_list').empty().html(tpl);
		});
	},
	shownews:function(id){
		postdata='l=en&id='+id;
		ajax('get_news',postdata,'json',function(response){
			var tpl = $('#newstpl').html();
			var tmp = tpl;
			$.each(response.rows,function(i,x){
				tmp = tmp.replace(/({title})/ig,x.title);
				tmp = tmp.replace(/({msg})/ig,x.msg);
				tmp = tmp.replace(/({keyword})/ig,x.keyword);
				tmp = tmp.replace(/({tdate})/ig,x.tdate);
				tmp = tmp.replace(/({id})/ig,x.id);
			});
			$('.news_right').hide();
			$('.news_right').empty().html(tmp);
			$('.news_right').show(200,function(){
				scroll('.news_right .newcontent',1);
			});
		});
	}
};
