/**
 * Klasse, um das Abspielen von Videos f?r die VideoFeatureBoxCoverflow zu unterst?tzen.
 * @param baseid
 * @param maxItems
 * @param actionRoot
 * @param imageRoot
 * @param sectionColor
 * @param isAllage
 * @return
 */
function FOLGE3_VIDEOFEATURECOVERFLOW(baseid, maxItems, actionRoot, imageRoot, sectionColor, isAllage){ 
	this.isAllage = isAllage;
	this.baseid="#"+baseid;
	this.maxItems = maxItems;
	this.current = 1;
	this.imageRoot=imageRoot;
	this.actionRoot=actionRoot;
	this.color=sectionColor;
	this.imageDir="shared/arrow_round_btn/"
	// allage path setzen
	if(this.isAllage)	this.imageDir="allage/icons/";
	this.width = 405;
	if(this.isAllage)	this.width = 370;

	//those values are browser specific so they come from the css
	//this.outerWidth = parseInt($("#coverflow_navi_values_when_navi_visible").css("width"));
	//this.marginLeft = parseInt($("#coverflow_navi_values_when_navi_visible").css("margin-left"));
//	alert("width: "+this.outerWidth+"     margin-left: "+this.marginLeft)
	if (this.maxItems > 1) {
		$(this.baseid+" #next").show();
		$(this.baseid+" #prev").show();
		//$(this.baseid+" .coverflow-block").css("width", this.width);
		//$(this.baseid+" .coverflow_navi_outer").css("width", this.outerWidth);
		//$(this.baseid+" .coverflow_videonavi_outer").css("width", this.outerWidth);
		//$(this.baseid+" .videofeaturebox_ie_width").css("margin-left", this.marginLeft);
	}

	
	//alert("baseid"+baseid+ " maxItems:"+maxItems+ " width:"+width+ " imageRoot:" +imageRoot);
	
	this.hasNext = function(){
		//alert("hasNext");
		return this.current < this.maxItems;
		
	};
	
		
	this.hasPrev = function(){
			return this.current > 1;
		};
		
	this.next = function(){
		
		if(! this.hasNext()) return;
			
			//alert("Next a");
			
			this.scrollLeft(this.current);
			
			//alert("Next b");
			
			this.current +=1;
			this.scrollLeft(this.current);
			
			this.updatePrevNextButtons();
		};
		
	this.prev = function(){
			if(!this.hasPrev()) return;
			this.scrollRight(this.current);
			this.current -=1;
			this.scrollRight(this.current);
			this.updatePrevNextButtons()
		};
		
	this.scrollLeft = function (index){
			var elem = $(this.baseid+" #coverflow_block-"+index);
			//alert("elem"+elem.css("left")+"xx");
			var leftTarget = parseInt(elem.css("left")) - this.width;
			//alert(leftTarget);
			elem.animate({ 
					left:  leftTarget
					//opacity: 0.4,
				  }, 1000
				  );
		};
	 
	this.scrollRight = function (index){
		var elem = $(this.baseid+" #coverflow_block-"+index);
		var leftTarget = parseInt(elem.css("left")) +this.width;
		elem.animate({ 
				left:  leftTarget
				//opacity: 0.4,
			  }, 1000 );
		};
		
	 this.updatePrevNextButtons = function() {
		   // wegen allage eingefuegt
		   var leftArrow;
		   var leftNoActivArrow;
		   var rightArrow;
		   var rightNoActivArrow;
		   
		   if(this.hasNext()){
			    (this.isAllage)? rightArrow = this.imageDir+"arrow_right.png" : rightArrow = this.imageDir+"next_"+this.color+".gif";
			   
				$(this.baseid+" #next-button").attr("src", this.imageRoot+rightArrow);
				$(this.baseid+" #next-button").addClass("pointer");
			}
			else {
				(this.isAllage)? rightNoActivArrow = this.imageDir+"arrow_right_ro.png" : rightNoActivArrow = this.imageDir+"next_no_activ_"+this.color+".gif";
				$(this.baseid+" #next-button").attr("src", this.imageRoot+rightNoActivArrow);
				$(this.baseid+" #next-button").removeClass("pointer");
			}
			if(this.hasPrev()){
				

			    (this.isAllage)? leftArrow = this.imageDir+"arrow_left.png" : leftArrow = this.imageDir+"prev_"+this.color+".gif";
				
				$(this.baseid+" #prev-button").attr("src", this.imageRoot+leftArrow);
				$(this.baseid+" #prev-button").addClass("pointer");
			}
			else {
				(this.isAllage)? leftNoActivArrow = this.imageDir+"arrow_left_ro.png" : leftNoActivArrow = this.imageDir+"prev_no_activ_"+this.color+".gif";
				$(this.baseid+" #prev-button").attr("src", this.imageRoot+leftNoActivArrow);
				$(this.baseid+" #prev-button").removeClass("pointer");
			}
		
	};
	
	/**
	 * Spielt den flv mit der ?bergebenen id im SWF mit der appid.
	 * Wenn das SWF noch nicht geladen ist, dass heisst, die loadXML-Methode
	 * noch nicht erreichbar ist, dann wird in einem Interval gewartet, bis
	 * das SWF da ist.
	 */
	this.playMovie = function(appId, videoid, teaserNr){
		var waitForFlashInterval;
		var swf = this.getApp(appId);
		var caller = this;
		
		if(typeof(swf.loadXml) == "function") {
			this.startMovie(appId, videoid);
		}
		else if(waitForFlashInterval == null)
		{
			waitForFlashInterval = setInterval(function(){
				if(typeof(swf.loadXml) != "function") return;
				caller.startMovie(appId, videoid);
				clearInterval(waitForFlashInterval);
				waitForFlashInterval = null;
			}, 1000);
		}
	}
	
	this.getApp = function(appName) {
		
		/*
		$(".teaser_container").hide();
		$("#flashvideocontainer").show();
		$(".prozent, td.prozent a").hide();
		$(".videoprozent ,td.videoprozent a").show();
			
		*/
		
		if (navigator.appName.indexOf ("Microsoft") !=-1) {
		       window[appName];
		       return document[appName];
		   } 
		   else {
		       return document[appName];
		}
	}
	
	/**
	 * Spielt das Movie mit der id videoid im Player appid
	 */
	this.startMovie = function(appId, videoid){
		var swf = this.getApp(appId);
		swf.loadXml(this.actionRoot+'ajax/videofeaturebox_ajax?id='+videoid); 
	}
}
