var photoViewerCurrentImageDefaultWidth = 0;
var photoViewerCurrentImageDefaultHeight = 0;

$(document).ready(function()
{
	$(".videos-wrapper").width($(".video-image").length * 90);
	
	$(".block").each(function()
	{
		var thisBlock = $(this);
		thisBlock.find(".heading").click(function()
		{
			var thisHeading = $(this);
			var visibleBlockWrappers = $(".block .block-content-wrapper:visible");
			
			if ($("#video-player").length > 0)
			{
				var video = document.getElementsByTagName('video')[0];
				video.pause();
			}
			
			var nameId = thisHeading.parent().attr("id");
			if ($(".navigation a[href=#"+nameId+"]").length > 0)
			{
				if ($(".navigation a[href=#"+nameId+"]").parent().hasClass("selected"))
				{
					$(".navigation li").removeClass("selected");
					$(".navigation a[href=#intro]").parent().addClass("selected");
				}
				else
				{
					$(".navigation li").removeClass("selected");
					$(".navigation a[href=#"+nameId+"]").parent().addClass("selected");
				}
			}
			
			if (!thisBlock.find(".block-content-wrapper").is(":visible"))
			{
				//showing
				$(".block .heading a:first.selected").removeClass("selected");
				thisBlock.find(".heading a:first").addClass("selected");
				thisBlock.find(".block-content-wrapper").width(0);
				visibleBlockWrappers.animate({ width: 0 }, {step: function(now, fx)
				{
					thisBlock.find(".block-content-wrapper").show().width(741-now);
					
					if (now == 0) visibleBlockWrappers.hide();
				}});
				$('.latest-news-wrapper').jScrollPane({ showArrows: true });
			}
			else
			{
				//hiding
				var newBlock;
				switch(thisBlock.attr("id"))
				{
					case "press":
						$("#intro").parent().width(0);
						thisBlock.find(".block-content-wrapper").animate({ width: 0 }, { step: function(now, fx)
						{
							$("#intro").parent().show().width(741-now);
							
							if (now == 0) thisBlock.find(".block-content-wrapper").hide();
						}});
						break;
					case "video":
						newBlock = $(".block#press");
						$(".block .heading a:first.selected").removeClass("selected");
						newBlock.find(".heading a:first").addClass("selected");
						newBlock.find(".block-content-wrapper").width(0);
						visibleBlockWrappers.animate({ width: 0 }, {step: function(now, fx)
						{
							newBlock.find(".block-content-wrapper").show().width(741-now);
							
							if (now == 0) visibleBlockWrappers.hide();
						}});
						$('.latest-news-wrapper').jScrollPane({ showArrows: true });
						break;
					case "audio":
						newBlock = $(".block#video");
						$(".block .heading a:first.selected").removeClass("selected");
						newBlock.find(".heading a:first").addClass("selected");
						newBlock.find(".block-content-wrapper").width(0);
						visibleBlockWrappers.animate({ width: 0 }, {step: function(now, fx)
						{
							newBlock.find(".block-content-wrapper").show().width(741-now);
							
							if (now == 0) visibleBlockWrappers.hide();
						}});
						$('.latest-news-wrapper').jScrollPane({ showArrows: true });
						break;
					default:
						$("#intro").parent().width(0);
						$(".block .heading a:first.selected").removeClass("selected");
						thisBlock.find(".block-content-wrapper").animate({ width: 0 }, { step: function(now, fx)
						{
							$("#intro").parent().show().width(741-now);
							
							if (now == 0) thisBlock.find(".block-content-wrapper").hide();
						}});
						break;
				}
			}
			
			$('.videos').jScrollPane({ showArrows: true });
			
			return false;
		});
	});
	
	$(".video-image").hover(
		function(e)
		{
			var thisVideo = $(this);
			thisVideo.find(".pop-title").show();
		},
		function()
		{
			var thisVideo = $(this);
			thisVideo.find(".pop-title").hide();
		}
	);
	
	$(".video-image").mousemove(function(e)
	{
		var thisVideo = $(this);
		thisVideo.find(".pop-title").css("left", e.layerX + 10);
		thisVideo.find(".pop-title").css("top", e.layerY - 50);
	});
		
	$(".player .startControl").click(function()
	{
		if ($(this).hasClass("button-play"))
		{
			$(this).removeClass("button-play");
			$(this).addClass("button-pause");
			$("#jplayer_play_time").jPlayer("play");
		}
		else
		{
			$(this).removeClass("button-pause");
			$(this).addClass("button-play");
			$("#jplayer_play_time").jPlayer("pause");
		}		
		return false;
	});
	
	$(".player .progress-sound .handle").draggable({
		axis: 'x',
		containment: 'parent',
		drag: function(event, ui)
		{
			//parseFloat($(this).css("left")) - 8) / 183 * 100
			var volume = (parseFloat($(this).css("left")) - 8) / 47 * 100;
			$("#jplayer_play_time").jPlayer("volume", volume);
			$(".progress-sound .progress").css({'width': volume + "%"});
		}
	});
	
	$(".pop-contact").click(function()
	{
		$(this).fadeOut(300);
	});
	
	$(".close").click(function()
	{
		$("#bio").parent().fadeOut(400);
		$(".block").each(function()
		{
			if (!$(this).find("#bio").length > 0)
				$(this).fadeIn(400);
		});
		$(".block .heading").fadeIn(400);
		
		$(".pop-contact").hide();
		$(".block #bio, .block #intro, .block #zoomermedia, .block #photoviewer").parent().hide();
		$(".block .block-content-wrapper:visible").hide();
		$(".block .heading").show();
		
		$("#intro").show();
		$("#intro").parent().width("auto").show();
		$(".block .heading").show();
		
		return false;
	});
			
	$(".navigation li a, .back-to-menu").click(function()
	{
		$(".navigation li.selected").removeClass("selected");
		$(this).parent().addClass("selected");
		
		if ($(this).attr("href") != "#bio" && $("#bio").is(":visible") && $(this).attr("href") != "#contact")
		{
			$("#bio").parent().fadeOut(400);
			$(".block").each(function()
			{
				if (!$(this).find("#bio").length > 0)
					$(this).fadeIn(400);
			});
			$(".block .heading").fadeIn(400);
		}
		
		if ((($(this).attr("href") != "#zoomermedia" && $("#zoomermedia").is(":visible")) || ($(this).attr("href") != "#photoviewer" && $("#photoviewer").is(":visible"))) && $(this).attr("href") != "#contact")
		{
			$("#zoomermedia, #photoviewer").parent().fadeOut(400);
			$(".block").each(function()
			{
				if (!$(this).find("#zoomermedia, #photoviewer").length > 0)
					$(this).fadeIn(400);
			});
			$(".block .heading").fadeIn(400);
		}
		
		if ($("#video-player").length > 0)
		{
			var video = document.getElementsByTagName('video')[0];
			video.pause();
		}
		
		if ($(this).attr("href") == "#contact")
		{
			$(".pop-contact").css("bottom", "-220px").css("left", "50%");
			
			var angle =  Math.floor(Math.random()*70 - 35);
			$(".pop-contact").css("-webkit-transform", "rotate(" + angle + "deg)");
			$(".pop-contact").css("transform", "rotate(" + angle + "deg)");
			$(".pop-contact").css("-moz-transform", "rotate(" + angle + "deg)");
			$(".pop-contact").show();
			$(".pop-contact").animate({bottom: Math.floor(Math.random()*200)+100, left: (Math.floor(Math.random()*10)+40)+"%" }, 200);
		}
		else
		{
			$(".pop-contact").hide();
			$(".block #bio, .block #intro, .block #zoomermedia, .block #photoviewer").parent().hide();
			$(".block .block-content-wrapper:visible").hide();
			$(".block .heading").show();
		}
		
		if ($(this).attr("href") == "#bio")
		{
			$(".block").each(function()
			{
				if (!$(this).find("#bio").length > 0)
					$(this).fadeOut(400);
			});
			
			$($(this).attr("href")).fadeIn(400);
			$($(this).attr("href")).parent().width("auto").fadeIn(400, function() { $("#bio .bio-content").fadeIn(400, function() { $('.bio-content').css("overflow", "auto").jScrollPane({ showArrows: true	}); }); });
			$("#bio .bio-content").css("overflow", "hidden").hide();
			
			$(".block .heading").hide();
		}
		else if ($(this).attr("href") == "#zoomermedia")
		{
			$(".block").each(function()
			{
				if (!$(this).find("#zoomermedia").length > 0)
					$(this).fadeOut(400);
			});
			$("#zoomermedia").fadeIn(400);
			$("#zoomermedia").parent().width("auto").fadeIn(400);
			$('.zoomermedia-wrapper').jScrollPane({ showArrows: true });
			
			$(".block .heading").hide();
		}
		else if ($(this).attr("href") == "#intro")
		{
			$(".block .heading a:first.selected").removeClass("selected");
			$($(this).attr("href")).show();
			$($(this).attr("href")).parent().width("auto").show();
			$(".block .heading").show();
		}
		else if ($($(this).attr("href")).find(".block-content-wrapper").length > 0)
		{
			$($(this).attr("href")).find(".heading").show();
			$(".block .heading a:first.selected").removeClass("selected");
			$($(this).attr("href")).find(".heading a:first").addClass("selected");
			$($(this).attr("href")).find(".block-content-wrapper").show().width("741px");
			$('.latest-news-wrapper').jScrollPane({ showArrows: true });
		}
		
	
		$('.videos').jScrollPane({ showArrows: true });		
		
		return false;
	});
	
	$(".press-board .press-image").click(function()
	{
		$(".press-board .press-image.selected").removeClass("selected");
		$(this).addClass("selected");
		
		$(".preview-info").html('\
			<img src="' + $(this).find(".full_image").html() + '" alt="" />\
			<h5>' + $(this).find(".title").html() + '</h5>\
			<small>' + $(this).find(".date").html() + '</small>\
		');
		
		return false;
	});
	
	$(".video-image:first").each(function()
	{
		$(".video-image.selected").removeClass("selected");
		$(this).addClass("selected");
		
		if ($(this).find(".video_youtube").html().length > 0)
			$(".block-video").html('\
				<div class="video-holder">\
					<object width="480" height="320"><param name="movie" value="http://www.youtube.com/v/'+$(this).find(".video_youtube").html()+'?rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+$(this).find(".video_youtube").html()+'?rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="320"></embed></object>\
				</div><!-- end of video holder -->\
				<h2>' + $(this).find(".title").html() + '</h2>\
				<small>' + $(this).find(".date").html() + '</small>\
				' + $("#vi-" + $(this).find(".extraid").html()).find(".description").html() + '\
			');
		else
			$(".block-video").html('\
				<div class="video-holder">\
					<video width="480" height="320" id="video-player" poster="" src="' + $(this).find(".video_mp4").html() + '" controls="true" autobuffer="true">\
					</video>\
				</div><!-- end of video holder -->\
				<h2>' + $(this).find(".title").html() + '</h2>\
				<small>' + $(this).find(".date").html() + '</small>\
				' + $("#vi-" + $(this).find(".extraid").html()).find(".description").html() + '\
			');
		
		return false;
	});
	
	$(".video-image").click(function()
	{
		$(".video-image.selected").removeClass("selected");
		$(this).addClass("selected");
		
		if ($(this).find(".video_youtube").html().length > 0)
			$(".block-video").html('\
				<div class="video-holder">\
					<object width="480" height="320"><param name="movie" value="http://www.youtube.com/v/'+$(this).find(".video_youtube").html()+'?rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+$(this).find(".video_youtube").html()+'?rel=0" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="320"></embed></object>\
				</div><!-- end of video holder -->\
				<h2>' + $(this).find(".title").html() + '</h2>\
				<small>' + $(this).find(".date").html() + '</small>\
				' + $("#vi-" + $(this).find(".extraid").html()).find(".description").html() + '\
			');
		else
			$(".block-video").html('\
				<div class="video-holder">\
					<video width="480" height="320" id="video-player" poster="" src="' + $(this).find(".video_mp4").html() + '" controls="true" autobuffer="true">\
					</video>\
				</div><!-- end of video holder -->\
				<h2>' + $(this).find(".title").html() + '</h2>\
				<small>' + $(this).find(".date").html() + '</small>\
				' + $("#vi-" + $(this).find(".extraid").html()).find(".description").html() + '\
			');
		
		return false;
	});
	
	$(".audio-slider li").click(function()
	{
		$(".audio-slider li.selected").removeClass("selected");
		$(this).addClass("selected");
		
		$(".audio-preview-container").html('\
			<h3>' + $(this).find(".audio_title").html() + '</h3>\
			<small>' + $(this).find(".audio_date").html() + ' <span class="duration">[x min]</span></small>\
			<div class="line">\
				<span>Interviewer</span>\
				' + $(this).find(".audio_interviewer").html() + '\
			</div><!-- end of line -->\
			<div class="line">\
				<span>Topic</span>\
				' + $(this).find(".audio_topic").html() + '\
			</div><!-- end of line -->\
		');
		
		$(".audio-description").html($(this).find(".audio_description").html())
		
		
		var i = $(".audio-slider li").index($(this));
		var m = $(".audio-slider li").length;
		var a = i;
		
		if (i+1 == m) a = i-1;
		var maxOffset = - ((a<1?1:a) - 1)*135;
		
		$(".audio-slider ul").animate({left: maxOffset}, 300, 'linear');
		$("#jplayer_play_time").jPlayer({"swfPath": "/wp-content/themes/moses/js/Jplayer.swf"});
		$("#jplayer_play_time").jPlayer("setFile", $(this).find(".audio_file").html());
		$("#jplayer_play_time").jPlayer("solution", "html");
		
		return false;
	});
	
	$(".audio-slider li:first").each(function()
	{
		$(".audio-slider li.selected").removeClass("selected");
		$(this).addClass("selected");
		
		$(".audio-preview-container").html('\
			<h3>' + $(this).find(".audio_title").html() + '</h3>\
			<small>' + $(this).find(".audio_date").html() + ' <span class="duration">[x min]</span></small>\
			<div class="line">\
				<span>Interviewer</span>\
				' + $(this).find(".audio_interviewer").html() + '\
			</div><!-- end of line -->\
			<div class="line">\
				<span>Topic</span>\
				' + $(this).find(".audio_topic").html() + '\
			</div><!-- end of line -->\
		');
		
		$(".audio-description").html($(this).find(".audio_description").html())
		
		
		var i = $(".audio-slider li").index($(this));
		var m = $(".audio-slider li").length;
		var a = i;
		
		if (i+1 == m) a = i-1;
		var maxOffset = - ((a<1?1:a) - 1)*135;
		
		$(".audio-slider ul").animate({left: maxOffset}, 300, 'linear');
		$("#jplayer_play_time").jPlayer({"swfPath": "/wp-content/themes/moses/js/Jplayer.swf"});
		$("#jplayer_play_time").jPlayer("setFile", $(this).find(".audio_file").html());
		$("#jplayer_play_time").jPlayer("solution", "html");	
		
		return false;
	});
	
	$(".switchBio").click(function()
	{
		if($(".short_biography").is(":visible"))
		{
			$(".short_biography").hide();
			$(".full_biography").show();
			$(this).html("View short version");
			
			$('.bio-content').jScrollPane({ showArrows: true });
		}
		else
		{
			$(".short_biography").show();
			$(".full_biography").hide();
			$(this).html("View full version");
			
			$('.bio-content').jScrollPane({ showArrows: true });
		}
		
		return false;
	});
	
	$(".preview-info img").live("click", function()
	{
		$(".block").each(function()
		{
			if (!$(this).find("#photoviewer").length > 0)
				$(this).fadeOut(400);
		});
		$("#photoviewer").fadeIn(400, function(){ populatePhotoViewer(); });
		$("#photoviewer").parent().width("auto").fadeIn(400);
		
		$(".block .heading").hide();
	});
})

function populatePhotoViewer()
{
	$(".photoviewer .photoviewer-thumbs").html("");
	$(".photoviewer .photoviewer-image").html("");
	
	$(".press-board a.press-image.selected").each(function()
	{
		var thisImage = $(this);
				
		$(".photoviewer .photoviewer-controls .title").html(thisImage.find(".title").html()+'<small>'+thisImage.find(".date").html()+'</small>');
		
		thisImage.find(".all_images .item").each(function()
		{
			var thisItem = $(this);
			var thumb = '\
				<a href="" class="press-image">\
					<span class="border"></span>\
					<span class="clipping"><img src="'+thisItem.find(".item-thumb").html()+'" alt="" /></span>\
					<span class="full_image">'+thisItem.find(".item-full").html()+'</span>\
				</a>\
			';
			
			$(".photoviewer .photoviewer-thumbs").append(thumb);
		});
		
		$(".photoviewer .photoviewer-thumbs a.press-image:first").each(function()
		{
			$(".photoviewer .photoviewer-thumbs a.press-image.selected").removeClass("selected");
			$(this).addClass("selected");
			var thisImage = $(this);
			
			$(".photoviewer .photoviewer-image").html('<img src="'+thisImage.find(".full_image").html()+'" alt="">');
			
			photoViewerCurrentImageDefaultWidth = $(".photoviewer .photoviewer-image img").width();
			photoViewerCurrentImageDefaultHeight = $(".photoviewer .photoviewer-image img").height();
			$(".photoviewer .photoviewer-image img").load(function()
			{
				photoViewerCurrentImageDefaultWidth = $(".photoviewer .photoviewer-image img").width();
				photoViewerCurrentImageDefaultHeight = $(".photoviewer .photoviewer-image img").height();
				
			});
		});
		
		$("#slider").slider({
			value:100,
			min: 32,
			max: 250,
			step: 1,
			slide: function(event, ui)
			{
				$(".zoom").val(ui.value+"%");
				$(".photoviewer .photoviewer-image img").width(photoViewerCurrentImageDefaultWidth*ui.value/100);
			}
		});
		$(".zoom").val($("#slider").slider("value") + "%");
		
		$(".photoviewer .expand-to-full").click(function()
		{
			var containerWidth = $(".photoviewer .photoviewer-image").width() - 10;
			var containerHeight = $(".photoviewer .photoviewer-image").height() - 10;
			
			scale = containerWidth/photoViewerCurrentImageDefaultWidth * 100;
			
			if (containerHeight/photoViewerCurrentImageDefaultHeight * 100 < scale)
				scale = containerHeight/photoViewerCurrentImageDefaultHeight * 100;
			
			$(".photoviewer .photoviewer-image img").width(photoViewerCurrentImageDefaultWidth*scale/100);
			$("#slider").slider("value", scale);
			$(".zoom").val(parseInt(scale) + "%");
			
			
			
			return false;
		});
		$(".photoviewer .expand-to-horizontal").click(function()
		{
			var scale = 100;
			var containerWidth = $(".photoviewer .photoviewer-image").width() - 20;
			scale = containerWidth/photoViewerCurrentImageDefaultWidth * 100;
			
			$(".photoviewer .photoviewer-image img").width(photoViewerCurrentImageDefaultWidth*scale/100);
			$("#slider").slider("value", scale);
			$(".zoom").val(parseInt(scale) + "%");
			
			return false;
		});
	});
	
	$(".photoviewer .photoviewer-thumbs a.press-image").live("click", function()
	{
		$(".photoviewer .photoviewer-thumbs a.press-image.selected").removeClass("selected");
		$(this).addClass("selected");
		var thisImage = $(this);
		
		$(".photoviewer .photoviewer-image").html('<img src="'+thisImage.find(".full_image").html()+'" alt="">');
		
		$("#slider").slider("value", 100);
		$(".zoom").val("100%");
		
		photoViewerCurrentImageDefaultWidth = $(".photoviewer .photoviewer-image img").width();
		photoViewerCurrentImageDefaultHeight = $(".photoviewer .photoviewer-image img").height();
		$(".photoviewer .photoviewer-image img").load(function()
		{
			photoViewerCurrentImageDefaultWidth = $(".photoviewer .photoviewer-image img").width();
			photoViewerCurrentImageDefaultHeight = $(".photoviewer .photoviewer-image img").height();
			
		});
		
		return false;
	});
}
