var addressChange;
$(document).ready(function() {
	$(document).pngFix(); 
	$stage = $("#stage");
	$class = null;
	$stageContent = $("#stage .paper");
	$body = $('body');
	$side_paper = $('.center .side_paper');
	$titles = $('.center .online_titles');
	$submissions = $("#submissions");
	$.data($body, 'section', 'home');
	$.data($body, 'subsection', '');
	$.data($body, 'page', '1');
	$.data($body, 'id', '');
	var title = document.title;
	
	addressChange = function(className, page, id){
		var $class = className;
		var $page = page;
		var $id = id;
		if($.data($body, 'section') != $class){
			$.data($body, 'page', '');
			$("a."+$.data($body, 'section')+" span").removeClass("selected");
			$("a."+$.data($body, 'section')).removeClass("selected");
			$.data($body, 'section', $class); 
			$.data($body, 'subsection', '');
			$("a."+$.data($body, 'section')+" span").addClass("selected");
			$("a."+$.data($body, 'section')).addClass("selected");
			if($.data($body, 'section') != 'online') showTitles(false);
			
			if($.data($body, 'section') == 'home'){
				if( parseInt($submissions.css('bottom')) <= -200) flyBack();
				$stage.slideUp(1000, "easeinout");
			} else if($.data($body, 'section') == 'submissions' || $.data($body, 'section') == 'contactus'){
				flyIn();
			} else {
				if($.data($body, 'section') == 'online'){
					showTitles(true,$page);
					stage($.data($body, 'section'), '0');
				} else {
					stage($.data($body, 'section'), $page);
				}
			}
			
		} else if($page != $.data($body, 'page')){
			$.data($body, 'page', $page);
			if($.data($body, 'section') == 'online'){
				showTitles(true,$page);
			} else {
				getContent($.data($body, 'section'), $page);
			}
		}
		if($id != $.data($body, 'id') && $id != '' && $.data($body, 'section') == 'online'){
			stage($.data($body, 'section'), $id);
			$.data($body, 'id', $id);
		}
	}
	
	$.address.change(function(event) {
		var $path = $.address.pathNames();
		if($path[1] && $path[1].match(/\d+$/) >= 1){
			var $classTemp = $path[0];
			var $page = $path[1].match(/\d+$/);
		} else if ($path[1]){
			var $classTemp = $path[1];
		} else {
			var $classTemp = $path[0];
		}
		var $id = ($path[2])? $.URLDecode($path[2]) : '';
		if($classTemp != null){ 
			$class = $classTemp;
			addressChange($class, $page, $id);
		}
		//Set Page title
		var names = $.map(event.pathNames, function(n) {
			n = n.split('_').join(' ').split('-').join(' ');
			return n.substr(0, 1).toUpperCase() + n.substr(1);
		}).concat(event.parameters.id ? event.parameters.id.split('.') : []);
		var titlebar = $.URLDecode([title].concat(names).join(' | '));
		$.address.title(titlebar);
	});

	//if( $("#submissions").is(':animated') && $("#submissions").css('bottom') < -200){
	
	var current_request;
	function getContent(section, pagenum){
		if(current_request) current_request.abort();
		current_request = $.get("/"+section+".inc.php", {page:pagenum},
			function(data){
				$stageContent.html(data);
				$stage.slideDown(1000, "easeinout");			  
			}
		);
	}
	
	function stage(section, page){
		if ( $stage.is(":hidden") ){
			getContent(section, page);
		} else {
			$stage.slideUp(1000, function(){
				getContent(section, page);
			}, "easeinout");
		}
		if( parseInt($submissions.css('bottom')) <= -200){
			flyBack();
		}
	}
	
	var titles_request;
	function showTitles(show,pagenum){
		if(titles_request) titles_request.abort();
		if(show == true){
			$.data($body, 'page', pagenum);
			titles_request = $.get("/online_titles.inc.php", {page:pagenum},
				function(data){
					$titles.html(data);
					//$side_paper.animate({left: '0px'}, 1000, "easeout");			  
				}
			);
		} else {
			$side_paper.animate({left: '-205px'}, 1000, "easein");
		}
	}
	
	//Fly Kite In
	function flyIn(){
		$stage.slideUp(1000, "easeinout");
		if( parseInt($submissions.css('bottom')) <= -200 && parseInt($submissions.css('bottom')) > -767 && ($.data($body, 'section') == 'contactus' || $.data($body, 'section') == 'submissions')){
			$submissions
				.stop()
				.animate({bottom: '-767px', right: '-5%'}, {queue:false, duration:2000, complete:function(){
							flyIn();
						}}, "easein");
			$('#submissions .form').fadeOut(1000);
			$('#submissions .thankyou').fadeOut(100);
		} else {
			$submissions.animate({bottom: '-200px', right: '28%'}, 2000, "easeout", function(){
				$('#submissions .form.'+$.data($body, 'section')).fadeIn(500);
			});
		}
		
		$('#submissions .thankyou').fadeOut(100);
	}
	//Fly Kite back
	function flyBack(){
		$submissions
			.stop()
			.animate({bottom: '-767px', right: '-5%'}, {queue:false, duration:2000}, "easein");
		$('#submissions .form').fadeOut(1000);
		$('#submissions .thankyou').fadeOut(100);
	}
	//Fly Kite away
	function flyAway(){
		$submissions.animate({bottom: '35px', right: '100%'}, 3250, "easein");
		$submissions.animate({bottom: '-767px', right: '-5%'}, 1, "linear", function(){
			$("a."+$.data($body, 'section')+" span").removeClass("selected");
			$("a."+$.data($body, 'section')).removeClass("selected");
			$.data($body, 'section', 'home');
		});
	}
	
	//Overwrite default link behavior
	$("#header .nav_box a, #footer .nav ul a").click(function(e){
		if(!$(e.target).hasClass("blog")){
			e.preventDefault();
			window.location = "#"+$(e.target).closest("a").attr("href");
		}
	});
	
	$('#file').change(function(){
		$('#file_view').val($('#file').val());
	});
	
	$('#submissions_form').ajaxForm({
		beforeSubmit:	function() {
            return $('#submissions_form').validate().form();
        },
		success:		function() {
            $('#submissions .form').fadeOut(750);
			$('#submissions .thankyou').fadeIn(750);
			flyAway();
        },
		target: 		'#submissions .thankyou',
		url: 			'includes/email.php'
	});
	$('#contactus_form').ajaxForm({
		beforeSubmit:	function() {
            return $('#contactus_form').validate().form();
        },
		success:		function() {
            $('#submissions .form').fadeOut(750);
			$('#submissions .thankyou').fadeIn(750);
			flyAway();
        },
		target: 		'#submissions .thankyou',
		url: 			'includes/leads.php'
	});
	
	var validator = $("#submissions_form").validate({
		highlight: function(element,errorClass) {
			$(element).addClass("error");
		},
		unhighlight: function(element,errorClass) {
			$(element).removeClass("error");
		}
	});
	var validator = $("#contactus_form").validate({
		highlight: function(element,errorClass) {
			$(element).addClass("error");
		},
		unhighlight: function(element,errorClass) {
			$(element).removeClass("error");
		}
	});

	//Animation loop functions
	function fadeOutLoop($target, time, In, Out){
		$target.animate({opacity: Out}, time, function(){
			fadeInLoop($target, time, In, Out);
		});
	}
	function fadeInLoop($target, time, In, Out){
		$target.animate({opacity: In}, time, function(){
			fadeOutLoop($target, time, In, Out);
		});
	}
	
	//Smoke
	$smoke1 = $('#smoke1');
	$smoke2 = $('#smoke2');
	$smoke2.animate({opacity: 0}, 1);
	fadeOutLoop($smoke1, 5000, 1, 0);
	fadeInLoop($smoke2, 5000, 1, 0);
	//Smoke small
	$smokeSmall1 = $("#smoke_small1");
	$smokeSmall2 = $("#smoke_small2");
	$smokeSmall2.animate({opacity: 0}, .75);
	fadeOutLoop($smokeSmall1, 4000, .75, 0);
	fadeInLoop($smokeSmall2, 4000, .75, 0);
	
	//Twinkling stars
	$star1 = $("#star1");
	$star3 = $("#star3");
	$star6 = $("#star6");
	fadeOutLoop($star1, 2000-Math.floor(Math.random()*1500), 1, .4);
	//fadeOutLoop("#star2", 2000-Math.floor(Math.random()*1500), 1, .4);
	fadeOutLoop($star3, 2000-Math.floor(Math.random()*1500), 1, .4);
	//fadeOutLoop("#star4", 2000-Math.floor(Math.random()*1500), 1, .4);
	//fadeOutLoop("#star5", 2000-Math.floor(Math.random()*1500), 1, .4);
	fadeOutLoop($star6, 2000-Math.floor(Math.random()*1500), 1, .4);
	//fadeOutLoop("#star7", 2000-Math.floor(Math.random()*1500), 1, .4);
		
	//
	function params(centerpoint, radii, starting, ending, direction){
		var arc_params = {
			center: centerpoint,  
			radius: radii,    
			start: starting,
			end: ending,
			dir: direction
		 }
		 return arc_params;
	}
	function flykite($target, time, ease, centerpoint, radii, starting, ending, direction){
		$target.animate({path : new $.path.arc(params(centerpoint, radii, starting, ending, direction))}, time, ease, function(){ flykite($target, time, ease, centerpoint, radii, ending, starting, direction*-1) });
	}
	$kite1 = $('#kite1');
	$kite2 = $('#kite2');
	$kite3 = $('#kite3');
	flykite($kite1, 11000, "easeinout", [0,0], 25, 335, 115, -1);
	flykite($kite2, 12500, "easeinout", [-270,-100], 25, 55, 355, 1);
	flykite($kite3,  9750, "easeinout", [-200,105], 25, 350, 130, 1);
}); 

