var locationUrl = "http://" + location.host;
$(document).ready(function () {

    var layoutResize=function(){
        var _height=$(window).height()-48;
        $('#iframeWebPC').height(_height);
    }
    layoutResize();
    $(window).resize(layoutResize);

    InitView();
    Init();

    //判断是否云网站带参数过来
    checkISLoadKevin();
    var id = $("#hidID").val();
    InitData(id, 0, "", "yes");
    var id = GetCurrentID();

    //终端切换
    $(".ycb_bar_tab li").click(function () {
        //切换终端样式
        $(this).addClass("ycb_bar_hover").siblings().removeClass("ycb_bar_hover");
        //切换ifarme
        var iframeName = $(this).attr("mid");
        $("#" + iframeName).parents(".ycb_box_in").show().siblings().hide();
        //保存切换ifarme
        $("#hidIframeName").val(iframeName);


        //每次执行scrollbar都清空一次html 防止因为id重复导致的事件失效
        
        var _random = Math.floor(Math.random() * 100000);
        var moblieUrl = $("#hidsample_moblie").val();
        var wUrl = $("#hidsample_w").val();
        var pcUrl = $("#hidsample_pc").val();

        if (iframeName == "iframeWebW") {
            iFrameHeight(iframeName);
            clareScroll("1");
            clareScroll("2");
            $("#iframeWebW").attr("src", wUrl);
            $("#hidCurrentMt").val("3");
			$("#iframeWebW").height(565);
            scrollbar("ycb_box_mob_con_w", "3");
        } else if (iframeName == "iframeWebM") {
            iFrameHeight(iframeName);
            clareScroll("2");
            clareScroll("3");
            //$("#iframeWebM").attr("src", moblieUrl);
            $("#hidCurrentMt").val("2");
            //scrollbar("ycb_box_mob_con", "1");
        } else {
            //$("#iframeWebPC").attr("src", pcUrl);
            $(".ycb_box_in").hide();
            $("#hidCurrentMt").val("1");
        }
        $(".ycb_bar_fun_list").hide();

        var id = GetCurrentID();
    });

    //上一个创意，下一个创意事件
    $(".ycb_pos_icon_pre , .ycb_pos_icon_next").click(function () {

        var iframeName = $("#hidIframeName").val();

        if (iframeName == "iframeWebPC") {
            clareScroll("1");
            clareScroll("2");
            clareScroll("3");
        } else if (iframeName == "iframeWebM") {
            clareScroll("2");
            clareScroll("3");
        } else {
            clareScroll("1");
            clareScroll("2");
        }
        // var currentMt = $("#hidCurrentMt").val();
        $(".ycb_bar_tab [mid='" + iframeName + "']").addClass("ycb_bar_hover").siblings().removeClass("ycb_bar_hover");
        $("#" + iframeName).parent().show().siblings().hide();
        //重置创意
        var id = $("#hidID").val();
        var next = $(this).attr("mid");
        var startcountnumber = $("#Oncount").html().substr(0, $("#Oncount").html().length - 1);
        var endcountnumber = $("#Upcount").html().substr(0, $("#Upcount").html().length - 1);
        if (parseInt(startcountnumber) == 0 && $(this).hasClass("ycb_pos_icon_pre")) {
            mAlert("系统提示", "已经是第一套了");
            return false;
        }
        if (parseInt(endcountnumber) == 0 && $(this).hasClass("ycb_pos_icon_next")) {
            mAlert("系统提示", "已经是最后一套了");
            return false;
        }

        var type = 0;
        if ($("#hidSource").val() != "" && iframeName == "iframeWebM") {
            type = 2;
        } else if ($("#hidSource").val() != "" && iframeName == "iframeWebW") {
            type = 3;
        } else {
            type = 0;
        }

        InitData(id, type, next, "");
    });

    //点击返回
    $("#gohomename").click(function () {
		var locationUrl = "http://" + location.host;
		window.location.href = locationUrl;
    });

    //查看更多页面 
    $(".ycb_pos_icon_sb").click(function () {
        if (webmode==2){
            location.href=webroot + 'template/';
        }else{
            location.href=webroot + 'list.asp?classid=1';
        }
    });

    $(window).resize(function () {
        var url = $("#art_iframe").attr("src");
        if (typeof (url) == 'undefined') {
            return;
        }
        if (url.indexOf("SkinList.aspx") > -1) {
            var win_w = $(window, document).width(); //屏幕当前宽度
            $("#art_iframe").width(win_w);
        }
    });
});

function InitView() {
    //控制当前进入端的显示状态
    var type = $("#hidCurrentMt").val();
    switch (type) {
        case "1":
            $("#pc").show().siblings().hide();
            break;
        case "2":
            $("#moblie").show().siblings().hide();
            break;
        case "3":
            $("#weixin").show().siblings().hide();
            break;
    }
}

//供子页面调用，设置查询条件
function SaveCondition(value) {
    $("#hidSaveCondition").val(value);
}
function dcloseHide() {
    //alert("异常");
    mAlert("系统提示", "异常");
    $("#d_close").hide();
    $(".d-title").hide();
}

