// JavaScript Document
//States 0:Default, 1:Image, 2:Video
var DisplayState = 0;
var currentImage;
var currentVideo;
var VPflashvars = {};
		
/* flv player parameters */		
//VPflashvars.playerpath = swfobject.getQueryParamValue("playerpath");
VPflashvars.playerpath = "http://dev.emjot.de/kpl/swf/videoplayer";
VPflashvars.contentpath = "http://dev.emjot.de/kpl/flv";
						
//VPflashvars.loop = swfobject.getQueryParamValue("loop");
			
VPflashvars.skin = "custom2.swf";
//VPflashvars.skincolor = swfobject.getQueryParamValue("skincolor");
VPflashvars.skinscalemaximum = 2;				

//VPflashvars.smoothing = swfobject.getQueryParamValue("smoothing");		

VPflashvars.autoscale = false;
VPflashvars.autoplay = swfobject.getQueryParamValue("autoplay");
VPflashvars.videowidth = 380;
VPflashvars.videoheight = 285;

//VPflashvars.captions = swfobject.getQueryParamValue("captions");
//VPflashvars.preroll = swfobject.getQueryParamValue("preroll");

//VPflashvars.preloader = swfobject.getQueryParamValue("preloader");
//VPflashvars.buttonoverlay = swfobject.getQueryParamValue("buttonoverlay");

//VPflashvars.islive = swfobject.getQueryParamValue("islive");

//VPflashvars.debug = true;
/* end */		
			
var VPparams = {};
VPparams.scale = "noscale";
VPparams.allowfullscreen = "true";
VPparams.salign = "tl";

var VPattributes = {};
VPattributes.align = "left";


function displayPic(picIndex){
	$("content").scrollTo();
	if(picIndex != currentImage){
		if(currentImage != undefined){
			$("gImages").getElementsByClassName("item")[currentImage].hide();
		}
		$("gImages").getElementsByClassName("item")[picIndex].show();
		if($("gImages").getElementsByClassName("item")[picIndex].down("span.image") != undefined){
			var imgCode = "<img src='" + $("gImages").getElementsByClassName("item")[picIndex].down("span.image").innerHTML + "' alt='" + $("gImages").getElementsByClassName("item")[picIndex].down("h3").innerHTML + "' class='exhibit' />";
			$("gImages").getElementsByClassName("item")[picIndex].down("span.image").replace(imgCode);
		}
		currentImage = picIndex;
		if($("gImages").getElementsByClassName("item").length > 1){
			if(currentImage == 0){
				$("gImages").down("div.functions").down("ul").down("li.prev").down("a").hide();
				$("gImages").down("div.functions").down("ul").down("li.prev").down("span.inactive").show();
			} else {
				$("gImages").down("div.functions").down("ul").down("li.prev").down("a").show();
				$("gImages").down("div.functions").down("ul").down("li.prev").down("span.inactive").hide();
			}			
			if(currentImage == $("gImages").getElementsByClassName("item").length - 1){
				$("gImages").down("div.functions").down("ul").down("li.next").down("a").hide();
				$("gImages").down("div.functions").down("ul").down("li.next").down("span.inactive").show();				
			} else {
				$("gImages").down("div.functions").down("ul").down("li.next").down("a").show();
				$("gImages").down("div.functions").down("ul").down("li.next").down("span.inactive").hide();				
			}
		}
	}
	if(DisplayState != 1){
		setDisplayState(1);
	}
}

function previousPic(){
	displayPic(currentImage - 1);
}

function nextPic(){
	displayPic(currentImage + 1);
	
}

function setDisplayState(state){
	if(state != DisplayState){
		if(DisplayState == 2){
			deleteVid();
		}
		if(state == 0){
			$("navi").show();
			$("content").show();			
			$("gallery").hide();
		} else if(state == 1){
			$("navi").hide();
			$("content").hide();			
			$("gallery").show();
			if ($("gVideos") != null) { $("gVideos").hide(); }
			$("gImages").show();
			if($("gImages").getElementsByClassName("item").length > 1){
				$("gImages").down("div.functions").down("ul").show();
			} else {
				$("gImages").down("div.functions").down("ul").hide();
			}
		} else if(state == 2){
			$("navi").show();
			$("content").hide();			
			$("gallery").show();
			if ($("gImages") != null) { $("gImages").hide(); }
			$("gVideos").show();
			if($("gVideos").getElementsByClassName("item").length > 1){
				$("gVideos").down("div.functions").down("ul").show();
			} else {
				$("gVideos").down("div.functions").down("ul").hide();
			}
		}
		DisplayState = state;
	}
}

