//javascript we use in festivalblog
$(document).ready(function() {
	var wettHead = "Wettbewerb 2010";
	var panHead =  "Panorama 2010";
	var forumHead = "Forum 2010";
	var persHead = "Perspektive Deutsches Kino 2010";
	var headArray = new Array();
	headArray[44] = wettHead;
	headArray[45] = panHead;
	headArray[46] = forumHead,
	headArray[47] = persHead;
	$(".hnavSectionMovie_element").click(function(){
		var sectionIdValue = $(this).attr('id');
		var sectionText = headArray[sectionIdValue];
		$.getJSON('/rest/section_movie_titles/index.php', { sectionid: sectionIdValue}, function(data){
			getMovieListSectionMin(data, sectionText);
		})		
	})
})

function getMovieListSectionMin(data, sectionText){
	var movies = data;
	var listHtml = new String;
	$("#alpha-inner").fadeOut("normal", function(){
		$("#alpha-inner").empty();
		listHtml += "<h2 style='margin-top: 5px; margin-bottom: 20px;'>"+sectionText+"</h2>";
		$.each(movies, function(key, val) {
			listHtml  += getHeadlineHTML(val);
		})
        $("#alpha-inner").append(listHtml);			
		$("#alpha-inner").wrapInner("<div id=\"klappe\" style=\"margin-bottom: 40px;\"></div>");
		$("#alpha-inner").fadeIn("normal");
		$(".movietitle").click(function(){
			var id = $(this).attr('id');
			var infoId = "#info_"+id;
			var clickedTitle = this;
			var currentArrow = $(this).children(":first");
			if( "" ===  $(infoId).text()){
			  $.getJSON('/rest/movies/index.php', {movieid: $(this).attr('id')}, function(data){
				  var divId = "#info_"+data["id"]; 
				  $(divId).append(getCreditHTML(data));
				  $(divId).toggle('fold',{},"fast");
				  $(clickedTitle).toggleClass("active");
	          })
		    } else {
				  $(infoId).toggle('fold',{},"fast");
				  $(clickedTitle).toggleClass("active");
		    }
		})
	})
}

function getMovieListSection(data){
	var movies = data;
	$("#alpha-inner").fadeOut(2000, function(){
		//$("#alpha-inner").empty();
		$.each(movies, function(key, val) {
	        var creditHtml = getCreditHTML(val);
	        $("#alpha-inner").append(creditHtml);			
		})
		//$("#alpha-inner").append("</div>");
		$("#alpha-inner").wrapInner("<div id=\"klappe\"></div>");
		$("#alpha-inner").fadeIn(2000);
		/*$(function() {
			$("#klappe").accordion();
		})*/
		$(".movietitle").click(function(){
			$(this).next("div").toggle('fold',{},"slow").css("color:red");
		})
	});

}
/*
 * HTML view of credits to a specific movie
 */
function getCreditHTML(creditData) {
	var movie_id = creditData["id"];
	var title_de = creditData["title_de"];
	var title_org = creditData["title_org"];
	var title_en = creditData["title_en"];
	var title_alternative = new String();
	if (title_de.length > 0 && title_de !== title_org) {
		title_alternative = title_de;
	} else {
		if (title_en.length > 0 && title_en !== title_org) {
			title_alternative = title_en;
		}
	}
	var cast = creditData["cast"];
	var countries = creditData["countries"];
	var duration = creditData["duration"];
	var year = creditData["year"];
	var shortdesc = creditData["shortdesc"];
	var articleLink = creditData["article"];
	var creditHtml = new String;
	var castHTML = getCastHTML(cast);
	var countriesString = getCountriesLight(countries);
	var countriesYearDuration = countriesString + ", " + year + "; " + duration+" Minuten";
	var headlineHtml = getHeadlineHTML(creditData);	
	creditHtml += "<div class='movieinfo'>";
	creditHtml += "<p>"+countriesYearDuration+"</p>";
	creditHtml += castHTML;
	if(shortdesc.length > 0 ){
	  creditHtml += "  <p class='credit_element'>Kurzbeschreibung</p><p class='credit_content'>" + shortdesc + "</p>\n";
	}	
	if(articleLink){
		creditHtml += "<img src=\"/images/film_edit.png\" style='padding-right: 10px;'/>";
		creditHtml += "<a href=\""+articleLink+"\" style='vertical-align: top;'>Festivalblog Kritik</a>";
	}	
    creditHtml += "</div>";
	return creditHtml;
}

function getHeadlineHTML(creditData){
	var headlineHTML = new String;
	var title_de = creditData["title_de"];
	var title_org = creditData["title_org"];
	var title_en = creditData["title_en"];
	var has_article = creditData["has_article"]
	var title_alternative = new String();
	if (title_de.length > 0 && title_de !== title_org) {
		title_alternative = title_de;
	} else {
		if (title_en.length > 0 && title_en !== title_org) {
			title_alternative = title_en;
		}
	}
	var movie_id = creditData["id"];
	headlineHTML += "<h4 style='cursor:pointer;line-height: 2' class='movietitle ui-widget-header ui-corner-all' id='"+movie_id+"'>";
	headlineHTML += "<span style='padding: 0.5em 0.5em 0.5em 2.2em;text-decoration: none'>"+title_org+"</span>";
	if (title_alternative.length > 0) {
		headlineHTML += " (" + title_alternative + ")";
	}
	if(has_article == true){
		headlineHTML += "<span class='has_article'/>";	
	}	
	var movieInfoId = "info_"+movie_id;
	headlineHTML += "</h4>";
	headlineHTML += "<div class='movieframe ui-widget-content ui-corner-all' id='"+movieInfoId+"'></div>";
	
	return headlineHTML;
}


/*
 * HTML view of the cast (director, actor etc.) to a specific movie
 */

function getCastHTML(cast) {
	var castHTML = new String();
	castHTML += "<div class=\"credits_list\">\n";
	for (position in cast) {
		var team = cast[position];
		castHTML += "    <p class=\"position_list part_function\">" + position
				+ "</p>\n";
		// console.log(position);
		// console.log(team.length);
		for ( var i = 0; i < team.length; i++) {
			var person = team[i];
			castHTML += "      <p class=\"person_list\">";
			castHTML += person["first"] + " " + person["last"];
			castHTML += "</p>\n";
			// console.log(person["first"]+" "+person["last"]);
		}
	}
	castHTML += "</div>";
	return castHTML;
}

function getCountriesLight(countries){
	var countryAll = new Array();
	for (countryKey in countries) {
		var country = countries[countryKey];
		if(country["short"] != null){
		  countryAll.push((country["short"]));
		}
	}
	return countryAll.join(", ");
}