var Ledgeview = {
	
	init : function()
	{
		$(document).pngFix();
		
		$('#individualSlides').cycle({
			fx: 'fade', 
    		timeout: 8000,
			speed: 2000,
			random: 1
		});
		
		$('a[href=sitemap]').bind('click', Ledgeview.go_to_sitemap);
		
	},
	
	go_to_sitemap : function()
	{
		$.scrollTo($('#footer'), 800, {easing:'easeInOutCubic'});
		return false;
	},
	
	make_popup : function(content,id,opt)
	{
		$('object').hide();
		
		id = id || 'popup_box';
		var width = (opt && opt.width) ? opt.width : 600;
		
		if(!$('#overlay').length)
		{
			var overlay = $$('div',
						'',
						{ className:'overlay', id:'overlay' });
						
			overlay.css({
				height:$(document).height(),
				width:$(document).width(),
				opacity:0.7
			});
			
			$('body').append(overlay);
		}
		
		var box = $$('div',
					$$('div',content,{ className:'inset' }),
					$$('a',
						$$('img','',{ src:'/images/buttons/close.gif' }),
						{ className:'close_button', id:id+'_close_button' }),
					{ className:'popupbox', id:id });

		
		var top = 100+$(document).scrollTop()+($('.box').size()*10);
		box.css({
			width:width,
			top:top,
			left:($(window).width()*.5)-(width/2)+($('.box').size()*10)
		});

		$('body').append(box);
		$('#'+id+'_close_button').bind('click', function()
		{ 
			$('#'+id).remove();
			if(!$('.box').size()) $('#overlay').remove();
			$('object').show();
		});
		
	},
	
	kill_popup : function(id)
	{
		if($('.popupbox').size()>1)
		{
			$('.popupbox:last').remove();
		}
		else
		{
			$('.popupbox:last').remove();
			$('#overlay').remove();
			$('object').show();
		}
	},
	
	validate_form : function(form)
	{
		if(!form) form = document.forms[0];
		var submit_it = true;
		var email_pattern = new RegExp(/[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i);

		$(form).find(".REQUIRED").each(function(req)
		{
			if(($(this).attr('type')!='checkbox' && this.value == '') || 
				($(this).attr('type')=='checkbox' && !this.checked) || 
				($(this).hasClass('email') && !email_pattern.test($(this).val()))) 
			{
				$(this).parents("div.form").addClass('error');
				submit_it = false;
			}
			else
			{
				$(this).parents("div.form").removeClass('error');
			}			
		});
		if( !submit_it ) 
		{
			$('#error_message').html('Please review the highlighted, required fields and resubmit.').fadeIn(250);
			$.scrollTo('#error_message', 800, {easing:'easeInOutCubic'});
		}
		return submit_it;
	}
}

$(document).ready(function() {
	Ledgeview.init();
});

