var resizeSetup = false;
var tikatok_fullscreen = false;
var handlerTarget = null;
var fsOverlay = null;
var oldSize = null;

function getViewportSize() {
    if ('undefined' != typeof window.innerWidth) {
        // standards compliant browsers
        return [ window.innerWidth, window.innerHeight];
    }
    // IE6 uses either documentElement or document's body
    var d = document.documentElement;
    if ('undefined' == typeof document.documentElement) {
        d = document.getElementsByTagName('body')[0];
    }
    return [ d.clientWidth, d.clientHeight ];
}

function windowResizeHandler() {
    if (!tikatok_fullscreen || !handlerTarget)	{
        return;
    }

    if (!Prototype.Browser.IE) {
        if (!fsOverlay) {
            fsOverlay = $(document.createElement('div'));
            fsOverlay.id = 'overlay';
            var b = document.getElementsByTagName('body')[0];
            b.insertBefore(fsOverlay, b.firstChild);
        }
        fsOverlay.show();
    }

    var vs = getViewportSize();
    oldSize = handlerTarget.getHeight();
    var p = handlerTarget.offsetParent;
    var lft = 10, tp = 20;
    if (p) {
        lft -= parseInt(p.offsetLeft);
        tp -= parseInt(p.offsetTop);
    }
    handlerTarget.setStyle({
        position: 'absolute',
        left: lft + 'px',
        top: tp + 'px',
        width: (vs[0]-40) + 'px',
        height: (vs[1]-40) + 'px',
        zIndex: 12000});
}

function fix(e) {
    e.style.overflow = 'hidden';
    e.scrollTop = 0;
}

function loose(e) {
    e.style.overflow = 'auto';
}

function _tik_fullScreen(id) {
    tikatok_fullscreen = true;
    handlerTarget = $(id);
    windowResizeHandler();
    if (!resizeSetup) {
        Event.observe(window, 'resize', windowResizeHandler);
        resizeSetup = true;
    }
    $('allcontent').setStyle({zIndex: 10000});
//    fix(document.getElementsByTagName('body')[0]);
    fix(document.getElementsByTagName('html')[0]);
}

function makeBookFullscreen() {
    _tik_fullScreen('bookiframe')
}

function makeBookNormal() {
    tikatok_fullscreen = false;
    if (!handlerTarget) return;
    handlerTarget.setStyle({position: 'static', width: '100%', height: '400px' });
    $('allcontent').setStyle({zIndex: 10});
    if (fsOverlay) fsOverlay.hide();
    loose(document.getElementsByTagName('body')[0]);
    loose(document.getElementsByTagName('html')[0]);
    handlerTarget = null;
}

function startEditor(url) {
    if (typeof(loggedIn) != 'undefined' && !loggedIn) {
        document.location.href = url;
    } else {
        if (!window.XMLHttpRequest) {
            url += '/ie/true'; // mark IE6 (no XMLHTTP) for special cookie treatment...
        }
        _tik_fullScreen('editoriframe');
        handlerTarget.src = url;
        handlerTarget.show();
    }
    return false;
}

function closeEditor(changed) {
    tikatok_fullscreen = false;
    if (!handlerTarget ) return;
    handlerTarget.src = 'about:blank';
    handlerTarget.hide();
    fsOverlay.hide();
    loose(document.getElementsByTagName('body')[0]);
    loose(document.getElementsByTagName('html')[0]);
    handlerTarget = null;
    var t = $('bookiframe');
    if (t) {
        var url = t.src;
        // rewrite url to close the book
        url = url.replace(/#.*$/, '');
        url = url.replace(/\/$/, '') + '/unlock/true';
        t.src = url;
    }
}

function isInappropriate() {
    Effect.Appear('flag-popup', {
        afterFinish: function() {
            $('flag-reason').focus();
        }
    });
};

function sendInappropriate() {
    new Ajax.Request($('flag-reason').up('form').action, {
        parameters: { 'reason': $F('flag-reason') }
    });
    var link = $('flag-link');
    link.href = '#';
    link.className = '';
    closeMessagePopup($('flag-reason'));
};

