

function getQueryString(strSrc,name) 
{
        var reg = new RegExp("(^|&|&amp;)" + name + "=([^&]*|[^&amp;]*)(&|&amp;|$)", "i");
        var r = strSrc.match(reg);
        if (r != null) 
			return unescape(r[2]);
        return null;
}

var returnData,isLoaded=false,credit;
function AfterSMCompleted(){
	if(!isLoaded){
		externalSMObj=SM;
		returnData = SM.exec("Ext","GetData","InitThemeData");
		returnData = returnData["json"]["value"];

		SM.exec('Theme', 'GetCridential', 'Credential');
		credit=SM.exec('Ext', 'GetData', 'Credential');
		credit=credit['json']['value'];

		isLoaded=true;

		$CONFIG.credential=credit;
		$CONFIG.clientVersion = getQueryString(returnData,'v');
		
		$CONFIG.serviceDomain = getQueryString(returnData,'web');
		$CONFIG.imgCDN = getQueryString(returnData,'cdn');
		$CONFIG.skinNum = {recommend: { count: 15, top: 3, normal: 12 },history: { count: 6 }};
		$CONFIG.api = {getSkins: getQueryString(returnData,'web') + 'getSkins',counter: getQueryString(returnData,'web') + 'setcounter'};
		
		initSkin();
	}
}


(function ($) {
    $.extend({
        API: {
            // 切换皮肤
            changeSkin: function (skin) {
				SM.exec("Theme","ChangeTheme","",skin);
            },

            // 获取皮肤的本地存放目录路径
            getSkinLocalDirPath: function () {
            },

			//v5状态暂时没有
            // 更新PC客户端窗体状态
            requsetErrorCode: function (strErrorCode) {
            },

            //获取本地脚本路径
            GetFetionSkinScriptPath: function () {
            }
        }
    });
})(jQuery);

// 刷新loading...状态
function RefreshDownloadStatus(skin,params) {
    var object = params;
    var skinId = object.SkinId;
    var status = object.Status.toLowerCase();
    $('li>a:first-child').each(function () {
        var skinData = JSON.parse($(this).attr('data-field'));
        if (skinData.Code == skinId) {
            var loadbox = $(this).siblings(".loadbox");
            if (status == 'start') {
                loadbox.removeClass("none");
            } else if(status=='end'){
                loadbox.addClass("none");
            }else{
				if(loadbox.next().length<=0){
				    var error=$('<div class="load_fail">皮肤同步失败!</div>');
				    loadbox.after(error);
				}
				else{
					loadbox.next().removeClass('none');
				}
				
				loadbox.addClass('none');

				loadbox.next().animate({top:'0'},800);
				
				window.setTimeout(function(){
					loadbox.next().animate({top:'-20px'},800);
				},1200);
				/*
				window.setTimeout(function(){loadbox.next().addClass('none');},1500);
				*/
			}
        }
    });
}

// 设置历史皮肤
function SetHistorySkins(data) {
    var skins = JSON.parse(data);
    skinUI.bindHistorySkinList(skins);
}