var StartUrl = "";
var CurrentUrl = String(decodeURIComponent(window.location)).split('#!')[0].split('?')[0];
var CurrentHash, ProjectTracking = '';

$(document).ready(function()
{
	// Init
	var ajaxRequestInfos;
	var ajaxRequestStoryboard;
	
	// Init Homepage
	current_showcase = null;
	current_showcase_target = null;
	sHtmlStartPlusLoading = '<p class="loading">LOADING ...</p>';
	sHtmlStartPlus = '<li class="plus on"><div class="arrow"></div><a href="#" class="close"></a><div class="inner">' + sHtmlStartPlusLoading + '</div></li>';
	
	// Init Player
	current_active = null;
	current_zoom = null;
	sHtmlPlayerLoading = '<div class="loading"></div>';
	
	/* -----------------------------------
	 * GLOBAL
	 * ----------------------------------- */
	
	// Introduction
	$("div.showcase ul li div.top div.anim").each(function(i){
	  d = 2000 + (Math.random()*1000);
	  $(this).delay(d).fadeIn("slow");
	});
	
	// Function
	function NoHttp(s) {
		s = s.replace("http://", "");
		return(s);
	}
	
	
	/* -----------------------------------
	 * HOMEPAGE
	 * ----------------------------------- */
	
	// Button
	/*$("ul li.project a.link1").click(function(){
		if (location.hash != "#!" + $(this).attr("rel")) UpdateHash("#!" + $(this).attr("rel"));
		else UpdateHash("#!home");
		return false;
	});*/
	
	if ($("#project-tracking").length >= 1) ProjectTracking = $("#project-tracking").val();
	CheckHash();
	$(window).hashchange(function(){
		CheckHash();
	});
	
	// Function
	function CheckHash()
	{
		var hash = location.hash;
		
		if (hash != '')
		{
			hash = hash.split("#!");
			hash = String(hash[1]);
		}
		
		if (hash == '' && ProjectTracking != '') hash = ProjectTracking;

		if (hash != '' && hash != 'home')
		{
			if ($("#player").is(":visible")) {
				player_close();
			}
			
			ProjectTracking = '';
			CurrentHash = hash;
			var rel = hash;
			var item = $("#"+hash);
			var target = item.nextAll("li.pos3").eq(0);
			var plus = $("ul li.plus");
			var pos = "pos" + (item.is(".pos1") ? 1 : (item.is(".pos2") ? 2 : 3));
			var div = item.closest("ul").parent();
			
			$("ul li.project").removeClass("active");
			
			if (div.is(".showcase")) {
				if (item.is(".pos3")) target = item;
				else if (target.length == 0) target = $("div.showcase ul li.project").last();
			} else if (div.is(".archive")) {
				target = item;
			}
			
			if (plus.length > 0) {
				// Plus is open
				if (div.is(".showcase") && target.attr("id") == current_showcase_target.attr("id")) {
					// On same line
					plus.find("div.inner").html(sHtmlStartPlusLoading);
					LoadInfos(rel);
				} else {
					// On new line
					$("ul li.plus").addClass("off").animate(
						{height: "0px"},
						{queue:false, duration:"fast", complete:function(){$(this).remove();OpenInfos(target, rel, pos);}}
					);
				}
			} else {
				// Plus is close
				OpenInfos(target, rel, pos);
			}
			
			item.addClass("active");
			current_active = item;
			plus.removeClass("pos1").removeClass("pos2").removeClass("pos3").addClass(pos);
			
			current_showcase = rel;
			current_showcase_target = target;
		}
		else if (hash == 'home')
		{
			$("ul li.project.active").removeClass("active");
			current_showcase = null;
			$("ul li.plus").animate(
				{height: "0px"},
				{queue:false, duration:"slow", complete:function(){$(this).remove();}}
			);
		}

		// Lien share
		/*if (!hash_film && CurrentFilmIndex == 0) {
		  if (tag_id != 0) {
				$("p.process span").html(SetShareUrlTag(window.location, tag_id));
				UpdateShareUrl(SetShareUrlTag(window.location, tag_id));
		  } else if (director_id == 0) {
				$("p.process span").html(String(window.location));
				UpdateShareUrl(window.location);
			} else {
				$("p.process span").html(SetShareUrlReal(window.location, director_id));
				UpdateShareUrl(SetShareUrlReal(window.location, director_id));
			}
		} else {
			if (tag_id != 0) {
				$("p.process span").html(SetShareUrlTagFilm(window.location, tag_id, hash_film[0]));
				UpdateShareUrl(SetShareUrlTagFilm(window.location, tag_id, hash_film[0]));
		  } else {
		  	$("p.process span").html(SetShareUrl(window.location, hash_film[0]));
		  	UpdateShareUrl(SetShareUrl(window.location, hash_film[0]));
	  	}
		}*/
	}
	
	function UpdateShareUrl(url) {
		/*$("div.share-directors a").attr("addthis:url", encodeURIComponent(url));
		window.addthis.ost = 0;
		window.addthis.ready();*/
	}
	
	function UpdateHash(v) {
		window.location = CurrentUrl + v;
	}
	
	function OpenInfos(target, rel, pos) {
		$("body").scrollTo(target, 500, {axis:'y'});
		target.after(sHtmlStartPlus);
		$("ul li.plus.on").addClass(pos).animate(
			{height: "138px"},
			{queue:false, duration:"fast", complete:function(){LoadInfos(rel);}}
		);
		AddCommandToPlus();
	}
	
	function AddCommandToPlus() {
		$("ul li.plus a.close").click(function(){
			UpdateHash("#!home");
			return false;
		});
	}
	
	function LoadInfos(id) {
		if (ajaxRequestInfos) ajaxRequestInfos.abort();
		ajaxRequestInfos = $.ajax({url:StartUrl+'/infos/'+id, dataType:"xml", success:function(xml){
			var sHTML = "";
			
			// COL 1
			sHTML += '<div class="col1">';
			if ($(xml).find('agence').text()) sHTML += '<h2>AGENCE</h2>' + $(xml).find('agence').text();
			if ($(xml).find('client').text()) sHTML += '<h2>CLIENT</h2>' + $(xml).find('client').text();
			sHTML += '</div>';
			
			// COL 2
			sHTML += '<div class="col2">';
			sHTML += '<h2>NOTRE INTERVENTION</h2>' + $(xml).find('long_text').text();
			sHTML += '</div>';
			
			// COL 3
			sHTML += '<div class="col3">';
			if ($(xml).find('website_url').text() || $(xml).find('website_url').text()) sHTML += '<h2 class="special">LIENS</h2>';
			if ($(xml).find('website_url').text()) sHTML += '<a href="' + $(xml).find('website_url').text() + '" class="link" target="_blank">' + NoHttp($(xml).find('website_url').text()) + '</a>';
			if ($(xml).find('storyboard_url').text()) sHTML += '<a href="#" class="link storyboard" rel="' + $(xml).find('storyboard_url').text() + '">Voir les Story-Boards</a>';
			if ($(xml).find('more').text()) sHTML += '<h2 class="spacer">ÉGALEMENT</h2><p>' + $(xml).find('more').text() + '</p>';
			sHTML += '<div class="addthis_toolbox addthis_default_style "><a addthis:url="'+CurrentUrl+'?project='+id+'" class="share addthis_button">Share</a></div>';
			sHTML += '<div class="clear"></div></div>';

			sHTML += '<div class="clear"></div>';
			
			$("ul li.plus div.inner").html(sHTML);
			
			window.addthis.ost = 0;
			window.addthis.ready();
			
			$("ul li.plus").animate(
				{height: ($("ul li.plus div.inner").height()+100) + "px"},
				{queue:false, duration:"fast"}
			);
			
			AddPlayerLink();
		}});
	}
	
	function AddPlayerLink() {
		$("ul li.plus a.storyboard").click(function(){
			GetStoryboard($(this).attr("rel"));
			return false;
		});
	}
	
	function GetStoryboard(dir) {
		$("#player span.pager").html("&nbsp;");
		$("#player-content").show().html(sHtmlPlayerLoading);
		$("#player").show();
		$("#content").hide();
		$("#footer").addClass("player");
		$("body").scrollTo(0, 0, {axis:'y'});
		
		if (ajaxRequestStoryboard) ajaxRequestStoryboard.abort();
		ajaxRequestStoryboard = $.ajax({url:StartUrl+'/storyboard/get.php?d='+dir, dataType:"xml", success:function(xml){
			FileFound = 0;
			sHTML = "<h2>" + current_active.find("h1").html() + "</h2>";
			$(xml).find('item').each(function(){
				var d = $(this).find('dir').attr("d");
				var n = $(this).find('dir').text();
				sHTML += '<div class="inner"><span class="title">' + String(unescape(n)).substring(3) + '</span>';
				sHTML += '<ul>';
				$(this).find('file').each(function(){
					FileFound++;
					sHTML += '<li><a href="#"><img src="/storyboard/' + d + "/" + n + "/-thumbnails/" + encodeURI($(this).text()) + '" /></a></li>';
				});
				sHTML += '</ul><div class="clear"></div></div>';
			});
			
			if (FileFound == 0) sHTML += '<div class="inner"><span class="title">Aucun storyboard disponible.</span></div>';
			
			$("#player-content").show().html(sHTML);
			$("#player").show();
			$("#content").hide();
			$("#footer").addClass("player");
			$("body").scrollTo(0, 0, {axis:'y'});
			
			/*$("#player-content li img").lazyload({ 
				effect : "fadeIn"
			});*/
			$("#player-content a").click(function(){
				ZoomImg($("#player-content a").index(this));
				return false;
			});
		}});
	}
	
	
	/* -----------------------------------
	 * PLAYER
	 * ----------------------------------- */
	
	function getSlug(str) {
		str = str.replace(/^\s+|\s+$/g, '');
		var from = "ÀÁÄÂÈÉËÊÌÍÏÎÒÓÖÔÙÚÜÛàáäâèéëêìíïîòóöôùúüûÑñÇç·/_,:;";
		var to   = "aaaaeeeeiiiioooouuuuaaaaeeeeiiiioooouuuunncc------";
		for (var i=0, l=from.length ; i<l ; i++) str = str.replace(from[i], to[i]);
		str = str.replace(/[^a-zA-Z0-9 -]/g, '').replace(/\s+/g, '-').replace(/-+/g, '-').toLowerCase();
		return str;
	}
});
