﻿function addLoadEvent(func){
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		}
	}
}

//setTab script;
function setTab(name,cursel,n){
  for(i=1;i<=n;i++){
  var menu=document.getElementById(name+i);
  var con=document.getElementById("con_"+name+"_"+i);
  menu.className = (i==cursel)?"hover":"";
  con.style.display = (i==cursel)?"block":"none";
 }
}


//form script;
function focusLabels() {
  if (!document.getElementsByTagName) return false;
  var labels = document.getElementsByTagName("label");
  for (var i=0; i<labels.length; i++) {
    if (!labels[i].getAttribute("for")) continue;
    labels[i].onclick = function() {
      var id = this.getAttribute("for");
      if (!document.getElementById(id)) return false;
      var element = document.getElementById(id);
      element.focus();
    }
  }
}

function resetFields(whichform) {
  for (var i=0; i<whichform.elements.length; i++) {
    var element = whichform.elements[i];
    if (element.type == "submit") continue;
	if (element.type == "radio") continue;				//加上此句,不对单选按钮进行处理
    if (!element.defaultValue) continue;
    element.onfocus = function() {
    if (this.value == this.defaultValue) {
      this.value = "";
     }
    }
    element.onblur = function() {
      if (this.value == "") {
        this.value = this.defaultValue;
      }
    }
  }
}

function validateForm(whichform) {
  for (var i=0; i<whichform.elements.length; i++) {
    var element = whichform.elements[i];
    if (element.className.indexOf("required") != -1) {
      if (!isFilled(element)) {
        alert("请输入您想搜索的产品的名称");
        return false;
      }
    }
  }
  return true;
}

function isFilled(field) {
  if (field.value.length < 1 || field.value == field.defaultValue) {
    return false;
  } else {
    return true;
  }
}

function prepareForms() {
  for (var i=0; i<document.forms.length; i++) {
    var thisform = document.forms[i];
    resetFields(thisform);
    thisform.onsubmit = function() {
      return validateForm(this);
    }
  }
}
addLoadEvent(focusLabels);
addLoadEvent(prepareForms);


//搜索显示商品提示目录
function _showMenu(){
	var g_name = $('#one-ddheader');
	
	//如果搜索的字符串为空,就清空
	if(g_name.val() == ''){
		$('#goods_munu_list').hide();
		return;
	}
	
	$.ajax({
      	  type: "POST",
		  dataType: "json",
	      url: "search.php?",
	      data:'&ajax=1&goods_name='+g_name.val(),
	      success: function(msg){
		 	 if(msg.status==false){
		 	 	return;
	  		 }else if(msg.status==null){
 		 		$('#goods_munu_list').hide();
 		 		return;
	  		 }
			
			$('#goods_munu_list').html(msg.info);
			$('#goods_munu_list').show();
			$('#one-ddcontent').show();return;
		  }
      });
}

//隐藏搜索提示目录列表
function _hideMenu(){
	$('#goods_munu_list').hide();
}


//search goods script
var DDSPEED = 10;
var DDTIMER = 15;
function ddMenu(id,d){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');

  clearInterval(c.timer);
  if(d == 1){
    clearTimeout(h.timer);
  
    if(c.maxh && c.maxh <= c.offsetHeight){return}
    else if(!c.maxh){
      c.style.display = 'block';
      c.style.height = 'auto';
      c.maxh = c.offsetHeight;
      c.style.height = '0px';
      
    }
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }else{
    h.timer = setTimeout(function(){ddCollapse(c)},50);
  }
}

function ddCollapse(c){
  c.timer = setInterval(function(){ddSlide(c,-1)},DDTIMER);
}

function cancelHide(id){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
}

function ddSlide(c,d){
  var currh = c.offsetHeight;
  var dist;
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DDSPEED));
  }else{
    dist = (Math.round(currh / DDSPEED));
  }
  if(dist <= 1 && d == 1){
    dist = 1;
  }
  c.style.height = currh + (dist * d) + 'px';
  c.style.opacity = currh / c.maxh;
  c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
  if((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1)){
    clearInterval(c.timer);
  }
}


