jQuery.getQueryString = function (name) {
    var svalue = location.search.match(new RegExp("[\?\&]" + name + "=([^\&]*)(\&?)", "i"));
    return svalue ? svalue[1] : svalue;
}
jQuery.fn.Select = function (settings) {
    var options = jQuery.extend({
        "eventType": "mouseover"
    }, settings);

    return this.each(function () {
        var t = $(this),
		    valuebar = t.find("div.select-valuebar").eq(0),
	        optionsbar = t.find("div.select-optionsbar").eq(0), //.slideUp(0).css("visibility", "visible")
            arg = jQuery.getQueryString(options.startVal) || options.initvalue,
			timer = null,
			currI = null,
			closed = false;
        
        var _lis = optionsbar.find("li").not(".noStock").hover(
		    function () {
				var _t = $(this);
				if (!_t.hasClass("click")) $(this).addClass("hover") 
		    }, 
		    function () { 
			   var _t = $(this);
			   if (!_t.hasClass("click")) $(this).removeClass("hover") 
		    }
		)
		   .mousedown(function (event) { event.stopPropagation(); })
           .click(function () {
               if (!options.hasvalfun) {
                   if (options.callback) {
                       if (options.callback.call(null, this.getAttribute("sel_value")) === null) return false;
                   }
                   valuebar.find("em:eq(0)").html(this.innerHTML);

               } else {
                   if (options.callback) {
                       if (options.callback.call(null, $(this), valuebar.find("em:eq(0)")) === null) return false;
                   };
               }
			   var _t = this;
			   optionsbar.find("li").removeClass("click").removeClass("hover");
			   $(_t).addClass("click");
			   /*each(function (i, n){
				   if(_t == this){
					  $(_t).addClass("click");   
				   }else{
					  $(n).removeClass("click");
				   }
				});*/
               //if (currI) currI.removeClass();
               //currI = $(this).addClass("click");
               if (!options.notclosed) __mouseout();
           });
		   if(arg && arg != "")_lis.each(__each);

        function __each(i, n){
               //if (arg == "" || !arg) return false;
               var _self = $(n);
               if (_self.attr("sel_value") == arg) {
                   if (options.startVal) _self.unbind("click").click(__mouseout);
                   valuebar.find("em:eq(0)").html(_self.text());
                   currI = _self.addClass("click");
                   return false;
               };	
		}
		
        function __mouseover() {
            valuebar.addClass("hover");
        }
        function __open() {
            valuebar.removeClass("hover").addClass("open");
            if (!closed) { optionsbar.show(); closed = !closed; }
            else { optionsbar.hide(); valuebar.removeClass("open").addClass("hover"); closed = !closed; };
            //timer = setTimeout(function () {}, 200)
        }
        function __mouseout() {
            //clearTimeout(timer);
            valuebar.removeClass("hover").removeClass("open");
            optionsbar.hide();
            closed = false;
        }
        t.hover(__mouseover, __mouseout).mousedown(__open);
		t.data("iscontrol", "true");
    });

};

$.numbtn = {
  active : false,
  init : function (){
	$.num.div = $("<div />").addClass("numTis")
	.css({
	  "position" : "absolute",
	  "border" : "1px solid #FFCC00"	
	})
	.hide()
	.appendTo(document.body);
  }
}
$.fn.numbtn = function(settings){
 settings = jQuery.extend({
	 style : "click",//默认点击样式
	 tt:1,
	 max   : 10 //默认最大值
 },settings);
 return this.each(function (){
	var t = $(this), text = t.find("input:eq(0)"), up = t.find("s:eq(0)"), down = t.find("s:eq(1)");
	function _mouseout(){$(this).removeClass("click");}
	function _click_up(){
	  $(this).removeClass("click");
	  var val = text.val() << 0;
	  val = val ++ >= settings.max? settings.max : val;
	  text.val(val);
	  if(settings.callback)settings.callback.call(text[0], val);
	}
	function _click_down(){
	  $(this).removeClass("click");
	  var val = text.val() << 0;
	  val = val -- < 2? 1 : val;
	  text.val(val); 
	  if(settings.callback)settings.callback.call(text[0], val);
	}
	function _mousedown(){$(this).addClass(settings.style);}
	up.mouseout(_mouseout).mousedown(_mousedown).click(_click_up);
	down.mouseout(_mouseout).mousedown(_mousedown).click(_click_down);
	text.keyup(function(){
	  var val = $(this).val();
	  val = val.replace(/\D/g,"");
	  if(val == "")val = 1;
	  val = val << 0;
	  if(val < 1)val = 1;
	  if(val > settings.max)val = settings.max;
	  if(settings.callback)settings.callback.call(text[0], val);
	  $(this).val(val);
	});
 });
};

