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;";
  }
});
$.tabs = function(containerId, start) {
    var ON_CLASS = 'selected';
    var id = containerId;
    var i = (typeof start == "number") ? start - 1 : 0;
    $(id + '>div:lt(' + i + ')').add(id + '>div:gt(' + i + ')').hide();
	  $(id + '>ul>li:eq(' + i + ')').addClass(ON_CLASS);
	  var url = $(id + '>ul>li:eq(' + i + ')').attr("url");
	  if (url) $(id + '>div:eq('+i+')').load(url);
    //$(id + '>ul>li:nth-child(' + i + ')').addClass(ON_CLASS);
    $(id + '>ul>li').click(function() {
        if (!$(this).is('.' + ON_CLASS)) {
			//$(id + '>ul>li>a').click(function() {
			//if (!$(this.parentNode).is('.' + ON_CLASS)) {
            //var re = /([_\\-\\w]+$)/i;
            //var target = $('#' + re.exec(this.href)[1]);
            //var target = $($(this).attr("href"));
			var idt = $(id + '>ul>li').index(this);
			var target = $(id + '>div:eq('+idt+')');
			var url = $(this).attr("url");
			if (url) target.load(url);
            if (target.size() >= 0) {
                $(id + '>div:visible').hide();
                target.show();
                $(id + '>ul>li').removeClass(ON_CLASS);
                $(this).addClass(ON_CLASS);
            } else {
                alert('There is no such container.');
            }
        }
        return false;
    });
};
function drop_confirm(msg, url){
    if(confirm(msg)){
        window.location = url;
    }
}

function content_refresh(id,url){
	 URL = SITE_URL + url;
   $.getJSON(url,{'id':id},function(data){
   		if (data.done)
     	{
     		$('span[fieldname="updatetime"][fieldid="'+id+'"]').text(data.retval);
     		$('span[fieldname="updatetime"][fieldid="'+id+'"]').addClass('red');
    	}else{
    		alert(data.msg);
    	}
   });
}

/* 显示Ajax表单 */
function ajax_form(id, title, url, width)
{
    if (!width)
    {
        width = 400;
    }
    var d = DialogManager.create(id);
    d.setTitle(title);
    d.setContents('ajax', url);
    d.setWidth(width);
    d.show('center');

    return d;
}
function goto(url){
    window.location = url;
}

function change_captcha(jqObj){
    jqObj.attr('src', 'index.php?app=captcha&' + Math.round(Math.random()*10000));
}

/* 格式化金额 */
function price_format(price){
    if(typeof(PRICE_FORMAT) == 'undefined'){
        PRICE_FORMAT = '&yen;%s';
    }
    price = number_format(price, 2);

    return PRICE_FORMAT.replace('%s', price);
}

function number_format(num, ext){
    if(ext < 0){
        return num;
    }
    num = Number(num);
    if(isNaN(num)){
        num = 0;
    }
    var _str = num.toString();
    var _arr = _str.split('.');
    var _int = _arr[0];
    var _flt = _arr[1];
    if(_str.indexOf('.') == -1){
        /* 找不到小数点，则添加 */
        if(ext == 0){
            return _str;
        }
        var _tmp = '';
        for(var i = 0; i < ext; i++){
            _tmp += '0';
        }
        _str = _str + '.' + _tmp;
    }else{
        if(_flt.length == ext){
            return _str;
        }
        /* 找得到小数点，则截取 */
        if(_flt.length > ext){
            _str = _str.substr(0, _str.length - (_flt.length - ext));
            if(ext == 0){
                _str = _int;
            }
        }else{
            for(var i = 0; i < ext - _flt.length; i++){
                _str += '0';
            }
        }
    }

    return _str;
}

/* 收藏内容 */
function collect_content(modelid, id)
{
    var url = SITE_URL + '/index.php?app=my_favorite&act=add&&ajax=1';
    $.getJSON(url, {'id':id,'modelid':modelid}, function(data){
        alert(data.msg);
    });
}

/* 收藏网店 */
function collect_group(id)
{
    var url = SITE_URL + '/index.php?app=my_favorite&act=add&type=group&jsoncallback=?&ajax=1';
    $.getJSON(url, {'item_id':id}, function(data){
        alert(data.msg);
    });
}
/* 火狐下取本地全路径 */
function getFullPath(obj)
{
    if(obj)
    {
        //ie
        if (window.navigator.userAgent.indexOf("MSIE")>=1)
        {
            obj.select();
            return document.selection.createRange().text;
        }
        //firefox
        else if(window.navigator.userAgent.indexOf("Firefox")>=1)
        {
            if(obj.files)
            {
                return obj.files.item(0).getAsDataURL();
            }
            return obj.value;
        }
        return obj.value;
    }
}

/**
 *    启动邮件队列
 *
 *    @author    Liangu
 *    @param     string req_url
 *    @return    void
 */
function sendmail(req_url)
{
    $(function(){
        var _script = document.createElement('script');
        _script.type = 'text/javascript';
        _script.src  = req_url;
        document.getElementsByTagName('head')[0].appendChild(_script);
    });
}
//读写cookie函数
function GetCookie(c_name)
{
	if (document.cookie.length > 0)
	{
		c_start = document.cookie.indexOf(c_name + "=")
		if (c_start != -1)
		{
			c_start = c_start + c_name.length + 1;
			c_end   = document.cookie.indexOf(";",c_start);
			if (c_end == -1)
			{
				c_end = document.cookie.length;
			}
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return null
}

function SetCookie(c_name,value,expiredays)
{
	var exdate = new Date();
	exdate.setDate(exdate.getDate() + expiredays);
	document.cookie = c_name + "=" +escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString()); //使设置的有效时间正确。增加toGMTString()
}

// 焦点轮换图片
function addLoadEvent(func){
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		}
	}
}

