/*
* @author: hyj
* 
* EXP:
* hyj,JS框架
*/
jQuery.extend({
  getCookie : function(sName) {
    var aCookie = document.cookie.split("; ");
    for (var i=0; i < aCookie.length; i++){
      var aCrumb = aCookie[i].split("=");
      if (sName == aCrumb[0]) return decodeURIComponent(aCrumb[1]);
    }
    return '';
  },
  setCookie : function(sName, sValue, sExpires) {
    var sCookie = sName + "=" + encodeURIComponent(sValue);
    if (sExpires != null) sCookie += "; expires=" + sExpires;
    document.cookie = sCookie;
  },
  removeCookie : function(sName) {
    document.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
  }
});
function AjaxYbl(methd,url,param,callback) {
        $.ajax({
            type:methd,
            url: url,
            data:param,
            dataType:'json',
            success: function(msg) {
                if (        
                        $.isFunction(callback))
                        callback(msg);
                    },
             error:function(msg)
                    {                    
                         if ($.isFunction(callback))
                         callback(msg);
                    }
        });
     }
     
   var zjx=new Object();
   zjx.ui={
   rollAd:function(id, navPath, picPath, navCurrClass ){
		var index = 1;
		var adNav = $(id).find(navPath);
		var adPictures = $(id).find(picPath);
		var prePic = null;
		var currPic = null;
		var preNav = adNav.eq(0).addClass( navCurrClass );
		var currNav = null;
		var autoPlayTimeId = null;
		var continuePlayTimeId = null;
		var picWidth = adPictures[0].clientWidth;
		var jumpInterval = 2000;
		var watchingTime = 1000;
		var moveSpeed = 100;
		var rollEffectName = "slide";
		var picWrapper = adPictures.parent();
		var keepRoll = true;
		this.setRollEffect = function( effect ){ rollEffectName = effect };
		this.setWatchingTime = function( time ){ watchingTime = time };
		this.setJumpInterval = function( time ){ jumpInterval = time };
		this.setMoveSpeed = function( speed ){ moveSpeed = speed };
		this.isKeepRoll = function ( boolean ){ keepRoll = boolean };
		function rollEffect(){
			switch( rollEffectName ){
				case "fade":
					prePic = adPictures.hide().eq(0).show();
					return makeFade();
					break;
				case "slide":
					return makeSlide();
					break;
				case "flashing":
					prePic = adPictures.hide().eq(0).show();
					return makeFlashing();
					break;
				default:
					prePic = adPictures.hide().eq(0).show()
					return makeFade( index );
					break;
			};
		};
		function makeFade(){
			currPic = adPictures.eq( index );
			prePic.css("display", "none" );
			currPic.fadeIn(500 ).css({ display:"block" });
			currNav = adNav.eq( index ).addClass( navCurrClass );
			preNav.removeClass( navCurrClass );
			prePic = currPic;
			preNav = currNav;
		};
		function makeSlide(){
			picWrapper.animate({ marginLeft : -1*index*picWidth+"px" }, moveSpeed );
			currNav = adNav.eq( index ).addClass( navCurrClass );
			preNav.removeClass( navCurrClass );
			preNav = currNav;
		};
		function makeFlashing(){
			currPic = adPictures.eq( index );
			prePic.hide();
			currPic.show();
			currNav = adNav.eq( index ).addClass( navCurrClass );
			preNav.removeClass( navCurrClass );
			prePic = currPic;
			preNav = currNav;
		};
		function continuePlay(){
			continuePlayTimeId = setTimeout( function(){
					autoPlayTimeId = setInterval( function(){					
						index++;
						if(index == adNav.length){ index = 0; }
						rollEffect();
					} , jumpInterval);
				
			}, watchingTime );
		};
		this.autoPlay = function(){
			autoPlayTimeId = setInterval( function(){					
				rollEffect();
				index++;
				if( index == adNav.length ){ index = 0; }
			} , jumpInterval );	
		};
		this.initClick = function(){
			adNav.click( function(){
					if( autoPlayTimeId ){ clearInterval( autoPlayTimeId );}
					if( continuePlayTimeId ){ clearTimeout( continuePlayTimeId );}
					index = adNav.index( this );
					if( $( this ).attr( "class" ) != navCurrClass ){
						rollEffect();
					};
					if ( keepRoll ) { continuePlay() };
				});
		};
		this.initHover = function(){
			adPictures.hover( function(){
					if( autoPlayTimeId ){ clearInterval( autoPlayTimeId );}
					if( continuePlayTimeId ){ clearTimeout( continuePlayTimeId );}
				},
				function(){
					index = adPictures.index( this );
					if ( keepRoll ) { continuePlay() };
				});
		};
	},
   foldMenu:function(parentId,level1TagName){
		var _parentId=parentId;
		var _level1TagName=level1TagName;
		$("#"+_parentId).find("li[class!=selected]").find(_level1TagName).next().css("display","none");
		$("#"+_parentId).find(_level1TagName).click(
			function(){
				if($(this).next().children().html()!=null){
					if($(this).parent().attr("class")=="selected"){
						$(this).next().slideUp(200);
						$(this).parent().removeAttr("class");
					}else{
						$(this).next().slideDown(200);
						$(this).parent().addClass("selected");
					}
				}
			}
		);
	},
	storeTabChange:function(id,contentClass){
		
		var preNav=$("#"+id+" li");
		var preContent=$("#"+id).nextAll(contentClass);
		var currNav=null;
		var currContent=null;
		var idName=null;
		
		this.initClick=function(){
			$("#"+id+" li a").click(
				function(){
					if($(this).parent().attr("class")!="current" ){	
						preNav.removeClass("current");
						currNav=$(this).parent();
						if(currNav.hasClass("translate"))
						{
						    currNav.removeClass("translate");
					    }
					    else{
					        $("#translate_b").addClass("translate");
                       }
						currNav.addClass("current");
						preNav=currNav;
						idName=$(this).attr("href");
						preContent.css("display","none");
						preContent.removeClass("current");
						currContent=$("#"+id).nextAll(idName).css("display","block");
						preContent=currContent;
					}
					return false;
				}
			);
		}
		
		this.initContent=function(){
			$("#"+id).nextAll(contentClass).css("display","none");
			$($("#"+id).nextAll(contentClass)[0]).css("display","block");
		}
	}
}

   var pu = null;
  
  function GoodsListClick(type, val) {
   switch (type) {
        case 3: return pu.GetParam(val, null, true); break; 
        case 5: goChange(val); break; 
    }
}