//show and hide goods_list menu
function SDMenu(id) {
	if (!document.getElementById || !document.getElementsByTagName)
		return false;
	this.menu = document.getElementById(id);
	this.submenus = this.menu.getElementsByTagName("div");
	this.remember = true;
	this.speed = 3;
	this.markCurrent = true;
	this.oneSmOnly = false;
}
SDMenu.prototype.init = function() {
	var mainInstance = this;
	for (var i = 0; i < this.submenus.length; i++)
		this.submenus[i].getElementsByTagName("span")[0].onclick = function() {
			mainInstance.toggleMenu(this.parentNode);
		};
	if (this.markCurrent) {
		var links = this.menu.getElementsByTagName("a");
		for (var i = 0; i < links.length; i++)
			if (links[i].href == document.location.href) {
				links[i].className = "current";
				break;
			}
	}
	/*
	if (this.remember) {
		var regex = new RegExp("sdmenu_" + encodeURIComponent(this.menu.id) + "=([01]+)");
		var match = regex.exec(document.cookie);
		if (match) {
			var states = match[1].split("");
			for (var i = 0; i < states.length; i++)
				this.submenus[i].className = (states[i] == 0 ? "collapsed" : "");
		}
	}*/
};
SDMenu.prototype.toggleMenu = function(submenu) {
	if (submenu.className == "collapsed")
		this.expandMenu(submenu);
	else
		this.collapseMenu(submenu);
};
SDMenu.prototype.expandMenu = function(submenu) {
	var fullHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
	var links = submenu.getElementsByTagName("a");
	for (var i = 0; i < links.length; i++)
		fullHeight += links[i].offsetHeight;
	var moveBy = Math.round(this.speed * links.length);
	
	var mainInstance = this;
	var intId = setInterval(function() {
		var curHeight = submenu.offsetHeight;
		var newHeight = curHeight + moveBy;
		if (newHeight < fullHeight)
			submenu.style.height = newHeight + "px";
		else {
			clearInterval(intId);
			submenu.style.height = "";
			submenu.className = "";
			mainInstance.memorize();
		}
	}, 30);
	this.collapseOthers(submenu);
};
SDMenu.prototype.collapseMenu = function(submenu) {
	var minHeight = submenu.getElementsByTagName("span")[0].offsetHeight;
	var moveBy = Math.round(this.speed * submenu.getElementsByTagName("a").length);
	var mainInstance = this;
	var intId = setInterval(function() {
		var curHeight = submenu.offsetHeight;
		var newHeight = curHeight - moveBy;
		if (newHeight > minHeight)
			submenu.style.height = newHeight + "px";
		else {
			clearInterval(intId);
			submenu.style.height = "";
			submenu.className = "collapsed";
			mainInstance.memorize();
		}
	}, 30);
};
SDMenu.prototype.collapseOthers = function(submenu) {
	if (this.oneSmOnly) {
		for (var i = 0; i < this.submenus.length; i++)
			if (this.submenus[i] != submenu && this.submenus[i].className != "collapsed")
				this.collapseMenu(this.submenus[i]);
	}
};
SDMenu.prototype.expandAll = function() {
	var oldOneSmOnly = this.oneSmOnly;
	this.oneSmOnly = false;
	for (var i = 0; i < this.submenus.length; i++)
		if (this.submenus[i].className == "collapsed")
			this.expandMenu(this.submenus[i]);
	this.oneSmOnly = oldOneSmOnly;
};
SDMenu.prototype.collapseAll = function() {
	for (var i = 0; i < this.submenus.length; i++)
		if (this.submenus[i].className != "collapsed")
			this.collapseMenu(this.submenus[i]);
};
SDMenu.prototype.memorize = function() {
	if (this.remember) {
		var states = new Array();
		for (var i = 0; i < this.submenus.length; i++)
			states.push(this.submenus[i].className == "collapsed" ? 0 : 1);
		var d = new Date();
		d.setTime(d.getTime() + (30 * 24 * 60 * 60 * 1000));
		document.cookie = "sdmenu_" + encodeURIComponent(this.menu.id) + "=" + states.join("") + "; expires=" + d.toGMTString() + "; path=/";
	}
};