$.fn.zoom = function (settings) {
    var options = $.extend({
        width: 300,
        height: 300,
        top: 0,
        left: 0
    }, settings);
    var t = this, img, div, bar, imgsucess = false;
    var _w = t.width(),
       _h = t.height(),
	   _x = t.offset().left,
	   _y = t.offset().top,
	   minx, miny, maxx, maxy;

    function initialize() {
        img = new Image();
        bar = $("<div />").addClass("jqZoomPup").css({ top: 0, left: 0 }).hide();
        div = $("<div />").addClass("jqZoomWindow").css({
            //"backgroundColor": "#827157", 
            "color": "#333",
            textAlign: "center",
            lineHeight: options.height + "px",
            width: options.width,
            height: options.height
        }).html(" 正在加载放大图... ")
		.css({
		    //top: _y + options.top,
		    //left: _x + t.width() + options.left,
		    display: "none"
		}).appendTo(document.body);
        t.append(bar);
        //minx = bar.width() / 2 + 1;
        //miny = bar.height() / 2 + 1;
        //maxx = _w - minx;
        // maxy = _h - miny;

        t.find("a:eq(0)").click(_click);
    }
    function _hover() {
        //ishover = true;
        if (!imgsucess) {
            img.src = t.find("a:eq(0)").attr("href");
            img.onload = _imgload;
            imgsucess = true;
        }
        _pos_init_div();
        bar.show();
        //if (t.find("a:eq(0)").attr("href") != img.src) img.src = t.find("a:eq(0)").attr("href"); //侦测图片变化
    };

    function _pos_init_div() {
        _x = t.offset().left;
        _y = t.offset().top;
        div.css({
            top: _y + options.top,
            left: _x + t.width() + options.left
        }).show();
    }

    function _hout() {
        div.hide();
        bar.hide();
    }
    function _move(e) {
        var x = e.pageX - _x, y = e.pageY - _y;
        var left, top, _left, _top;
        left = x < minx ? minx : x > maxx ? maxx : x;
        top = y < miny ? miny : y > maxy ? maxy : y;
        _left = options.width * .5 - left / _w * img.width;
        _top = options.height * .5 - top / _h * img.height;
        bar.css({ 'left': left - minx, 'top': top - miny });
        _left = Math.round(_left);
        _top = Math.round(_top);
        if (imgsucess) {
            div.css("backgroundPosition", _left + "px " + _top + "px")//.css("color", "red");
            //div.html("x:" + _left + "|y:" + top +","+_y +","+y + "," + miny + "," + _h)
        } else {
            _pos_init_div();
            bar.show();
            if (typeof img.onload == "object") {
                img.src = t.find("a:eq(0)").attr("href");
                img.onload = _imgload;
                imgsucess = true;
            }
        }
    }
    function _imgload() {

        if (img.width < options.width || img.height < options.height) {
            t.unbind();
        } else {
            bar.css({
                width: _w * options.width / img.width,
                height: _h * options.height / img.height
            });
            div.css({
                "backgroundImage": "url(" + img.src + ")",
                "backgroundColor": "white",
                "backgroundPosition": "0 0"
            }).html("");
            minx = bar.width() / 2 + 1;
            miny = bar.height() / 2 + 1;
            maxx = _w - minx;
            maxy = _h - miny;
        }
    }
    function _click() { return false; }
    initialize();
    t.mousemove(_move);
    return this.hover(_hover, _hout);
}

