/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is the the typhoGraphic library.
 *
 * The Initial Developer of the Original Code is
 * Andy Edmonds <andyed@gmail.com>
 *
 * Portions created by the Initial Developer are Copyright (C) 2008
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 * ***** END LICENSE BLOCK ***** */


jQuery.fn.center = function()
{
    this.css("position","absolute");
    this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
}


function addAnimation(ele, duration, offset) {
	ele.css("webkitAnimationName","appear" );
}

var debugMode = true;

// Not yet functional
function reinit() {
		window.scrollTo(0,1);
		$("div").each( 
	 	function() {
			$(this).css("webkitAnimationName"," ");
			$(this).css("webkitAnimationDuration"," ");
			$(this).css("webkitAnimationDelay"," ");
			
			
		});
		playAnim();
}

function playAnim() {

	window.scrollTo(0,1);
	var offset = 250; // slight delay before animation start to let URL bar disappear
	 $("div").each( 
		function() {			
			if(debugMode) console.log(this + $(this).attr("duration") + $(this).attr("effect")  +$(this).text()  +  typeof($(this).attr("start")) );
			
			
			$(this).css("font-size",  ($(window).width() / $(this).html().length) * .7) + "px";
			
			if(debugMode) console.log( ($(window).width() / $(this).html().length) * .7 )

			$(this).center();
			// Position Image if present. TODO: Needs to know image size
			if($(this).html().indexOf("<img") > -1 ) $(this).css("top","0px");
			
			window.scrollTo(0,1);
			// hide the item
			$(this).css("opacity", 0);
			var start = 0;
			// Adjust start time based upon HTML declaration 
			if(typeof($(this).attr("start")) != 'undefined') {
				start = parseInt(offset) + parseFloat($(this).attr("start"));
			} else {
				start = offset;
			}
			if(debugMode) console.log("start: " + start + " at offset: " + offset);
			
			var duration =  parseInt($(this).attr("duration"));
			// Set DIV to appear at right time
			//addAnimation($(this), duration/1000, start/1000   );
			
			// Add 3 part animation
			$(this).css("webkitAnimationDelay",  start/1000 + "s," +start/1000 +"s," + (start/1000 + duration/1000) + "s");
			$(this).css("webkitAnimationDuration", duration/1000 +"s,200s,200s" );
			$(this).css("webkitAnimationName", $(this).attr("effect")  + ",appear,hide");
			
			// Increment temporal offset
			offset =start +  duration;
			
			//$(this).class( $(this).attr("effect") );
			
			
		 }
	 )	
  // Resetting the animation does not yet work.
	//window.setTimeout("reinit()", offset + 500);
 
}

$(document).ready(function (evt) { 
		playAnim();
});
