var resizeSetup = false;
var tikatok_fullscreen = false;
var handlerTarget = null;
var fsOverlay = null;
var oldSize = null;

function getViewportSize() {
	var res = [0, 0];
	if ('undefined' != typeof window.innerWidth) {
		// standards compliant browsers
		res[0] = window.innerWidth;
		res[1] = window.innerHeight;
	} else {
		// IE6 uses either documentElement or document's body
		var d = document.documentElement;
		if ('undefined' == typeof document.documentElement) {
			d = document.getElementsByTagName('body')[0];
		}
		res[0] = d.clientWidth;
		res[1] = d.clientHeight;
	}

	return res;
}

function windowResizeHandler() {
    if (!tikatok_fullscreen) {
        return;
    }
    
    if (!handlerTarget)	{
        return;
    }
    
    if (!fsOverlay) {
	fsOverlay = $(document.createElement('div'));
	fsOverlay.id = 'overlay';
	document.getElementsByTagName('body')[0].appendChild(fsOverlay);
    }
    fsOverlay.show();
    
    var vs = getViewportSize();
    oldSize = handlerTarget.getHeight();
    handlerTarget.setStyle({
	position: 'absolute',
	left: '20px',
	top: '20px',
	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;
    }
    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' });
	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'));
};