// JavaScript Document
function _X(obj){
    var x = obj.offsetLeft;
    while(obj = obj.offsetParent)
    {
        x += obj.offsetLeft;
    }
    return x;
}
function _Y(obj){
    var y = obj.offsetTop;
    while(obj = obj.offsetParent)
    {
        y += obj.offsetTop;
    }
    return y;
}

function SetXY(obj, x, y){
	if(typeof(obj)=='string'){
		var obj = document.getElementById(obj);	
	}
	try{
		obj.style.position = "absolute";
		obj.style.left = x.toString() + "px";
		obj.style.top = y.toString() + "px";
	}
	catch(e){
	}
}

function SetSearch(obj){
	var inputs = $(obj, "input");
	var input;
	var btn;
	
	var ul = document.createElement("ul");
	obj.insertBefore(ul, obj.firstChild);
	
	for(var i = 0; i < inputs.length; i++){
		if(inputs[i].type == "button"){
			btn = inputs[i];	
		}
		if(inputs[i].type == "text"){
			input = inputs[i];	
		}
	}
	
	btn.onclick = function(){
		enter();
	}
	
	var selectIndex = -1;
	var old = escape(input.value);
	
	var enter = function(){
		window.location.href = "default.aspx?search=" + escape(input.value);
	}
	
	var show = function(index){
		for(var i = 0; i < ul.childNodes.length; i++){
			if(selectIndex == i || index == i){
				ul.childNodes[i].style.backgroundColor = "#990000";	
				ul.childNodes[i].style.color = "#FFFFFF";
				input.value = ul.childNodes[i].innerHTML;
			}
			else{
				ul.childNodes[i].style.backgroundColor = "#FFFFFF";
				ul.childNodes[i].style.color = "";
			}
		}
		if(selectIndex == -1){
			input.value = unescape(old);
		}
	}
	input.onfocus = function(){
		input.value = "";
		this.onfocus = null;
	}
	input.onkeyup= function(ev){
		ev = ev || window.event;
		if(ev.keyCode == 38 || ev.keyCode == 40){
			return;	
		}
		_("search_time", 500);
	}
	input.onkeydown = function(ev){
		ev = ev || window.event;
		var n = 0;
		if(ul.hasChildNodes()){
			n = ul.childNodes.length;	
		}
		if(ev.keyCode == 38){
			if(selectIndex == -1){
				selectIndex = n - 1;	
			}
			else{
				selectIndex--;	
			}
			show(selectIndex);
		}
		else if(ev.keyCode == 40){
			if(selectIndex == n - 1){
				selectIndex = -1;	
			}
			else{
				selectIndex++;	
			}
			show(selectIndex);
		}
		else if(ev.keyCode == 13){
			enter();	
		}
	}
	
	_["search_time"] = function(){
		if(input.value.Trim() == ""){
			ul.innerHTML = "";
			selectIndex = -1;
		}
		else if(old != escape(input.value)){
			_("search", "App_Handler/searchName.ashx", "search=" + escape(input.value));
		}
		old = escape(input.value);
		_("search_time", false);
	}
	
	_["search"] = function(ajax){
		if(ajax.responseText){
			var names = ajax.responseText.ToStrs();
			ul.innerHTML = "";
			for(var i = 0; i < names.length; i++){
				var li = document.createElement("li");
				li.innerHTML = names[i];
				li.id = "search_li_" + i;
				li.onmouseover = function(){
					var index = parseInt(this.id.replace(/search_li_/, ""));
					selectIndex = index;
					show();
				}
				li.onmouseout = function(){
					selectIndex = -1;
					show();
					input.focus();
					input.value = input.value;
				}
				li.onclick = function(){
					input.focus();
					input.value = input.value;
					enter();
				}
				ul.appendChild(li);
			}
			selectIndex = -1;
		}
		else{
			ul.innerHTML = "";
			selectIndex = -1;
		}
	}
}


function SetMessage(obj){
	var input = $(obj, "input")[$(obj, "input").length - 1];
	input.onclick = function(){
		if($("name").value.Trim() == ""){
			alert("name is empty");
			return;
		}
		
		if(!isEmail($("email"))){
			alert("email is false");
			return;
		}
		
		if($("content").value.Trim() == ""){
			alert("content is empty")
			return;
		}
		_("message", "App_Handler/message.ashx", data(obj));
		this.disabled = true;
		//$(this.parentNode, "font")[0].innerHTML = "wait......";
	}
	
	_["message"] = function(ajax){
		if(ajax.responseText){
			alert("Send Succeed!");
			$("name").value = "";
			$("email").value = "";
			$("title").value = "";
			$("content").value = "";
		}
		else{
			alert("Send Missed!");	
		}
		input.disabled = false;
	}
}

