var _config = 
{
	total_thumbs: 0,
	thumbs_per_page: 12,
	current_page: 1,
	current_min: 1,
	current_max: 0,
	fade_speed: 1000,
	total_news: 0,
	news_per_page: 5,
	current_news_page: 1,
	current_news_min: 1,
	current_news_max: 0
};

var _currentProject = 0;
var _currentRoll = 0;
var _newsTop = 0;
var _newsBottom = 0;

$(document).ready(function() 
{
	var loc = document.location.toString();
	var initSlug = "";
	
	if (loc.match('#')) initSlug = loc.split('#')[1];
	
	$("#project").hide();
	$("#video_overlay").hide();
	
	if (initSlug != "home" && initSlug != "") 
	{
		$.history.add(initSlug);
		showProject();
	}
	else $.history.add("home");
	
	$("#collection_items li").click(projectClick);
	$("#close").click(closeClick);
	
	$(window).history(function(e, currentHash, previousHash)
	{
		processHistory(currentHash, previousHash);
	});
	
	$('#video_thumbnail').load(function()
	{
		$("#video_thumbnail").fadeIn();
		hideLoader();
		
		$("#video_thumbnail").click(function()
		{
			$("#video_thumbnail").fadeOut(500);
			
			var so = new SWFObject("/assets/flash/videoplayer.swf", "movieplayer", "480", "360", "9", "#000000");
			so.addVariable("videoFile", "/assets/"+$PROJECTS[$.history.getCurrent()].spot1);
			so.write("flash");
			
			return false;
		});
		
		$("#video_overlay").click(function()
		{
			$("#video_thumbnail").fadeOut(500);
			$("#video_overlay").fadeOut(500);
			
			hideDownload(0);
			hideShare(0);
			
			var so = new SWFObject("/assets/flash/videoplayer.swf", "movieplayer", "480", "360", "9", "#000000");
			so.addVariable("videoFile", "/assets/"+$PROJECTS[$.history.getCurrent()].spot1);
			so.write("flash");
			
			return false;
		});
		
		$("#video_thumbnail").hover(function()
		{
			$("#video_overlay").fadeIn();
		},
		function()
		{
		});
		
		$("#video_overlay").hover(function()
		{
		},
		function()
		{
			$("#video_overlay").fadeOut();
		});
	});
	
	/*
	$('#flash').hover(
	function()
	{
		//rollover functionality here
		console.log("over");
		return false;
	},
	function()
	{
		if($("flash"))
		{
			getFlashMovie("movieplayer").jsStopDrag();
		}
		
		console.log("out");
		return false;
	});
	*/
	
	hideDownload(0);
	$("#reveal_download").click(showDownload);
	$("#download_close").click(hideDownload);

	hideShare(0);
	$("#reveal_share").click(showShare);
	$("#share_close").click(hideShare);
	
	$("#share_small").click(shareSmall);
	$("#share_medium").click(shareMedium);
	$("#share_large").click(shareLarge);
	
	initNews();
});

function getFlashMovie(movieName) 
{
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[movieName] : document[movieName];
}

function processHistory(currentHash, previousHash)
{
	if(currentHash == "home")
	{
		hideProject();
	}
	else
	{
		showProject();
	}
}

function projectClick()
{
	$.history.add(this.id.toString());
	showProject();
}

