/*
@author		Charles Mastin - http://charlesmastin.com
@copyright	2009 Mike Goubeaux
*/

/* Global Variables */
var boardsloaded = false;
var oldtab = 'work';
var contact = false;
var RATE_FAST = 250;
var workPagination = null;
var reelPagination = null;
var vimeo_id = '';

$(document).ready(function() {
	init();
});

/* Init */

function init()
{	
	var obj = this;	
	var tA = $('#nav a');
	$.each(tA,function(){
		$(this).bind('click',function(e){
			viewTab($(this).attr('href'));
			e.preventDefault();
			e.stopImmediatePropagation();
			return false;
		});
	});
	
	//loop through proj nav
	var tA = $('a.proj');
	$.each(tA,function(){
		$(this).bind('click',function(e){
			handleProject($(this),1);
			e.stopPropagation();
			return false;
		});
	});
	
	//loop through specs categories
	var tA = $('.filter.categories li a');
	$.each(tA,function(){
		$(this).bind('click',function(e){
			getNewsCategory(this);
			e.stopPropagation();
			return false;
		});
	});
	
	$('#nav_contact').bind('click',function(e){
		toggleContact();
		e.stopPropagation();
		return false;
	});
	
	$('#contact a.close').bind('click',function(e){
		contact = false;
		showContact();
		e.stopPropagation();
		return false;
	});
	
	$('.filter a.all').bind('click',function(e){
		filterWork('all');
		e.stopPropagation();
		return false;
	});
	
	$('.filter a.directing').bind('click',function(e){
		filterWork('directing');
		e.stopPropagation();
		return false;
	});
	
	if(window.location.hash != ''){
		workPagination = null;
		workPagination = new pagination($('#content .tab.work .projects'),$('#content .tab.work .pagination'),6,$('#content .tab.work .projects .project'));		
		runCommand();
	}else{
		filterWork('all');
	}
	
	switchPolaroids();
}

function getNewsCategory(elem)
{
	var tA = $('.filter.categories li a');
	for(var i=0;i<tA.length;i++){
		$(tA[i]).removeClass('active');
	}
	$(elem).addClass('active');
	
	//do ajax call
	var tA = $(elem).attr('href').split('_');
	var cat = '';
	if(tA[1]){
		cat = tA[1];
	}
	
	$.post('/news/archives','&category=' + cat,handleAjax,"html");
}

function handleAjax(data,textStatus)
{
	if(textStatus == "success"){
		var obj = $('.tab.specs .col2.content');
		$(obj).slideUp(RATE_FAST,function(){
			$(obj).empty();
			$(obj).append(data);
			$(obj).slideDown(RATE_FAST);
		});
	}
}


function runCommand()
{
	var hash = window.location.hash;
	var tA = hash.split('/');
	if(tA[0] == '#work' || tA[0] == '#reel'){
		if(tA[1] != undefined){
			var elem = $('#' + tA[1] + ' a.proj');
			$(elem).addClass('active');
			if(tA[0] == '#work'){
				workPagination.showPage(workPagination.findPageOfElement($('#' + tA[1])[0]));
			}
		}
	}
	viewTab(tA[0]);
}

/* Tab Navigation */

function viewTab(e)
{
	var elem = e.substr(1);
	var delay = 0;
	
	$('#vimeo_player').remove();
	
	if(elem == 'work' || elem == 'reel'){
		$('#padmaster').animate({'height': '470px'},RATE_FAST,'');
		$('#showcase').show();
		var obj;

		if(elem == 'work'){
			obj = $('#content .tab.work .projects a.proj.active')[0];
			if(!obj){
				obj = $($('#content .tab.work .project')[0]).find('a')[0];
			}
		}
		if(elem == 'reel'){
			obj = $('#content .tab.reel .reels a.proj.active')[0];
			if(!obj){
				obj = $($('#content .tab.reel .project')[0]).find('a')[0];
			}
		}

		handleProject($(obj),0);
		//setTimeout(function(){ handleProject($(obj),0)},RATE_FAST);
	}else{
		$('#padmaster').animate({'height': '100px'},RATE_FAST,'');
		$('#showcase').hide();
	}	
	
	var tabA = ['work','reel','specs','boards'];
	
	//change footer
	var colorA = ['#a52b6a','#5f143b','#921757','#000000'];
	for(var i=0;i<tabA.length;i++){
		var obj = $('#content .tab.' + tabA[i]);
		if(tabA[i] == elem){
			obj.slideDown(RATE_FAST);
			$('#footer .footer_content').css('background-color',colorA[i]);
		}else{
			obj.slideUp(RATE_FAST);
		}
	}
	if(elem == 'boards' && boardsloaded == false){
		//loadImages
		var tA = $('#content .tab.boards img');
		for(var i=0;i<tA.length;i++){
			$(tA[i]).show();
		}
		boardsloaded = true;
	}
	
	contact = false;
	showContact();
	switchPolaroids();
	oldtab = elem;
	//window.location.hash=e;
	//return false;	
}

