//var url = "/bio/generator/BIO/Navigation/DE/Service/Videos/qc-videos-gesamt?view=renderVideoSliderXml";			
//var url = "/BIO/Navigation/DE/Service/Videos/qc-videos-gesamt?view=renderVideoSliderXml";

$(document).ready(function(){
  // Defaulteinstellung: Alle  Kategorien
  var category = "category_0";
  if(typeof videoUrl!= "undefined") {
  	url = videoUrl;
    
	  $.get(url, function(xml) { 
	  	  
	  	  var cat = getParam('category');
  	
		  if(typeof cat != "undefined"){
			  	$(".navi").find(".current").removeClass("current");
				$(".navi").find("."+cat).parent().addClass("current");
				getXML(cat);
		  }else {
		   	  // Alle Kategorien werden angezeigt 
		  	  getXML(category);
	  	  }
	  	  
	      $(".videoSliderWrapper .navi a").click(function () {
	      	 // Kategorie wird aus der Linkklasse gelesen.
	      	 category = $(this).attr("class");
			 
			 $(this).parent().parent().find(".current").removeClass("current");
			 $(this).parent().addClass("current");
			 
			 
	      	 // Vorher Funktionsaufruf den Contentbereich leeren...
	      	 $(".videoSliderWrapper .content").empty();
	      	 // Scrollpfeile zurücksetzen
	     	 $('a.scrollUp').fadeTo(0,0.33).css("cursor","default");
	     	 $('a.scrollDown').fadeTo(0,1.0).css("cursor","pointer");     	    			     
		     // Teaser der entspr. Kategorie anzeigen
		     getXML(category); 	     
		     return false;
		  }); 
		  
		  function getXML(category) {
			 jQuery.each($("slider",xml).children(category).children("entry"), function() {
			 	 // Scrollposition zurücksetzen
		     	$(".content").scrollTo("0px",10);	     	
			  	var title = $(this).children("title").text(); 
			  	var image = $(this).children("image").text(); 
			  	var href = $(this).children("url").text();    
			    var index = title.indexOf(":");
				if(index<0) {
					var name = title;
					var desc = "";
				} else {// split name and description after colon
					var name = title.substr(0, index+2);
					var desc = title.substr(index+2, title.length);
				} 			  
			    $(".videoSliderWrapper .content").append('<span><a href="' + href + '"><img src="' + image + '" alt="' + title + '" height="94" width="124" /></a><span class="title"><a href="' + href + '">' + name + ' </a></span><span class="subtitle">' + desc + '</span></span>');
			  });
			 
			  if($("slider",xml).children(category).children("entry").length == 0) {
			     var val= $("#emptyVideoMsg").html();
			  	 $(".videoSliderWrapper .content").append('<div class="novideo">'+ val +'</div>');
			  }
			  //Scrollpfeile abblenden, wenn alle Teaser im sichtbaren Bereich liegen
		   	  if($(".content").children("span").length <= 12) {
		    	 $('a.scrollUp').fadeTo(0,0.33).css("cursor","default");
		    	 $('a.scrollDown').fadeTo(0,0.33).css("cursor","default");
		      } 
		  }
	    
		  /* Scrollfunktionalitäten */
		  var spanHeight = $(".content span").outerHeight({ margin: true });  
		  $('a.scrollUp').fadeTo(0,0.33).css("cursor","default");
		  $('a.scrollUp').click(function(){	 
		  		$(".content").scrollTo("-=" + spanHeight*2 + "",1000);			
				if($(".content span:last").position().top >= 279) {
					$('a.scrollDown').fadeTo(1000,1.0).css("cursor","pointer");
				}		
				if($(".content span:first").position().top >= -330) {
					$(this).fadeTo(1000,0.33).css("cursor","default");									
				}
				return false;		
		  });
		  
		  $('a.scrollDown').click(function(){ 		
				$(".content").scrollTo("+=" + spanHeight*2 + "",1000);						
				if($(".content span:last").position().top <= 609) {
					$(this).fadeTo(1000,0.33).css("cursor","default");
				} 
				if($(".content span:first").position().top <= 104) {
					$('a.scrollUp').fadeTo(1000,1.0).css("cursor","pointer");
				} 
				return false;
				
		  });	  
		  
		  $('a.scrollDown').focus(function(){
			 $(this).blur();
		  });
		  
		  $('a.scrollUp').focus(function(){
			 $(this).blur();
		  });   	
	  		  	
	  });  
  }
});