function moveElement(elementID,final_x,final_y,interval) {
  if (!document.getElementById) return false;
  if (!document.getElementById(elementID)) return false;
  var elem = document.getElementById(elementID);
  if (elem.movement) {
    clearTimeout(elem.movement);
  }
  if (!elem.style.left) {
    elem.style.left = "0px";
  }
  if (!elem.style.top) {
    elem.style.top = "0px";
  }
  var xpos = parseInt(elem.style.left);
  var ypos = parseInt(elem.style.top);
  if (xpos == final_x && ypos == final_y) {
		return true;
  }
  if (xpos < final_x) {
    var dist = Math.ceil((final_x - xpos)/10);
    xpos = xpos + dist;
  }
  if (xpos > final_x) {
    var dist = Math.ceil((xpos - final_x)/10);
    xpos = xpos - dist;
  }
  if (ypos < final_y) {
    var dist = Math.ceil((final_y - ypos)/10);
    ypos = ypos + dist;
  }
  if (ypos > final_y) {
    var dist = Math.ceil((ypos - final_y)/10);
    ypos = ypos - dist;
  }
  elem.style.left = xpos + "px";
  elem.style.top = ypos + "px";
  var repeat = "moveElement('"+elementID+"',"+final_x+","+final_y+","+interval+")";
  elem.movement = setTimeout(repeat,interval);
}

function classNormal(focus_turn_btn,focus_turn_tx){
	var focusBtnList = $('#'+focus_turn_btn).find('li');
	var focusTxList = $('#'+focus_turn_tx).find('li');
	for(var i=0; i<focusBtnList.length; i++) {
		focusBtnList[i].className='normal';
		focusTxList[i].className='normal';
	}
}

function classCurrent(focus_turn_btn,focus_turn_tx,n){
	var focusBtnList = $('#'+focus_turn_btn).find('li');
	var focusTxList = $('#'+focus_turn_tx).find('li');
	focusBtnList[n].className='current';
	focusTxList[n].className='current';
}


function indexfocusChange() {
	if(!$('#index_focus_turn')||!$('#index_focus_turn_btn')) return;
	$('#index_focus_turn').onmouseover = function(){atuokey = true};
	$('#index_focus_turn').onmouseout = function(){atuokey = false};
	var focusBtnList = $('#index_focus_turn_btn').find('li');
	if(!focusBtnList||focusBtnList.length==0) return;
	var listLength = focusBtnList.length;
		focusBtnList[0].onmouseover = function() {
			moveElement('index_focus_turn_picList',0,0,5);
			classNormal('index_focus_turn_btn','index_focus_turn_tx');
			classCurrent('index_focus_turn_btn','index_focus_turn_tx',0);
		}
	if (listLength>=2) {
		focusBtnList[1].onmouseover = function() {
			moveElement('index_focus_turn_picList',0,-188,5);
			classNormal('index_focus_turn_btn','index_focus_turn_tx');
			classCurrent('index_focus_turn_btn','index_focus_turn_tx',1);
		}
	}
	if (listLength>=3) {
		focusBtnList[2].onmouseover = function() {
			moveElement('index_focus_turn_picList',0,-376,5);
			classNormal('index_focus_turn_btn','index_focus_turn_tx');
			classCurrent('index_focus_turn_btn','index_focus_turn_tx',2);
		}
	}
	if (listLength>=4) {
		focusBtnList[3].onmouseover = function() {
			moveElement('index_focus_turn_picList',0,-564,5);
			classNormal('index_focus_turn_btn','index_focus_turn_tx');
			classCurrent('index_focus_turn_btn','index_focus_turn_tx',3);
		}
	}
}

setInterval('indexautoFocusChange()',5000);
var atuokey = false;
function indexautoFocusChange() {
	if(!$('#index_focus_turn')||!$('#index_focus_turn_btn')) return;
	if(atuokey) return;
	var focusBtnList = $('#index_focus_turn_btn').find('li');
	var listLength = focusBtnList.length;
	for(var i=0; i<listLength; i++) {
		if (focusBtnList[i].className == 'current') var currentNum = i;
	}
	if (currentNum==0&&listLength!=1 ){
		moveElement('index_focus_turn_picList',0,-188,5);
		classNormal('index_focus_turn_btn','index_focus_turn_tx');
		classCurrent('index_focus_turn_btn','index_focus_turn_tx',1);
	}
	if (currentNum==1&&listLength!=2 ){
		moveElement('index_focus_turn_picList',0,-376,5);
		classNormal('index_focus_turn_btn','index_focus_turn_tx');
		classCurrent('index_focus_turn_btn','index_focus_turn_tx',2);
	}
	if (currentNum==2&&listLength!=3 ){
		moveElement('index_focus_turn_picList',0,-564,5);
		classNormal('index_focus_turn_btn','index_focus_turn_tx');
		classCurrent('index_focus_turn_btn','index_focus_turn_tx',3);
	}
	if (currentNum==3 ){
		moveElement('index_focus_turn_picList',0,0,5);
		classNormal('index_focus_turn_btn','index_focus_turn_tx');
		classCurrent('index_focus_turn_btn','index_focus_turn_tx',0);
	}
	if (currentNum==1&&listLength==2 ){
		moveElement('index_focus_turn_picList',0,0,5);
		classNormal('index_focus_turn_btn','index_focus_turn_tx');
		classCurrent('index_focus_turn_btn','index_focus_turn_tx',0);
	}
	if (currentNum==2&&listLength==3 ){
		moveElement('index_focus_turn_picList',0,0,5);
		classNormal('index_focus_turn_btn','index_focus_turn_tx');
		classCurrent('index_focus_turn_btn','index_focus_turn_tx',0);
	}
}
addLoadEvent(indexfocusChange);