var view_point='earth';

function sky(){
	view_point='sky';
	screen_lock(true);
	$('#pounce_logo').css({'z-index':'0'});
	$('#barn_level').animate({bottom:'-540'}, 600);
	$('#street_level').animate({bottom:'-620'}, 700);
	$('#moon').animate({bottom:'-=75'}, 600);
	$('#modal_sky').animate({top:'110'}, 600, function(){
		screen_lock(false);
	});
}

function earth(){
	view_point='earth';
	screen_lock(true);
	$('#pounce_logo').css({'z-index':'0'});
	$('#barn_level').animate({bottom:'0'}, 600);
	$('#street_level').animate({bottom:'0'}, 700);
	$('#moon').animate({bottom:'+=75'}, 600);
	$('#modal_sky').animate({top:'-555'}, 600, function(){
		screen_lock(false);
	});
}

/********************************************************
	core functions
********************************************************/
function clear_windows(){
	$('.window').empty();
}

function inArray(arr, obj) {
	for(var i=0; i<arr.length; i++) {
		if (arr[i] == obj) return true;
	}
	return false;
}

function screen_lock(flag){
	if(flag){
		$('#click_mask').css('z-index','1000');
		$('#pounce_logo').css({'z-index':'0'});
	//	$('#nav').hide('slide',{direction: 'down'}, 'fast');
	} else {
		$('#click_mask').css('z-index','-1');
		$('#pounce_logo').css({'z-index':'100'});
	//	$('#nav').show('slide', {direction: 'down'}, 'fast');
	}
}

function screen_lock_check(){
	return ($('#click_mask').css('z-index') == 1000);
}

function getAnchorID(myStr){
	if (myStr.match('#')) {
		return myStr.split('#')[1];
	}
}

function removeAnchorID(myStr){
	if (myStr.match('#')) {
		return myStr.split('#')[0];
	}
	return myStr;
}

function getSlide(){
	i=0;
	slide_num=0;
	$('#slideshow').children().each(function() {
		if($(this).attr('id') == getAnchorID(ajax_url)){
			slide_num=i;
			return false;
		}
		i++;
	});
	return slide_num;
}

/********************************************************
	cat tricks & treats & window clicks
********************************************************/
var ajax_url=document.location.pathname.toString();

function trick_or_treat(catEvent){
	var catEvents = new Array('ufo');
	if(inArray(catEvents, catEvent) == false){
		var rand_num = Math.floor(Math.random() * catEvents.length)
		catEvent = catEvents[rand_num];
	};
	
	if(!screen_lock_check()){
		eval(catEvent + '()');
	}
}

function ufo(){
	screen_lock(true);
	// append html to page... (should preload images first to prevent delays)...
	ufo_html = '<img id="ufo" src="/assets/img/ufo.png" style="position:absolute; top:-166px; left:-208px; width:208px; height:90px;" alt="UFO!">';
	ufo_html += '<img id="ufo_cat" src="/assets/img/cat.png" style="position:absolute; top:630px; left:738px; width:110px; height:56px; display:none;" alt="cat">';
	ufo_html += '<img id="ufo_beam" src="/assets/img/ufo_beam.png" style="position:absolute; top:50px; left:694px; display:none;" alt="UFO Light Beam!">';
	$('#pg_wrapper').append(ufo_html);
	
	// set ufo and cat to starting positions.
	$('#ufo').css({'top':'-166px', 'left':'-208px'});
	$('#ufo_cat').css({'display':'none','opacity':'1', 'width':'110px', 'height':'56px','top':'630px', 'left':'738px'});
	
	$('#cat').fadeOut("slow", function(){
		$('#pounce_logo').fadeOut("slow",function(){
			$('#ufo_cat').fadeIn("slow",function(){
				$('#ufo').animate({top:'84px',left:'221px'}, 1000, function() {
					$("#ufo").animate({border:'0'}, 500,function(){ // simple timer (fake animation)
						$('#ufo').animate({top: '10px',left:'731px'}, 1500, function() {
							$("#ufo_beam").show();
							$("#ufo_beam").animate({border:'0'}, 300,function(){ // simple timer (fake animation)
								$("#ufo_cat").animate({top:'55px', left:'803px', height:'37px', width:'73px'}, 2000,function(){
									$('#ufo_cat').animate({opacity:'0.0', height:'0', width:'0',top:'60px', left:'820px'}, 200, function(){;
										$('#ufo_beam').animate({border:'0'}, 200,function(){ // simple timer
											$('#ufo_beam').hide(0);
											$('#pounce_logo').fadeIn(400);
											$('#ufo').animate({top: '-40px',left:'1212px'}, 1500, function() {
												$('#cat').fadeIn(1000);
												screen_lock(false);
												
												$('#ufo').remove();
												$('#ufo_cat').remove();
												$('#ufo_beam').remove();
											});
										});
									});
								});
							});
						});
					});
				});
			});
		});
	});
};


function URLhandler(url){
	

	url_array=url.split("/")
	url_array=cleanArray(url_array);
	url_hash="";
		
	if(url_array.length > 1){
		url_hash  = ("#" + url_array[url_array.length - 1]);
		url_array=url_array.splice(url_array[url_array.length], 1);
		
		return ("/" + url_array.join('/') + "/" + url_hash);
	} else if(url_array.length == 1){
		return ("/" + url_array.join('/') + "/");		
	} else {
		return ("/" + url_array.join('/'));
	}
}


function cleanArray(actual){
  var newArray = new Array();
  for(var i = 0; i<actual.length; i++){
      if (actual[i]){
        newArray.push(actual[i]);
    }
  }
  return newArray;
}


$(document).ready(function(){

	ajax_url=URLhandler(window.location.pathname);

	if(ajax_url.length > 1){
		$.post(removeAnchorID(ajax_url), {"-js": "true"},
			function(data){
				$('#modal_sky .content_wrap').html(data);
				if(view_point!='sky'){
					sky();
				}
				$('#slideshow').cycle(getSlide());
		});
	}






	$('#pounce_logo, #modal_sky .close').click(function() {
		
		if(view_point == 'earth'){
			sky();
		} else {
			earth();
		}
		return false;
	});
	
	$('#home').click(function() {
		
		if(view_point != 'earth'){
			earth();
		}
		return false;
	});

	$('a').live('click',function(){
		url_click=URLhandler($(this).attr('href'));
		
		if($(this).attr('rel') == 'external'){
			window.open($(this).attr("href"));
			return false;
		}else if($(this).siblings('#cat').attr('id') == 'cat' || $(this).attr('id') == 'cat'){
			// cat was clicked... go nuts (do a random trick-or-treat).
			trick_or_treat();
			
		} else if(removeAnchorID(ajax_url) == removeAnchorID(url_click)) {
			// destination same as current ... go to slide associated with anchor.
			ajax_url=url_click;
			if(view_point!='sky'){
				sky();
			}
			$('#slideshow').cycle(getSlide());

		} else if($(this).attr('href') != 'undefined' && url_click != '#'){

			ajax_url=url_click;
			
			$.post(removeAnchorID(url_click), {"-js": "true"},
				function(data){
					$('#modal_sky .content_wrap').html(data);
					if(view_point!='sky'){
						sky();
					}
					$('#slideshow').cycle(getSlide());
			});
			
		};
		return false;
	});
	
});
