
var windowheight = $(window).height();
var windowwidth = $(window).width();
var scale = 1;
var maxheight = 900;
if(windowheight > maxheight) {
	displayheight = maxheight; 
	toppx = Math.floor((windowheight - maxheight)/2);
} else {
	displayheight = windowheight;
	toppx = 0;
}

$(function() {

	if( ($.browser.msie && $.browser.version.substring(0,1)<8)
		|| ($.browser.mozilla && $.browser.version.substring(0,3)<"1.9")
	) {
		alert('You are currently using an older browser which will not be able to render w139.nl properly.\n Please update your browser to the newest version.\n\n U gebruikt op dit moment een browser die w139.nl niet goed kan laten zien.\n Update uw browser naar de nieuwste versie.');
	}


	var imgheight = 619;
	var imgwidth = 1024;
	scale = windowheight/imgheight;

	leftpx = Math.round(((scale*imgwidth)-550)/2);
	middle = Math.round(windowwidth/2);
	//console.log(scale + ' ' + windowwidth + ' ' + (windowwidth/scale) + ' l '+ document.getElementById('front').clientWidth + ' l' + document.getElementById('front').offsetWidth + document.getElementById('front').scrollWidth);
	$('#back').css({'left': (middle-(scale*(imgwidth/2)))+'px'});
	$('#front').css({'left': (middle-(scale*(imgwidth/2)))+'px'});
	$('#darken').click(function() { checkForDestruction(); });

	// scale images
	$('#backgroundimage').css({'width': windowwidth+'px'});
	$('#backgroundimage').css({'height': windowheight+'px'});
	$('#front').css({'height': windowheight+'px'});
	$('#back').css({'height': windowheight+'px'});

	// scale imagemap
	var touch = $('#touch').attr('coords').split(",");
	for(i=0;i<touch.length;i++) {
		touch[i] = Math.round((touch[i]*scale))
	}
	$('#touch').attr('coords',touch.join(','));
	var ball = $('#ball').attr('coords').split(",");
	for(i=0;i<ball.length;i++) {
		ball[i] = Math.round((ball[i]*scale))
	}
	$('#ball').attr('coords',ball.join(','));
	var print = $('#print').attr('coords').split(",");
	for(i=0;i<print.length;i++) {
		print[i] = Math.round((print[i]*scale))
	}
	$('#print').attr('coords',print.join(','));

	// set images, + check for new image each x secs
        frontimg = getLatestSrc+Math.random();
	$("#back").attr('src',frontimg);
	if($.browser.msie && $.browser.version<="6.0") {
		document.write(' Deze site kan niet ervaren worden met Internet Explorer 6.<br/> <br/> W139: Ruimte voor continue productie en presentatie van hedendaagse kunst<br/> Warmoesstraat 139, 1012JB Amsterdam. Tel: +31 20 6229434<br/> Openingstijden: dagelijks 11:00 - 19:00 u.<br/> <br/> This site can not be experienced with Internet Explorer 6.  <br/> <br/> W139: Space for continuous production and presentation of Contemporary Art<br/> Warmoesstraat 139, 1012JB Amsterdam. Tel: +31 20 6229434<br/> Entrance times: daily 11:00 - 19:00 h.<br/><br/> Please upgrade your browser (we recommend <a href="http://firefox.com">Firefox</a> but you could also go to <a href="http://windowsupdate.microsoft.com">Windows Update</a>');	
		$('#back').attr('src','');
		$('front').attr('src','');
	} else {
		doBackground();
	}
	function doBackground() {
		setTimeout(
			function() { 
                               	frontimg = getLatestSrc+Math.random();
				$("#front").attr('src',frontimg).fadeIn(1500, function() {
					$('#back').attr('src',$('#front').attr('src'));
					$("#front").fadeOut(500);
				});		
				doBackground();
			}, 30000
		);
	}


var requesturi = location.href;
requesturi = requesturi.replace(/http:\/\/[^\/]*\//,"");
if(requesturi.match(/^activate.php/)) {
	regex = new RegExp(/^activate.php(.*)/);
	match = regex.exec(requesturi);
	showPrintActivate(match[1]);
} else if(requesturi.match(/^print.*/)) {
	showPrint();
} else if(requesturi.match(/^ball/)) {
	showBall();
} else if(requesturi.match(/.+/)) {
	touchsrc += '/'+requesturi;
	showTouch();
} else if(specificsrc.match(/w139.nl/)) {
	touchsrc = specificsrc.replace(/.nl/,".nl/wordpress");
}

});
function showTouch() {
	setTimeout(function() {
		checkForDestruction();
		var leftpx = 0;
		$('#schilcontent').append('<iframe id="iframetouch" class="iframe" height="'+displayheight+'px" width="550px" scrolling="no" border="0" frameborder="0" style="left: '+leftpx+'px; top: '+toppx+'px; border:0px;">');
		$('#iframetouch').attr('src',touchsrc);
		return false;
		}, 100
	);
}

function showBall() {
	setTimeout(function() {
		checkForDestruction();
		//var adjleftpx = leftpx - 100;
		var adjleftpx = middle - ((displayheight-25)/2);
		$('#schilcontent').append('<iframe id="iframeball" class="iframe" height="'+(displayheight)+'px" width="'+(displayheight-25)+'px" scrolling="no" border="0" frameborder="0" style="left: '+adjleftpx+'px; border:0px;"/>');
		$('#iframeball').attr('src',ballsrc);
		return false;
		}, 100
	);
}

function showPrintActivate(uid) {
	setTimeout(function() {
		checkForDestruction();
		var leftpx = windowwidth - 550;
		$('#schilcontent').append('<iframe id="iframeprint" class="iframe" height="'+displayheight+'px" width="550px" scrolling="no" border="0" frameborder="0" style="left: '+leftpx+'px; top: '+toppx+'px; border:0px;"/>');
		$('#iframeprint').attr('src',printactivatesrc+uid);
		$('#iframeprint').load(function() { $('#iframeprint').css({'background-color':'#ff'}); });
		return false;
		}, 100
	);
}

function showPrint() {
	setTimeout(function() {
		checkForDestruction();
		var leftpx = windowwidth - 550;
		$('#schilcontent').append('<iframe id="iframeprint" class="iframe" height="'+displayheight+'px" width="550px" scrolling="no" border="0" frameborder="0" style="left: '+leftpx+'px; top: '+toppx+'px; border:0px;"/>');
		$('#iframeprint').attr('src',printsrc);
		$('#iframeprint').load(function() { $('#iframeprint').css({'background-color':'#ff'}); });
		return false;
		}, 100
	);
}

function checkForDestruction() {
	$('.iframe').remove();
	$('#darken').toggle();
}
