//Event.addBehavior({
//	'.level1, .level1on, .level2, .level2first':function() {
//		this.onclick = function() {
//			if($(this.id).className=='level1') {
//				$$('.level1on').each( function(e){ e.className='level1' } );
//				$$('.submenu').invoke('hide');
//				$(this.id).className = 'level1on';
//				showContent('inc/' + this.id + '.php');
//			} else if ($(this.id).className=='level1on') {
//				showContent('inc/' + this.id + '.php');
//			};
//			elem = $(this.id+'-1');
//			/*if(elem) {
//				if(elem.getAttribute('view')=='true') {
//					elem.hide();
//					elem.setAttribute('view','false');
//				} else {
//					elem.show();
//					elem.setAttribute('view','true');
//				}
//			}*/
//			if (elem) {
//				elem.show();
//			};
//		};
//	}
//});

//Event.addBehavior({
//	'.level3, .level3first':function() {
//		this.onclick = function() {
//		 	showContent('inc/event.php?eventID=' + this.getAttribute('eventID'));
//		};
//	}
//});

//Event.addBehavior({
//	'#agendaView':function() {
//		this.onclick = function() {
//			limit = this.getAttribute('state') == 'full' ? 0 : -1;
//		 	getAgenda(limit);
//		};
//	}
//});

/*Event.addBehavior({
	'menu-0':function() {
		this.onclick = function() {
		 	showContent('inc/index.php');
		};
	}
});*/

var content;
var movie;
var ar;

document.observe('dom:loaded', function(){
	showMenuCategory(cat);
	getAgenda(5);
});

function showContent(theURL) {
	ar = new Ajax.Request(theURL,
		{
			method:'get',
			onSuccess: function(transport){
				response = transport.responseText || "no response text";
				content = response;
				writeData(content);
	    	},
	    	onFailure: function(){ alert('Something went wrong... (function showContent()') }
	  });
}

function writeData(theContent) {
	$('content').innerHTML = theContent;
	movie = $$('.movieLink').first().getAttribute('movieurl');
	//document.fire('tm_event:loaded');
	// destroy the ajax request object
	ar = null;
}

/*function loadMovie(theMovie) {
	playerConfig = {controlBarBackgroundColor: 0x333333,controlBarGloss: 'none'};
	playerConfig.videoFile = "../movies/" + theMovie;
	//alert (playerConfig['videoFile']);
	flowplayer.setConfig(playerConfig);
}
*/


function loadMovie(theMovie) {
	

	var vids = document.getElementsByTagName('video');
	if(vids.length > 0) {
		$("flash_api").setAttribute("src","http://topmanagement.clients.telemak.com/movies/" + theMovie);
		$("flash_api").play();
	} else {
		var t = document.getElementById('flash').firstChild
		var player = flowplayer();
		if(t.id=='flash_api') {
			player.setClip('http://topmanagement.clients.telemak.com/movies/' + theMovie);
			player.play();
		} else {
			loadNewMovieForExplorer('http://topmanagement.clients.telemak.com/movies/' + theMovie);
		}
	}
	
	
	//var player = flowplayer();
	//
	//player.setClip('http://topmanagement.clients.telemak.com/movies/' + theMovie);
	//player.play();

	//var isFlash = $("flash_api").getAttribute("src").indexOf("swf");
	//if(isFlash > -1) {
	//	alert('standard html flowplayer version');
	//	$f("flash").setClip('http://topmanagement.clients.telemak.com/movies/' + theMovie);
	//  //$f("flash").play();
	//} else {
	//  $("flash_api").setAttribute("src","http://topmanagement.clients.telemak.com/movies/" + theMovie);
	//  $("flash_api").play();
	//}
	
	
	
	
}

function loadNewMovieForExplorer(movie) {
	$f('flash').unload();
	$f("flash", "player/flowplayer.commercial-3.2.7.swf", {
	
		key: ['#@5497ef453c5a0504e41'],

		clip: {
			url: movie,
			autoPlay: true,
			autoBuffering: true,
			provider: 'pseudo',
			scaling: 'fit',
			width: 480,
			height: 298
		},
		
		// streaming plugins are configured normally under the plugins node
		plugins: {
			pseudo: { 
				url: 'flowplayer.pseudostreaming-3.2.7.swf'
			}
		}
	}).ipad();

}


