// plays a single mp3 track in a jquery popup window
// title without .mp3 suffix, whitespaces must be replaced w/ underscores
// cover, info are optional
// mp3 tracks must be in music/, covers in music_thumbs/
// the corresponding divs must be inside the html document
$.mp3 = new Object();
$.mp3.playList="playlist.xml";
$.mp3.pathMusic="music/";
$.mp3.pathCover="music_thumbs/";
$.mp3.close = function() {
    $('#mp3popup').dialog('close'); 
}
$.mp3.open = function(playLink) {
	if (!playLink || $.mp3.playListXML == "FAILED")
		return false;
	if (!$.mp3.playListXML) {
		$.ajax({
			url: $.mp3.playList,
			cache: false,
			dataType: "xml",
			success: function(xml) {
				$.mp3.playListXML = $(xml);
				$.mp3.open(playLink);
			},
			error: function() {
				$.mp3.playListXML = "FAILED";
			}
		});
		return false;
	}
	var trackData = $.mp3.playListXML.find( "title[ link = '"+playLink+"' ]");
	var mp3  = trackData.attr('mp3');
	if (!mp3)
		return false;
	var info = trackData.text();
	if (info)
		$("#mp3popupinfo").html(info);
	else
		$("#mp3popupinfo").html("");
	var cover  = trackData.attr('cover');
	if (cover)
		$("#mp3popupcover").css("background","url("+$.mp3.pathCover+cover+")");
	else
		$("#mp3popupcover").css("background","#000000");
	var title = trackData.attr('name');
	if (!title) {
		title = mp3.substring(0,mp3.lastIndexOf(".")).replace(/_/g," ");
	}
	var posData = $.mp3.playListXML.find( "position[ page = '"+$.navigation.currPage+"' ]");
	$.mp3.posX = posData.attr('x');
	$.mp3.posY = posData.attr('y');
	if (!$.mp3.posX || !$.mp3.posY) {
		posData = $.mp3.playListXML.find( "position[ page = '*' ]");
		$.mp3.posX = posData.attr('x');
		$.mp3.posY = posData.attr('y');
	}
	if (!$.mp3.posX || !$.mp3.posY) {
		$.mp3.posX = 200;
		$.mp3.posY = 100;
	} else {
		$.mp3.posX = parseInt($.mp3.posX);
		$.mp3.posY = parseInt($.mp3.posY);
	}
	// declares a popup for the single mp3 plaver 
	$('#mp3popup').dialog({
		autoOpen: false, 
		modal: false,
		resizable: false,
		open: function() {
			$.mp3.posHlpDiffTop = 0;
			$.mp3.posHlpDiffLeft = 0;
			$.mp3.timerHlp=window.setInterval(function () {
			      $('#mp3popup').dialog('option',"position",[$.mp3.posX-$.mp3.posHlpDiffLeft,$.mp3.posY-$.mp3.posHlpDiffTop]);
			},
			2000);
		},
		close: function() {
			window.clearInterval($.mp3.timerHlp);
		},
		dragStart: function() {
			$.mp3.posHlpTop = $(this).offset().top;
			$.mp3.posHlpLeft = $(this).offset().left;
		},
		dragStop: function() {
			$.mp3.posHlpDiffTop = $.mp3.posHlpDiffTop + ($.mp3.posHlpTop - $(this).offset().top);	
			$.mp3.posHlpDiffLeft = $.mp3.posHlpDiffLeft + ($.mp3.posHlpLeft -$('#mp3popup').offset().left);	
		}	
	});


	var so = new SWFObject("lib/mp3player/singleplayer.swf", "mp3singleplayer", "300", "20", "9", "#000000");
	so.addParam("allowFullScreen", "true");
	so.addVariable("mp3", $.mp3.pathMusic+mp3);
	so.addVariable("width", "272");
	so.addVariable("height", "20");
	so.addVariable("autoplay","1");
	so.addVariable("volume","150");
	so.addVariable("showstop","12");
	so.addVariable("showvolume","1");
	so.addVariable("buttonwidth","30");
	so.addVariable("volumeheight","8");
	so.addVariable("loadingcolor","cccccc");
	so.addVariable("bgcolor","333333");
	so.addVariable("bgcolor1","333333");
	so.addVariable("bgcolor2","333333");
	so.addVariable("slidercolor1","dddddd");
	so.addVariable("slidercolor2","999999");
	so.addVariable("sliderovercolor","ff0000");
	so.addVariable("buttoncolor","dddddd");
	so.addVariable("buttonovercolor","ff0000");
	so.write("mp3popupflash");

	$('#mp3popup').data('title.dialog',title);
	$('#mp3popup').dialog('option',"position",[$.mp3.posX,$.mp3.posY]);
	$('#mp3popup').dialog('open'); 

}

