$(document).ready(function() {
	FreshBeat.init();
});
window.onload = function(event) {
	FreshBeat.loaded();
}

var FreshBeat = new function() {}
FreshBeat.loaded = function() {
	// scroll panes
	$(".scroll-pane").jScrollPane({showArrows:true});
}
FreshBeat.init = function() {
	// check _GET
	var bStyle = true;
	var bFlash = true;
	var sUrl = location.search;
	var aPrs = sUrl.split("?");
	if (aPrs.length>1) {
		var aVrs = aPrs[1].split("&");
		for (var i=0;i<aVrs.length;i++) {
			var aVar = aVrs[i].split("=");
			switch (aVar[0]) {
				case "f": bFlash = aVar[1]==1; break;
				case "s": bStyle = aVar[1]==1; break;
			}
		}					
	}

	var sPage = $("div#content").attr("class");

	// first elements
	$("ul#menu>li:eq(0)").addClass("first");
	$("div#content>div:eq(0)").addClass("first");

	// home, commercials and soundclips
	var bComm = sPage=="commercials";
	var bHome = sPage=="home";
	var bSndc = sPage=="soundclips";
	if (bComm||bHome||bSndc) {
		sHref = "";//$("ul.media>li:eq(0)>a").attr("href");
		sImg = "";//bSndc?"":$("ul.media>li:eq(0)>a>img").attr("src");
		sNfo = "";//$("ul.media>li:eq(0)>p").html();
		FreshBeat.placeMedia(sHref,sImg,sNfo,false);
	}

//	if (bHome) $("#screen").css("margin-top","-1000px");
//	if (bHome) $("#screen").css("margin-bottom","1000px");
	if (bHome) {
		if (!$.browser.msie) {
			$("#screen").css("visibility","hidden");
		} else {
			$("#screen").width(0);
			$("#screen").height(0);
			$("#screen").css("overflow","hidden");
		}
	}

	// showreel
	if (sPage=="showreel") FreshBeat.placeMedia("data/video/showreel.flv","data/video/showreel.jpg","",true);

	// media js code
	var aAnchors = $("ul.media>li");
	for (var i=0;i<aAnchors.length;i++) $(aAnchors[i]).children("a").attr("href","javascript:FreshBeat.playMedia("+i+");");

	// google maps
	var mGglMps = document.getElementById("googlemaps");
	if (mGglMps) {
		if (GBrowserIsCompatible()) {
			var mmCenter = new GLatLng(52.349209,4.91385);
			var map = new GMap2(mGglMps);

			map.setCenter(mmCenter, 15);
			map.addControl(new GSmallMapControl());

			$("#googlemaps>div>span").css( {fontFamily:"Verdana, sans-serif",fontSize:"9px",letterSpacing:"-1px"});
			$("#googlemaps>div>a").css( {fontFamily:"Verdana, sans-serif",fontSize:"9px",letterSpacing:"-1px"});

			// icon
			var mmPoint = new GLatLng(52.349209,4.91385);
			//var mmIcon = new GIcon();
			//mmIcon.image  = "wp-content/themes/urbanaction/style/gm_icon.png";
			//mmIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
			//mmIcon.iconSize = new GSize(32, 32);
			//mmIcon.shadowSize = new GSize(64, 32);
			//mmIcon.iconAnchor = new GPoint(16, 32);
			//mmIcon.infoWindowAnchor = new GPoint(5, 1);
			//var mmMarker = new GMarker(mmPoint, mmIcon);
			var mmMarker = new GMarker(mmPoint);
			GEvent.addListener(mmMarker, "click", function() {
				var sHtml = "<div id=\"gminfo\">";
				sHtml += "<h5>FreshBeat :: Studio Amsterdam</h5>";
				sHtml += "<p><a target=\"_blank\" href=\"http://maps.google.nl/maps?f=q&hl=en&geocode=&q=Schollenbrugstraat+11,+amsterdam&sll=52.469397,5.509644&sspn=3.634286,6.547852&ie=UTF8&ll=52.349681,4.91385&spn=0.007117,0.012789&z=16&iwloc=addr&om=1&iwstate1=dir\">Click here for directions.</a></p>";
				sHtml += "</div>";
				mmMarker.openInfoWindowHtml(sHtml);
			});
			map.addOverlay(mmMarker);
		}
	}
}
FreshBeat.playMedia = function(iNr,sFile,sImg,sNfo,bPlay) {
//	$("#screen").css("margin-top","0px");
//	$("#screen").css("margin-bottom","0px");
//	$("#screen").html($("#screen").html());
	if (!$.browser.msie) {
		$("#screen").css("visibility","visible");
	} else {
		$("#screen").width("100%");
		$("#screen").height("100%");
		$("#screen").css("overflow","visible");
	}
	sendEvent('playitem',iNr);
	setInfo(iNr);
}
FreshBeat.setInfo = function(iNr) {
	$("div#screen>p").html($("ul.media>li:eq("+iNr+")>p").html());
}
FreshBeat.placeMedia = function(sFile,sImg,sNfo,bPlay) {
	var sPage = $("div#content").attr("class");
	var iW;
	var iH;
	if (sPage!="showreel") sFile = sPage+"playlist.php";
	switch (sPage) {
		case "soundclips":
			iW = 300;
			iH = 20;
		break;
		case "showreel":
//			iW = 435; // 720
//			iH = 348; // 576
//		break;
		default:
			iW = 300; // 320;
			iH = 240; // 240;
	}
	var aType = sFile.split(".");
	var sType = aType[aType.length-1];
	var sBase = sType=="flv"?"../":"";
	//
	var sFlshVrs = "";//"?w="+$("body").width()+"&h="+$("body").height();; // flvplayer
	var fo = new SWFObject("style/mediaplayer.swf"+sFlshVrs, "mpl", iW+"px",iH+"px", "8", "#957C54");
	fo.addParam("allowfullscreen","true");
	fo.addVariable("file",sBase+sFile);
	fo.addVariable("autostart",bPlay); 
	if (sImg&&sImg!=""&&sPage=="showreel") fo.addVariable("image",sImg);
	fo.addVariable("shuffle", "false");
	fo.addVariable("backcolor","0x311F15");//4C3F51
	fo.addVariable("frontcolor","0xffcc00");
	fo.addVariable("lightcolor","0xffffff");
	//fo.addVariable("callback","mediacallback.php");
	fo.addVariable("enablejs","true");
	fo.addParam("salign", "top");
	fo.write("screen");
	//
	if (sPage!="showreel") $("div#screen").prepend("<p></p>");//"+sNfo+"
	
}
FreshBeat.closeMedia = function() {
	$("div#screen").remove();
}
FreshBeat.toggleGM = function(oThis) {
//	bDisplay = $("#googlemaps").css("margin-top")=="-1000px";
//	bDisplay = $("#googlemaps").css("display")=="none";
	bDisplay = $("#googlemaps").css("visibility")=="hidden";
	$(oThis).html(bDisplay?"hide map":"show map");
	//$(oThis).prepend($("#googlemaps").css("margin-top"));
	//$("#googlemaps").css("margin-top",bDisplay?"0px":"-1000px");
//	$("#googlemaps").css("display",bDisplay?"block":"none");
	$("#googlemaps").css("visibility",bDisplay?"visible":"hidden");
}

