
$(document).ready(function(){
	
	// Vars
	var siteRoot = "http://www.plastikk.no";
	var templateUrl = siteRoot + "/wp-content/themes/plastikk"; //TODO Set in header file
	var firstRun = true;
	var $hidden = $('#hidden');

	function updateFlashElements() {
		// alert("Trying to ");
		
		// If showreel needed (on front page)
		if ($('#showreel').length != 0) {
			//alert ("There is showreel!");
			swfobject.embedSWF(templateUrl + "/flash/showreelplayer.swf", "showreel", "830", "320", "9.0", templateUrl + "/flash/expressinstall.swf", {movie: "showreel"},{allowfullscreen:"true", wmode:"transparent"});
		}
		
		// If flvplayer needed (in category 'arbeider')
		if ($('#flvplayer').length != 0) {
			swfobject.embedSWF(templateUrl + "/flash/flvplayer.swf", "flvplayer", "830", "467", "9.0", templateUrl + "/flash/expressinstall.swf", {videoURL: videoToBeLoaded, imgURL: imgToBeLoaded}, {allowfullscreen:"true", wmode:"transparent"}); // Var set in single.php
		}
		
		// If indicators needed (in category 'arbeider')
		if ($('#indicators').length != 0) {
			var greenPrc = $('#green_indicator').text();
			var bluePrc = $('#blue_indicator').text();
			var orangePrc = $('#orange_indicator').text();
			var redPrc = $('#red_indicator').text();
		
			swfobject.embedSWF(templateUrl + "/flash/indicator.swf", "green_indicator", "50", "50", "9.0", templateUrl + "/flash/expressinstall.swf", {percent: greenPrc, color: "green"}, {wmode: "transparent"});
			swfobject.embedSWF(templateUrl + "/flash/indicator.swf", "blue_indicator", "50", "50", "9.0", templateUrl + "/flash/expressinstall.swf", {percent: bluePrc, color: "blue"}, {wmode: "transparent"});
			swfobject.embedSWF(templateUrl + "/flash/indicator.swf", "orange_indicator", "50", "50", "9.0", templateUrl + "/flash/expressinstall.swf", {percent: orangePrc, color: "orange"}, {wmode: "transparent"});
			swfobject.embedSWF(templateUrl + "/flash/indicator.swf", "red_indicator", "50", "50", "9.0", templateUrl + "/flash/expressinstall.swf", {percent: redPrc, color: "red"}, {wmode: "transparent"});
		}
	}
	
	function handleChange(event) {
		// alert("handleChange: " + event.path);
		// console.log('You clicked the the menu point pointing to ' + target);

		// $('#content').load(target);
		
		// If this isn't the first time the app runs
		// alert (event.path);
		// alert (event.path);
		if ((!firstRun || event.path != '/') && (event.path != "more-90")) {
			
			
			// Get extracted content from extractor feed on web root
			var path = (event.path != '/') ? event.path : ''; 
			
			$('#media_player').slideUp('medium');
			$('#content').slideUp('medium', function() {
				$.get(siteRoot + "/extractor.php?path=" + event.path, null, function(data, textStatus) {
					$('#media_player').remove();
					$('#content').remove();
					
					$('#menu').after(data);
					
					$('#media_player').hide();
					$('#content').hide();
					
					$('#media_player').slideDown('slow', updateFlashElements);
					$('#content').slideDown('slow');
					
					
				});
			});
		} else {
			firstRun = false;
		}
	}

	$('#logo, #menu li a').click(function(event){ // , TODO: Differentiate logo behavior on index.php/single.php/ ()
		
		// alert ('clicked menu point');
		var target = $(this).attr("href");
		var targetArray = target.split('plastikk.no/');
		target = targetArray[targetArray.length - 1];
		
		if (target == "") {
			target = "category/forside/";
		}
		
		// Set SWFAddress
		SWFAddress.setValue(target);
		
		// alert("No navigation for you, petty mortals!!");
		event.preventDefault();
		
	});
	
	// Add SWFAddress change event handler
	SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleChange);

	// Static flash elements
	var flashvars = {};
	var params = { wmode: "opaque", bgcolor: "#111111" };
	var attributes = {};
	swfobject.embedSWF(templateUrl + "/flash/clouds.swf", "relish", "100%", "200", "9.0", "<?php bloginfo('template_url'); ?>/flash/expressinstall.swf", flashvars, params, attributes);
	updateFlashElements()
});
