
var c_min=0;	
var c_max=600;	//max width
var c_lft=0;	
var m_lft=360; //subject width
var news_size = "";
		
var selecteasing="easeInOutQuad";
var animspeed=800;



$(document).ready(function(){
						   
	$('.pgewrapper').hide();
	$('.pgemask').css({opacity: 0.8});
		
	$('#book').click(function(){
		$('.pgewrapper').show();
		$('.pgecontainer').pgeConCen();	
	});
						   
	$(window).bind("resize",function(){
		$('.pgecontainer').pgeConCen();					   
	});
	
	$('.pgemask').click(function(){
		$('.pgewrapper').hide();				
	});	
	
	
	
	
						  
$('#join').submit(function(){
	joinemail = $('#joinemail').val(); 
	
	if(CheckEmail(joinemail))
	{
		$.post("media/php/baf/baf2.php",{email: joinemail}, function(data){
			$('#baf_return').text(data).css({color: "#fff"});
			$('#joinemail').val("");
		});	
	}
	else
	{
		$('#baf_return').html("Please enter a valid email address.")
		.css({color: "#fff"});	
	}
	return false;
	});
	
	
	//variables
	var contain = $('#sections');
	var section = contain.children();
	var nav = section.children('.nav');
	var nav_ul = nav.children('ul');
	var section_name = $('a.section_name');
	var section_name = $('a.section_name');
	var pagecontent = $('.pagecontent');
	
	var s_maxwidth = 648;
	var s_minwidth = 30;



	//startup	
	nav_ul.fadeOut();
	//pagecontent.hide();
	expand(section.eq(0));
	
	
	
	
	//events
	section_name.click(function(){
		expand($(this).parent().parent());
		
		return false;
	});
	
	$('input#joinemail').example(function() {
	   return $(this).attr('title');
 	});

	
	
	//expand section elements
	function expand(element){
		if(!element.hasClass('current')){
			
			//out
			$('.current')
			.animate({
				width: s_minwidth
			},{queue: false});
			$('.current').children().children('ul').fadeOut();
			
			
			$('.current').children('.nav').animate({
				width: 9
			}).children('a.section_name').animate({
				width: 9
			}).css({backgroundPosition: "top right"});
			
			$('.current').children('.pagecontent').hide();
			$('.current').removeClass('current');
			

			
			//in
			element.children('.pagecontent').show();
			
			element
			.animate({
				width: s_maxwidth	 
			},{queue: false})
			.addClass('current');
			
			element.children('.nav').animate({
				width: 115
			}).children('a.section_name').animate({
				width: 115
			}).css({backgroundPosition: "0px -300px"});
			
			if(element.children().children('ul').children('li').length<2){ 
				element.children().children('ul').hide();
			} else {
				element.children().children('ul').fadeIn();
			}
			

			pageload(element.children('.nav').children('.section_name'));
			
		}
	}
	
nav.children().children().children().click(function(){
	pageload($(this));
	return false;	
});


});

function loadcontent(element) {
		element.fadeOut();
		element.bind("load", function(){
			element.fadeIn();			  
		});
	}

function pageload(button){
	
	$('.pagecontent').html('');
		
			if (button.hasClass("section_name")) {
				//setup variables
				var loadurl = button.parent().children('ul').children().children('a').eq(0).attr('href');
					
				//load the content
				//loadcontent(button.parent().parent().children('.pagecontent'));
				button.parent().parent().children('.pagecontent')
				.load(loadurl + " .ajax_content", function(){
				
				//callback
					pageload_callback(button.parent().children('ul').children().children('a').eq(0));
				});
				
			} else {
			
				//setup variables
				var loadurl = button.attr('href');
					
				//load the content
				//loadcontent(button.parent().parent().parent().parent().children('.pagecontent'));
				button.parent().parent().parent().parent().children('.pagecontent')
				.load(loadurl + " .ajax_content", function(){
				
				//callback
					pageload_callback(button);
				});
			}
}