function showProject()
{
	hideDownload(0);
	showLoader();
	$("#video_thumbnail").hide();
	$("#video_thumbnail").attr("src", $PROJECTS[$.history.getCurrent()].image);
	$("#project_title").html($PROJECTS[$.history.getCurrent()].title);
	$("#project_description").html($PROJECTS[$.history.getCurrent()].description);
	if($PROJECTS[$.history.getCurrent()].credits != "")
	{
		$("#project_credits").show(); 
		$("#project_credits").html("Additional credits	/  " + $PROJECTS[$.history.getCurrent()].credits);
	}
	else
	{
		$("#project_credits").hide();
	}
	
	if($PROJECTS[$.history.getCurrent()].iphone != "" || $PROJECTS[$.history.getCurrent()].ipod != "" || $PROJECTS[$.history.getCurrent()].quicktime != "")
	{
		$("#reveal_download").show();
		
		if ($PROJECTS[$.history.getCurrent()].iphone != "")
		{
			$("#iphone_gfx").show();
			$("#iphone_link").show();
			
			$("#iphone_link").attr("href", "/assets/"+$PROJECTS[$.history.getCurrent()].iphone);
			$("#iphone_gfx").attr("href", "/assets/"+$PROJECTS[$.history.getCurrent()].iphone);
		}
		else
		{
			$("#iphone_gfx").hide();
			$("#iphone_link").hide();
			$("#iphone_gfx").attr("href", "#");
			$("#iphone_link").attr("href", "#");
		}
		
		if ($PROJECTS[$.history.getCurrent()].ipod != "")
		{
			$("#ipod_gfx").show();
			$("#ipod_link").show();
			
			$("#ipod_gfx").attr("href", "/assets/"+$PROJECTS[$.history.getCurrent()].ipod);
			$("#ipod_link").attr("href", "/assets/"+$PROJECTS[$.history.getCurrent()].ipod);
		}
		else
		{
			$("#ipod_gfx").hide();
			$("#ipod_link").hide();
			$("#ipod_gfx").attr("href", "#");
			$("#ipod_link").attr("href", "#");
		}
		
		if ($PROJECTS[$.history.getCurrent()].quicktime != "")
		{
			$("#quicktime_gfx").show();
			$("#quicktime_link").show();
			
			$("#quicktime_gfx").attr("href", "/assets/"+$PROJECTS[$.history.getCurrent()].quicktime);
			$("#quicktime_link").attr("href", "/assets/"+$PROJECTS[$.history.getCurrent()].quicktime);
		}
		else
		{
			$("#quicktime_gfx").hide();
			$("#quicktime_link").hide();
			$("#quicktime_gfx").attr("href", "#");
			$("#quicktime_link").attr("href", "#");
		}
	}
	else
	{
		$("#reveal_download").hide();
	}
	
	$("#spot1").click(function()
	{
		$('#flash').html("");
		$("#video_thumbnail").fadeOut(500);
		
		//getFlashMovie("movieplayer").jsDestroy();
		var so = new SWFObject("/assets/flash/videoplayer.swf", "movieplayer", "480", "360", "9", "#000000");
		so.addVariable("videoFile", "/assets/"+$PROJECTS[$.history.getCurrent()].spot1);
		so.write("flash");
		return false;
	});
	
	if($PROJECTS[$.history.getCurrent()].spot2 != "")
	{
		$("#spot2").show();
		$("#spot2").click(function()
		{
			$('#flash').html("");
			$("#video_thumbnail").fadeOut(500);
			
			//getFlashMovie("movieplayer").jsDestroy();
			var so = new SWFObject("/assets/flash/videoplayer.swf", "movieplayer", "480", "360", "9", "#000000");
			so.addVariable("videoFile", "/assets/"+$PROJECTS[$.history.getCurrent()].spot2);
			so.write("flash");
			return false;
		});
	}
	else { $("#spot2").hide(); }
	
	if($PROJECTS[$.history.getCurrent()].spot3 != "")
	{
		$("#spot3").show();
		$("#spot3").click(function()
		{
			$('#flash').html("");
			$("#video_thumbnail").fadeOut(500);
			
			//getFlashMovie("movieplayer").jsDestroy();
			var so = new SWFObject("/assets/flash/videoplayer.swf", "movieplayer", "480", "360", "9", "#000000");
			so.addVariable("videoFile", "/assets/"+$PROJECTS[$.history.getCurrent()].spot3);
			so.write("flash");
			return false;
		});
	}
	else { $("#spot3").hide(); }
	
	if($PROJECTS[$.history.getCurrent()].still1 || $PROJECTS[$.history.getCurrent()].still2 || $PROJECTS[$.history.getCurrent()].still3)
	{
		$("#project_stills").html("Production Stills / ");
		
		if($PROJECTS[$.history.getCurrent()].still1 != "error.png" ||
			$PROJECTS[$.history.getCurrent()].still2 != "error.png" ||
			$PROJECTS[$.history.getCurrent()].still3 != "error.png")
		{
			$("#project_stills").show();
		}
		else
		{
			$("#project_stills").hide();
		}
		
		if($PROJECTS[$.history.getCurrent()].still1 != "error.png")
		{
			var still1 = $("<a href='#'>1</a>").attr("id", "still1").addClass("still");
			$("#project_stills").append(still1);
			
			still1.click(function()
			{
				$('#flash').html("");
				showLoader();
				$("#video_thumbnail").hide();
				$("#video_thumbnail").attr("src", $PROJECTS[$.history.getCurrent()].still1);
				return false;
			});
		}
		
		if($PROJECTS[$.history.getCurrent()].still2 != "error.png")
		{
			var still2 = $("<a href='#'>2</a>").attr("id", "still2").addClass("still");
			$("#project_stills").append(still2);
			
			still2.click(function()
			{
				$('#flash').html("");
				showLoader();
				$("#video_thumbnail").hide();
				$("#video_thumbnail").attr("src", $PROJECTS[$.history.getCurrent()].still2);
				return false;
			});
		}
		
		if($PROJECTS[$.history.getCurrent()].still3 != "error.png")
		{
			var still3 = $("<a href='#'>3</a>").attr("id", "still3").addClass("still");
			$("#project_stills").append(still3);
			
			still3.click(function()
			{
				$('#flash').html("");
				showLoader();
				$("#video_thumbnail").hide();
				$("#video_thumbnail").attr("src", $PROJECTS[$.history.getCurrent()].still3);
				return false;
			});
		}
	}
	
	$("#project").slideDown();
}

