var isSlidedDown = false;
var videoUrlListPlaylist = new Array();

onload = function(){
	
	//console.debug(window.location.pathname);
	
	if(gup("playlist")){
		playlistVideoIds = gup("playlist");
		playlistVideoIdArray = playlistVideoIds.split(",");
		//console.debug(playlistVideoIdArray);
		videoIdArray = playlistVideoIdArray;
		
		$.cookie("playlistVideos", videoIdArray, {path : "/", expires : 365});
		loadPlaylistVideos();
	}
	else{
		loadPlaylistVideos();
	}
	if(gup("playplaylist")){
		getVideoUrlList(videoIdArray);
		//console.debug(videoUrlListPlaylist);
		//console.debug(videoIdArray[0]);
	}
	
	animateThumbnails();
	
	$("#footerMailJs").append($("<a/>").text("ft@filmtecknarna.se").attr("href", "mailto:ft@filmtecknarna.se"));
	
	$(".searchButton").mouseover(function(){
		$(".searchButton").css("backgroundImage", "url('../../graphics/searchButton_02.jpg')");
	});
	$(".searchButton").mouseout(function(){
		$(".searchButton").css("backgroundImage", "url('../../graphics/searchButton_01.jpg')");
	});
	
	$(".embedTableData").click(function(){
		$(".embedTextField").show();
	});

	$(".addToPlaylistTableData").click(function(){
		//console.debug("hej");
		addVideoToPlaylist(this.id, false);
	})
	
	
	address = new JSAddress;
	
	if(address.getValue()){
		videoId = address.getValue();
		$("#playerWrapper").slideDown(200);
		isSlidedDown = true;
		loadFlashPlayer(videoId);
	}
	
		$(".thumbnailsMainWrapper").click(function(event){
			var thumbnailTarget = event.currentTarget;
			var children = $(thumbnailTarget).children();
			var videoSource = children[0].href;
			var videoId = children[0].id;
			address.setValue(videoId);
			
			if(!isSlidedDown){
				slideDownPlayer(videoId);
			}
			else{
				loadFlashPlayer(videoId);
			}
		});
		
		setPlaylistPosition();
		resetPlaylist();
		playlistOnclick();
		
		if(window.location.pathname == "/work/"){
			$("#playlistPlayButton").click(function(){
					startPlaylist();
				return false;
			});
		}
		else{
			$("#playlistPlayButton").click(function(){
					window.location = "http://www.filmtecknarna.se/work/?playplaylist=1";
				return false;
			});
		}

		$(".addToPlaylistButton").click(function(event){
			var thumbnailTarget = event.currentTarget;
			var parent = thumbnailTarget.parentNode;
			var grandparent = parent.parentNode;
			var children = $(grandparent).children()
			var videoId = children[0].id;

			playlistLimiter++;

			if(playlistSlidedDown == 1){
				playlistSlidedDown = -playlistSlidedDown;
				$("#playlistBody").slideDown(0, resetPlaylist());
			}
			
			addVideoToPlaylist(videoId, false);
			
			if(playlistLimiter > 5){
				addPaging();
			}

			return false;
			
		})

}


function getVideoUrlList(videoIds){
	for(var i = 0;i < videoIds.length;i++){
		$.post("getPlaylistVideoInfo.php", {videoId : videoIds[i]}, getVideoUrlListCallback, "json");
	}
}

function getVideoUrlListCallback(data){
	videoUrlListPlaylist.push(data.video_url);
	//console.debug(data);
	//console.debug(videoUrlListPlaylist);

	$("#playerWrapper").slideDown(200);
	isSlidedDown = true;
	FT_playListManager.loadPlaylistPlayer(videoUrlListPlaylist);
	
}


function slideDownPlayer(videoId){
	$("#playerWrapper").slideDown(200, loadFlashPlayer(videoId));
	isSlidedDown = true;
}

function gup( name ){
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null ){
    	return "";						
	}
	else{
		return results[1];
	}
}


function loadFlashPlayer(videoId){

	getVideoInfo(videoId);

}

function animateThumbnails(){
	var i = 1;
	var thumbnailWrapper = new Array();
	
	while(thumbnails = document.getElementById("thumbnails"+i)){
		thumbnailWrapper.push(thumbnails);
		i++;
	}
	
	for(var i = 0;i < thumbnailWrapper.length; i++){
		thumbnailsAnimation(thumbnailWrapper[i]);
	}
}


function getVideoInfo(videoId){
	$.post("getVideoInfo.php",{videoId : videoId} ,getVideoInfoCallback, "json");
}

function getVideoInfoCallback(data){
	

	var theFlashVars = {	theSourcePath : data.video_url,
							autoPlay : true	};
	var theParameters = { 	allowfullscreen:"true",
							wmode : "opaque" };
	
	isSwfPlayerLoaded = false;
	swfobject.embedSWF("../../flash/videoPlayerFT.swf", "theFlashPlayer", "677", "381", "9.0.0", "scr/expressInstall.swf", theFlashVars, theParameters);

	displayVideoData(data);
	
	document.title = "FilmTecknarna - " + data.videoTitle;

}

function getPlaylistVideoIds(){
	videoIdArrayNew = videoIdArray.join(",");
	
	return videoIdArrayNew;
}

