$('#form_container').hide() ;
$('#video_container').hide() ;

//CODE FOR GOOGLE ANALYTICS
try { var pageTracker = _gat._getTracker("UA-7154431-3"); pageTracker._trackPageview(); } catch(err) {}


//LOADING SCREEN

$(window).load(function()
{ 
	 	$('#window_loader').fadeOut(500);
});  


$(document).ready(function()
{	
	
	//BODY HEIGHT FUNCTION AND PAGE CONTENT
	
	window.fade_time = 500;
	window.resize_time = 2000;
	
	content_height = $("#content").css("height");
	
	//AJAX RESIZE
	
	//SET DEFAULTS
	$(".tab_ctrl").hide();
	$('#form_master').hide() ;
	$('#form_container').hide() ;
	$('#video_container').hide() ;
	$('#event_manager').hide() ;
	$.post("../content.php" , { 'id' : 'l1' } , function(data) { $('#content').html(data); resize_content(); } , "html");	
	
	
	$('#content').ajaxStart(function(){ $('#loading').fadeIn(window.fade_time); });
	$('#content').ajaxStop(function(){ $('#loading').fadeOut(window.fade_time); $('#content').fadeIn(window.fade_time); });
	
	$(".nav_element p").click(function()
									   {
										window.page_id = $(this).attr("id");
										//window.page_name = $(this).html();
										
										window.set_id = 1;
										return false ;
										//fadeOut_content();
										
	});
	
	//TAIL LINKS FUNCTION
	$(".tail_links").click(function()
									   {
										window.page_id = $(this).attr("id");
										//window.page_name = $(this).html();
										
										window.set_id = 1;
										$('body').scrollTo({top:'150px',left:'0px'}, 2000 );
										
										return false ;
										//fadeOut_content();
										});
	
	//TAIL LOGO IMAGES FUNCTION
	$('.logo_tail, .logo_head').mouseover(function(){
	
		var old_src = $(this).attr('src');
		var new_src = old_src.replace('BW','COR');
		
		$(this).attr('src',new_src);
	
	});
	
	$('.logo_tail, .logo_head').mouseout(function(){
	
		var old_src = $(this).attr('src');
		var new_src = old_src.replace('COR','BW');
		
		$(this).attr('src',new_src);
	
	});
	
	//TEXT LINKS
	$(".linkto").live('click', function()
									{
										window.page_id = $(this).attr("id");
										//window.page_name = $(this).html();
										
										window.set_id = 1;
										$('body').scrollTo({top:'150px',left:'0px'}, 2000 );
										
										return false ;
										//fadeOut_content();
									});
	
	//TAB APPEAR FUNCTION
	$(".nav_element").mouseover(function ()
											{
											var currentId = $(this).attr('id');
									
											$(" #"+currentId + "> .button").css({"background-image":"url(../assets/style/drop_on.png)"});
											$(" #"+currentId + "> .tab_ctrl").show();
											
											$("#"+currentId).mouseleave(function ()
																					{
																			$("#"+currentId + "> .tab_ctrl").hide();
																			$("#"+currentId + "> .button").css({"background-image":"url(../assets/style/drop_off.png)"});
																					});
										
											$("#"+currentId + " p").click(function ()
																					{
																			$("#"+currentId + "> .tab_ctrl").hide();
																			$("#"+currentId + "> .button").css({"background-image":"url(../assets/style/drop_off.png)"});
																					});
											
											});
	//NEWSLETTER FUNTION
	$("#newsletter_submit").click(function ()
											{
											email = $("#newsletter_field").val();
											
											$.post("../newsletter.php" , { 'email' : email } , function(data)
																											   {
																											   $('#newsletter_field').val(data);
																											   } , "html");
											email = null;
											
											
											});
	
	//SECURE LOGIN
	$("#submit").live('click' , function() {
										  var user = $('#user').attr('value');
										  var password = $('#password').attr('value');
										  
										  $.post("../content.php" , { 'user' : user , 'password' : password } , function(data) { $('#content').html(data); resize_content(); } , "html");
										
										  });
	
	//HEAD BUTTONS FUNCTION
	$('#purple_button').click(function (){
											
									$( '#window_loader' ) . css ( { 'background' : 'url(http://components.yesmeeting.org/styles/transparent.png) repeat' } ) ;
									
									$( '#form_container' ) . css ( { 'height' : '100px' , 'visibility' : 'visible' } ) ;
									
									
									function forms () { $('#event_manager').fadeIn(); }
													  
									function h_size_form () { $( '#form_container' ) . animate ( { height : 8000 } , 1000 , function() { forms() ; } ) ; }
									
									function w_size_form () { $( '#form_container' ) . animate ( { width : 1000 } , 1000 , function() { h_size_form() ; } ) ; }
									
									function fade_form () { $( '#form_container' ) . fadeIn ( 500 , function() { w_size_form() ; } ) ;  } 
											
											
											$ ( '#window_loader' ) . fadeIn ( 500 , function () { fade_form () } ) ;
											
											
											
										});
	
	
	// BUTTONS FUNCTION
	$('#form_container').hide();
	
	$('.call_form').live( 'click' , function ()
	{
									$( '#window_loader' ) . css ( { 'background' : 'url(http://components.yesmeeting.org/styles/transparent.png) repeat' } ) ;
									
									$( '#form_container' ) . css ( { 'height' : '100px' , 'visibility' : 'visible' } ) ;
									
									
									function forms () { $('#event_manager').fadeIn(); }
													  
									function h_size_form () { $( '#form_container' ) . animate ( { height : 8000 } , 1000 , function() { forms() ; } ) ; }
									
									function w_size_form () { $( '#form_container' ) . animate ( { width : 1000 } , 1000 , function() { h_size_form() ; } ) ; }
									
									function fade_form () { $( '#form_container' ) . fadeIn ( 500 , function() { w_size_form() ; } ) ;  } 
											
											
											$ ( '#window_loader' ) . fadeIn ( 500 , function () { fade_form () } ) ;
											
	});
	
	$('.video_call').click ( function () {
									   
									$( '#window_loader' ) . css ( { 'background' : 'url(http://components.yesmeeting.org/styles/transparent.png) repeat' } ) ;
									
									$( '#video_container' ) . css ( { 'visibility' : 'visible' } ) ;
									
									$ ( '#window_loader' ) . fadeIn ( 500 , function () { $( '#video_container' ) . fadeIn ( 500 ) } ) ;
											
									     });
	
	
	//CLOSE BLACK WINDOW
	
	$('#window_loader').click(function (){ 
											$('#window_loader') . fadeOut ( 500 ) ;
											
											$('#video_container') . fadeOut ( 500 ) ;
											
											$('#form_container') . fadeOut ( 500 ) ;
											
										  });
	
	$('#video_container').click(function(){ return false ; });
	
		
		
		
	if ( ! document.location.hash  || document.location.hash == '' )
	{
		document.location.hash = 'l1' ;
	}
	
	window.page_id = document.location.hash ;
	
	window.set_id = 0;

	setInterval ("checkAnchor()" , 300 ) 
	

});