function switchPolaroids()
{
	$('#polaroid_left').css('background-image','url(' + polaroidsLeft[getRandomInt(0,polaroidsLeft.length-1)]+')');
	$('#polaroid_right').css('background-image','url(' + polaroidsRight[getRandomInt(0,polaroidsRight.length-1)]+')');	
}

/* Contact */

function showContact(){
	var obj = $('#contact');
	if(contact){
		obj.slideDown(RATE_FAST);
	}else{
		obj.slideUp(RATE_FAST);
	}
}

function toggleContact()
{
	contact = !contact;
	showContact();
	return false;
}

/* Projects */

function viewProject(e,autoplay)
{
	vimeo_id = $($(e).find('span.vimeo_id')).text();
	
	if($('#vimeo_player').length){
		$('#vimeo_player').remove();
	}
	$('#vimeo').append('<div id="vimeo_player"></div>');
	
	var flashvars = {};
	var params = {};
	params.menu = "false";
	params.bgcolor = "#FFFFFF";
	params.allowfullscreen = "true";
	params.allowscriptaccess = "always";
	params.wmode = "transparent";
	var attributes = {};
	swfobject.embedSWF("http://vimeo.com/moogaloop.swf?clip_id=" + vimeo_id + "&amp;server=vimeo.com&amp;autoplay=" + autoplay + "&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=dd2282&amp;fullscreen=1", "vimeo_player", "590", "332", "9.0.0","/assets/swf/expressInstall.swf", flashvars, params, attributes);
	
	//flow in overview text
		
	
	var text = '';
	//Title
	text += '<span class="title">' + $($(e).find('span.title')).text() + '</span>';
	
	//Role 
	text += '<span class="role">' + $($(e).find('span.role')).text() + '</span>';
	
	//Studio
	if($($(e).parent().find('dd.studio')).length){
		text += '<span class="studio">Studio / Prod Co: ' + $($(e).parent().find('dd.studio')).text() + '</span>';
	}
	
	//Overview
	if($($(e).parent().find('dd.overview')).length){
		text += '<p class="overview">' + $($(e).parent().find('dd.overview')).text() + '</p>';
	}
	
	
		
	//$('#overview').css('opacity','0');
	
	$('#overview').slideUp(RATE_FAST/2,function()
	{
		$('#overview').empty();
		$('#overview').append(text);
		$('#overview').slideDown(RATE_FAST/2);
	});	
	
	
	//$('#overview').animate({'opacity': '1.0'},RATE_FAST*2,'');
	
	//adjust heights
	
}

function handleProject(elem,autoplay)
{
	//keep it constrained to the parent container
	var tA = $(elem).parent().parent().find('a.proj');
	$.each(tA,function(){
		$(this).removeClass('active');
	});
	$(elem).addClass('active');
	
	if( ( vimeo_id != $($(elem).find('span.vimeo_id')).text() ) && (oldtab == 'work' || oldtab == 'reel') ){
		viewProject(elem,autoplay);
	}else if( oldtab != 'work' || oldtab != 'reel' ){
		viewProject(elem,autoplay);
	}
	
	if($(elem).parent().hasClass('reel')){
		buildReel($(elem).parent().attr('id'));
	}
}