function urlChange(url) {
    window.location.href = url;
}

function goChange(name) {
    var tmpObj = $("#" + name);
    var val = Number(tmpObj.attr("value"));
    if (/^\d*$/.test(val)) {
        var c = Number(tmpObj.attr("c"));
        if (val > 0 && (c - val) > -1) {
            var index = GoodsListClick(3, "page");
            if (val == index) {
                j().fn.msgBox("listbbcmsg", "已经是当前页了!!!");
            }
            else {
                var url = tmpObj.attr("href") + "&page=" + val;
                urlChange(url);
            }
        }
        else {
            j().fn.msgBox("listbbcmsg", "请确认页号范围后再次输入!!!");
        }
    }
    else {
        j().fn.msgBox("listbbcmsg", "请不要输入数字以外的字符!!!");
    }
}

function ToSizeSelect(obj)
{
    obj.href = "data/size_check/size_template.html";
}
function GoodsDescTran(status)
{
    if(status=='0')
    {
        $('#goods_desc_tran').css('display','');
        $('#gdoff').css('display','');
        $('#gdopen').css('display','none');
    }
    else 
    {
        $('#goods_desc_tran').css('display','none');
        $('#gdoff').css('display','none');
        $('#gdopen').css('display','');
    }
}

function rightAwayBuy(obj,spec_id)
{
if(!spec_id)spec_id=$('#goods_spec_id').val();
obj.href= "index.php?p=shopping&act=shopping_cart&goods_number="+document.getElementById('goods_num').value+"&spec_id="+spec_id+"&right_away_buy=ture";
}
function addToMallMessage(obj,spec_id)
{
obj.href= "index.php?p=shopping&act=NoStockMessage&spec_id="+spec_id;
}   
function addToFavorite(id)
{
  AjaxYbl('GET','index.php?p=yblmember&act=add_favorite&id=' + id + '&ret_url=' + encodeURIComponent(location.href)+'&ajax=1','',addToFavoriteRespond);
}
function addToFavoriteRespond(result){
  eval("result ="+result+";");
  if (!result.done)
  {
    if (result.msg == 'NO_LOGIN')
    {
      document.location.href = result.retval;
      return;
    }
  }
  if (result.msg.length > 0){
      alert(result.msg);
  }
}

function clickToGallery(event){
  var e = event||window.e;
  var elem = e.srcElement||e.target;
  if (elem.tagName == 'IMG' && elem.parentNode.tagName == 'A'){
    if (elem.parentNode.href != location.href){
      var gallerylist = $('#gallerylist');
      for (var i=0;i< gallerylist.children() .length ;i++ ){
        if (gallerylist.children()[i].tagName == 'LI'){
          $(gallerylist.children()[i]).removeClass("current");
        }
      }
      var thumbImg = elem.parentNode.href;
      var thumb_org = elem.getAttribute("org");
      var goodsPic = $('#goods_pic');
      goodsPic.attr('src', thumbImg);
      goodsPic.attr('org', thumb_org);
      if (goodsPic.parent().css('backgroundImage'))goodsPic.parent().css('backgroundImage','url(' + thumbImg + ')');
      $(elem.parentNode.parentNode).addClass("current");
    }
  }
  return false;
}
function storeCateHandle(listId)
{
    var CateNum =  $('#'+listId).find('dl');
    this.initCateList = function()
    {
            for(var i =1;i<CateNum.length;i++)
            {
               CateNum.eq(i).find("dt[class!='current']").next('dd').hide();
               CateNum.eq(i).find('dt span').attr('class','off');
               CateNum.eq(i).find('dt').children('span').bind('click',
               function(){
                    if($(this).attr('class') == 'on')
                    {
                        $(this).parent().nextAll('dd').hide();
                        $(this).removeClass("on");
                        $(this).addClass("off").attr('title','open');
                    }
                    else
                    {
                        $(this).parent().nextAll('dd').show();
                        $(this).removeClass("off");
                        $(this).addClass("on").attr('title','close');
                    }
               }
               );
            }
    };
}

