
var isIE = testIE();
//var p=$("#page_header");
//p.load( function() { alert("Hello"); } );

function testIE() {
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) return true;
	else return false;
}
function loadWorks() {
		//alert("test---");
		if(!openWorks){
			openWorks=true;
			//if($("#worksDiv")){
				//$("#worksDiv").show();
			//}else{
				//alert(loadWorksOk);
				if(loadWorksOk==0){
					$.post("service.php", { action: "readWorks"},addWorksList);
					$("#workList").show();
					loadWorksOk=1;
				}else if(loadWorksOk==1){
					$("#workList").show();
				}else{
					$("#worksDiv").show();
				}
			//}
			
		}else{
			//worksDiv
			openWorks=false;
			$("#workList").hide();
			if($("#worksDiv"))$("#worksDiv").hide();
		}
	}
	
function addWorksList(data) {
	//alert(data);
	//if(openWorks){
		response = data.substring(0, data.length);
		loadWorksOk=2;
		if (response == "No results") {
			$("#workList").hide();
		} else {
			//$("#new_page_content").html($("#new_page_content").html() + response);
			
			$("#worksDiv").html(response);
			if(openWorks){
				$("#worksDiv").show();
			}
			//$("#workList").before(response);
			$("#workList").hide();
		
	
		}
	//}
}

function show(i){
	if(i==-1){
		$("#content_img").fadeIn();
		$("#content_about").fadeIn();
		$("#content_all").fadeIn();
	}else if(i==0){
		$("#content_img").hide();
		$("#content_about").hide();
	}else if(i==1){
		//initLeft();
		$("#content_all").hide();
		$("#content_img").fadeIn("slow");
		$("#content_about").hide();	
	
	}else{
			$("#content_all").hide();
		$("#content_img").hide();
		$("#content_about").fadeIn();
	}
}


$(document).ready(function() {
	$("body").show();
	initLeft();
	contentAllShowIn();
	show(-1);
});
$(window).load(function() { 
	
});
function contentAllShowIn(){
	var p = $("#content_all");
		var num=p.find("._content_all_thumb").length;
		if(num>0){
			for(var i=0;i<num;i++){
				setThumbBtn($(p.find("._content_all_thumb")[i]),i);
			}
		}else{
			num=$("#content_img_list").length;
			if(num>0){
					var p = $("#content_img_list");
					p.mousedown(mouseDownHandler);
					setContentImg(p);
			}
		}
}
function setThumbBtn(obj,i){
	obj.fadeTo(0, 0);
	obj.delay(i*60).fadeTo(600, 1);
	var p=obj.find("._content_all_image"); 
	if(p.find("._content_all_imageOver").length>0){
		var over=p.find("._content_all_imageOver"); 
		var out=p.find("._content_all_imageOut"); 
		var overHtml=over.html();
		var outHtml=out.html();
		over.html(overHtml);
		out.html(outHtml);
		over.fadeTo(0, 0);
		out.fadeTo(0, 0);
		
		p.append("<div class='_content_all_imageLoading'><img src='"+path+"loading.gif' width='20' height='20' /></div>");
		var loading=p.find("._content_all_imageLoading");
						var $img=p.find("img");
						var imgCount=$img.length;
							$img.load(function(){
									imgCount--;
									if(imgCount==0){
										out.fadeTo(600,1);
										loading.fadeTo(300,0,
													function(){
															loading.remove("._content_all_imageLoading");
													}
											);
										p.mouseover(function() {over.stop();over.fadeTo(100,1);  });
										p.mouseout(function() {over.stop();over.fadeTo(300,0); });
									}
									
								});
		
		
	}
}
function setContentImg(obj){
	var num=obj.find("div").length;
	for(var i=1;i<num-1;i++){
		imageLoading($(obj.find("div")[i]));	
	}
}
function imageLoading(p){
		var img;
		var l=0;
		var t=0;
		var w=0;
		var pHtml=p.html();

		p.html(pHtml);
		
		img=p.find("img");
		img.fadeTo(0, 0);
		l=Math.round(img.width()/2+10);
		t=Math.round(img.height()/2+10);
		
		
		//
		//alert(w+"::"+l+"::"+t+":"+img.height());
		p.append("<img class='_image_loading' style='position: relative;left:-"+String(l)+"px;top:-"+String(t)+"px;' src='"+path+"loading.gif' width='20' height='20' />");
		//alert("<img class='_image_loading' style='position: relative;left:-"+String(l)+"px;top:"+String(t)+"px;' src='image/loading.gif' width='20' height='20' />");
		var loading=p.find("._image_loading");
		var $img=p.find("img");
					var imgCount=$img.length;
							$img.load(function(){
									imgCount--;
									if(imgCount==0){
										//alert(loading.length);
										img.fadeTo(600,1);
										loading.remove("._image_loading");
										loading.fadeTo(300,0,
													function(){
															
													}
											);
									}
				});
				
}
function imageLoaded(obj){
	alert(obj);
}
function toShowDetails(str){
	alert(str);
}
function initLeft(){
	if(leftList.length==0){
		var p = $("#content_img_mask");
		p.scrollLeft(0);
		p = $("#content_img_list");
		var num=p.children("div").length;
		leftList=[];
		var minLeft=0;
		for(var i=0;i<num;i++){
			if(i==0){
				minLeft=$(p.children("div")[i]).position().left;
			}else{
				leftList.push($(p.children("div")[i]).position().left-minLeft);
			}
			//alert($(p.children("span")[i]).position().left);
		}
		
		autoMove();
	}
}
/*
var MyInterval=setInterval("onMove()",30);

  clearInterval(MyInterval);
  */
  var cacheX=0;
