
$.ajaxSetup({
    global: false,
    cache: false
});

var $CONFIG = {};
var externalSMObj;
var isRegistered=false;

function GetSkinInfo(data){

}

var skinUI = {
    initRecommendPage: function () {
        skinUI.loadRecommendSkins();
		//
        $('.img_box_recommend').delegate('a', 'click', function () {
            var data = $(this).attr('data-field');
            var reportCode = $(this).attr("reportCode");
            var skin = JSON.parse(data);
			var obj={"Id":skin.Code,"Vip":skin.Vip};
			if(!isRegistered){
				externalSMObj.ajax('Theme', 'RefreshDownloadStatus', '','',function(status,txt,json){
					RefreshDownloadStatus(skin,json);});
				isRegistered=true;
			}
            $.API.changeSkin(obj);
            $.get($CONFIG.api.counter, { 'c': $CONFIG.credential, 'countertype': reportCode, 'skinId': skin.SkinId,'version': $CONFIG.clientVersion});
            return false;
        });
    },

    loadRecommendSkins: function () {
		var getSkinUrl=$CONFIG.api.getSkins+'?version='+$CONFIG.clientVersion+'&callback=?';

		$.ajax({
			type: "GET",  
			async: true, 
            url: getSkinUrl,
			dataType:'jsonp',
			jsonpCallback:'GetSkinInfo',
            success: function (data) {
                if (data.StatusCode = 200) {
                    skinUI.bindRecommendSkinList(data.Skins);
                }
				$('#loadingpg').addClass('none');
				
                //$.API.requsetErrorCode(data.StatusCode);
            },
			timeout:23000,
            error : function (XMLHttpRequest, textStatus, errorThrown) {
					$('#loadingpg').addClass('none');
					//error,show error information
					$('.img_box_recommend').addClass('none');
					if($('.neta').length<=0){
						var divError=$('<div class="neta">'+
										'<div>'+
										'<img src="img/around_error.png"/><br>'+
										'加载失败,请检查网络<br><a href="javascript:;">重新加载</a>'+
										'</div>'+
										'</div>'
									  );
						
						$('body').append(divError);
						
						divError.find('a').bind('click',function(){
							divError.addClass('none');
							$('.img_box_recommend').removeClass('none');
							$('#loadingpg').removeClass('none');
							window.document.location.reload();
									});
					}
					else{
						$('.neta').removeClass('none');
					}
            //$.API.requsetErrorCode("404");
            }
        });
		
    },

    bindRecommendSkinList: function (skins) {
        var bigImgBox = $('.list_1');
        var normalImgBox = $('.list_2');
		
        for (var i = 0; i < $CONFIG.skinNum.recommend.count; i++) {
            var skin = skins[i];
            var isTop = i < $CONFIG.skinNum.recommend.top;
            var vipFlag = (skin.Vip == undefined || skin.Vip == 0) ? '' : (isTop ? '<em class="vip_b_en"/>' : '<em class="vip_s_en"/>');
            if (isTop) {  
				var big='<li id="li'+skin.Code+'"><a hidefocus="true" href="javascript:void(0)" class="img" reportCode="' + (i + 1) + '" data-field=\'' + JSON.stringify(skin) + '\'><img class="liimg" src="' + $CONFIG.imgCDN + skin.Thumb +'" alt="" onload=loadImg("'+skin.Code+'") onerror=loadImgError(this,"'+skin.Code+'") /></a><span class="bg"></span><a href="javascript:void(0)" class="txt"  reportCode="' + (i + 1) + '" data-field=\'' + JSON.stringify(skin) + '\'>' + skin.SkinName + '</a>' + vipFlag + '<span class="loadbox"><img alt="" src="img/loading.gif" complete="complete"/><br>正在加载，请稍候... </span></li>';
				bigImgBox.append(big);

				var liError='<li id="lierror'+skin.Code+'" class="none"><div class="load_fail_pb"><img src="img/load_f01.jpg" class="load_f01" /><p><a href="javascript:" onclick="reloadImage(\''+skin.Code+'\')">重新加载</a></p></div></li>';
				bigImgBox.append(liError);
            } else {
				var normal='<li id="li'+skin.Code+'"><a hidefocus="true" href="javascript:void(0)" class="img" reportCode="' + (i + 1) + '" data-field=\'' + JSON.stringify(skin) + '\'><img class="liimg" src="' + $CONFIG.imgCDN + skin.Thumb +'" alt="' + skin.SkinName + '" title="'+skin.SkinName+'" onload=loadImg("'+skin.Code+'") onerror="loadImgError(\''+skin.Code+'\')"/></a>' + vipFlag + '<span class="loadbox"><img alt="" src="img/loading.gif" complete="complete"/></span></li>';
                normalImgBox.append(normal);
				var liError2='<li id="lierror'+skin.Code+'" class="none"><div class="load_fail_ps"><img src="img/load_f01.jpg" class="load_f01" /><p><a href="javascript:" onclick="reloadImage(\''+skin.Code+'\')">重新加载</a></p></div></li>';
				
				normalImgBox.append(liError2);
            }
        }
    }
};

function initSkin () {
    var pageName = location.pathname.replace(/(.+)[\\/]/,'').toLowerCase();
    if (pageName == 'theme.html') {
        skinUI.initRecommendPage();
    } else if (pageName == 'history.html') {
        skinUI.initHistoryPage();
    }
    $.common.forbidden();
};
function loadImg(code){
	//loaded,stop loading
	$('#li'+code).find('.loadbox').addClass("none");
}

function loadImgError(code){
	$('#li'+code).addClass('none');
	//error,stop loading
	$('#li'+code).find('.loadbox').addClass("none");
	
	$('#lierror'+code).removeClass('none');
}

function reloadImage(code){
	$('#lierror'+code).addClass('none');
	$('#li'+code).removeClass('none');

	var src = $('#li'+code).find('.liimg').attr('src');
	$('#li'+code).find('.liimg').attr('src',src);
	//show loading
	$('#li'+code).find('.loadbox').removeClass("none");
}

(function ($) {
    $.extend({
        common: {
            forbidden: function () {
                // 禁止右键
                $(document).bind("contextmenu", function (e) {
                    e.target = e.srcElement || document;
                    // 只有输入框里有右键菜单
                    if (e.target.nodeName == 'INPUT' && e.target.type == 'text') {
                        return true;
                    } else {
                        return false;
                    }
                });
                // 禁止选择文字
                $(document).bind("selectstart", function () {
                    return false;
                });
                $(document).bind("dragstart", function () {
                    return false;
                });
                //禁止F5刷新
                document.onkeydown = function (e) {
                    e = e || event;
                    if (e.keyCode == 116) {
                        e.keyCode = 0;
                        e.cancelBubble = true;
                        return false;
                    }
                    return true;
                };
            },

            // 颜色RGB转16进制
            colorHex: function (value) {
                var color = value.replace(/[^\d,]/g, '').replace(/(\d+)/g, function (s, s1) {
                    var v = parseInt(s1).toString(16);
                    return v.length == 1 ? '0' + v : v;
                });
                return '#' + color.replace(/,/g, '').toUpperCase();
            }
        }
    });
})(jQuery);