/*拖拉效果api
*@author finder.hyj
*2009.08.27
*/
jQuery.fn.extend({
    drag: function(parent, limit, sFunc, mFunc, eFunc){
        return this.each(function(){
            var _this = parent ? this.parentNode : this;
            if(jQuery.browser.msie) {
                this.onselectstart = function(){return false};
                if(jQuery(_this).css("backgroundColor") == "transparent")_this.style.background = "#fff";
            }
            if(jQuery.browser.mozilla) this.style.MozUserSelect = 'none';
            this.style.cursor = "move";
            jQuery(this).mousedown(function(e){
                sFunc && sFunc.call(_this);
                var offset = jQuery(_this).offset();
                var screenX = e.screenX, screenY = e.screenY, w = _this.offsetWidth, h = _this.offsetHeight;
                jQuery(document).mousemove(function(e2){
                    if(jQuery.browser.msie && e.which != 1) return (jQuery(document).unbind(), eFunc.call(_this));
                     var curLeft = offset.left + e2.screenX - screenX, curTop = offset.top + e2.screenY - screenY;
                    if(!limit) limit = {minX: -20000, maxX: 20000, minY: -20000, maxY : 20000};
                    curLeft = curLeft < limit.minX ? limit.minX : ((curLeft + w) > limit.maxX ? (limit.maxX - w) : curLeft);
                    curTop = curTop < limit.minY ? limit.minY : ((curTop + h) > limit.maxY ? (limit.maxY - h) : curTop);
                    jQuery(_this).css({position: "absolute", left: curLeft, top: curTop});
                    if(jQuery.browser.msie && _this.tagName == "IMG") e2.preventDefault();
                    mFunc && mFunc.call(_this);
                });
                jQuery(document).mouseup(function(){
                    jQuery(document).unbind();
                    eFunc && eFunc.call(_this);
                });
                if(this.tagName == "IMG") e.preventDefault();
            });
        });
     }
});
/*
*遮罩层api
*@author finder.hyj
*2009.08.27
*/
$.extend({
           cover:{
               show:function(){
                   $.cover.hide();
                   var str="<div id='cover' style='position:absolute;background-color: #666;" ;
                       str+="width:"+100+"%;";
                       str+="height:"+$(document).height()+"px;";
                       str+="opacity:0.5;filter:alpha(opacity=50);" ;
                       str+="top:0;left:0;z-index=10'></div>";
                   $(document.body).append(str);
               },
               hide:function(){
                   $("#cover").remove();
               }
            }
   });
   
   function NoUse()
   {
        alert('此功能尚未开通，敬请期待！！！\(^o^)/~');
        return false;
   }
   
   /* 格式化货币 */
function priceFormat(s,type){
  s = Math.ceil(s * 100)/100;
  s = s.toString();
  if(/[^0-9\.\-]/.test(s))
    return "invalid value";
  s=s.replace(/^(\d*)$/,"$1.");
  s=(s+"00").replace(/(\d*\.\d\d)\d*/,"$1");
  s=s.replace(".",",");
  var re=/(\d)(\d{3},)/;
  while(re.test(s)){
      s=s.replace(re,"$1,$2");
  }
  s=s.replace(/,(\d\d)$/,"");
  if(type)
  {
    return   s;
   }else
   {
    return s+"日元";
   }

}
/*格式化中文价格*/
function cn_priceFormat(s,type){
  s = Math.ceil(s * 100)/100;
  s = s.toString();
  if(/[^0-9\.\-]/.test(s))
    return "invalid value";
  s=s.replace(/^(\d*)$/,"$1.");
  s=(s+"00").replace(/(\d*\.\d\d)\d*/,"$1");
  s=s.replace(".",",");
  var re=/(\d)(\d{3},)/;
  while(re.test(s)){
      s=s.replace(re,"$1,$2");
  }
  s=s.replace(/,(\d\d)$/,".$1");
  if(type)
  {
    return   s.replace(/^\./,"0.");
   }
   else
   {
    return   s.replace(/^\./,"0.")+"元";
   }
}