function clareScroll(id) {
    $("#xn_c_jsScroll_1_wrap_" + id).find("#scrollbar-y").remove();
    $("#xn_c_jsScroll_1_wrap_" + id).find(".scrollbar-bg").remove();
    $("#xn_c_jsScroll_1_wrap_" + id).find(".scrollbar-x").remove();
    $("#xn_c_jsScroll_1_wrap_" + id).find(".scrollbar-x-bg").remove();
}

//获取当前终端ID
function GetCurrentID() {
    return $("#hidID").val();
}
//初始化显示四核终端ifreme地址
function Init() {
    var pcUrl = $("#hidsample_pc").val();
    $("#iframeWebPC").parent().show().siblings().hide();
    $("#iframeWebPC").attr("src", pcUrl);
    //iFrameHeight("iframeWebPC");
}
//初始化结果,切换组合
function InitData(id, type, next, isLoad) {
    var iframeName = $("#hidIframeName").val();

    var t = $("#hidSource").val();
    if (t != "") {
        var userId = $("#hidUserId").val();
        if (type == 0) {
            type = 1;
        }
        return;
    }

    GetNextCreative(id, type, next, function (data) {
        if ($.trim(data) == "") {
            mAlert("系统提示", "没有数据了");
            return;
        }
        var result = data;
        if (type == 0) {
            var strs = new Array();
            strs = result.split("|");
            if (strs.length >= 7) {
                $("#hidID").val(strs[0]);

                var resultPc = strs[1];
                if (resultPc != "") {
					$("#hidsample_pc").val(resultPc);
					$("#iframeWebPC").attr("src", resultPc);
                }
                var resultMobiler = strs[2];
				if (checkIEVersion()) resultMobiler = webroot + "ajax/ie_wapsite.asp";
                if (resultMobiler != "") {
					$("#hidsample_moblie").val(resultMobiler);
					$("#iframeWebM").attr("src", resultMobiler);
                }
                var resultW = strs[3];
                if (resultW != "") {
					$("#hidsample_w").val(resultW);
					$("#iframeWebW").attr("src", resultW);
                }
                $("#Oncount").html(strs[4] + "个");
                $("#Upcount").html(strs[5] + "个");

				$("#spCreativeID").html(strs[6]);
            } else {
                mAlert("系统提示", "查询结果错误");
            }

        }

    });
}

//判断是否云网站带参数过来
function checkISLoadKevin() {
    if ($("#hidCurrentMt").val() != "") {
        var moblieUrl = $("#hidsample_moblie").val();
        var wUrl = $("#hidsample_w").val();
        var _random = Math.floor(Math.random() * 100000);
        var mtType = $("#hidCurrentMt").val();
        if (mtType == "1") {
            $(".ycb_bar_tab [mid='iframeWebPC']").addClass("ycb_bar_hover").siblings().removeClass("ycb_bar_hover");
            //iFrameHeight("iframeWebPC");
            $("#iframeWebPC").parents(".ycb_box_in").show().siblings().hide();
            $("#hidIframeName").val("iframeWebPC");
        } else if (mtType == "2") {
            $(".ycb_bar_tab [mid='iframeWebM']").addClass("ycb_bar_hover").siblings().removeClass("ycb_bar_hover");
            $("#iframeWebM").parents(".ycb_box_in").show().siblings().hide();
            iFrameHeight("iframeWebM");
            //scrollbar("ycb_box_mob_con", "1");
            $("#iframeWebM").attr("src", moblieUrl);
            $("#hidIframeName").val("iframeWebM");
        } else if (mtType == "3") {
            $(".ycb_bar_tab [mid='iframeWebW']").addClass("ycb_bar_hover").siblings().removeClass("ycb_bar_hover");
            $("#iframeWebW").parents(".ycb_box_in").show().siblings().hide();
            iFrameHeight("iframeWebW");
            scrollbar("ycb_box_mob_con_w", "3");
            $("#iframeWebW").attr("src", wUrl);
            $("#hidIframeName").val("iframeWebW");
        }
    }
}

function iFrameHeight(iframeName) {
    if ($(window.parent.document).find("#" + iframeName)) {
        var iframeObj = $(window.parent.document).find("#" + iframeName);
        var thisheight = $(document).height();
        //iframeObj.height(thisheight);
    }
}

function checkIEVersion() {
	var browser = getBrowserInfo();
	var verinfo = (browser + "").replace(/[^0-9.]/ig, "");
	if (verinfo == "6.0" || verinfo == "7.0" || verinfo == "8.0" || verinfo == "9.0" || verinfo == "10.0") {
		return true;
	} else {
		return false;
	}
}
function getBrowserInfo() {
	var agent = navigator.userAgent.toLowerCase();
	var regStr_ie = /msie [\d.]+;/gi;
	if (agent.indexOf("msie") > 0) {
		return agent.match(regStr_ie);
	}
}

var AjaxSkinList = webroot + "ajax/ajax.asp";

function GetNextCreative(id, type, NextOrUp, func) {
    var load = layer.load("数据加载中...");
    var result;
    $.post(AjaxSkinList, {
        action: "GetNextCreative",
        id: id,
        type: type,
        NextOrUp: NextOrUp
    }, function (data) {
        result = data;
        func(result);
        layer.close(load);
    })
    return result;
}