function buildReel(id)
{
	var tA = [];
	for(var i=0;i<reelData.length;i++){
		if(reelData[i].id == id){
			tA = reelData[i].work;			
			break;
		}
	}
	var target = $('#work_in_reel .projects');
	$(target).empty();
	
	reelPagination = null;
	
	if(tA.length > 0){
		for(var i=0;i<tA.length;i++){
			
			var obj = $('#'+tA[i]).clone(true);			
			$(target).append(obj);
			$($(obj).find('a')[0]).removeClass('active');
		}
		$('#work_in_reel').show();
	}else{
		$('#work_in_reel').hide();
	}

	if(tA.length > 3){		
		reelPagination = new pagination($('#work_in_reel .projects'),$('#work_in_reel .pagination'),3,$('#work_in_reel .projects .project'));
	}
}

function pagination(parent,controls,max,condition)
{
	this.parent = parent;
	this.controls = controls;
	this.max = max;
	this.condition = condition;
	
	this._parent = $(this.parent);
	this._condition = $(this.condition);
	
	//init and build controls	
	this.len = this._condition.length;
	this.pages = Math.ceil(this.len / this.max);
	
	var imax = this._parent.children().length;
	
	//hide anything in list not met by condition
	for(var i=0;i<imax;i++){
		var found = false;
		var obj = this._parent.children()[i];
		for(var j=0;j<this.len;j++){
			if(this._condition[j] == obj){
				found = true;
				$(obj).show();
				break;
			}
		}		
		if(!found){
			$(obj).hide();
		}
	}
	
	$(this.controls).empty();
	
	if(this.pages > 1){
		//page label
		var r = '<span class="title">Page</span>';
		
		//buttons
		r += '<div class="nav">';
		for(var i=0;i<this.pages;i++){
			r += '<a href="#"><span>' + (i+1) + '</span></a>';
		}
		r += '</div>';
		
		
		$(this.controls).append(r);
		
		//select em, add events for click son
		var navA = $(this.controls).find('a');
		var obj = this;
		
		$.each(navA,function(index){
			$(this).bind('click',{scope:obj},function(e) {
				var s = e.data.scope;
				s.showPage.apply(s,[index]); 
				e.stopPropagation();
				return false;
			});
		});
		
		//hide everything not in first page...
		
		for(var i=max;i<this.len;i++){
			$(this._condition[i]).hide();
		}
		
		//show first page
		this.showPage(0);
	
	}
	
}

pagination.prototype.showPage = function(index)
{
	//track pagination nav
	var tA = $(this.controls).find('a');
	for(var i=0;i<tA.length;i++){
		if(i == index){
			$(tA[i]).addClass('active');
		}else{
			$(tA[i]).removeClass('active');
		}
	}
	
	//manage items
	for(var i=0;i<this.len;i++){
		var obj = this._condition[i];
		if((i >= (index*this.max)) && (i < ((index+1)*this.max))){
			$(obj).show();
		}else{
			$(obj).hide();
		}
	}
}

pagination.prototype.findPageOfElement = function(element)
{
	var index = -1;
	for(var i=0;i<this.len;i++){
		if(this._condition[i] == element){
			index = i;
			break;
		}
	}
	if(index <= 0){
		index = 1;
	}
	return Math.ceil(index / this.max) - 1;
}

function filterWork(mode)
{
	if(mode == 'all'){
		$('.filter a.all').addClass('active');
		$('.filter a.directing').removeClass('active');
		workPagination = null;
		workPagination = new pagination($('#content .tab.work .projects'),$('#content .tab.work .pagination'),6,$('#content .tab.work .projects .project'));
		handleProject($($('#content .tab.work .projects .project')[0]).find('a')[0],0);
	}
	if(mode == 'directing'){
		$('.filter a.all').removeClass('active');
		$('.filter a.directing').addClass('active');
		workPagination = null;
		workPagination = new pagination($('#content .tab.work .projects'),$('#content .tab.work .pagination'),6,$('#content .tab.work .projects .project.directing'));
		handleProject($($('#content .tab.work .projects .project.directing')[0]).find('a')[0],0);
	}
}

/* Misc */

function getRandomInt(min, max)
{
  return Math.floor(Math.random() * (max - min + 1)) + min;
}