function hideProject()
{
	hideShare();
	hideDownload();
	$("#video_thumbnail").show();
	$('#flash').html("");
	
	$("#project").slideUp();

}

function closeClick()
{
	$.history.add("home");
	hideProject();
}

function showVideo()
{
	$("#video_thumbnail").hide();
}

function hideVideo()
{
	$("#video_thumbnail").show();
	$('#flash').html("");
}

function showLoader()
{
	$("#loader").show();
}

function hideLoader()
{
	$("#loader").hide();
}

function showDownload(time)
{
	$("#collection_items li").unbind('click', projectClick);
	$("#project_share").hide();
	(time == 0) ? $("#project_download").show() : $("#project_download").fadeIn(time);
	return false;
}

function hideDownload(time)
{
	$("#collection_items li").click(projectClick);
	(time == 0) ? $("#project_download").hide() : $("#project_download").fadeOut(time);
	return false;
}

function showShare(time)
{
	var embedStr = "";
	embedStr += '&lt;object width="400" height="300"&gt;';
	embedStr += '&lt;param name="allowfullscreen" value="true" /&gt;';
	embedStr += '&lt;param name="allowscriptaccess" value="always" /&gt;';
	embedStr += '&lt;param name="movie" value="http://sarofsky.webfactional.com/assets/flash/videoplayer.swf?videoFile=/assets/'+$PROJECTS[$.history.getCurrent()].spot1+'" /&gt;';
	embedStr += '&lt;embed src="http://sarofsky.webfactional.com/assets/flash/videoplayer.swf?videoFile=/assets/'+$PROJECTS[$.history.getCurrent()].spot1+'" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="300"&gt;&lt;/embed&gt;';
	embedStr += '&lt;/object&gt;';
	
	$("#code").html(embedStr);
	
	$("#collection_items li").unbind('click', projectClick);
	$("#project_download").hide();
	(time == 0) ? $("#project_share").show() : $("#project_share").fadeIn(time);
	return false;
}

function hideShare(time)
{
	$("#collection_items li").click(projectClick);
	(time == 0) ? $("#project_share").hide() : $("#project_share").fadeOut(time);
	return false;
}


function shareSmall()
{
	var embedStr = "";
	embedStr += '&lt;object width="400" height="300"&gt;';
	embedStr += '&lt;param name="allowfullscreen" value="true" /&gt;';
	embedStr += '&lt;param name="allowscriptaccess" value="always" /&gt;';
	embedStr += '&lt;param name="movie" value="http://sarofsky.webfactional.com/assets/flash/videoplayer.swf?videoFile=/assets/'+$PROJECTS[$.history.getCurrent()].spot1+'" /&gt;';
	embedStr += '&lt;embed src="http://sarofsky.webfactional.com/assets/flash/videoplayer.swf?videoFile=/assets/'+$PROJECTS[$.history.getCurrent()].spot1+'" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="400" height="300"&gt;&lt;/embed&gt;';
	embedStr += '&lt;/object&gt;';
	
	$("#code").html(embedStr);
	return false;
}

