/* ------------------------------------------------------------------------
	document ready stuff!
------------------------------------------------------------------------- */

	$(function(){
		jQuery.easing.def = "easeOutBack";
		window.setTimeout(function(){
			$('#content').animate({'top':-38},600);
		},1000);
		
		$('.contact-us').click(function(){
			contact.highlight();
		});
	});


/* ------------------------------------------------------------------------
	Class: player
	Description: Used for the videoplayer
------------------------------------------------------------------------- */
	var featured_top, footer_top, propaganda_top;
	player = {
		currentPage: 0,
		itemsPerPage: 3,
		totalPage: 0,
		init : function(json){
			player.totalPage = Math.ceil(json.length / player.itemsPerPage) - 1;
			
			if(player.totalPage >= 1) {
				$('#featured-projects .previous,#featured-projects .next').removeClass('hide');
			}
			
			// Build the list
			toInject = "";
			$.each(json,function(){
				toInject += "<li> \
								<a href='#"+this.id+"'><img src='"+this.thumbnail+"' width='173' height='99' alt='"+this.title+"' /><span>Play</span></a> \
								<strong>"+this.title+"</strong> \
							</li>";
			});
			
			// Inject the list
			$('#featured-projects ul')
				.html(toInject)
				.find('li')
				.each(function(i){
						$(this).css({
							'position':'absolute',
							'left':($(this).width() + 15) * i,
							'top':$(this).position().top
						})
					});
			
			// Bind the clicks
			$('#featured-projects ul a').each(function(i){
				$(this).click(function(){
					project_hash = $(this).attr('href');
					project_hash = project_hash.substr(project_hash.indexOf('#'),project_hash.length);
					player.open(i,json);
					location.hash=project_hash;
					return false;
				});
			});
			
			$('#featured-projects a.previous').click(function(){ player.changePage('previous'); return false; });
			$('#featured-projects a.next').click(function(){ player.changePage('next'); return false; });
			
			if(location.hash){ $('a[href^='+location.hash+']').trigger('click'); }
			
		},
		open : function(which,json){
			// Save these block position to animate them
			if(!featured_top) featured_top = $('#featured-projects').position().top;
			if(!footer_top) footer_top = $('#footer').position().top;
			if(!propaganda_top) propaganda_top = $('#propaganda').position().top;
			
			// Get the video id
			if(json[which].vimeo){
				video_id = json[which].vimeo;
				video_id = video_id.replace('http://vimeo.com/','');
				video = '<object width="595" height="380"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="http://vimeo.com/moogaloop.swf?clip_id='+video_id+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed src="http://vimeo.com/moogaloop.swf?clip_id='+video_id+'&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="595" height="380"></embed></object>';
			}else if(json[which].issuu){
				video = json[which].issuu;
			}
			title = '<h2>'+json[which].title+'</h2>';
			
			previous = $('<a href="#"><span>'+labels.previous+'</span></a>').addClass('previous').click(function(){
				previtem = which-1;
				if(previtem < 0) previtem = json.length-1;
				player.open(previtem,json);
				return false;
			});
			next = $('<a href="#"><span>'+labels.next+'</span></a>').addClass('next').click(function(){
				nextitem = which+1;
				if(nextitem > json.length -1) nextitem = 0;
				player.open(nextitem,json);
				return false;
			});
			close_ = $('<a href="#"><span>'+labels.closebutton+'</span></a>').addClass('close').click(function(){
				player.close_();
				return false;
			});
			nav = $('<div></div>')
					.addClass('video-nav')
					.append(previous)
					.append(next)
					.append(close_);
			
			$('#video-player .content')
				.html('')
				.append(title)
				.append(nav)
				.append(video);

			$('#video-player').attr('class','');
			$('#video-player').addClass(json[which].player_width);
			
			player_height = $('#video-player').height();
			
			$('#video-player').css({top: -player_height+20})
							.show()
							.animate({'top':-75},600);
							
			if($('#video-player').hasClass('wide')){
				$('#propaganda').animate({'top':propaganda_top+player_height-280},600);
			}else{
				$('#propaganda').animate({'top':propaganda_top},600);
			}
			$('#featured-projects').animate({'top':featured_top+player_height-280},600);
			$('#footer').animate({'top':footer_top+player_height-280},600);
		},
		changePage : function(direction){
			itemWidth = $('#featured-projects ul li:first').width() + 15;
			pageWidth = player.itemsPerPage * itemWidth;

			if(direction=='next'){
				player.currentPage ++;
				direction = -1;

				if(player.currentPage > player.totalPage){
					player.currentPage = 0;
				}
			}else{
				player.currentPage --;
				direction = 1;

				if(player.currentPage < 0){
					direction = -1; // Invert the direction to go to the last page
					player.currentPage = player.totalPage;
				}
			}

			// Slide the pages, logic ++
			$('#featured-projects li').each(function(i){
				$(this).animate({
					'left': (i * itemWidth) - (pageWidth * player.currentPage)
				});
			});
		},
		close_ : function(){
			player_height = $('#video-player').height();
			$('#video-player').animate({'top':-player_height-80},600,function(){ $(this).removeClass('wide').removeClass('normal').find('.content').html(''); });
			$('#featured-projects').animate({'top':featured_top},600);
			$('#propaganda').animate({'top':propaganda_top},600);
			$('#footer').animate({'top':footer_top},600);
		}
	}


/* ------------------------------------------------------------------------
	class: Contact
	Descritpion: Used to hightligh the contact footer
------------------------------------------------------------------------- */

	contact = {
		highlight : function(){
			if($('.overlay').size() == 0) {
				jQuery.easing.def = "jswing";
				$('body')
					.append('<div class="overlay"></div>')
					.find('.overlay')
					.css({
						width:$(document).width(),
						height:$(document).height(),
						opacity:0
					})
					.fadeTo('slow',0.30,function(){
						window.setTimeout(function(){ contact.highlight(); },1000);
						});
			}else{
				$('.overlay')
					.fadeTo('slow',0,function(){
						$(this).remove();
						jQuery.easing.def = "easeOutBack";
					});
			}
		}
	}