/////////////////////
// JW MediaPlayer //

// some variables to save
var currentPosition;
var currentVolume;
var currentItem;

// these functions are caught by the JavascriptView object of the player.
function sendEvent(typ,prm) { thisMovie("mpl").sendEvent(typ,prm); };
function getUpdate(typ,pr1,pr2,pid) {
	if(typ == "time") { currentPosition = pr1; }
	else if(typ == "volume") { currentVolume = pr1; }
	else if(typ == "item") { currentItem = pr1; setTimeout("getItemData(currentItem)",100); }
	var id = document.getElementById(typ);
	id.innerHTML = typ+ ": "+Math.round(pr1);
	pr2 == undefined ? null: id.innerHTML += ", "+Math.round(pr2);
	if(pid != "null") {
		document.getElementById("pid").innerHTML = "(received from the player with id <i>"+pid+"</i>)";
	}
};

// These functions are caught by the feeder object of the player.
function loadFile(obj) { thisMovie("mpl").loadFile(obj); };
function addItem(obj,idx) { thisMovie("mpl").addItem(obj,idx); }
function removeItem(idx) { thisMovie("mpl").removeItem(idx); }
function getItemData(idx) {
	var obj = thisMovie("mpl").itemData(idx);
	var nodes = "";
	nodes += "<li>id: "+idx+"</li>"; 
	for(var i in obj) { 
		nodes += "<li>"+i+": "+obj[i]+"</li>"; 
		//
		FreshBeat.setInfo(idx);
		//
	}
	document.getElementById("data").innerHTML = nodes;
};

// This is a javascript handler for the player and is always needed.
function thisMovie(movieName) {
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[movieName];
	} else {
		return document[movieName];
	}
};