function SetUL(obj){
	_[$(obj)] = function(){
		var x = $(obj, "_x");
		var lis = $(obj, "li");
		var w = 0;
		for(var i = 0; i < lis.length; i++){
			if(w >= 0 - x){
				if(i + 1 == lis.length){
					$$(obj, "_x", 0);
					_($(obj), 4000 + 20*R());
				}
				else{
					w += $(lis[i], "_w");
					$$(obj, "_x", 0 - w);
					_($(obj), 4000 + 20*R());
				}
				break;
			}
			w += $(lis[i], "_w");
		}
	}
	_($(obj), 5000 + 50*R());
	obj.onmouseover = function(){
		_($(this), false);
	}
	obj.onmouseout = function(){
		_($(this), true);
	}
}

//模拟设置浮动对象为表格自动高度模式
function table(obj, n){
	if(obj == undefined){
		for(p in table){
			if(typeof(table[p]) == 'function'){
				table[p]();
			}
		}
		return;
	}
	var nodes = new Array();
	for(var i = 0; i < obj.childNodes.length; i++){
		if(obj.childNodes[i].tagName){
			nodes[nodes.length] = obj.childNodes[i];
		}
	}
	
	var imgs = $(obj, "img");
	
	table[$(obj)] = function(){
		for(var i = 0; i < imgs.length; i ++){
			if(!imgs[i].width){
				return;	
			}	
		}
		for(var i = 0; i < nodes.length; i++){
			if((i + 1)%n == 0){
				var maxheight = $(nodes[i], "_h");
				for(var j = 1; j < n; j++){
					if($(nodes[i - j], "_h") > maxheight){
						maxheight = $(nodes[i - j], "_h");
					}
				}
				for(var j = 0; j < n; j ++){
					if(window.ActiveXObject){
						$(nodes[i - j], "_h", maxheight);
					}
					nodes[i - j].style.minHeight = maxheight.toString() + "px";
				}
				nodes[i].style.float = "none";
			}
		}
	}

	for(var i = 0; i <imgs.length; i++){
		if(imgs[i].width){
			setTimeout(function(){table();}, 500);
		}
		imgs[i].onload = function(){
			setTimeout(function(){table();}, 500);
		}
	}
	setTimeout(function(){table();}, 500);
}

//设置商品图片展示
function SetImg(obj){
	var ul = $(obj, "ul")[0];
	var div = $(obj, "div")[0];
	var thumbs = $(ul, "img");
	var images = $(div, "img");
	
	var objW = $(obj, "_w");
	var objH =  $(obj, "_h");
	var centerX = $(obj, "_w")/2 - 45;
	var selectIndex = 0;
	var moveX = 0;
	
	var maxW = $(ul, "li").length*90;
	
	var show = function(index){
		hover(index);
		$(div, "_x",  - selectIndex*objW);
		$(images[selectIndex], "_a", 0);
		$$(images[selectIndex], "_a", 100);
		
		if(maxW > objW){
			var thisX = index*90;
			var newX = centerX - thisX;
			if(newX > 0){
				newX = 0;
			}
			else if(newX < objW - maxW){
				newX = objW - maxW;
			}
			$$(ul, "_x", newX);
		}
	}
	

	var hover = function(index){
		for(var i = 0; i < thumbs.length; i++){
			if(i == index || i == selectIndex){
				thumbs[i].style.borderColor = "#F90";
				thumbs[i].style.borderWidth = "1px";
				thumbs[i].style.padding = "3px";
				thumbs[i].style.margin = "1px";
			}
			else{
				thumbs[i].style.borderColor = "#CCC";
				thumbs[i].style.borderWidth = "1px";
				thumbs[i].style.padding = "1px";
				thumbs[i].style.margin = "3px";
			}
		}
	}
	show(selectIndex);
	
	for(var i = 0; i < thumbs.length; i++){
		thumbs[i].id = "thumbs_" + i;
		images[i].id = "images_" + i;
		
		$(images[i], "_y", Math.floor(500 - $(images[i], "_h"))/2);
		images[i].onload = function(){
			$(this, "_y", Math.floor(500 - $(this, "_h"))/2);
		}
		
		thumbs[i].onmouseover = function(){
			var index = parseInt(this.id.replace(/thumbs_/, ""));
			hover(index);
		}
		thumbs[i].onmouseout = function(){
			hover(-1);
		}
		thumbs[i].onclick = function(){
			var index = parseInt(this.id.replace(/thumbs_/, ""));
			selectIndex = index;
			show(index);
		}
	}
	
	var next = true;
	_($(ul) + "_auto", 6000);
	_[$(ul) + "_auto"] = function(){
		if(thumbs.length < 2){
			return;	
		}

		if(selectIndex == thumbs.length - 1){
			next = false;
		}
		else if(selectIndex == 0){
			next = true;
		}
		
		if(next){
			selectIndex++;
		}
		else{
			selectIndex--;	
		}
		
		show(selectIndex);
	}
	obj.onmouseover = function(){
		_($(ul) + "_auto", false);
	}
	obj.onmouseout = function(){
		_($(ul) + "_auto", true);
	}
}