
var windowheight = $(window).height();
var windowwidth = $(window).width();
var scale = 1;
var maxheight = 900;
//if(windowheight > maxheight) { // todo, remove this, but do a check in image scroller
//	displayheight = maxheight; 
//	toppx = Math.floor((windowheight - maxheight)/2);
//} else {
	displayheight = windowheight;
	toppx = 0;
//}

$(function() {
	$('#urls').hide();
	$('#info').show();

	if( ($.browser.msie && $.browser.version.substring(0,1)<8)
		|| ($.browser.mozilla && $.browser.version.substring(0,3)<"1.9")
	) {
		if(confirm('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 or click OK to go to the mobile (light) version of our site.\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 of click OK om naar de mobiele (light) versie van onze site te gaan.')) {
			window.location = 'http://m.w139.nl/';
		}
	}


	var imgheight = 964; //619;
	var imgwidth = 1486; //1024;
	scale = windowheight/imgheight;
	middle = Math.round(windowwidth/2);

	// center
	$('#back').css({'left': (windowwidth - (imgwidth*scale))/2+'px'});
	$('#front').css({'left': (windowwidth - (imgwidth*scale))/2+'px'});
	$('#info').css({'left': (windowwidth - (imgwidth*scale))/2+'px'});

	// scale images
	$('#backgroundimage').css({'height': 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);
	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
		);
	}
        // position of text overlay
        var lsimplescreen = (windowwidth - (imgwidth*scale))/2+(260*scale);
        var lsimplesphere = (windowwidth - (imgwidth*scale))/2+ (((imgwidth)*scale)/2);
        var lsimpleprinter = (windowwidth - (imgwidth*scale))/2+((imgwidth-250)*scale);
        var tsimplescreen = (imgheight/2) * scale;
        var tsimplesphere = (imgheight * scale * 0.2);
        var tsimpleprinter = (imgheight * scale * 0.25);
        $('#tomakeitsimple').css({'width': (imgwidth*scale)+"px", 'height': $('#front').height()+"px", 'left': (windowwidth - (imgwidth*scale))/2+'px'}); // $('#front').offset().left+'px', 'top': $('#front').offset().top+"px"});
        $('#simplescreen').css({ 'top': tsimplescreen+'px', 'left': lsimplescreen+'px'});
        $('#simplesphere').css({ 'top': tsimplesphere+'px', 'left': lsimplesphere+'px'});
        $('#simpleprinter').css({ 'top': tsimpleprinter+'px', 'left': lsimpleprinter+'px'});
        $('#imagemap').hover( function() { $('.simpleclass').show(); }, function() { $('.simpleclass').hide(); });
        $('#info').hover( function() { $('.simpleclass').show(); }, function() { $('.simpleclass').hide(); });
        $('.simpleclass').hover(function() { $('.simpleclass').show(); }, function() { $('.simpleclass').show();});
	

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/)) {
	if(!specificsrc.match(/w139.nl\/wordpress/)) specificsrc = specificsrc.replace(/\.nl/,".nl/wordpress"); // todo
	showTouch(specificsrc);
}

});
function showTouch() {
	if(touchsrc == "") touchsrc = 'http://w139.nl/wordpress';
	showTouch(touchsrc);
}
function showTouch(bla) {
	setTimeout(function() {
		checkForDestruction();
		var leftpx = 0;
		var src = 'http://w139.nl/wordpress/';
		if(bla != undefined) src = bla;
		else if(touchsrc != undefined && touchsrc != "") src = touchsrc;
		$('#schilcontent').append('<iframe id="iframetouch" class="iframe" src="'+src+'" height="'+displayheight+'px" width="550px" scrolling="no" border="0" frameborder="0" style="left: '+leftpx+'px; top: '+toppx+'px; border:0px;">');
		return false;
		}, 150
	);

}

function showBall() {
	setTimeout(function() {
		checkForDestruction();
		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();
}
