var dev = false;
var active_cat_item = null;
var active_cat_id = 0;

	function initApp() {
		init_Sendungsauswahl();
		init_videoScroll();
		init_VideoInfos();
		if (cat == "") loadVideoPlaylist();
		
		var timer;
		$('#suche_input').keyup(function() {
			window.clearTimeout(timer);
			timer = window.setTimeout("searchVideoClipsByTitle();", 1000);
		});
		
		$("#frm_suche").submit(function() {
			window.clearTimeout(timer);
			return false;
		});
		
		$('#video_filter div').bind('click', function()	{changeFilter($(this));});
		//$('#debug').show();
	}
	
	
	function changeFilter(item) {
		$('#video_filter div').removeClass('selected');
		item.addClass('selected');
	}
	
	
	function scroll2 (target) {
		$('html,body').animate({
			scrollTop: $(target).offset().top
		}, 1000 , function (){/*location.hash = ziel;*/});
	}
	
	
	function showMailTo() {
		$('#set_note_startpoint').bind('click', function()				{setStartPoint();});
		$('#frm_advise').hide();
		$('#frm_advise').html('');
		$(':input#absender_name').val('');
		$(':input#absender_mail').val('');
		$(':input#empfaenger_name').val('');
		$(':input#empfaenger_email').val('');

		if ($('#mail').css('display') == "none") {
			$('#syndication').children().hide();
			$('#mail').slideDown(700 , function (){scroll2('#mail');})
		} else {
			$('#syndication').children().hide();
		}
		$('#absender_name').val($('#username').html());
		$('#absender_mail').val($('#usermail').html());
	}

	function showLink() {
		if ($('#link').css('display') == "none") {
			$('#syndication').children().hide();
			$('#link').slideDown(500 , function (){scroll2('#link');});
		} else {
			$('#syndication').children().hide();
		}
	}



	function init_Sendungsauswahl() {
		$('#sendungen_auswahl').children('.sendung').each(function(index) {
			$(this).mouseover(function() {
				$(this).children('div.subtitle').show();
				$(this).children('div.title_hl').show();
				$(this).children('div.title').hide();
			});
			$(this).mouseout(function() {
				$(this).children('div.subtitle').hide();
				$(this).children('div.title_hl').hide();
				$(this).children('div.title').show();
			});
		});
		$('#livestream_btn').children('.sendung').each(function(index) {
			$(this).mouseover(function() {
				$(this).children('div.subtitle').show();
				$(this).children('div.title_hl').show();
				$(this).children('div.title').hide();
			});
			$(this).mouseout(function() {
				$(this).children('div.subtitle').hide();
				$(this).children('div.title_hl').hide();
				$(this).children('div.title').show();
			});
		});
	}

	function init_videoScroll() {
		if ($('#videos').height() < $('#videos_traeger').height() )	 {
			$('#debug').html(
				$('#debug').html() + '$(#videos).height(): ' + $('#videos').height() + '<br>' + '$(#videos_traeger).height(): ' + $('#videos_traeger').height()
			);
		}
	}

	function slider() {
		$('#videos').jScrollPane();
	}


	function init_VideoInfos() {
		$('#video_traeger').children('.video').each(function(index) {
			var btn = $(this).find('.video_info_btn');
			var info = $(this).find('.more_infos');
			$(btn).attr({'alt':'anzeigen'});
			$(btn).mouseover(		function() {$(this).attr({alt:'anzeigen',src: 'http://www.nrw.tv/static/gfx/videoarchiv/btn-infomehr-2.png'});});
			$(btn).mouseout(		function() {$(this).attr({alt:'anzeigen',src: 'http://www.nrw.tv/static/gfx/videoarchiv/btn-infomehr-1.png'});});

			$(btn).click(function() {
				if ($(btn).attr('alt') == 'anzeigen') {
					closeVideoInfos($(btn));
					$(btn).attr({'alt':'zuklappen','src': 'http://www.nrw.tv/static/gfx/videoarchiv/btn-infoclose-2.png'});
					$(btn).mouseover(		function() {$(this).attr({'alt':'zuklappen','src': 'http://www.nrw.tv/static/gfx/videoarchiv/btn-infoclose-2.png'});});
					$(btn).mouseout(		function() {$(this).attr({'alt':'zuklappen','src': 'http://www.nrw.tv/static/gfx/videoarchiv/btn-infoclose-1.png'});});
					$(info).slideDown();
				} else {
					$(btn).attr({'alt':'anzeigen',src: 'http://www.nrw.tv/static/gfx/videoarchiv/btn-infomehr-2.png'});
					$(btn).mouseover(		function() {$(this).attr({'alt':'anzeigen',src: 'http://www.nrw.tv/static/gfx/videoarchiv/btn-infomehr-2.png'});});
					$(btn).mouseout(		function() {$(this).attr({'alt':'anzeigen',src: 'http://www.nrw.tv/static/gfx/videoarchiv/btn-infomehr-1.png'});});
					$(info).slideUp();
				}
			});
		});
	}

	function closeVideoInfos(btn) {
		//debug('closeVideoInofs');
		$('#video_traeger').children('.video').each(function(index) {
			if ($(this) != btn) {
			var btn = $(this).find('.video_info_btn');
			var info = $(this).find('.more_infos');		
			$(btn).attr({'alt':'anzeigen',src: 'http://www.nrw.tv/static/gfx/videoarchiv/btn-infomehr-1.png'});
			$(btn).mouseover(		function() {$(this).attr({'alt':'anzeigen',src: 'http://www.nrw.tv/static/gfx/videoarchiv/btn-infomehr-2.png'});});
			$(btn).mouseout(		function() {$(this).attr({'alt':'anzeigen',src: 'http://www.nrw.tv/static/gfx/videoarchiv/btn-infomehr-1.png'});});
			$(info).slideUp();
			}
		});	
	}

	function loadVideosFromCat(cat_id,start,anzahl,item) {
		var cat_url = 'http://player.tneg.de/dataprovider/nrw.tv/category/getClipsPerJSON.php';
		$('#video_traeger').html('');
		$('#video_loader').show();
		if (start == undefined) start=0;
		if (anzahl == undefined) anzahl=6;
		
		if (cat_id == undefined) {
			cat_id = active_cat_id;
		} else {
			active_cat_id = cat_id;
		}
		
		if (item != undefined) var state = highlightActiveCat(item);
		
		//allgemeine oder kat-videos werden geladen
		if (state == "default") {
			loadVideosFromCat();
			return true;
		}
		
		$.ajax({
			url: cat_url,
			data: "cat_id="+cat_id+"&start="+start+"&anzahl=" + anzahl + "&callback=?",
			dataType: "json",
			beforeSend: function(data) {
				$('#video_loader').show();
			},
			success: function(msg){
				if (msg) {$('#video_traeger').html(msg);}
			},
			complete: function() {
				$('#video_loader').hide();
				changeFilter($('#filter_newest'));
				suche_zuruecksetzen();
				init_VideoInfos();	
			}
		});
	}
	
	
	function highlightActiveCat(item) {
		
		if (active_cat_item==item) {
			$(active_cat_item).mouseout(function() {
				$(active_cat_item).children('div.title_hl').hide();
				$(active_cat_item).children('div.title').show();	
			});
			active_cat_item = null;
			active_cat_id = 0;
			return "default";
		} else {
			//altes item zu
			$(active_cat_item).children('div.title_hl').hide();
			$(active_cat_item).children('div.title').show();	
			active_cat_item = item;
			$(active_cat_item).mouseout(function() {
				$(active_cat_item).children('div.title_hl').show();
				$(active_cat_item).children('div.title').hide();	
			});
			$(active_cat_item).addClass('active');
			return "cat";
		}
	}

	function loadRatedVideos(start,anzahl) {
		var cat_url = 'http://player.tneg.de/dataprovider/nrw.tv/rating/getClipsPerJSON.php';
		$('#video_traeger').html('');
		$('#video_loader').show();
		if (start == undefined) start=0;
		if (anzahl == undefined) anzahl=6;
		if (active_cat_id != undefined) cat_id = active_cat_id;
		$.ajax({
			url: cat_url,
			data: "start="+start+"&anzahl=" + anzahl + "&cat_id="+cat_id+"&callback=?",
			dataType: "json",
			beforeSend: function(data) {
				$('#video_loader').show();
			},
			success: function(msg){
				if (msg) {$('#video_traeger').html(msg);}
			},
			complete: function() {
				$('#video_loader').hide();
				suche_zuruecksetzen();
				init_VideoInfos();	
			}
		});
	}
	
	function loadVideoPlaylist(start,anzahl) {
		var cat_url = 'http://player.tneg.de/dataprovider/nrw.tv/playlist/getVideoitems.php';
		$('#video_traeger').html('');
		$('#video_loader').show();
		if (start == undefined) start=0;
		if (anzahl == undefined) anzahl=6;
		if (active_cat_id != undefined) cat_id = active_cat_id;
		$.ajax({
			url: cat_url,
			data: "start="+start+"&anzahl=" + anzahl + "&cat_id="+cat_id+"&callback=?",
			dataType: "json",
			beforeSend: function(data) {
				$('#video_loader').show();
			},
			success: function(msg){
				if (msg) {$('#video_traeger').html(msg);}
			},
			complete: function() {
				$('#video_loader').hide();
				suche_zuruecksetzen();
				init_VideoInfos();	
			}
		});
	}
	
	
	
	
	
	var init_video_timer;
	var video_timer_count = 1;
	function loadVideoFromURL(url) {
		var provider_url = 'http://player.tneg.de/dataprovider/nrw.tv/videoclip/getVideoclipFromURL.php';

		$.ajax({
			url: provider_url,
			data: "url="+url+"&callback=?",
			dataType: "json",
			beforeSend: function(data) {
			},
			success: function(msg){
				if (msg) {	
					if (actionscript_status == true || video_timer_count >6) {
						$('#script_area').html(msg);	
					} else {
						window.clearTimeout(init_video_timer);
						init_video_timer = window.setTimeout("loadVideoFromURL('"+url+"');", 1000);
						video_timer_count++;
					}
				}
			},
			complete: function() {	
			}
		});
	}
	
	function loadVideoInformationFromVideoUrl(url) {

		var provider_url = 'http://player.tneg.de/dataprovider/nrw.tv/videoclip/getVideoClipInformationPerJSON.php';
		$('#information').html('');
		$.ajax({
			url: provider_url,
			data: "url="+url+"&dev="+dev+"&callback=?",
			dataType: "json",
			beforeSend: function(data) {
			},
			success: function(msg){
				if (msg) {$('#videoinformation').html(msg);}
			},
			complete: function() {
				prepareMailFrm();
				setRating();
			}
		});
	}
	
	function loadVideoFromShortURL(url) {
		var provider_url = 'http://player.tneg.de/dataprovider/nrw.tv/videoclip/getVideoclipFromURL.php';

		$.ajax({
			url: provider_url,
			data: "shorturl="+url+"&callback=?",
			dataType: "json",
			beforeSend: function(data) {
			},
			success: function(msg){
				if (msg) {	
					if (actionscript_status == true || video_timer_count >6) {
						$('#script_area').html(msg);	
					} else {
						window.clearTimeout(init_video_timer);
						init_video_timer = window.setTimeout("loadVideoFromShortURL('"+url+"');", 1000);
						video_timer_count++;
					}
				}
			},
			complete: function() {	
			}
		});
	}

	
	
	function searchVideoClipsByTitle(start,limit,title,order) {
		$('#filter_newest').unbind();
		$('#filter_rated').unbind();
		$('#filter_newest').bind('click',function() {changeFilter($(this));searchVideoClipsByTitle(0,6,title,'date')});
		$('#filter_rated').bind('click',function() {changeFilter($(this));searchVideoClipsByTitle(0,6,title,'rating')});
		if (start == undefined) start=0;
		if (limit == undefined) limit=6;
		if (title == undefined) title='';
		if (order == undefined) order='date';
		
		var s_title=$('#suche_input').val();
		
		if (s_title != "" && s_title != " " && s_title != "  ") {
			var cat_url = 'http://player.tneg.de/dataprovider/nrw.tv/videoclip/searchVideoClipsByTitlePerJSON.php';
			$('#video_traeger').html('');
			$('#video_loader').show();
			$("#suche_loeschen").fadeOut(100);
			$("#suche_loader").fadeIn(200);			
				$.ajax({
					url: cat_url,
					data: "s_title="+s_title+"&start="+start+"&limit="+limit+"&order="+order+"&callback=?",
					dataType: "json",
					beforeSend: function(data) {
					},
					success: function(msg){
						if (msg) {$('#video_traeger').html(msg);}
					},
					complete: function() {
						  $("#suche_loader").fadeOut(400);
						  $("#suche_loeschen").fadeIn(600);
						  $('#video_loader').hide();
						  init_VideoInfos();
						  if (order != 'rating')changeFilter($('#filter_newest'));
						  if (order == 'rating')changeFilter($('#filter_rated'));
					}
				});
		}		
	}

	function suchfeld(item) {
		if ($(item).val()=='Suchbegriff eingeben...') {
			$(item).val('');
		} else if ($(item).val()=='') {
			$(item).val('Suchbegriff eingeben...');
		}	
	}
	function suche_zuruecksetzen() {
		
		  $("#suche_result").html('');
		  $("#suche_result_trager").slideUp(400);
		  $('#suche_input').val('Suchbegriff eingeben...');
		  
		  $('#filter_newest').unbind();
		  $('#filter_rated').unbind();
		  $('#filter_newest').bind('click',function() {changeFilter($(this));loadVideosFromCat()});
		  $('#filter_rated').bind('click',function() {changeFilter($(this));loadRatedVideos()});
	}

	
	function prepareMailFrm() {
		initRateVideo();
		$("#mailvideo").validate({
			errorLabelContainer: "#frm_advise",
			rules: {
				absender_name :{required: true},
				absender_mail: {required: true, email: true},
				empfaenger_name :{required: true},
				empfaenger_email: {required: true, email: true}
			},
			messages: {
				absender_name: {
					required: "Bitte geben Sie Ihren Namen an.<br>"
				},
				absender_mail: {
					required: "Bitte geben Sie Ihre E-Mail-Adresse an!<br>",
					email: "Ihre E-Mail muss im Format 'name@domain.de' sein.<br>"
				},
				empfaenger_name: {
					required: "Bitte geben Sie den Namen des Empf&auml;nger an.<br>"
				},
				empfaenger_email: {
					required: "Die Empf&auml;nger E-Mail fehlt<br>",
					email: "Die Empf&auml;nger E-Mail muss im Format 'name@domain.de' sein.<br>"
			     }
			 }
		});
	}
	
	
	function sendVideoToFriend(frm) {

		$('#frm_advise').hide();
		$('#frm_advise').html('');
		$(':input#absender_name').css('border-color','#CCCCCC');
		$(':input#absender_mail').css('border-color','#CCCCCC');
		$(':input#empfaenger_name').css('border-color','#CCCCCC');
		$(':input#empfaenger_email').css('border-color','#CCCCCC');
		
		if ($(':input#absender_name').val() != "" && $(':input#absender_mail').val() != "" && $(':input#empfaenger_name').val() != "" && $(':input#empfaenger_email').val() != "") {
			$('#frm_advise').html('Ihre E-Mail wurde versendet.');
			$('#frm_advise').css('color','#22bb22');
			$('#frm_advise').show();
			
			var cat_url = '/videoarchiv/broadcast/send_video.php';
			$.ajax({
				url: cat_url,
				data: $(frm).serialize()+"&callback=?",
				dataType: "json",
				beforeSend: function(data) {
				},
				success: function(msg){
					if (msg) {$('#frm_advise').html($('#frm_advise').html() +"<br> Server-Nachricht: "+msg);}
				},
				complete: function() {
					window.setTimeout("$('#frm_advise').fadeIn(800,function () {$('#mail').fadeOut(2000,function(){scroll2('#b');})});", 1000);
				}
			});		
			
		} else {
			if ($(':input#absender_name').val() == "") 		$(':input#absender_name').css('border-color','#CC3333');
			if ($(':input#absender_mail').val() == "")		$(':input#absender_mail').css('border-color','#CC2222');
			if ($(':input#empfaenger_name').val() == "")	$(':input#empfaenger_name').css('border-color','#CC2222');
			if ($(':input#empfaenger_email').val() == "")	$(':input#empfaenger_email').css('border-color','#CC2222');
			$('#frm_advise').show();
		}			
	}


	
	function actionscript_error(e) {
		debug(e,"red");
	}


	function toggleDebug() {
		$('#debug').fadeOut();
		if ($('#debug').css('display') != "none") getVoDPlayer().toggleDebug();
	}
	
	function togglePlayerDebug() {
		getVoDPlayer().toggleDebug();
	}


	
	
	function debug(msg,color) {
	//	$('#debug').show();
		$('#debug').width('40%');
		$('#debug').height('30%');
		$('#debug').css('font-size','11px');
		
		if (color != undefined)	if (color != false) msg = "<span style='color:"+color+"'>"+msg+"</span>";
		$('#debug').html($('#debug').html() + msg + "<br/>");	
	}
	