//goods_detail pic auotab
function initAutoFocus(){
    autoShiftFocus("focustab", "a", "focuscont", "div");
    function autoShiftFocus(tabsid, tabstagname, contentid, contenttagname){
        var tabs = document.getElementById(tabsid).getElementsByTagName(tabstagname);
        tabs[0].className = "act";
        var contents = document.getElementById(contentid).getElementsByTagName(contenttagname);
        contents[0].style.display = "block";
        var cur_index_num = 0;
        if (tabstagname == "a") {
            for (var a = tabs.length - 1; a >= 0; a--) {
                tabs[a].onclick = function(){
                    return false;
                }
            }
        }
        function getContentsArr(){
            var contentsarr = new Array();
            for (var z=0;z<contents.length; z++) {
                if(contents[z].id.indexOf(contentid)>=0){
                    contentsarr[contentsarr.length] = contents[z];
                }                
            }
             return contentsarr;
        }
        var contarr=getContentsArr();
        function autoSlideTopic(){
            for (var a = tabs.length - 1; a >= 0; a--) {
                tabs[a].className = "";
            }
            
            for (var b = contarr.length - 1; b >= 0; b--) {                
                    contarr[b].style.display = "none";
            }
            contarr[cur_index_num].style.display = "block";
            
            tabs[cur_index_num].className = "act";
            var total_num = tabs.length;
            cur_index_num++;
            if (cur_index_num >= total_num) {
                cur_index_num = 0;
            }
        }
        for (var c = tabs.length - 1; c >= 0; c--) {
            tabs[c].onmouseover = function(){
                changeTabs(this.name);
            }
            tabs[c].onmouseout = function(){
                clearInterval(autoSlideTopic);
            }
        }
        function changeTabs(num){
            var thenum = num - 1;
            for (var n = tabs.length - 1; n >= 0; n--) {
                tabs[n].className = "";
            }
            tabs[thenum].className = "act";
            for (var m = contarr.length - 1; m >= 0; m--) {
                contarr[m].style.display = "none";
            }
            contarr[thenum].style.display = "block";
            cur_index_num = thenum;
        }
        
    }
    
}