//jquery.Marquee
$.fn.Marquee = function () {
    var t = $(this),
        warp = t.children("ul:eq(0)"),
	    prev = t.children(".prev"),
	    next = t.children(".next"),
        step = warp.height(),
        len = warp.children().length,
        starti = 0,
	    fx = 1,
        timer;
    if (len <= 1) return this;

    for(var _i = 0; _i<len; _i++){
	  warp.append(warp.children().eq(_i).clone(true));	
	}

    function __fun() {
        warp.stop(true).animate({
            "scrollTop": (starti + fx) * step
        }, "slow", function () {
			starti += fx ;
            if (fx>0 && this.scrollTop >= len * step) { this.scrollTop = 0; starti = 0; }
			if (fx<0 && this.scrollTop == 0){ this.scrollTop = len * step; starti = len; }
        });

    }
	
    function __mouseover() {
        clearInterval(timer);
        timer = null;
    }
    function __mouseout() {
        if (timer == null)timer = setInterval(__fun, 3000);
    }
	prev
	.mousedown(function (){
	  $(this).css( "marginTop" , 1);	
	})
	.click(function (){
	  $(this).css("margin",0);
	  fx = -1;
	  clearInterval(timer);
	  __fun();
	});
	next
	.mousedown(function (){
	  $(this).css("marginTop" , 1);	
	})
	.click(function (){
	  $(this).css("margin",0);
	  fx = 1;
	  clearInterval(timer);
	  __fun();
	});
	
    timer = setInterval(__fun, 2000);
    warp.scrollTop(0);
    return this.hover(__mouseover, __mouseout);
}

//Focus

function Focus(options){
	var t = this, 
	    div = options.div.hover(function (){
		  t.stop();	
		}, 
		function (){
		  if(!t.ispause)t.play();
		});
	div.css({width: options.width+"px",height : options.height+"px"});
	t.time = options.time * 1000;
	t.pics = div.find("li");
	//t.pics.not("li:eq(0)").css("opacity", 0);
	t.zIndex = this.pics.length;
	t.autoTimer = null;
	t.currI = 0;
	t.ispause = false;
	var nums = $("<div/>").addClass("nums");
	/*var pause = $("<div>Pause</div>").addClass("pause")
	.click(function (){
	  t.ispause = !t.ispause;
	  if(t.ispause)$(this).html("Play").addClass("play");
	  else $(this).html("Pause").removeClass("play");
	});*/
	function __span_event(){
	  t.__change(this.getAttribute("_focus_i") << 0);	
	}
    t.pics.each(function (i, n){
	  var span = $("<span>"+ (i+1) +"</span>").attr("_focus_i", i).appendTo(nums)
	      .mouseover(__span_event)
		  .addClass(i==0?"selected" : "");
	  $(this).css("zIndex", t.zIndex-i);
    });
	t.tabs = nums
	  .appendTo(div)
	  //.append(pause) 
	  .find("span");
	nums = null;
	t.play();
}
jQuery.extend(Focus.prototype, {
	__change : function (i){
		var t = this;
		if(i == t.currI)return;
		if(document.all){//blendTrans revealTrans(duration=0.5,transition=23)
		 var _currpic = t.pics.eq(i).css({"zIndex": ++t.zIndex, "filter":"blendTrans(duration=0.3)"});
		 _currpic.hide();
		 _currpic[0].filters[0].apply();
		 _currpic.show();
		 _currpic[0].filters[0].play();
		}else{
		 t.pics.eq(t.currI).clearfx();
		 t.pics.eq(i).clearfx().css({"zIndex": t.zIndex++, "opacity": "0"})
		 .animate({"opacity": 1}, 500, function (){
			$(this).css("opacity","");
		 })
		}
		t.setSpanStyle(i);
		t.currI = i;
	},
	setSpanStyle : function (i){
		this.tabs.eq(this.currI).removeClass();
	    this.tabs.eq(i).addClass("selected");
	},
	__autoPlay : function (){
		var i = this.currI;
		this.__change(++i >= this.pics.length ? 0 : i );
		
	},
	play : function (){
		var t = this;
	    this.autoTimer = setInterval(function(){t.__autoPlay();},t.time);	
    },
	stop : function (){
	  	clearInterval(this.autoTimer);
	}
});