$(window).load(function(){
	$("#kites").animate({left: '40%', bottom: '-65px'}, 3000, "easeout");
	
	$("#clouds").fadeIn(5000);
	$("#moon").fadeIn(2500);
	
	//Balloon Animation
	$balloon = $("#balloon");
	$balloon.fadeIn(33);
	$balloon.animate({"right": "100%", "bottom": "165px"}, 100000);
});

function navigate(url){
	window.location = url;
}
	
function clickclear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
		thisfield.value = "";
	}
}
function clickrecall(thisfield, defaulttext) {
	if (thisfield.value == "" || thisfield.value == defaulttext) {
		thisfield.value = defaulttext;
	}
}

//Scrolling function
function makeScroll(){
	var time = 550;
	var increment = 212;
	var stage_height = 425;
	var margin = 0;
	var $up = $(".up");
	var $down = $(".down");
	var $scrollContent = $(".center .side_paper .online_titles .content ul");
	var content_height = $scrollContent.height();
	$up.css("display","none");
	if(content_height-margin <= stage_height){
		$down.css("display","none");
	}
	scroll_func = function(direction){
		$scrollContent.stop();
		var up = parseFloat($scrollContent.css("top"))+increment;
		var down = parseFloat($scrollContent.css("top"))-increment;
		
		if(direction == "up" && up>=0){
			var anim_val = "0px";
			$up.css("display","none");
		} else if(direction == "up"){
			var anim_val = up+"px"
		}
		
		if(direction == "down" && down<=stage_height-content_height){
			var anim_val = stage_height-content_height+"px";
			$down.css("display","none");
		} else if(direction == "down"){
			var anim_val = down+"px"
		}
		return anim_val;
	}
	$up.click(function(){					  
		$scrollContent.animate({"top": scroll_func("up")}, time, "easeinout");
		$down.css("display","block");
	});
	$down.click(function(){
		$scrollContent.animate({"top": scroll_func("down")}, time, "easeinout");
		$up.css("display","block");
	});
}