function checkAnchor()
{
	//Check if it has changes
	if ( document.location.hash != '#' + window.page_id )
	{
		
		//if there is not anchor, the loads the default section
		if ( window.set_id == 1 )
		{
			// alert ('LINK CLICKED -> ' + window.page_id + ' | CURRENT HASH -> ' + document.location.hash );
			
			document.location.hash = '#' + window.page_id ;
			
			//return false ;
			
			window.set_id = 0;
			
		}
		else if ( document.location.hash == '' )
		{
			document.location.hash = '#l1';
			
			window.page_id = document.location.hash;
		}
		else
		{
			// alert('HASH CHANGED TO ' + document.location.hash );
			
			window.page_id = document.location.hash.substring( 1 ) ;
					
		}
		
		fadeOut_content();
		
	}
}

function resize_content()
{
	//alert (content_height);
	if ( $('#content img').length ) { 
		
		$('img:not(.logo_tail, .logo_head)').load(function() {
															 
			//alert('Image Loaded'); 
			
								var content_height = $('#content').height(); //alert('content height -> '+content_height);
								// var correction = content_height.length; alert('correction -> '+correction);
								//var corrected_height = content_height.substr(0,correction - 2); alert('corrected height -> '+corrected_height);
								
								corrected_height = content_height + 115; //alert(corrected_height);
								corrected_height = corrected_height+"px";
			
								$('#body_container').animate({ height:corrected_height } , window.resize_time );
		});  
		
	} else {
			
								var content_height = $('#content').height(); //alert('content height -> '+content_height);
								// var correction = content_height.length; alert('correction -> '+correction);
								//var corrected_height = content_height.substr(0,correction - 2); alert('corrected height -> '+corrected_height);
								
								corrected_height = content_height + 115; //alert(corrected_height);
								corrected_height = corrected_height+"px";
			
								$('#body_container').animate({ height:corrected_height } , window.resize_time );
	}
	
}

function fadeOut_content()
{
	$('#content').fadeOut(window.fade_time , function()
							{
							//alert(window.page_id);
							$.post("../content.php" , { 'id' : window.page_id } , function(data) { $('#content').html(data); resize_content(); } , "html");
							var page_name = '/'+window.page_id;
							
							//INSERT ANALYTICS PAGE VIEW
							pageTracker._trackPageview(page_name);								
							});
	
							// A LINE TO TAKE CARE OF!!!

}