/*document.observe('tm_event:loaded', function() {
	flowplayer = flashembed("flash", 
			{
				src:'flash_players/FlowPlayerDark.swf',
				width: 480,
				height: 298,
				wmode: "transparent"
			},
			{config: {
				autoPlay: false,
				menuItems: [ 1, 1, 1, 1, 1, 0, 0 ],
				videoFile: '../movies/' + movie,
				controlsOverVideo: 'no',
				controlBarBackgroundColor: 0x333333,
				controlBarGloss: 'none'
				}
			}
		);
	$$('.movieLink').each(function(elem) {elem.onclick = function() { loadMovie(elem.getAttribute('movieurl')) } } );
	}
)
*/

document.observe('tm_event:loaded', function() {

	$f("flash", "player/flowplayer.commercial-3.2.7.swf", {
	
		key: ['#@5497ef453c5a0504e41'],

		clip: {
			//url: 'http://elo.vod.http.telemakstream.net/vod/elo_teaser.mp4',
			url: 'http://topmanagement.clients.telemak.com/movies/' + movie,
			autoPlay: true,
			autoBuffering: true,
			provider: 'pseudo',
			scaling: 'fit',
			width: 480,
			height: 298
		},
		
		// streaming plugins are configured normally under the plugins node
		plugins: {
			pseudo: { 
				url: 'flowplayer.pseudostreaming-3.2.7.swf'
			}
		}
	}).ipad();
	
	$$('.movieLink').each(function(elem) {elem.onclick = function() { loadMovie(elem.getAttribute('movieurl')) } } );
	}
)


function loadFirstEventMovie() {

	$f("flash", "player/flowplayer.commercial-3.2.7.swf", {
	
		key: ['#@5497ef453c5a0504e41'],

		clip: {
			//url: 'http://elo.vod.http.telemakstream.net/vod/elo_teaser.mp4',
			url: 'http://topmanagement.clients.telemak.com/movies/' + movie,
			autoPlay: true,
			autoBuffering: true,
			provider: 'pseudo',
			scaling: 'fit',
			width: 480,
			height: 298
		},
		
		// streaming plugins are configured normally under the plugins node
		plugins: {
			pseudo: { 
				url: 'flowplayer.pseudostreaming-3.2.7.swf'
			}
		}
	}).ipad();
	
	$$('.movieLink').each(function(elem) {elem.onclick = function() { loadMovie(elem.getAttribute('movieurl')) } } );
}



function getAgenda(items) {
	
	numberOfItems = items > 0 ? items : items < 0 ? items : 5;
	ar = new Ajax.Request('../inc/agenda.php',
		{
			method:'post',
			parameters: {agendaItems: numberOfItems},
			onSuccess: function(transport){
				response = transport.responseText || "no response text";
				content = response;
				writeAgenda(content);
	    	},
	    	onFailure: function(){ alert('Something went wrong...(function getAgenda)') }
	  });
}

function writeAgenda(theContent) {
	$('agenda').innerHTML = theContent;
	//$("agendaView").observe("click", function(){limit = 0;console.log("agendaView click with limit: "+limit);getAgenda(limit);
	$('agendaView').observe('click', function(){
		limit = this.getAttribute('state') == 'full' ? 0 : -1;
		getAgenda(limit);
	});
}

function showMenuCategory(theCategory) {
	// reset all submenu items
	$$('.submenu').invoke('hide');
	$$('.level1on').each( function(e){ e.className='level1' } );
	
	// show chosen category
	var cat=theCategory+'';
	$('menu-'+cat).className='level1on';
	try {
		$$('#menu-'+cat+'-1 .submenu').invoke('show');
		$('menu-'+cat+'-1').show();
	} catch(err) {}
}
