// Browser Window Size and Position
// copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005
// you may copy these functions but please keep the copyright notice as well
function pageWidth() {return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?       document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;} 
function pageHeight() {return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;} 
function posLeft() {return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;} 
function posTop() {return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;} 
function posRight() {return posLeft()+pageWidth();} 
function posBottom() {return posTop()+pageHeight();}
	
var curw=0, curh=0;
var imagetweak_stat_seconds = 0;

function setBG() {
	curw = pageWidth();
	curh = pageHeight();

	var frame = document.getElementById("frame");
	var rawcontent = document.getElementById("rawcontent");
	frame.style.width = curw+"px";
	frame.style.height = Math.round(curw / 2.39)+"px";
	frame.style.top = Math.round((curh - curw / 2.39) / 2) + "px";
	frame.style.left = curw - curw + "px";
	rawcontent.style.width = curw - 64 - 16 + "px";
	rawcontent.style.height = Math.round(curw / 2.39) - 100 - 16 + "px";

	curw = (curw + 64) - (curw % 64);
	curh = (curh + 64) - (curh % 64);
	frame.style.backgroundImage = document.getElementById("bg").style.backgroundImage = 'url(bg.php?w='+curw+'&h='+curh+')';
}

function onLoadHandler() {
	if (document.referrer.indexOf('cjb.net') != -1) 
		top.location.href = 'http://cafxx.strayorange.com';
	else
		setBG();

	if ( document.getElementById("imagetweak_counter") ) {
		function update_download_counter() { 
		  document.getElementById("imagetweak_counter").innerHTML = 
		    "ImageTweak has been downloaded " + Math.round( imagetweak_stat_total + imagetweak_stat_mean * ( imagetweak_stat_seconds++ ) ) + " times from addons.mozilla.org, in the last week it has been downloaded on average " + 
			( imagetweak_stat_mean < 1 ? "once every " + Math.round( 10 / imagetweak_stat_mean ) / 10 + " seconds." : Math.round( imagetweak_stat_mean * 10 ) / 10 + " times per second." ); 
		}

		update_download_counter();
		window.setInterval( update_download_counter, 1000 );
	}		
}

window.onload = onLoadHandler;
window.onresize = setBG;

function switchToEditMode() {
	document.getElementById("switch").style.display = "none";
	document.getElementById("controlform").style.display = "block";
	document.getElementById("content").style.display = "none";
	document.getElementById("rawcontent").style.display = "block";
}