//goods_detail show assemble
slider.names = new Array();
function slider()
{
	this.id = slider.names.length;
	slider.names[this.id] = this;
	this.target	 = document.getElementById(arguments[0]);	//第一个参数：被操作div的id
	this.direction = arguments[1];//第二个参数：div弹出的方向
	this.height = arguments[2];//第三个参数：div的高度
	this.width = arguments[3];//第四个参数：div的宽度
	this.step = arguments[4];//第五个参数：希望动作分解为几步完成
	this.timer = 10 * arguments[5];//第六个参数：每个动作的间隔时间，10ms为一个单位
	this.startopa = arguments[6];//第七个参数：div开始的透明度
	this.sparent = this.target.parentNode;//获取操作div的父容器
	this.intervalid = null;//循环定时的id
	this.i = 0;//循环的计数器
	this.status = 0;//slider层的状态：0-可以展开；1-不可以展开
	this.target.style.display = "none";//先将div隐去
	return this;
}
slider.prototype.initialize = function()
{
	this.sparent.style.overflow = "hidden";//设置父容器overflow
	this.target.style.width = Number(this.width) + 'px';//设置目标div的宽度
	this.target.style.height = Number(this.height) + 'px';//设置目标div的高度
	this.target.style.position = "";//设置目标div的定位方式
	this.target.style.display = "";//设置目标div的显示方式
	this.target.style.filter = 'Alpha(opacity=' + Number(this.startopa) + ')';//设置目标div的透明度为初始透明度
	this.target.style.overflow = "hidden";//设置overflow
	switch(this.direction)//根据弹出方向设定div的margin
	{
		case 1://left to right
			this.target.style.marginLeft = "-" + this.width + "px";
			break;
		case 2://top to bottom
			this.target.style.marginTop = "-" + this.height + "px";
			break;
		case 3://right to left
			this.target.style.marginRight = "-" + this.width + "px";
			break;
	}
}
slider.prototype.show = function()
{
	if (this.status==0)//检查状态是否已经展开
	{
		this.initialize();//操作div及其父容器的初始化
		this.intervalid = window.setInterval("slider.names["+this.id+"].cycle()",this.timer);//设置动作循环
	}
}
slider.prototype.hide = function()
{
	if (this.status==1)//检查状态是否已经展开
	{
		this.intervalid = window.setInterval("slider.names["+this.id+"].decycle()",this.timer);//设置动作循环
	}
}
slider.prototype.cycle = function()	//单步循环动作
{
	var opa = this.target.style.filter.split("=")[1].split(")")[0]//获取目标div的透明度数值
	var opastep = Math.round(((100 - Number(opa)) / this.step)+2.5);//计算每步增加的透明度
	var nopa = Number(opa) + Number(opastep);//当前透明度
	if (nopa>100){this.target.style.filter = 'Alpha(opacity=100)';}else{this.target.style.filter = 'Alpha(opacity=' + String(nopa) + ')';}//给div透明度赋值
	switch(this.direction)//根据弹出方向计算和设定div的动作
	{
		case 1:		//left to right
			var opx = this.target.style.marginLeft.split("px")[0];
			var pxstep = Math.round((this.width / this.step)+0.5);
			var npx = Number(opx) + Number(pxstep);
			if (npx>0){this.target.style.marginLeft = '0px';}else{this.target.style.marginLeft = String(npx) + 'px';}
			break;
		case 2:		//top to bottom
			var opx = this.target.style.marginTop.split("px")[0];
			var pxstep = Math.round((this.height / this.step)+0.5);
			var npx = Number(opx) + Number(pxstep);
			if (npx>0){this.target.style.marginTop = '0px';}else{this.target.style.marginTop = String(npx) + 'px';}
			break;
		case 3:		//right to left
			var opx = this.target.style.marginRight.split("px")[0];
			var pxstep = Math.round((this.width / this.step)+0.5);
			var npx = Number(opx) + Number(pxstep);
			if (npx>0){this.target.style.marginRight = '0px';}else{this.target.style.marginRight = String(npx) + 'px';}
			break;
	}
	this.i++	//计数器+1
	if (this.i>(this.step-1)){window.clearInterval(this.intervalid);this.i=0;this.status=1;}	//循环完毕，清除循环定时
}
slider.prototype.decycle = function()	//单步循环动作
{
	var opa = this.target.style.filter.split("=")[1].split(")")[0]//获取目标div的透明度数值
	var opastep = Math.round(((100 - Number(opa)) / this.step)+2.5)*2;//计算每步增加的透明度
	var nopa = Number(opa) - Number(opastep);//当前透明度
	if (nopa<this.startopa){this.target.style.filter = 'Alpha(opacity=' + this.startopa + ')';}else{this.target.style.filter = 'Alpha(opacity=' + String(nopa) + ')';}//给div透明度赋值
	switch(this.direction)//根据弹出方向计算和设定div的动作
	{
		case 1:		//left to right
			var opx = this.target.style.marginLeft.split("px")[0];
			var pxstep = Math.round((this.width / Math.round(this.step*0.5))+0.5);
			var npx = Number(opx) - Number(pxstep);
			if (Math.abs(npx)>this.width+2){this.target.style.marginLeft = '-' + this.width + 'px';}else{this.target.style.marginLeft = String(npx) + 'px';}
			break;
		case 2:		//top to bottom
			var opx = this.target.style.marginTop.split("px")[0];
			var pxstep = Math.round((this.height / Math.round(this.step*0.5))+0.5);
			var npx = Number(opx) - Number(pxstep);
			if (Math.abs(npx)>this.height+2){this.target.style.marginTop = '-' + this.height + 'px';}else{this.target.style.marginTop = String(npx) + 'px';}
			break;
		case 3:		//right to left
			var opx = this.target.style.marginRight.split("px")[0];
			var pxstep = Math.round((this.width / Math.round(this.step*0.5))+0.5);
			var npx = Number(opx) - Number(pxstep);
			if (Math.abs(npx)>this.width+2){this.target.style.marginRight = '-' + this.width + 'px';}else{this.target.style.marginRight = String(npx) + 'px';}
			break;
	}
	this.i++	//计数器+1
	if (this.i>(Math.round(this.step*0.5)-1)){window.clearInterval(this.intervalid);this.i=0;this.status=0;this.target.style.display = "none";}	//循环完毕，清除循环定时
}

