function $(o)
{
    return document.getElementById(o)
}

function $s(i, a)
{
    $(i).style.display = a ? '' : 'none';
}

function hit(i)
{
    $(i).style.display = $(i).style.display == 'block' ? 'none' : 'block';
}


function getCookie(name)
{
    var strCookies = document.cookie;
    var cookieName = name + "=";
    var valueBegin, valueEnd, value;
    valueBegin = strCookies.indexOf(cookieName);
    if (valueBegin ==  - 1)
        return null;
    valueEnd = strCookies.indexOf(";", valueBegin);
    if (valueEnd ==  - 1)
        valueEnd = strCookies.length;
    value = strCookies.substring(valueBegin + cookieName.length, valueEnd);
    return unescape(value);
}

function setCookie(name, value)
{
    var argv = setCookie.arguments;
    var argc = setCookie.arguments.length;
    var path = (argc > 2) ? argv[2]: null;
    var expireDays = (argc > 3) ? argv[3]: 365;
    var domain = (argc > 4) ? argv[4]: ".2144.cn";
    var secure = (argc > 5) ? argv[5]: false;
    var date = new Date();
    date.setTime(date.getTime() + expireDays * 24 * 3600 * 1000);
    deleteCookie(name);
    document.cookie = name + "=" + escape(value) + ((expireDays == null) ? "" :
        ("; expires=" + date.toGMTString())) + ((path == null) ? "" : (
        "; path=" + path)) + ((domain == null) ? "" : ("; domain=" + domain)) +
        ((secure == true) ? "; secure" : "");
}

function checkCookieExist(name)
{
    if (getCookie(name))
        return true;
    else
        return false;
}

function deleteCookie(name)
{
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}




var fullbarhtml = "<div id='fullbar'><span class='left'><strong>" + title + "</strong> <a href=\"javascript:openintro();\">[查看游戏说明]</a></span><span class='right'><button id='BestSizeButton' onclick='bestsize();'>最佳画面</button> <button id='MaxSizeButton' onclick='maxsize();' disabled='true'>最大画面</button> <button onclick='closefull();'>退出全屏</button></span></div>";

var pwidth=670;
var pheight=480;

var file;
var fullscreen = false;
var tt = 0;
var width = 670;
var height = 480;
var ScreenSize;
if (screen.width < 1024)
{
    width = 550;
    height = 400
}
var Type;
function getType()
{
    return file.split('?')[0].replace(/.+\.(.*)$/g,"$1")
}

function getScreenSize()
{
    var w = 0;
    var h = 0;
    if (self.innerHeight)
    {
        w = self.innerWidth;
        h = self.innerHeight
    }
    else if (document.documentElement && document.documentElement.clientHeight)
    {
        w = document.documentElement.clientWidth;
        h = document.documentElement.clientHeight
    }
    else if (document.body)
    {
        w = document.body.clientWidth;
        h = document.body.clientHeight
    }
    return{width: w, height: h}
}

function getplaysize(w, h)
{
    if (pwidth > 0 && pheight > 0)
    {
        if (pwidth / pheight > w / h)
        {
            h = pheight * (w / pwidth)
        }
        else
        {
            w = pwidth * (h / pheight)
        }
    }
    return {width: w, height: h}
}

function bestsize()
{
    setCookie('play_mode', 'bestsize', '/');
    if (!fullscreen)
    {
        FullScreen()
    }
    else
    {
        var a = (pwidth <= ScreenSize.width && pwidth > 0) ? pwidth :ScreenSize.width;
        var b = (pheight <= (ScreenSize.height - $('fullbar').clientHeight) && pheight > 0) ? pheight : ScreenSize.height - $('fullbar').clientHeight;
        var c = getplaysize(a, b);
        $('flashgame').style.width = $('gamec').style.width = c.width + 'px';
        $('flashgame').style.height = $('gamec').style.height = c.height + 'px';
        $('gamec').style.marginTop = (ScreenSize.height - c.height - $('fullbar').clientHeight) / 2+'px';
        $('MaxSizeButton').disabled = false;
        $('BestSizeButton').disabled = true;
        document.location.hash = "#bestsize"
    }
   // alert("best");
}


function maxsize()
{
    setCookie('play_mode', 'maxsize', '/');
    if (!fullscreen)
    {
        FullScreen()
    }
    else
    {
        var a = ScreenSize.width;
        var b = ScreenSize.height - $('fullbar').clientHeight;
        var c = getplaysize(a, b);
        $('gamec').style.width = $('flashgame').style.width = c.width + 'px';
        $('gamec').style.height = $('flashgame').style.height = c.height + 'px';
        $('gamec').style.marginTop = (b - c.height) / 2+'px';
        $('MaxSizeButton').disabled = true;
        $('BestSizeButton').disabled = false;
        document.location.hash = "#maxsize"
    }
     //alert("max");
}