//二级菜单
function showselected(){
	if (typeof One_CateID != "undefined")$('#nav li[cid="' + One_CateID + '"]').addClass('selected');  	 
  }
  
function clearselected(){
	 $('#nav li').removeClass('selected'); 
}
 
$.fn.nav = function (divs){
  
return this.each(function (){
	  var t = $(this), timer = null;
	  var cid = t.attr("cid");
	  var div = divs.filter("[cid="+ cid +"]");
	  
	function _init(){
	  div.css({
		top : t.position().top + t.height(),
		left: t.position().left
	  })
	  .hover(_cleartimer, function (){
		  _setTimer();
	      showselected();
	  })
	  .show()
	  .slideUp(0);
	}
	
	function _cleartimer(){
	  clearTimeout(timer);
	  //clearTimeout(__timer);
	  timer = null;
	  _submenushow();
	}
	
	function _setTimer(){
	  if(timer === null){
		 timer = setTimeout(_submenuhide, 50);  
	  }	
	}
	
	function _submenuhide(){
	  div.slideUp("fast");	
	  t.removeClass("hover");
	}
	
	function _submenushow(){
	  div.slideDown("fast");
	  t.addClass("hover");
	  clearselected();
	}
	
	if(div.length == 0){
		t.hover(function (){
		  clearselected();
	   }, function(){
		 showselected(); 
	   });
	 }else{
		 _init();
		 t.hover(function (){
		   _cleartimer();
		   clearselected()
	     }, function (){
			_setTimer();
		    showselected();		
		 });
     }	
  });
}

$.fn.resize = function (w, h) {
    return this.each(function () {
        $(this).load(function () {
            DrawImage(this, w, h);
        });
    });
}

function DrawImage(ImgD, iwidth, iheight) {
    var image = new Image();
    image.src = ImgD.src;

    if (image.width > iwidth) {
        ImgD.width = iwidth;
        ImgD.height = (image.height * iwidth) / image.width;
    }
  
    /*if (image.width > 0 && image.height > 0) {

        if (image.width / image.height >= iwidth / iheight) {
            if (image.width > iwidth) {
                ImgD.width = iwidth;
                ImgD.height = (image.height * iwidth) / image.width;
            } else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }
        }
        else {
            if (image.height > iheight) {
                ImgD.height = iheight;
                ImgD.width = (image.width * iheight) / image.height;
            } else {
                ImgD.width = image.width;
                ImgD.height = image.height;
            }
        }
    }*/
} 


/*$.fn.submenu = function (div) {
	if(!div){
		return this.hover(function (){
		$('#nav li').removeClass('selected');
	  }, function(){
		 $('#nav li[cid="' + One_CateID + '"]').addClass('selected');  
	  });;
	}	
	var t = this, timer = null;
	
	function _init(){
	  div.css({
		top : t.position().top + t.height(),
		left: t.position().left
	  })
	  .hover(_cleartimer, _setTimer)
	  .show()
	  .slideUp(0);
	}
	
	function _cleartimer(){
	  clearInterval(timer);
	  timer = null;
	  _submenushow();
	}
	
	function _setTimer(){
	  if(timer === null){
		 timer = setTimeout(_submenuhide, 150);  
	  }	
	}
	
	function _submenuhide(){
	  div.slideUp("fast");	
	  t.removeClass("hover");
	  $('#nav li[cid="' + One_CateID + '"]').addClass('selected');	
	}
	
	function _submenushow(){
	  div.slideDown("fast");
	  t.addClass("hover");
	}
	_init();
	return this.hover(function (){
		_cleartimer();
		$('#nav li').removeClass('selected');
	}, _setTimer);
};*/