String.prototype.trim = function()
{
   return this.replace(/(^\s+)|\s+$/g,"");
}

//关于如何使用这个代码的说明：
//上面的代码可以封装成为一个单独的js文件，然后包含在页面当中
//然后使用下面的代码进行slider初始化，一个页面可以使用多个slider，下面的代码必须在页面底部</body>之前，否则可能会报错

//pup windows script
function _(id) { 
  return document.getElementById(id); 
} 

var ti = null; 
function loading(){ 
    var tmd = 0; 
    var x1 = document.documentElement.clientWidth;  
    var y1 = document.body.offsetHeight; 
    var y2=document.documentElement.clientHeight;//整个页面的高度 
    with(_("div1")){ 
       style.width=x1+"px"; 
       if(y2>y1){ 
       style.height=y2+"px"; 
       }else{ 
       style.height=y1+"px"; 
       } 
       style.overflowX="hidden"; 
       style.overflowY="hidden"; 
       style.visibility="visible"; 
    } 
    _("div1").style.left=0; 
    _("div1").style.filter='Alpha(Opacity=0)'; 
    //document.body.style.overflowX="hidden"; 
    //document.body.style.overflowY="hidden"; 
    _("div2").style.top=parseInt(document.documentElement.scrollTop)+((document.documentElement.clientHeight-310)/2)+"px";  
    _("div2").style.left="50%"; 
    _("div2").style.marginLeft="-336px" 
    _("div2").style.visibility="visible";
    ti = setInterval("hei()",10); 
} 
var x = 0; 
function hei(){ 
    x+=10; 
    if(x<31){ 
        if(document.all){ 
            _("div1").style.filter='Alpha(Opacity='+x+')'; 
        }else{ 
            _("div1").style.opacity=""+x/100+"";     
        } 
    } 
} 
function unload(){ 
	_("div1").style.visibility="hidden"; 
	_("div2").style.visibility="hidden"; 
	clearInterval(ti); 
	x=0; 
} 




$(function(){
		   
	$('.dispat img').mouseover(function(){
		$(this).css('cursor' , 'pointer');
	});
	
	$('.dispat img').click(function(){
		window.location = $(this).attr('title');						
	});
	
	//商品销售排行榜，默认选中全部
	$('.nav_txt1').click();
});

/**
 * 切换选项卡
 */
function changeTab(name,cursel,n){
	for(i=1;i<=n;i++){
	  var menu = $('.'+name+i);
	  var con = $('#'+name+'_'+i);

	  menu.addClass((i == cursel) ? 'curr' : 'none');
	  menu.removeClass((i == cursel) ? 'none' : 'curr');
	  con.css('display', (i == cursel)? 'block' : 'none');
	}
}