function shareMedium()
{
	var embedStr = "";
	embedStr += '&lt;object width="500" height="375"&gt;';
	embedStr += '&lt;param name="allowfullscreen" value="true" /&gt;';
	embedStr += '&lt;param name="allowscriptaccess" value="always" /&gt;';
	embedStr += '&lt;param name="movie" value="http://sarofsky.webfactional.com/assets/flash/videoplayer.swf?videoFile=/assets/'+$PROJECTS[$.history.getCurrent()].spot1+'" /&gt;';
	embedStr += '&lt;embed src="http://sarofsky.webfactional.com/assets/flash/videoplayer.swf?videoFile=/assets/'+$PROJECTS[$.history.getCurrent()].spot1+'" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="500" height="375"&gt;&lt;/embed&gt;';
	embedStr += '&lt;/object&gt;';
	
	$("#code").html(embedStr);
	return false;
}

function shareLarge()
{
	var embedStr = "";
	embedStr += '&lt;object width="600" height="450"&gt;';
	embedStr += '&lt;param name="allowfullscreen" value="true" /&gt;';
	embedStr += '&lt;param name="allowscriptaccess" value="always" /&gt;';
	embedStr += '&lt;param name="movie" value="http://sarofsky.webfactional.com/assets/flash/videoplayer.swf?videoFile=/assets/'+$PROJECTS[$.history.getCurrent()].spot1+'" /&gt;';
	embedStr += '&lt;embed src="http://sarofsky.webfactional.com/assets/flash/videoplayer.swf?videoFile=/assets/'+$PROJECTS[$.history.getCurrent()].spot1+'" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="600" height="450"&gt;&lt;/embed&gt;';
	embedStr += '&lt;/object&gt;';
	
	$("#code").html(embedStr);
	return false;
}

function initNews()
{
	_config.total_news = $(".newsitem").length;
	
	if(_config.total_news <= _config.news_per_page)
	{
		_config.current_news_max = _config.total_news;
		$("#news_next").hide();
		$("#news_prev").hide();
	}
	else
	{
		_config.current_news_max = _config.news_per_page;
		$("#news_next").show();
		$("#news_prev").hide();
	}
	
	if(_config.total_news == 0) $("news_pagination").hide();
	
	$("#news_next").click(function(){move_news("next"); return false;});
	$("#news_prev").click(function(){move_news("prev"); return false;});
	
	setNewsMask(_config.current_news_min, _config.current_news_max);
}

function move_news(dir)
{
	var newPage = _config.current_news_page;
	var min = 0;
	var max = 0
	
	$("#news_next").hide();
	$("#news_prev").hide();
	
	if(dir == "next")
	{
		newPage++;
		min = _config.current_news_min + _config.news_per_page;
	}
	else
	{
		newPage--;
		min = _config.current_news_min - _config.news_per_page;
	}
	
	if((newPage * _config.news_per_page) > _config.total_news)
	{
		max = _config.total_news;
	}
	else
	{
		max = _config.news_per_page * newPage;
	}
	
	if((newPage * _config.news_per_page) >= _config.total_news)
	{
		$("#news_next").hide();
	}
	else
	{
		$("#news_next").show();
	}
	
	if(newPage == 1)
	{
		$("#news_prev").hide();
	}
	else
	{
		$("#news_prev").show();
	}
	
	//alert(min + " vs " + max)
	
	if(dir == "next")
	{
		shiftNewsUp(_config.current_news_min, _config.current_news_max, _config.fade_speed);
	}
	else
	{
		shiftNewsDown(min, max, _config.fade_speed);
	}
	
	setNewsMask(min, max);
	
	_config.current_news_min = min;
	_config.current_news_max = max;
		
	_config.current_news_page = newPage;
}

function setNewsMask(start, end)
{
	var news_height = 0;
	var diff = 0;
	
	//if(jQuery.browser.safari) diff = 40;
	
	for (var i = start; i <= end; i++)
	{
		news_height += $("#news" + i).height() + diff;
	}

	$('#newsmask').animate({height: news_height + 15}, _config.fade_speed);
	$('#news_pagination').animate({marginTop: news_height + 15}, _config.fade_speed);
	//$("#news_pagination").css("margin-top", news_height + 25);
	//$('#newsMask').height((news_height + 25) + "px");
}

function shiftNewsUp(start, end, duration)
{
	var news_height = 0;
	
	for (var i = start; i <= end; i++)
	{
		news_height += $("#news" + i).height();
	}
	
	_newsTop -= news_height;
	
	$('#newsholder').animate({top: _newsTop}, duration);
}

function shiftNewsDown(start, end, duration)
{
	var news_height = 0;
	
	for (var i = start; i <= end; i++)
	{
		news_height += $("#news" + i).height();
	}
	
	_newsTop += news_height;
	
	$('#newsholder').animate({top: _newsTop}, duration);
}