function displayVideoData(videoData){
	$(".videoTitle").empty();
	$(".videoTitle").text(videoData.videoTitle);
	
	//console.debug(videoData);
	
	$(".moreFilms a").empty();
	
	$(".addToPlaylistTableData").attr("id", videoData.video_id);
	
	//console.debug(videoData);
	var directorsIds = videoData.directors_ids;
	//console.debug(directorsIds);
	var directors = videoData.director;
	directors = directors.split("&");
	for(var j = 0;j < directors.length;j++){
		$(".moreFilms").append($("<a/>").text(directors[j]).attr("href", "http://www.filmtecknarna.se/directors/work/?director=" + directorsIds[j]));
	}
		
	var embedText = "<object width='677' height='381'><param name='movie' value='http://www.filmtecknarna.se/flash/videoPlayerFT.swf'></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param><param name='FlashVars' value='theSourcePath="+ videoData.video_url +"'></param><embed src='http://www.filmtecknarna.se/flash/videoPlayerFT.swf' type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width='677' height='381' FlashVars='theSourcePath="+ videoData.video_url +"'></embed></object>";
	
	$(".embedTextField").hide();
	$(".embedTextField").val(embedText);
	$(".embedButton").click(function(){
		$(".embedTextField").show();
	})
	
	if(videoData.category_id == 1){
		//displayCommercialInfo(videoData);

		$(".teamRow").hide();
		$(".recordArtistRow").hide();
		$(".recordCompanyRow").hide();

		$(".videoDirector").empty()
		$(".videoDirector").text(videoData.director)
		if(videoData.director != ""){
			$(".directorRow").show();
		}
		else{
			$(".directorRow").hide();
		}

		$(".videoProductionCompany").empty()
		$(".videoProductionCompany").text(videoData.production_company);
		if(videoData.production_company != ""){
			$(".productionCompanyRow").show();
		}
		else{
			$(".productionCompanyRow").hide();
		}

		$(".videoAgency").empty()
		$(".videoAgency").text(videoData.agency);
		if(videoData.agency != ""){
			$(".agencyRow").show();
		}
		else{
			$(".agencyRow").hide();
		}

		$(".videoProducer").empty()
		$(".videoProducer").text(videoData.producer);
		if(videoData.producer != ""){
			$(".producerRow").show();
		}
		else{
			$(".producerRow").hide();
		}

		$(".videoClient").empty()
		$(".videoClient").text(videoData.client);
		if(videoData.client != ""){
			$(".clientRow").show();
		}else{
			$(".clientRow").hide();
		}
		
	}

	if(videoData.category_id == 2){
		//displayMusicVideoData(videoData);
		$(".agencyRow").hide();
		$(".productionCompanyRow").hide();
		
		$(".videoDirector").empty()
		$(".videoDirector").text(videoData.director)
		if(videoData.director != ""){
			$(".directorRow").show();
		}
		else{
			$(".directorRow").hide();
		}
		
		$(".videoTeam").empty()
		$(".videoTeam").text(videoData.team)
		if(videoData.team != ""){
			$(".teamRow").show();
		}
		else{
			$(".teamRow").hide();
		}

		$(".videoRecordArtist").empty()
		$(".videoRecordArtist").text(videoData.record_artist)
		if(videoData.record_artist != ""){
			$(".recordArtistRow").show();
		}
		else{
			$(".recordArtistRow").hide();
		}

		$(".videoRecordCompany").empty()
		$(".videoRecordCompany").text(videoData.record_company)
		if(videoData.record_company != ""){
			$(".recordCompanyRow").show();
		}
		else{
			$(".recordCompanyRow").hide();
		}

		$(".videoProducer").empty()
		$(".videoProducer").text(videoData.producer);
		if(videoData.producer != ""){
			$(".producerRow").show();
		}
		else{
			$(".producerRow").hide();
		}
	}
	if(videoData.category_id == 3){
		//displayShortData(videoData);
		$(".agencyRow").hide();
		$(".clientRow").hide();
		$(".recordArtistRow").hide();
		$(".recordCompanyRow").hide();


		$(".videoDirector").empty()
		$(".videoDirector").text(videoData.director)
		if(videoData.director != ""){
			$(".directorRow").show();
		}
		else{
			$(".directorRow").hide();
		}
		

		$(".videoProducer").empty()
		$(".videoProducer").text(videoData.producer);
		if(videoData.producer != ""){
			$(".producerRow").show();
		}
		else{
			$(".producerRow").hide();
		}


		$(".videoProductionCompany").empty()
		$(".videoProductionCompany").text(videoData.production_company);
		if(videoData.production_company != ""){
			$(".productionCompanyRow").show();
		}
		else{
			$(".productionCompanyRow").hide();
		}


		$(".videoTeam").empty()
		$(".videoTeam").text(videoData.team)
		if(videoData.team != ""){
			$(".teamRow").show();
		}
		else{
			$(".teamRow").hide();
		}
	}
	if(videoData.category_id == 4){
		//displayBroadcastDesignData(videoData);
		$(".teamRow").hide();
		$(".agencyRow").hide();
		$(".clientRow").hide();
		$(".recordArtistRow").hide();
		$(".recordCompanyRow").hide();
		
		$(".videoDirector").empty()
		$(".videoDirector").text(videoData.director)
		if(videoData.director != ""){
			$(".directorRow").show();
		}
		else{
			$(".directorRow").hide();
		}
		

		$(".videoProducer").empty()
		$(".videoProducer").text(videoData.producer);
		if(videoData.producer != ""){
			$(".producerRow").show();
		}
		else{
			$(".producerRow").hide();
		}


		$(".videoProductionCompany").empty()
		$(".videoProductionCompany").text(videoData.production_company);
		if(videoData.production_company != ""){
			$(".productionCompanyRow").show();
		}
		else{
			$(".productionCompanyRow").hide();
		}
		delete videoData;
	}
}