/**
 * Variables
 */
	
	// jQuery objects
	var $container;
	var $loading;
	var $scroll = {content:new Object(),footer:new Object()};
	var $header
	var $content;
	var $footer;
	var $counter;
	var $info;
	
	var $timer = {
		now: new Date(),
		end: new Date(2010,4,4),
		ticker: ''
	};
	
	/**
	 * $html
	 * Holds HTML content
	 */
	var $html = {
		// default error HTML
		error:'<div id="error"><h1><span>Error</span></h1></div>'
	};
	
	/**
	 * $cache
	 * HTML cache
	 */
	var $cache = new Object();
	
	/**
	 * $params
	 * Various script parameters
	 */
	var $params = {
		// current slide
		current:'0',
		// rnd function
		rnd:'0123456789abcdefghiklmnopqrstuvwxyz'.split(''),
		// animation speeds
		speed:{scroll:'500',fade:'200'},
		// pages
		pages:['index','step1','step2','step31','step32','step33','step34','step35','step36','step37','step38','step41','step42','step43','step44','final']
	};
	
/**
 * Functions
 */
	
	/**
	 * rnd
	 * Generates random string
	 * @uses 
	 * @return random string
	 */
	function rnd() {
		var ret = '';
		for(var i=0; i<20; i++) ret += $params.rnd[Math.floor(Math.random() * $params.rnd.length)];
		return ret;
	}
	
	/**
	 * next
	 * Show slide
	 */
	function show( id, back ) {
		$scroll.content.scrollTo("#cslide-"+id,$params.speed.scroll,{axis:'x'})
		// progress
		$(".progress img").animate({'width':(100/($params.pages.length-1)*$params.current)+'%'});
		// last slide bug
		if( back && id == 14 ) id = 1;
		if( $("#fslide-"+id).length )
			$scroll.footer.scrollTo("#fslide-"+id,$params.speed.scroll,{axis:'x'})
	}
	
	/**
	 * timer
	 */
	function timer() {
		
		$timer.now = new Date();
		diff = $timer.end.valueOf() - $timer.now.valueOf();
		
		if( diff > 0 ) {
			
			// fade in counter html
			$counter.fadeIn();
			$info.fadeOut();
			
			// remove miliseconds
			diff = Math.ceil(diff/1000);
			
			// count days
			var iDays = Math.ceil((diff/24/60/60)-1);
			diff = Math.ceil(diff-(iDays*24*60*60));
			var sDays = iDays.toString();
			if(iDays<10) sDays = '0'+sDays;
			sDays = sDays.split('');
			$("#day-1").removeClass().addClass("n"+sDays[0]);
			$("#day-2").removeClass().addClass("n"+sDays[1]);
			
			var iHours = Math.ceil((diff/60/60)-1);
			diff = Math.ceil(diff-(iHours*60*60));
			var sHours = iHours.toString();
			if(iHours<10) sHours = '0'+sHours;
			sHours = sHours.split('');
			$("#hour-1").removeClass().addClass("n"+sHours[0]);
			$("#hour-2").removeClass().addClass("n"+sHours[1]);
			
			var iMinutes = Math.ceil((diff/60)-1);
			diff = Math.ceil(diff-(iMinutes*60));
			var sMinutes = iMinutes.toString();
			if(iMinutes<10) sMinutes = '0'+sMinutes;
			if(iMinutes==60) sMinutes = '00';
			sMinutes = sMinutes.split('');
			$("#min-1").removeClass().addClass("n"+sMinutes[0]);
			$("#min-2").removeClass().addClass("n"+sMinutes[1]);
			
			iSeconds = diff;
			var sSeconds = iSeconds.toString();
			if(iSeconds<10) sSeconds = '0'+sSeconds;
			sSeconds = sSeconds.split('');
			if(iSeconds==60) sSeconds = '00';
			$("#sec-1").removeClass().addClass("n"+sSeconds[0]);
			$("#sec-2").removeClass().addClass("n"+sSeconds[1]);
			
			$timer.ticker = setTimeout( "timer()", 1000 );
			
		} else {
			
			timerStop();
			
		}
	}
	
	function timerStop() {
		$counter.fadeOut();
		$info.fadeIn();
		clearTimeout( $timer.ticker );
	}

/**
 * jQuery
 */
$(document).ready(function(){
	
	// setup
	$container = $("#container");
	
	// $html.error
	$.get("/parama/assets/error.html?"+rnd(),function(r){$html.error=r});
	
	$container
		.load("/parama/assets/load.html?"+rnd(),function(r,s){
			// objects
			$loading = $("#loading");
			$scroll.content = $("#content");
			$scroll.footer = $("#footer");
			$header = $("#hcol-2");
			$content = $("#content-landing");
			$footer = $("#footer-landing");
			$info = $("#info");
			
			// counter
			if( $("#counter").length > 0 ) {
				$counter = $("#counter");
				timer();
			}
			
			// load pages
			$.each($params.pages,function(k,v){
				$.get("/parama/guide/"+v+".html?"+rnd(),function(r){
					$cache[k] = r;
					//$cache[k] = r;
					if(k==($params.pages.length-1)) 
						$container.trigger("init");
				});
			});
		})
		.bind("init",function(){
			// ie array key sort bugfix
			var $temp = new Object();
			for(var i=0;i<=($params.pages.length-1);i++) { $temp[i] = $cache[i]; }
			$cache = $temp;
			// landing css
			$content.css({width:0,position:'absolute'});
			$footer.css({width:0,position:'absolute'});
			// assembly
			$.each($cache,function(k,v){
				// content slides
				$content.show().css({'width':$content.width()+960}).append('<div id="cslide-'+k+'" class="slide">'+jQuery("#content-landing",v).html()+'</div>');
				// footer slides
				switch(k){ 
					case '0': case '1': case ($params.pages.length-1).toString():
						$footer.show().css({'width':$footer.width()+960}).append('<div id="fslide-'+k+'" class="slide">'+jQuery("#footer-landing",v).html()+'</div>'); 
					break;
				}
			});
			// bind button events
			$("#pradzia").removeAttr("href").bind("click",function(){ $params.current = 0; timer(); show( $params.current ); return false; })
			$("#pradedam, #toliau").removeAttr("href").bind("click",function(){ $params.current++; timerStop(); show( $params.current ); return false; });
			$(".atgal").removeAttr("href").bind("click",function(){ $params.current--; if( $params.current == 0 ) timer(); show( $params.current, true ); return false; });
			// show
			$loading.fadeOut($params.speed.fade,function(){
				$(".hcol-2 div").fadeIn($params.speed.fade);
				show( $params.current );
			});
		})
		.ajaxError(function(){
			$loading.fadeOut($params.speed.fade,function(){
				$content.html($html.error).fadeIn($params.speed.fade);
			});
		});
	
});