var oldLeft=0;
var cacheLeft=0;
var leftList=[];
  var speedX=0;
  var moveA=0.8;
  var moveState=0;
  var moveInterval=null;
  var showX=0;
   var _showX=0;
   function startMove(){
  		if(moveInterval==null){
			moveInterval=setInterval("onMove()",30);
		}
  }
  function onMove(){
	  var p = $("#content_img_mask");
	  var xx;
	  var cutX=p.scrollLeft();
  		if(moveState==0){
			xx=(showX-_showX)/6;
		//	$("#xxx").val(_showX+"::"+showX);
			if(Math.abs(xx)<0.1){
				_showX=showX;
				p.scrollLeft(showX);
				clearInterval(moveInterval);
				moveInterval=null;
			}else{
				_showX=_showX+xx;
				p.scrollLeft(_showX);
			}
		}else if(moveState==1){
			speedX=cutX-cacheLeft;
			//speedX=(speedX+cutX-cacheLeft)/2;
		}else if(moveState==2){
			//alert(p.scrollLeft()+"::"+speedX);
			xx=cutX+speedX;
			if(xx<leftList[0]-86){
				xx=leftList[0]-86;
				speedX=0;
			}
			if(xx>leftList[leftList.length-1]+86){
				xx=leftList[leftList.length-1]+86;
				speedX=0;
			}
			p.scrollLeft(xx);
			speedX*=moveA;
			if(Math.abs(speedX)<1){
				//autoMove();
			}
		}else{
			
		}
		
		cacheLeft=cutX;
  }
function autoMove(){
	var p = $("#content_img_mask");
	var temX=p.scrollLeft();
	speedX=temX-cacheLeft;
	_showX=temX;
	var isFind=false;
	var minX=leftList[leftList.length-2];
	for(var i=0;i<leftList.length-2;i++){
		if(temX<leftList[i+1] ){
			if(speedX<=0){
				if(Math.abs(temX-leftList[i])>86 ){
					minX=leftList[i];
					 isFind=true;
				}
				break;
			}else{
				if(Math.abs(temX-leftList[i+1])>86 ){
					minX=leftList[i+1];
					 isFind=true;
					
				}
				break;
			}
			break;
		}else if(temX==leftList[i+1] ){
			minX=leftList[i+1];
		}
	}
	
	showX=minX;
	//*/
	//showX=leftList[0];
	//alert(showX);
	if(isFind){
		moveState=2;
	}else{
		moveState=2;
	}
	startMove();
	//alert("cc:"+showX);
	//p.animate({scrollLeft:showX}, 300 );
}
	function mouseDownHandler(event) { 
			var p = $("#content_img_mask");
			p.stop();
			oldLeft=p.scrollLeft();
			cacheX=event.clientX+p.scrollLeft();
			$(document).bind("mouseup", mouseupHandler);
			$(document).bind("mousemove", mousemoveHandler);
			moveState=1;
			event.preventDefault();

		}	

		function mouseupHandler(event) { 
							var p = $("#content_img_mask");
							p.scrollLeft(cacheX-event.clientX);
							
							$(document).unbind("mousemove", mousemoveHandler);
							$(document).unbind("mouseup", mouseupHandler);
							
							autoMove();
							event.preventDefault();
		}				

function mousemoveHandler(event){
		//alert(event.clientX);
	var p = $("#content_img_mask");
	//$("#xxx").val(cacheX+"::"+(cacheX-event.clientX)); 
	
	p.scrollLeft(cacheX-event.clientX);
	if(oldLeft==p.scrollLeft()){
			cacheX=event.clientX+p.scrollLeft();
	}
	_showX=p.scrollLeft();
	oldLeft=p.scrollLeft();
	event.preventDefault();
}
