var scrollPos;
var start=0;
var count=18;
var index=0;
var isfirst=true;
var model=1;
var currentPage=1;
var pageItemCount=36;
var isPager=false;
var isClass=false;
var currentClass="";

var xml_url = "xml.php?t=brand";

$(function(){
	if(typeof document.compatMode!='undefined'&&document.compatMode!='BackCompat'){
		scrollPos=document.documentElement;
		}else if(typeof document.body!='undefined'){
			scrollPos=document.body;
	}
	AddCase();
	function findValueCallback(event,data,formatted){
//		if(data){window.location.href=data.url;}
		if(data){window.open(data.url);}
	}
	function formatItem(row){
		return row[1];
	}
	function formatResult(row){
		return row[1];
	}
	$("#search").autocomplete(project,{minChars:0,width:180,matchContains:true,autoFill:false,formatItem:function(row,i,max){return row.name;},formatMatch:function(row,i,max){return row.name;},formatResult:function(row){return row.name;}});
	$(":text, textarea").result(findValueCallback).next().click(function(){$(this).prev().search();});
});
function ScreenScroll(){
	var next_top=$("div.more").offset().top;$(scrollPos).animate({scrollTop:next_top},"slow");
}

function AddCase(){
	$("div.more").css("visibility","visible");
	$("div.solutionpage").css("visibility","hidden");
	$.ajax({
			url:xml_url,type:'GET',dataType:'xml',timeout:10000,beforeSend:function(){
				$("#loading").append("<center><img src='images/skin/loading.gif' /></center>");
				if(isfirst){
					start=0;count=18;$('#case').empty();
				}else{
					count=6;start=18+count*(index-1);
				}
			},
			error:function(){$('#loading').empty();$("#loading").append("<center>数据错误</center>");},
			success:function(xml){
				$('#solution').empty();
				$('#loading').empty();
				var i=0;
				$(xml).find('item').each(function(){
					i+=1;
					if(i>start&&i<=start+count){
						var name=$(this).children("name").text();
						var url=$(this).children("url").text();
						var img=$(this).children("image").text();
						var alt=$(this).children("name").text();
						var content=$(this).children("content").text();
						var markUp1=$(this).children("markUp1").text();
						var markUp1_href=$(this).children("markUp1").attr("href");
						var markUp2=$(this).children("markUp2").text();
						var markUp2_href=$(this).children("markUp2").attr("href");
						var markUp3=$(this).children("markUp3").text();
						var markUp3_href=$(this).children("markUp3").attr("href");
						var markUp4=$(this).children("markUp4").text();
						var markUp4_href=$(this).children("markUp4").attr("href");
						var classId=$(this).children("className").text();
						var mk='<a href="'+markUp1_href+'">'+markUp1+'</a>';
						if(markUp2!=""){
							mk+='<a  href="'+markUp2_href+'">'+markUp2+'</a>';
						}
						if(markUp3!=""){
							mk+='<a  href="'+markUp3_href+'">'+markUp3+'</a>';
						}
						if(markUp4!=""){
							mk+='<a  href="'+markUp4_href+'">'+markUp4+'</a>';
						}
						var html='<a href='+url+' target="_blank"><img src="'+img+'" alt='+alt+' border=0/></a>'
								+'<DT><a href='+url+' target="_blank">'+name+'</a></DT>'
								+'<DD><P>'+content+'</p>'
								+'<div class="tag">'+mk+'<a style="cursor:pointer;" onclick=ReloadCase("'+classId+'")>'+classId+'</a>'
								+'</div></DD>';
						$('<DL></DL>').html(html).appendTo('#case');
					}
				});

				if(isfirst){
					isfirst=false;
				}else{
					var next_top=$("div.more").offset().top;$(scrollPos).animate({scrollTop:next_top-400},1500);
				}
		}
	});

	index+=1;
}

