// JavaScript Document
var CLICKFL = true;
var colDEFINE = false;
var coloruseclick = false;
function skinBgchange(mainbgsrcnow){
	$("#colorinuse").hide();
	$("#picinuse").hide();
	coloruseclick = false;
	tateSAVE = mainbgsrcnow.split("|");
	if(tateSAVE[1].length != 0){
		UPBG_BUT(tateSAVE[1]);
	}
	else{
		$("#prepic").css("background","url(images/skinpicleftunclick.png) no-repeat");
		STATEREMOVE($("#prepic"));
		CLICKFL = false;
		$("#nextpic").show();
	}
	if(BGkind == 0){
		UPBG_BUT(tateSAVE[0]);
		$("#skinchangebgtop").children("img").attr("src",tateSAVE[0]).attr("class",tateSAVE[0]).show();
		$("#skinchangebgtop").css("background","none");
		$("#skin li#nowbg").children(".kbg").children("img").attr("src","images/skinpicbg.png");
		var posNOWBG = $("#skin li#nowbg").offset();
		$("#picinuse").css("left",posNOWBG.left+118).show();
		$("#colorinuse").hide();
	}
	else if(BGkind == 1){		
		$("#skinchangebgtop").children("img").hide();
		$("#skinchangebgtop").css("background",tateSAVE[0]);	 
		for(var bk=0; bk<$("#purecolor>li").length-1;bk++){ 
			var bkcom=$("#purecolor>li").eq(bk).css("backgroundColor");
			if(bkcom==tateSAVE[0]){ 
				var POSCOLNOW = $("#purecolor>li").eq(bk).offset();
				$("#colorinuse").css("left",POSCOLNOW.left+4).show();
				break; 
			}
		}
		if($("#colorinuse").css("display") == "none"){
			coloruseclick = true;
			var POSCOLNOW = $("#purecolor li#colorplus").offset();
			$("#colorinuse").css("left",POSCOLNOW.left+4).show();			
		}
		$("#picinuse").hide();
		$("#skin li#nowbg").children(".kbg").children("img").attr("src","images/skinpicbgnormal.png");
	}
}
function isSimilarColor(sHexColorA,sHexColorB,nOffset){ 
  if(sHexColorA.indexOf("#") != -1){
	  sHexColorA = sHexColorA.substring(1);
  }
  this.offsetNum=Math.abs(nOffset); 
  this.offsetNum>255?this.offsetNum=this.offsetNum-256:""; 
  var arrNumA=[parseInt(sHexColorA.substring(0,2),16), 
  parseInt(sHexColorA.substring(2,4),16), 
  parseInt(sHexColorA.substring(4,6),16)]; 
  if(sHexColorB.indexOf("#") != -1){
	  sHexColorB = sHexColorB.substring(1);
  }
  var arrNumB=[parseInt(sHexColorB.substring(0,2),16), 
  parseInt(sHexColorB.substring(2,4),16), 
  parseInt(sHexColorB.substring(4,6),16)];
 
  for(var i=0;i<arrNumA.length;i++){ 
    if((arrNumA[i]-arrNumB[i])<this.offsetNum){ 
     //alert("不相似\n第"+i+"组的差值为:"+(arrNumA[i]-arrNumB[i])+",\n超过了最大误差:"+this.offsetNum); 
	 //$("#BACKGROUND_IMAGE_TEXT").html(Math.abs(arrNumA[i]-arrNumB[i]));
	 $(".titlel").css("display","block");
     return false; 
    }
	else{
		$(".titlel").css("display","none");
	}
  }
  //alert("相似色\n"+sHexColorA+"\n"+sHexColorB);
  return true; 
} 
function UPBG_BUT(LASTSAVEBG){
	$("#nextpic").hide();
	for(var ii=0;ii<$("#skin>li").length;ii++){ 
		var usesrc=$("#skin>li").eq(ii).children("#abrepic").children("img").attr("class");	
		if(LASTSAVEBG==usesrc){
			$("#skin>li").eq(ii).attr("id","nowbg");		
			var width_but_init=parseInt($("#skin>li").width())+parseInt($("#skin>li").css("marginRight"));
			var nbut_init=$("#skin>li").length;	
			if(ii==0){
				$("#skin").css("left",0);
				$("#prepic").css("background","url(images/skinpicleftunclick.png) no-repeat");
				CLICKFL = false;
				STATEREMOVE($("#prepic"));
				$("#nextpic").show();
			}	
			else if(ii==1){
				$("#skin").css("left",0);
				$("#prepic").css("background","url(images/skinpicleftunclick.png) no-repeat");
				CLICKFL = false;
				STATEREMOVE($("#prepic"));
				$("#nextpic").show();
			}
			else if(ii>1 && ii<nbut_init-3){
				$("#skin").css("left",-(ii-1)*width_but_init);
				$("#prepic").css("background","url(images/skinpicleft.png) no-repeat");
				CLICKFL = true;
				STATEADD($("#prepic"));
				$("#nextpic").show();
			}
			else if(ii>=nbut_init-3 && ii<nbut_init-2){				
				$("#skin").css("left",-(nbut_init-4)*width_but_init);
				$("#prepic").css("background","url(images/skinpicleft.png) no-repeat");
				CLICKFL = true;
				STATEADD($("#prepic"));
				$("#nextpic").show();
			}
			else if(ii==nbut_init-2){
				$("#skin").css("left",-(nbut_init-3)*width_but_init);
				$("#nextpic").hide();
				$("#prepic").css("background","url(images/skinpicleft.png) no-repeat");
				CLICKFL = true;
				STATEADD($("#prepic"));
			}
			else if(ii==nbut_init-1){
				$("#skin").css("left",-(nbut_init-3)*width_but_init);
				$("#nextpic").hide();
				$("#prepic").css("background","url(images/skinpicleft.png) no-repeat");
				CLICKFL = true;
				STATEADD($("#prepic"));
			}
			break;
		}
	}
}
$(function(){ 
	$("#skin li:not('#bgplus') #abrepic img").live("click",function(){																	
		coloruseclick = false;
		var bg=$(this).attr("class");
		$("#skinchangebgtop").children("img").attr("src",bg).attr("class",bg).show();
		$("#skinchangebgtop").css("background","none");
		$("#skin li#nowbg").children(".kbg").children("img").attr("src","images/skinpicbgnormal.png");
		$("#skin li#nowbg").removeAttr("id");
		$(this).closest("li").attr("id","nowbg").children(".kbg").children("img").attr("src","images/skinpicbg.png");
		var posNOWBG = $("#skin li#nowbg").offset();
		$("#picinuse").css("left",posNOWBG.left+118).show();
		$("#colorinuse").hide();
		try{ 
			UIToBackApp("SET_SKINDLG_SKIN",$(this).attr("class")); 
		} 
		catch(e){
			
		} 
	})
	$("#skin li#bgplus #abrepic img").live("click",function(){
		coloruseclick = false;													
		UIToBackApp("SHOW_ADD_CUSTOM_BKIMAGE_DLG","");
	})
	$("#skin li.adddefine").live("mouseenter",function(){	   
		$(this).find(".del").show();		
	})
	$("#skin li.adddefine").find(".del").live("click",function(){
		var skinLEN = $("#skin>li")	.length;
		var THISARC = $(this).closest("li").find("#abrepic").children("img").attr("class");	
		var posLEFT = $("#skin").css("left");
		posLEFT = posLEFT.substring(0,posLEFT.indexOf("px")) - 0;	
		var CURBG ="";
		if($("#skinchangebgtop").children().length == 0){	
			 $(this).closest("li").remove();
			 if(posLEFT == -165*(skinLEN-4)){
				$("#nextpic").hide();
			 }
			 else if(posLEFT <= -165*(skinLEN-4)){
				 $("#skin").css("left",posLEFT+165);
				 var posNOWBG = $("#skin li#nowbg").offset();
				 $("#picinuse").css("left",posNOWBG.left+118).show();
			 }
			 else{
			 }
			 $("#skin").width((skinLEN-1)*165);	
			 UIToBackApp("REMOVE_CUSTOM_BKIMAGE",THISARC); 
			 return;
		 }
		 else{	
			  CURBG = $("#skinchangebgtop").children("img").attr("class");	
			  if(CURBG == THISARC){
				  var instead = $(this).closest("li").prev().find("#abrepic").children("img").attr("class");	
				  $("#skinchangebgtop").children("img").attr("src",instead).attr("class",instead);
				  $(this).closest("li").removeAttr("id");
				  $(this).closest("li").prev().attr("id","nowbg").children(".kbg").children("img").attr("src","images/skinpicbg.png");
				  var posNOWBG =  $(this).closest("li").offset();
				  $("#picinuse").css("left",posNOWBG.left+118).show();
				  UIToBackApp("SET_SKINDLG_SKIN",instead); 
				  $(this).closest("li").remove();
				  if(posLEFT == -165*(skinLEN-4)){
					  $("#nextpic").hide();
				  }
				  else if(posLEFT <= -165*(skinLEN-4)){
					   $("#skin").css("left",posLEFT+165);
				  }
				  else{
				  }
				  $("#skin").width((skinLEN-1)*165);					
				  UIToBackApp("REMOVE_CUSTOM_BKIMAGE",THISARC); 				  
				  return;
			  }
			  else{	
				  $(this).closest("li").remove();
				  if(posLEFT == -165*(skinLEN-4)){
					  $("#nextpic").hide();
				  }
				  else if(posLEFT <= -165*(skinLEN-4)){
					  $("#skin").css("left",posLEFT+165);
				  }
				  else{
				  }
				  $("#skin").width((skinLEN-1)*165);					
				  UIToBackApp("REMOVE_CUSTOM_BKIMAGE",THISARC); 
				  return;
			  }
		 }
	}); 
	$("#skin li.adddefine").live("mouseleave",function(){
		$(this).find(".del").hide();
	})
	$("#purecolor li:not('#colorplus')").live("click",function(){
		coloruseclick = false;		
		var colorBG=$(this).css("backgroundColor");
		$("#skinchangebgtop").children("img").hide();
		$("#skinchangebgtop").css("background",colorBG);
		var POSCOLNOW = $(this).offset();
		$("#colorinuse").css("left",POSCOLNOW.left+4).show();
		$("#picinuse").hide();
		$("#skin li#nowbg").children(".kbg").children("img").attr("src","images/skinpicbgnormal.png");
		try{ 
			UIToBackApp("SET_SKINDLG_SKIN",colorBG); 
		} 
		catch(e){
			
		} 
	})
	$("#purecolor li#colorplus").live("click",function(){
		coloruseclick = true;
		colDEFINE = true;
		UIToBackApp("SHOW_ADD_CUSTOM_BKCOLOR_DLG","");
	})
	$("#colorinuse").click(function(){
		if(coloruseclick){
			colDEFINE = true;
			UIToBackApp("SHOW_ADD_CUSTOM_BKCOLOR_DLG","");
		}
	})
})
$("#nextpic").live("click",function(){
	var n = $("#skin").css("left");
	var m = $("#picinuse").css("left");
	n = n.substring(0,n.indexOf("px"))-0;
	m = m.substring(0,m.indexOf("px"))-0;
	$("#skin").add("#picinuse").css("left",n-165);
	$("#picinuse").css("left",m-165);
	if(n == 0){
		$("#prepic").css("background","url(images/skinpicleft.png) no-repeat");	
		CLICKFL = true;
		STATEADD($("#prepic"));
	}
	else if(n <= -165*($("#skin>li").length-4)){
		$("#nextpic").hide();
	}
})
var nextShow,preShow ,n;
$("#nextpic").live("mousedown",function(){
	nextShow = setInterval(function(){
		n = $("#skin").css("left");
		m = $("#picinuse").css("left");
		n = n.substring(0,n.indexOf("px"))-0;
		m = m.substring(0,m.indexOf("px"))-0;
		$("#skin").add("#picinuse").css("left",n-165);
		$("#picinuse").css("left",m-165);
		if(n == 0){
			$("#prepic").css("background","url(images/skinpicleft.png) no-repeat");	
			CLICKFL = true;
			STATEADD($("#prepic"));
		}
		else if(n <= -165*($("#skin>li").length-4)){
			$("#nextpic").hide();
			clearInterval(nextShow);
		}
	},500)
		
})
$("#nextpic").live("mouseup",function(){
	clearInterval(nextShow);
})
$("#nextpic").live("mouseout",function(){
	clearInterval(nextShow);
})
$("#prepic").live("click",function(){
	if(CLICKFL){							   
		var n = $("#skin").css("left");
		var m = $("#picinuse").css("left");
		n = n.substring(0,n.indexOf("px"))-0;
		m = m.substring(0,m.indexOf("px"))-0;
		$("#skin").css("left",n+165);
		$("#picinuse").css("left",m+165);
		if(n == -165){
			$("#prepic").css("background","url(images/skinpicleftunclick.png) no-repeat");
			CLICKFL = false;
			STATEREMOVE($("#prepic"));
		}
		else if(n == -165*($("#skin li").length-3)){
			$("#nextpic").show();
		}
	}
})
$("#prepic").live("mousedown",function(){	
	if(CLICKFL){									   
		preShow = setInterval(function(){
			n = $("#skin").css("left");
			m = $("#picinuse").css("left");
			n = n.substring(0,n.indexOf("px"))-0;
			m = m.substring(0,m.indexOf("px"))-0;
			$("#skin").css("left",n+165);
			$("#picinuse").css("left",m+165);
			if(n == -165){
				$("#prepic").css("background","url(images/skinpicleftunclick.png) no-repeat");
				CLICKFL = false;
				STATEREMOVE($("#prepic"));
				clearInterval(preShow);
			}
			else if(n == -165*($("#skin li").length-3)){
				$("#nextpic").show();
			}								
		},500)	
	}
})
$("#prepic").live("mouseup",function(){
	if(CLICKFL){									 
		clearInterval(preShow);
	}
})
$("#prepic").live("mouseout",function(){
	if(CLICKFL){									 
		clearInterval(preShow);
	}
})
$(function TransparentSET(e){
	var percent=0;
	var handle=$(".draghandle");
	var dragbar=$(".dragbar");
	dragbar.css("width","168px");
	handle.css({"width":"16px"});
	var maxlen=parseInt(dragbar.width()-parseInt(handle.outerWidth()));
	handle.bind("mousedown",function(e){
		var x=e.pageX;
		var hx=parseInt(handle.offset().left);
		var ehx=parseInt(dragbar.offset().left);
		$(document).bind("mousemove",function(e){
			var left=e.pageX-x+hx-ehx<=0?0:(e.pageX-x+hx-ehx>=maxlen?maxlen:e.pageX-x+hx-ehx);			
			handle.css({left:left});
			percent=(left/maxlen*100).toFixed(0);
			UIToBackApp("SET_SKINDLG_TRANSPARENT",100-percent); 
			$("#tranper").html((percent)+"%");
			return false;
		});
		$(document).bind("mouseup",function(){
			$(this).unbind("mousemove");
		})
	})
})