function displayVid(vidIndex){
	$("content").scrollTo();
	if(vidIndex != currentVideo){
		if(currentVideo != undefined){
			deleteVid();
		}
		$("gVideos").getElementsByClassName("item")[vidIndex].show();
		if($("gVideos").getElementsByClassName("item")[vidIndex].down("span.image") != undefined){			
		/* embed flv player */			
		// adapt the path to flvplayer.swf and expressInstall.swf
		// adapt the display size of the flash file
		displaySWFVideo("videoDisplay", $("gVideos").getElementsByClassName("item")[vidIndex].down("span.image").innerHTML, $("gVideos").getElementsByClassName("item")[vidIndex].down("span.video").innerHTML);
		/*VPflashvars.preview = $("gVideos").getElementsByClassName("item")[vidIndex].down("span.image").innerHTML;
		VPflashvars.video = $("gVideos").getElementsByClassName("item")[vidIndex].down("span.video").innerHTML;
		//swfobject.embedSWF("../swf/videoplayer/flvplayer.swf", "videoDisplay", "380", "304", "9.0.28", "expressInstall.swf", VPflashvars, VPparams, VPattributes);
		insertFlash("../swf/videoplayer/flvplayer.swf", "videoDisplay", "380", "304", "9.0.28", VPflashvars, VPparams, VPattributes);
		*/
		/* end */
		}
		currentVideo = vidIndex;
		if($("gVideos").getElementsByClassName("item").length > 1){
			if(currentVideo == 0){
				$("gVideos").down("div.functions").down("ul").down("li.prev").down("a").hide();
				$("gVideos").down("div.functions").down("ul").down("li.prev").down("span.inactive").show();
			} else {
				$("gVideos").down("div.functions").down("ul").down("li.prev").down("a").show();
				$("gVideos").down("div.functions").down("ul").down("li.prev").down("span.inactive").hide();
			}			
			if(currentVideo == $("gVideos").getElementsByClassName("item").length - 1){
				$("gVideos").down("div.functions").down("ul").down("li.next").down("a").hide();
				$("gVideos").down("div.functions").down("ul").down("li.next").down("span.inactive").show();				
			} else {
				$("gVideos").down("div.functions").down("ul").down("li.next").down("a").show();
				$("gVideos").down("div.functions").down("ul").down("li.next").down("span.inactive").hide();				
			}
		}
	}
	if(DisplayState != 2){
		setDisplayState(2);
	}
}

function previousVid(){
	displayVid(currentVideo - 1);
}

function nextVid(){
	displayVid(currentVideo + 1);
	
}

function deleteVid(){
	$("videoDisplaySWF").remove();
	$("gVideos").getElementsByClassName("item")[currentVideo].hide();
	currentVideo = undefined;
}

function insertFlash(swf, container, width, height, version, vars, params, attributes) {
	$(container).innerHTML = "<div id='" + container + "SWF'></div>";
	swfobject.embedSWF(swf, container + "SWF", width, height, version, "swf/expressInstall.swf", vars, params, attributes);
}

/**
 * Displays FLV Video in a Flash video player.
 * @param the_id		ID of the element which should be replaced by the player (element will be made visible)
 * @param the_preview	absolute path of the preview image (no host/port necessary)
 * @param the_video		absolute path of the flv video file (no host/port necessary)
 *
 * @todo FIXME this function is currently used by the rails backend, clickdummy and rails usage will need to be aligned later
 */
function displaySWFVideo(the_id, the_preview, the_video) {
  // remove any leading / trailing whitespace
  the_preview = (the_preview==null) ? "" : the_preview.strip();
  the_video = (the_video==null) ? "" : the_video.strip();
  if (!the_preview.empty() && !the_video.empty()) {
	// settings
	var VPflashvars = {};
	VPflashvars.playerpath = "/swf/videoplayer";
	VPflashvars.skin = "custom2.swf";
	VPflashvars.skinscalemaximum = 2;	
	VPflashvars.autoscale = false;
	VPflashvars.autoplay = swfobject.getQueryParamValue("autoplay");
	VPflashvars.videowidth = 380;
	VPflashvars.videoheight = 285;
	VPflashvars.contentpath = ''; // absolute path needs to be specified in the_preview and the_video (no host/port necessary)
	/*VPflashvars.debug = true;*/
	var VPparams = {};
	VPparams.scale = "noscale";
	VPparams.allowfullscreen = "true";
	VPparams.salign = "tl";
	var VPattributes = {};
	VPattributes.align = "left";
	// set the preview image and video files (potential query string in given URLs will be removed before) 
	VPflashvars.preview = the_preview.sub(/\?.*/i,''); 
	VPflashvars.video = the_video.sub(/\?.*/i,''); 
	// show and start video player
	$(the_id).show();
	$(the_id).innerHTML = "<div id='" + the_id + "SWF' />";
	swfobject.embedSWF("/swf/videoplayer/flvplayer.swf", the_id + "SWF", "380", "304", "9.0.28", "swf/expressInstall.swf", VPflashvars, VPparams, VPattributes);
  }
}