function ReloadCase(id){
	isClass=true;selectImgButton();
	currentClass=id;
	model=2;
	$("div.more").css("visibility","hidden");
	$("div.solutionpage").css("visibility","visible");
	$.ajax({
			url:xml_url,type:'GET',dataType:'xml',timeout:10000,beforeSend:function(){
			$('#case').empty();
			$('#loading').empty();
			$("#loading").append("<center><img src='images/skin/loading.gif' /></center>");
			},error:function(){
					$('#loading').empty();
					$("#loading").append("<center>数据错误</center>");
			},success:function(xml){
				$('#loading').empty();
				$('#case').empty();
				$('#solution').empty();
				var item=0;
				$(xml).find('item').each(function(){
					var classId=$(this).children("className").text();
					if(classId==id){
						item+=1;
						if(item>(pageItemCount*(currentPage-1))&&item<=pageItemCount*currentPage){
							var name=$(this).children("client").text();
							var url=$(this).children("url").text();
							var img=$(this).children("image").text();
							var alt=$(this).children("name").text();
							var html='<a href="'+url+'"><img src="'+img+'" /></a><a href="'+url+'">'+name+'</a>';
							$('<li></li>').html(html).appendTo('#solution');
						}
					}
				});

				addPager(item);
				isfirst=true;
			}
		});
}

function changeShowModel(id){
	isClass=false;
	if(id!=model||isPager==true){
		isPager=false;model=id;
		if(id==2){
			selectImgButton();
			isfirst=true;
			$("div.more").css("visibility","hidden");
			$("div.solutionpage").css("visibility","visible");
			$.ajax({
					url:xml_url,type:'GET',dataType:'xml',timeout:10000,beforeSend:function(){},error:function(){
						$('#loading').empty();
						$("#loading").append("<center>案例加载完成</center>");
					},success:function(xml){
						var item=0;
						$('#loading').empty();
						$('#case').empty();
						$('#solution').empty();
						$(xml).find('item').each(function(){
							item+=1;
							if(item>(pageItemCount*(currentPage-1))&&item<=pageItemCount*currentPage){
								var name=$(this).children("client").text();
								var url=$(this).children("url").text();
								var img=$(this).children("image").text();
								var alt=$(this).children("name").text();
								var html='<a href="'+url+'"><img src="'+img+'"/></a><a href="'+url+'">'+name+'</a>';
								$('<li></li>').html(html).appendTo('#solution');
							}
						});

						addPager(item);
					}
				});
		}else{
			selectTxtButton();
			AddCase();
		}
	}
}

function addPager(item){
	$('.solutionpage').empty();
	var pageCount=Math.ceil(item/pageItemCount);
	if(pageCount>1){
		for(var i=1;i<=pageCount;i++){
			var str="";
			if(i==currentPage){
				str='<a style="color:#ff3300;cursor:pointer" onclick="changePage('+i+')" >'+i+'</a>';
			}else{
				str='<a style="cursor:pointer" onclick="changePage('+i+')" >'+i+'</a>';
			}
			$(str).appendTo('.solutionpage');
		}
	}
}

function changePage(id){
	if(id!=currentPage){
		currentPage=id;
		isPager=true;
		if(isClass){
			ReloadCase(currentClass);
		}else{
			changeShowModel(2);
		}
	}
}

var img1="images/skin/list1_1.jpg";
var img2="images/skin/list1.jpg";
var text1="images/skin/list2_1.jpg";
var text2="images/skin/list2_1.jpg";

function img_over(){$("#imgModel").attr("src",img1);}
function img_out(){$("#imgModel").attr("src",img2);}
function text_over(){$("#textModel").attr("src",text1);}
function text_out(){$("#textModel").attr("src",text2);}

function selectImgButton(){
	img2=img1="images/skin/list1_1.jpg";
	text1="images/skin/list2_1.jpg";
	text2="images/skin/list2.jpg";
	img_over();
	text_out();
}

function selectTxtButton(){
	img2="images/skin/list1.jpg";
	img1="images/skin/list1_1.jpg";
	text2=text1="images/skin/list2_1.jpg";
	img_out();text_over();
}