/*
    site.js - jQuery 1.4.2
    teppefall.no 2010 Jan Erik Paulsen
    538 228/386 - 944 400
*/
var maximized = false
var normalTopLeft, contentHeight
function doBackgroundAnimation() {
	$('#current_animation_layer').animate({/*left: '-=50',*/ width: ['toggle']}, 500, function() {
		$('#current_animation_layer').animate({width: ['toggle']}, 500, function() {
		});
	});
}
function doShrinkAnimation() {
	//$('#current_animation_layer').css({background: "#02a0d8"})
	$('#current_animation_layer').animate({/*left: '-=50',*/ width: ['toggle']}, 500, function() {
		$('#current_animation_layer').animate({width: ['toggle']}, 500, function() {
			$('.basic').animate({width: 100.0}, 700, function() {
				$("a").each(function(index, element) {
			        $(element).fadeOut().fadeIn()
				/*
			        $(element).css("backgroundColor", "green");
			        $(element).css("color", "white");
			        $(element).css("border", "1px solid white");
			        */
			        // sjekk etter mouseover ?
			        if($(!this).is("#stop")) {
						return false;
					}
				});
				$("a").each(function(index, element) {
			        $(element).slideUp().delay(1000).slideDown();
		        });
				$('#do_animation').prepend('<span style="color:yellow">Done! </span>');
			});
		});
	});
}
function doPlayerAnimation(eventObject) {
	var clickBoxHeight = 200
	if(maximized) clickBoxHeight = 350
	if(eventObject.pageY >= ($('#test').offset().top + clickBoxHeight)) {
		doBackgroundAnimation()
		return
	}
	if(!maximized) {
		normalTopLeft = $('#test').position()
		$("#test").css({position: "absolute"})
		$('#test').css({height: "700px"}) // har ingen effekt
		$('#test video').css({width:"944px",height:"400px"}) // opera
		$('#test').animate({top:"0px", left:"0px", width: "100%"}, 500, function() { /*doBackgroundAnimation()*/});
		maximized = true;
	}
	else {
		//944 400
		$('#test').animate({top: normalTopLeft.top, left: normalTopLeft.left, width: normalTopLeft.width}, 500, function() {
			$('#test').css({height: "auto"})
			$('#test').css({position: "inherit" })
			$('#test video').css({width:"538px",height:"229px"}) // opera
			$("#animate2 img").css({width: "auto", height:"auto"}) //border:"4px solid blue",
		 });
		maximized = false;
	}
}
function menu() {
	//if($('.share_menu').is(':visible')) { 
	$('.share_menu').toggle(); 
}
function toggleBackgroundColor() {
    var width = $(window).width();
    if(width > 900)
	$(".presentation").css({background: "none"})
	else
	$(".presentation").css({background: "black"})
//	$('.presentation').prepend(width+",");
}
$(window).load(function () {
	$('#do_animation').click(doShrinkAnimation);
	$('#animate').click(doBackgroundAnimation); //gammelt
	$('#animate2').click(doPlayerAnimation);
	var frontpage = false
	if(frontpage) {
		$(window).bind("resize", toggleBackgroundColor);
		toggleBackgroundColor()
	}
	//window.addEventListener("online", function() { });
	//window.addEventListener("offline", function() { });
	$(window).bind("online offline", function() {
		$('#do_animation').prepend('<span style="color:yellow">Not supported</span>');
	});
	var f = function() { $('.selected').prepend('<span>OK</span>');}
	// angst
	$('#s_design').click(f)
	$('#s_code').click(f)
	$('#s_transform').click(f)
	$('#s_assets').click(f)
	$('#s_api').click(f)
	$('#s_search').click(f)
	//alert(location.href)
	if(location.href.indexOf("#contact")!=-1) {
		$('#contactbox').css({background:"#173c61"})
	}
//	$('body').click(menu)
	$('#share_link').click(menu)
	$('.share_menu').click(menu)
/*	$('.share_this').mouseover(function() {
		$('.share_menu').show();
		//$('.share_menu').prepend('<p>OK</p>');
	})*/
	/*
	$('.tiledandwide').append("<div class=\"edge\"></div>");
	$('.footer').append("<div class=\"content_edge\"></div>");
	*/
	try {
		work();
	}
	catch(e) {}
});
function work() {
	var worker = new Worker("/static/site_worker.js");
    worker.onmessage = function(event) {
		workerEvent = event.data
		if(workerEvent.visible)
        document.getElementById("do_animation").textContent = "Animate ("+workerEvent.message+")";
        document.getElementById("do_animation").title = "Last event was: "+workerEvent.message;
//        dump("Got: " + event.data + "\n");
    };
    worker.onerror = function(error) {
        document.getElementById("do_animation").title = "Error: "+error.message;
//        dump("Worker error: " + error.message + "\n");
        //throw error;
    };
//    worker.postMessage("sdf");
}
function api(check) {
	return typeof(check) != "undefined"
}
function register() {
	if(api(navigator.registerProtocolHandler)) {
		try {
			var scheme = "app"
			navigator.registerProtocolHandler(scheme, "http://teppefall.no/app.jsp?url=%s", "TeppefallApp")
//	navigator.registerContentHandler("application/x-teppefall-app", "app?url=%s", "TeppefallApp")
			alert(scheme + ":// registrert")
		}
		catch(e) {
			alert(e)
		}
	}
	else {
		//alert("Ingen slik API")
	}
}
function database() {
	try {
		if(window.openDatabase) {
			alert("ok")
		}
	}
	catch(e) {
		alert(e)
	}
}