function pageload_callback(button){
	var ajaxhook = button.parent().parent().parent().parent().children('.pagecontent').children('.ajax_content');


	if($('.mar').length>0) {
		$('.mar').click(function(){
			$('.pgewrapper').show();
			$('.pgecontainer').pgeConCen();	
		});
	}
	
	if($('a.hkback').length>0){
		$('a.hkback').click(function(){
				var loadurl = $(this).attr('href');
					
				//load the content
				//loadcontent(button.parent().parent().parent().parent().children('.pagecontent'));
				$(this).parent().parent().parent().parent().children('.pagecontent')
				.load(loadurl + " .ajax_content", function(){
				
				//callback
					pageload_callback(button);
				});
				return false;
		});
	}

if($('a.summary').length>0){
		$('a.summary').click(function(){
				var loadurl = $(this).attr('href');
					
				//load the content
				//loadcontent(button.parent().parent().parent().parent().children('.pagecontent'));
				$(this).parent().parent().parent().parent().parent().parent().parent().children('.pagecontent')
				.load(loadurl + " .ajax_content", function(){
				
				//callback
					pageload_callback(button);
				});
				return false;
		});
	}
	if($('#video').length > 0){	
		var flashpath = "media/video/" + $('#video').attr('rel');
		flashembed("video", {src: flashpath, wmode: 'transparent'});
	}
	
	if ($('#hk_switch').length>0){
				$('#hk_switch').click(function(){
				var loadurl = $('#hk_switch').attr('href');
					
				//load the content
				//loadcontent(button.parent().parent().parent().parent().children('.pagecontent'));
				$('#hk_switch').parent().parent().parent().children('.pagecontent')
				.load(loadurl + " .ajax_content", function(){
				
				//callback
					pageload_callback(button);
				});
				return false;
		});
	}
	
	if($('#hk_news').length > 0){
			
		news_size = $('#innercon').children('div').size();
		m_lft = $('#innercon div').width();
		c_max = m_lft * news_size;
		//alert(c_max);
		
		//animate arrows
		$('#arrowright').click(function(){
			$('#innercon').IcRight(selecteasing, animspeed);		
			return false;	
		});
		
		$('#arrowleft').click(function(){
			$('#innercon').IcLeft(selecteasing, animspeed);		
			return false;
		});
		

	}
	
	if($('#hk_cs').length > 0){
			
		cs_size = $('#innercon').children('div').size();
		m_lft = $('#innercon div').width();
		c_max = m_lft * cs_size - $('#hk_cs').width() + 100;
		
		//animate arrows
		$('#arrowright').click(function(){
			$('#innercon').IcRight(selecteasing, animspeed);		
			return false;	
		});
		
		$('#arrowleft').click(function(){
			$('#innercon').IcLeft(selecteasing, animspeed);		
			return false;
		});
		

	}
	
	/* REVIEWS PAGE FUNCTIONS */
	if($('.revLst').length>0){
		
		$('.revC').css({top:500,display:'block'}).eq(0).css({top:0});
		$('.revMask').animate({opacity:.8},{duration:0,queue:false});
		revSel=0;
		
		$('.revLst').css({zIndex:60}).children('li').children('a').click(function(){
			thsId=$('.revLst li a').index(this);
			secCount=$('.revC').size()-1;
			if(revSel!=thsId){
				aniIn=thsId
				$('.revC').each(function(){
					if($(this).css('top')=="0px"){
						thsIndx=$('.revC').index(this);
						aniO=thsIndx;
					}
				});
				revSel=thsId;
				$('.revC').eq(aniIn).css({zIndex:5}).animate({top:0},{duration:500,queue:false});
				$('.revC').eq(aniO).css({zIndex:0}).animate({top:-500},500,function(){$(this).css({top:500})});

			}
		});
		
	}
	
	if(ajaxhook.children('.icontent').children('a.showhide').length > 0){
					//img controls
					dura = 500;
					moveMax = "-420px";
					movelmax = "-260px";
					movelmin = 20;
					moveMin = 20;
					
					var tvisable = true;
						$('a.showhide').click(function() {
							if(tvisable == true) {
								//show text	
								$(this).parent('.icontent').animate({
									
									top: moveMax,
									left: movelmax
									
								},{duration: dura, queue: false});
								$(this).html('show text');
								//$(this).parent().parent().parent().children(".imgcontrol").fadeOut();
								
								tvisable = false;
						
							} else {
								//hide text	
								$(this).parent('.icontent').animate({
																
									top: moveMin,
									left: movelmin
									
								},{duration: dura, queue: false});
								$(this).html('hide text');
								//$(this).parent().parent().parent().children(".imgcontrol").fadeIn();
								
								tvisable = true;
					
							}
							return false;
						});
					
					
				}//end if a.showhide
				
				if (ajaxhook.children('.images').length > 0) {
					ajaxhook.children('.images').cycle();
				}//end if #images
	}

function CheckEmail(inputemail) {
	AtPos = inputemail.indexOf("@");
	StopPos = inputemail.lastIndexOf(".");

	if (AtPos == -1 || StopPos == -1)
	{
		return false;
	}
	else
	{
		return true;	
	}

}

$.fn.pgeConCen = function(){
		Pheight=$(window).height()/2;
		Thisheight=$(this).height()/2;
		Cheight=Pheight-Thisheight;
		$(this).animate({
			"top":Cheight+"px"			
		},{ duration: 200, queue: false });
};