// get view port dimensions
$.clientCoords = function() {
     var dimensions = {width: 0, height: 0};
     if (document.documentElement) {
         dimensions.width = document.documentElement.offsetWidth;
         dimensions.height = document.documentElement.offsetHeight;
     } else if (window.innerWidth && window.innerHeight) {
         dimensions.width = window.innerWidth;
         dimensions.height = window.innerHeight;
     }
     return dimensions;
}

// fixes the dimensions of several div tags,
// to have better functionality in lower resolutions
// TODO Test in ie7
$.fixRes = function() {
	var d = $.clientCoords();
	var widthFull=1250, widthLeft=700, widthRight=510, margRight=40, cntLeft=620, padLeft=80, txtOvlTop=400; txtOvlLft = 500;
	if (d.width < 1250) { // this is for leaving original design
		var minWidth = 790;
		widthFull = (d.width > minWidth ? d.width : minWidth); 
		widthLeft = widthFull-510; widthRight = 470; margRigth = 40; cntLeft = widthFull-510-80; padLeft = 80; txtOvlTop = 300; txtOvlLft = 450;
	}
	$('div.fullBox').css("width",widthFull);
	$('div.leftBox').css("width",widthLeft);
	$('div.rightBox').css("width",widthRight);
	$('div.shortInfo').css("margin-right",margRight);
	$('#content').css("left",cntLeft);
	$('#content').css("padding-left",padLeft);
	if($.browser.msie6) {  //msie6 must be declared before, this is no jquery default functionality
		$('#control').css('left',widthLeft);
	} else if (!$.browser.msie7) {
		$('#control').css('margin-left',widthLeft);
	}
	$('#headlines').css('left',widthLeft);
	$('#textOverlay').css('top',txtOvlTop);
	$('#textOverlay').css('left',txtOvlLft);
}

// returns a parameter value for a given parameter name, 
// or null, if the parameter is not present
// Note, that (back)slashed are not allowed, 
// any values containing one of these will be ignored
$.getReqPrm = function(prmName) {
  var uQuery = window.top.location.toString();
  if (uQuery.length > 0 ) {
	  var uPrmName = prmName + "=";
      beg = uQuery.indexOf(uPrmName);
      if (beg != -1) {
         beg += uPrmName.length;
         end = uQuery.indexOf("&",beg);
      	 if (end == -1) {
           end = uQuery.length;
         }
         var result = unescape(uQuery.substring(beg,end));
		 if (result.indexOf("/") == -1 && result.indexOf("\\") == -1 && result.indexOf("#") == -1)
			return result;
      }
   }
   return null;
}

// Scrolls to an html anchor tag,
// the jquery scroll function doesn't work in all browsers
$.scrollToAnc = function(ancName) {
	if (ancName) {
		var elms = document.getElementsByName(ancName);
		var elm = null;
		for (var i = 0; i < elms.length; i++) {
			if (elms[i].tagName == "A") {
				elm = elms[i];
				break;
			}
		}
		if (elm) {
			window.scrollTo(0, elm.offsetTop-10);
		}
	}
}