window.onload=function(){
  objDiv = document.getElementById('draghandle');
  drag(objDiv);
};


function drag(dv){
  dv.onmousedown=function(e){
      var d=document;
      e = e || window.event;
      
      var x= e.layerX || e.offsetX;
      //var y= e.layerY || e.offsetY;
      
      //设置捕获范围
      if(dv.setCapture){
          dv.setCapture();
      }else if(window.captureEvents){
          window.captureEvents(Event.MOUSEMOVE | Event.MOUSEUP);
      }
     

      d.onmousemove=function(e){
           e= e || window.event;
           if(!e.pageX)e.pageX=e.clientX;
           if(!e.pageY)e.pageY=e.clientY;
           var tx=e.pageX-x;
          // var ty=e.pageY-y;
           
           dv.style.left=tx;
           //dv.style.top=ty;
      };

      d.onmouseup=function(){
           //取消捕获范围
           if(dv.releaseCapture){
              dv.releaseCapture();
           }else if(window.captureEvents){
              window.captureEvents(Event.MOUSEMOVE|Event.MOUSEUP);
           }
           
          //清除事件
          d.onmousemove=null;
          d.onmouseup=null;
      };
   };
}








$(function(){		  
	$("#nextpic").live("mouseover",function(){
		$(this).css("background","url(images/skinpicrighthover.png) -1px 0px no-repeat");
	})
	$("#nextpic").live("mouseout",function(){
		$(this).css("background","url(images/skinpicright.png) no-repeat");
	})
	$("#nextpic").live("mousedown",function(){
		$(this).css("background","url(images/skinpicrightclick.png) no-repeat");
	})
	$("#nextpic").live("mouseup",function(){
		$(this).css("background","url(images/skinpicright.png) no-repeat");
	})		
	$("#skinclose").hover(function(){ 
		$(this).children(".closeskinchange").attr("src","images/closehovernew.png");
	},function(){ 
		$(this).children(".closeskinchange").attr("src","images/closenew.png");
	})
	$("#skinclose").live("mousedown",function(){ 
		$(this).children(".closeskinchange").attr("src","images/closeclicknew.png");
	})   
})
function STATEADD($OBJECT){
	$OBJECT.bind("mouseover",prepicHOVER);
	$OBJECT.bind("mouseout",prepicOUT);
	$OBJECT.bind("mousedown",prepicHOVERCLICK);
	$OBJECT.bind("mouseup",prepicUP);
}
function STATEREMOVE($OBJECT){
	$OBJECT.unbind("mouseover",prepicHOVER);
	$OBJECT.unbind("mouseout",prepicOUT);
	$OBJECT.unbind("mousedown",prepicHOVERCLICK);
	$OBJECT.unbind("mouseup",prepicUP);
}
function prepicHOVER(){
	$(this).css("background","url(images/skinpiclefthover.png) 0px -1px no-repeat");
}
function prepicOUT(){
	$(this).css("background","url(images/skinpicleft.png) no-repeat");
}
function prepicHOVERCLICK(){
	$(this).css("background","url(images/skinpicleftclick.png) no-repeat");
}
function prepicUP(){
	$(this).css("background","url(images/skinpicleft.png) no-repeat");
}





