$(function(){
	crisis.navLeftIni();
	
	bef.scrollDown();
	crisis.pageCurls();
	crisis.preloadImages();
});

crisis = {
	box_width:480,
	fold_height:40,
	fold_max:50,
	pageCurls:function(){
		this.addCurl($('#page_curls .page_curl:last'));
	},
	addCurl:function(element)
	{
		
		var turn_object = element.find('#turn_object');
		if (turn_object.length == 0) {
			element.prepend('<div class="thing_fold" style="display:none;width:400px;height:260px;" />');
			var prevClass = element.prev().attr('class');
			element.children('.thing_fold').addClass(prevClass).fold({
				turnImage: 'lib/images/fold_sw_white.png',
				startingWidth: this.fold_height,
				startingHeight: this.fold_height,
				maxHeight: this.fold_max
			});
			element.children('#turn_wrapper').click(crisis.wipeOutPage);
		}
		else
		{
			element.children('#turn_wrapper').show();
			turn_object.animate({width:crisis.fold_height+'px',height:crisis.fold_height+'px'}, 500);
		}
		var next_link = element.find('.next_link');
		if (next_link.length == 0) {
			element.append('<a class="next_link" href="javascript:void(0);"></a>');
			element.children('.next_link').click(crisis.wipeOutPage);
		}
	},
	wipeOutPage:function(e)
	{
		var page_curl = $(this).closest('.page_curl');
		var turn_wrapper = page_curl.children('#turn_wrapper');
    	var turn_object = turn_wrapper.children('#turn_object');
		var prev = page_curl.prev();
		prev.show();
		prev.children().hide();
		page_curl.children(':not(#turn_wrapper)').fadeOut(500,function(){
		
			turn_wrapper.unbind('mouseover').unbind('mouseout');
			turn_object.animate({width:'268px',height:'268px'}, 500);
			page_curl.animate({width:"0px"}, 750, 'swing',function(){ 
				var prev = page_curl.prev();
				if (prev.children(".back_link").length == 0) {
					prev.append('<a class="back_link" href="javascript:void(0);">back</a>');
					prev.children('.back_link').click(crisis.wipeInPage);
				}
				if (prev.prev().length > 0) {
					crisis.addCurl(page_curl.prev());
					
				}
		  		prev.children(':not(#turn_wrapper)').fadeIn(500);
				
			});
    	});
		
	},
	wipeInPage:function(e)
	{
		var back_link = $(this);
		var page_curl = back_link.closest('.page_curl');
		var next = page_curl.next();
		var turn_wrapper = next.children('#turn_wrapper');
    	var turn_object = turn_wrapper.children('#turn_object');
		page_curl.find('#turn_object').animate({width:'0px',height:'0px'}, 500);
		page_curl.children(':not(#turn_wrapper)').fadeOut(500);
		turn_wrapper.bind('mouseover', {'turn_object':turn_object}, function(){
          turn_object.stop().animate({
            width: crisis.fold_max,
            height: crisis.fold_max
          });
        });
		turn_wrapper.bind('mouseout', {'turn_object':turn_object}, function(e){
          turn_object.stop().animate({
            width: crisis.fold_height,
            height: crisis.fold_height
          });
        });
		/*turn_wrapper.hover(
	        function(e){
	          turn_object.stop().animate({
	            width: crisis.fold_max,
	            height: crisis.fold_max
	          });
	        },
	        function(e){
	          turn_object.stop().animate({
	            width: crisis.fold_height,
	            height: crisis.fold_height
	          });
	        }
	    );*/
		turn_object.animate({width:crisis.fold_height+'px',height:crisis.fold_height+'px'}, 750);
		next.animate({width:crisis.box_width+'px'}, 750, 'swing',function(){ 
			
	  		next.children(':not(#turn_wrapper)').fadeIn(500);
		});
		
	},

	navLeftIni:function(e){
		var e = e || null;
		if(e) e.stopPropagation();
		//if($(this).is("[class*='_on']") ) return false;
		
		var nav;
		
		bef.toggleLeftNavPage(nav);
	},


	preloadImages:function(){
		var imgs = [];
		imgs.push('/lib/images/water/crisis/light_blue_bkg.jpg');
		imgs.push('/lib/images/water/crisis/dark_tan_bkg.png');
		imgs.push('/lib/images/water/crisis/tan_bkg.png');
		imgs.push('/lib/images/water/crisis/green_bkg.png');
		imgs.push('/lib/images/water/crisis/light_green_bkg.png');
		for (var i = 0; i < imgs.length; i++) {
			$("<img>").attr("src", imgs[i]);
		}
		
	}
	


	
}