// follows a given "link", that is, a given sub page (without .php suffix) as "page" parameter
// scrolling to an anchor tag is also supported by a parameter "to"
$.followLink = function() {
	var link = $.getReqPrm("page");
	if (link && link != "") {	
		var anchor = $.getReqPrm("to");
		var elm = document.getElementById(link);
		if (elm && typeof elm =="object" && elm.className == "sub") {
			$.loadContent(elm,anchor); // page inside navigation
			$.navigation.open(elm.parentNode.id);
		} else {
			$.loadContent(link,anchor) // page outside navigation
		}
		return true;
	}
	return false;
}

// loads content (sub)page,
// given is an dom element (from navigaton)
// elm can also be a string (the name of the php file , w/o suffix), 
// to navigate to a page outside navigation
// pages must end with ".php" and be located under /pages
$.loadContent = function(elm,anchor,mood,noWait) {
	if (!elm)
		return false;
	var id, moodId, headline;
	if (typeof elm == "object") { // from navigation
		id = elm.id;
		moodId = elm.parentNode.parentNode.id;
		headline = $(elm).html();
	} else { // string
		id = elm;
		headline = ""; // No headline
		moodId = mood || ($("#mood").css("background-image") == "" ? null : "start"); // Only change the mood if 3rd param, init with "start"
	}
	if (moodId)
		$("#mood").css("background-image","url('img/mv_"+moodId+".jpg')");
	$('#textOverlay').css({display:"none"});
	if (noWait == null || !noWait)
		$("#content").html("<img src='img/30-0.gif' style='margin-top: 150px; margin-left: 210px;'>");
	$("#headlines h1").html(headline);
	// load content and fix resoultion & scroll to anchor (if present), when finished
	// on error load homepage
	$("#content").load("pages/"+id+".php",function(response,status) { 
		if (status == "error") {
			$.loadHome();
		} else {
			$.navigation.currPage = id;
			$.fixRes(); 
			$.scrollToAnc(anchor); 
		}
	});	
}

// loads the homepage
$.loadHome = function() {
	$.loadContent("home",null,"start");
	$.navigation.close();
}

// navigation
$.navigation = new Object();
$.navigation.currPage = '';
$.navigation.openObj = new Object();
// open navigation
$.navigation.open = function(accId) {
	$.navigation.clearOpen();
	$.navigation.openObj = window.setTimeout(function () {
		$("#"+accId).slideDown(500);
		$("#nav").find("ul.sub:not('#"+accId+"')").slideUp("slow");
	}
	,400);
}
// stop loading
$.navigation.clearOpen = function() {
	window.clearTimeout($.navigation.openObj);
}
// close navigation accordion
$.navigation.close = function() {
	$.navigation.clearOpen();
	$("#nav").find("ul.sub").slideUp("slow");
}

$(document).ready(function() {
	// init a variable to indicate, if this browser is ie
	$.browser.msie6 = $.browser.msie && parseInt($.browser.version) == 6 && !window["XMLHttpRequest"];
	$.browser.msie7 = $.browser.msie && parseInt($.browser.version) == 7;
	// follow links, if present through request parameters; otherwise, load home page
	if (!$.followLink())
	  $.loadContent("home",null,"start",true);
	// fix div sizes for lower resolutions
	$.fixRes();
	// resizes div sizes, if user resizes his browser window
	$(window).resize(function() {
	  $.fixRes();
	});

	$("#nav a.main").hover(
		function() { $.navigation.open($(this).next("ul.sub").attr("id")) },
		$.navigation.clearOpen
	);
	
	$('#control li.sub').hover(
			function(){
				$(this).css("color","orange");
			},
			function(){
				$(this).css("color","white");
			}
	);

	$('#control li.sub').click( function () {
		$.loadContent(this);
	});
	
	// test for "play" request parameter end eventually plays the mp3, if present
	var play = $.getReqPrm("play");
	if (play) 
		$.mp3.open(play);

});