function FullScreen()
{
	
    if (!$('ContentDiv'))
    {
        document.location.hash == '#bestsize';
        return 
    }
    
    if (fullscreen)
        return ;
    fullscreen = true;
    if ($('leftnet'))
        $s('leftnet');
    if ($('rightnet'))
        $s('rightnet');
    if ($('divc'))
        $s('divc');
    //$s('p_info');
    ScreenSize = getScreenSize();
    //alert("debug->2");
    $s('fullbar', 1);
    //alert("debug->2->1");
    $('fullbar').innerHTML = fullbarhtml;
    if (pwidth == "0" || pheight == "0" || pwidth > ScreenSize.width || pheight> ScreenSize.height - $('fullbar').clientHeight)
    {
        $s('MaxSizeButton');
        $s('BestSizeButton')
    }
    //alert("debug->3");
    document.body.style.overflow = "hidden";
    document.getElementsByTagName('body')[0].scroll = "no";
    document.body.style.width = ScreenSize.width + 'px';
    document.body.style.height = ScreenSize.height + 'px';
    $('ContentDiv').className = "fulldiv";
    $('ContentDiv').style.width = ScreenSize.width + 'px';
    $('ContentDiv').style.height = ScreenSize.height + 'px';
    //alert("debug->3>1");
    if (getCookie('play_mode') == 'maxsize')
    {
        maxsize()
    }
    else
    {
        bestsize()
    }
   // alert("debug->4");
    window.scrollTo(0, 0)
}


function refullscreen()
{
    if (fullscreen)
    {
        fullscreen = false;
        FullScreen()
    }
}


function closefull()
{
    fullscreen = false;
    $s('fullbar');
    //$s('p_info', 1);
    if ($('leftnet'))
        $s('leftnet', 1);
    if ($('rightnet'))
        $s('rightnet', 1);
    if ($('divc'))
        $s('divc', 1);
    document.body.style.width = '';
    document.body.style.height = '';
    document.body.style.overflow = "auto";
    $('ContentDiv').className = $('ContentDiv').className.replace('fulldiv', '');
    
    var a = getplaysize(width, height);
    /*
    $('flashgame').style.width = '670px';
    $('flashgame').style.height = '480px';
    $('gamec').style.marginTop = (height - a.height) / 2+'px';
    $('gamec').style.disply ='none';
    $('ContentDiv').style.width ='670px';
    $('ContentDiv').style.height = '480px';
    document.location.href = "#best"
    */
    
    $('ContentDiv').style.width = $('gamec').style.width = "";
    $('ContentDiv').style.height = $('gamec').style.height = "";
    $('ContentDiv').className = $('ContentDiv').className.replace('fulldiv', '');
    var a = getplaysize(width, height);
    $('flashgame').style.width = $('gamec').style.width = a.width + 'px';
    $('flashgame').style.height = $('gamec').style.height = a.height + 'px';
    $('gamec').style.marginTop = (height - a.height) / 2+'px';
    document.location.href = "#best"


}

function addEvent(a, b, c)
{
    if (a.addEventListener)
    {
        a.addEventListener(b, c, true)
    }
    else
    {
        a.attachEvent("on" + b, c)
    }
}

function removeEvent(a, b, c)
{
    if (a.removeEventListener)
    {
        a.removeEventListener(b, c, true)
    }
    else
    {
        a.detachEvent("on" + b, c)
    }
}

function FullPlay()
{
    FullScreen()
}

function adddiv(i, c)
{
    var o = document.createElement("div");
    o.id = i;
    o.innerHTML = c;
    document.body.appendChild(o);
    return o
}

function openintro()
{
    if (document.all)
    {
        $s('flashgame');
    }
    var b = adddiv("coverdiv", "");
    b.onclick = function()
    {
        closeintro()
    };
    ScreenSize = getScreenSize();
    var a = "<div class='t'>游戏说明：</font></div><div class='c' style='color:black'>" + $('help').innerHTML + "</div><div class='q' onclick='closeintro();' title='关闭游戏说明'>关 闭</div>";
    var c = adddiv("fullintro", a);
    c.style.left = (ScreenSize.width - c.clientWidth) / 2+'px';
    c.style.top = (ScreenSize.height - c.clientHeight) / 2+'px'
}

function closeintro()
{
	if (document.all)
    {
        $s('flashgame', 1);
    }
    $('coverdiv').parentNode.removeChild($('coverdiv'));
    $('fullintro').parentNode.removeChild($('fullintro'))
}


function hid()
{
    $s('ffload')
}

function closeloadnet()
{
    $s('loadnet')
}



addEvent(window, "resize", refullscreen);
