// javascript

var Prestogeo = {
	
	current_menu:Object,
	
	button_int:Number = 0,
	
	dropdown_duration:Number = 250,
	dropdown_int:Number = 0,
	dropdown_showing:Boolean = false,
	dropdown_wait:Number = 500,
	
	subdropdown_int:Number = 0,
	
	slideshow_duration:Number = 4000,
	slideshow_int:Number = 0,
	slideshow_num:Number = 0,
	
	init : function()
	{
		$(".dropdown").bind('click', function(){ return false; }).parents('td:not(.selected)').each(function(index) {
			$(this).bind('mouseover', Prestogeo.dropdown_over)
				.bind('mouseover', Prestogeo.over)
				.bind('mouseout', Prestogeo.dropdown_out)
				.bind('mouseout', Prestogeo.out)
				.bind('mouseover', Prestogeo.cancel_timer)
				.bind('mouseout', Prestogeo.start_timer)
				.bind('click', function(){ return false; });
		});
		$('#mainmenu td:not(.dropdown)').each(function(index) {
			$(this).bind('mouseover', Prestogeo.over)
				.bind('mouseout', Prestogeo.out)
				.bind('click', function(){
					var a = $(this).children('a');
					if(!a.hasClass('.dropdown')) document.location.href = a.attr('href');
				});
		});
		$('#dropdown').bind('mouseout', Prestogeo.start_timer).bind('mouseover', Prestogeo.cancel_timer);
		$('.subsubmenu').each(function(index) {
			$(this).bind('mouseover', Prestogeo.show_subsubsubmenu)
				.bind('mouseout', Prestogeo.hide_subsubsubmenu);
		});
		$('a.expand').bind('click', Prestogeo.expand);
		
		if($('#photos').length)
		{
			Prestogeo.slideshow_int = setInterval(Prestogeo.switch_billboard,Prestogeo.slideshow_duration);
		}
		
		// embed flowplayer
		if($('#fplayer').length) flowplayer("fplayer", "/swf/flowplayer/flowplayer-3.1.5.swf", {plugins: {
		   controls: {
		      timeColor: '#126AAA',
		      timeBgColor: '#222222'
		   }
		}});
	},
	
	show_subsubsubmenu : function()
	{
		var d = $(this);
		clearTimeout(Prestogeo.subdropdown_int);
		$("div.dropdownmenu:not(d)").hide();
		
		var link = d.attr('id').substring(4);
		var excerpt = d.children('a').attr('rel');
		if(window.console) window.console.log(excerpt);
		
		d.css({'z-index':9000}).children('div').show();
		$('img.dropdownimage:visible').attr('src','/virtualimage/dropdown/'+link+'.jpg');
		$('p.excerptcontent:visible').html(excerpt);
		
	},
	
	hide_subsubsubmenu : function()
	{
		var d = $(this);
		d.css({'z-index':1000});
		Prestogeo.subdropdown_int = setTimeout(hideit,250);
		
		function hideit()
		{
			d.children('div.dropdownmenu').hide();
		}
	},
	
	switch_billboard : function()
	{
		var photos = ['home.jpg','home1.jpg','home2.jpg','home3.jpg'];
		
		var img = $$('img',
			'',
			{ src:"/images/billboards/"+photos[++Prestogeo.slideshow_num%photos.length] });
		
		img.hide().load(function()
		{
			$('#photos').append(img);
			img.fadeIn(750,function(){ img.siblings().each(function(index) {
				$(this).remove();
			}); });
		});
	},
	
	expand : function()
	{
		var id = $(this).attr('rel');
		if($.browser.msie && parseInt(jQuery.browser.version)<8)
		{
			$('#expand_'+id).toggle();
		}
		else
		{
			$('#expand_'+id).animate({"height": "toggle"}, Prestogeo.dropdown_duration, 'easeInOutCubic');
		}
		if($(this).hasClass('plus'))
		{
			$(this).addClass('minus').removeClass('plus');
		}
		else
		{
			$(this).addClass('plus').removeClass('minus');
		}
		return false;
	},
	
	over : function()
	{
		if (!$(this).hasClass('selected')) $(this).addClass('over');
	},
	
	out : function()
	{
		if (!$(this).hasClass('selected')) $(this).removeClass('over');
	},
	
	dropdown_over : function()
	{
		Prestogeo.current_menu = $(this);
		Prestogeo.button_int = setTimeout("Prestogeo.show_dropdown()",100);
	},
	
	dropdown_out : function()
	{
		if(Prestogeo.button_int) clearTimeout(Prestogeo.button_int);
	},
	
	show_dropdown : function()
	{
		var a = Prestogeo.current_menu;
		var area = a.children('a').attr('href').replace('/','');
		// var excerpt = a.children('a').attr('rel');
		
		// if (window.console) window.console.log(a.children('a').html());
		
		a.addClass('selected').removeClass('over').siblings('td').each(function(index) {
			$(this).removeClass('selected');
		});
		$('#'+area).find('img.dropdownimage').each(function(index){
			$(this).attr('src','/images/blank.gif');
		});
		
		$('#'+area).find('p.excerptcontent').each(function(index) {
					$(this).html('');
				});
		
		$('#'+area).show().siblings('div').each(function(index) {
			$(this).hide();
		});
		
		if(!Prestogeo.dropdown_showing) $('#dropdown').animate({"height": "toggle"}, Prestogeo.dropdown_duration, 'easeInOutCubic');
		Prestogeo.dropdown_showing = true;
		return false;
	},
	
	hide_dropdown : function()
	{
		$('#dropdown').fadeOut(100);
		$('.dropdown').parent('td').each(function(index) {
			$(this).removeClass('selected');
		});
		Prestogeo.dropdown_showing = false;
	},
	
	start_timer : function()
	{
		Prestogeo.dropdown_int = setTimeout(Prestogeo.hide_dropdown,Prestogeo.dropdown_wait);
	},
	
	cancel_timer : function()
	{
		clearTimeout(Prestogeo.dropdown_int);
	},

	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.formfield").addClass('error');
				submit_it = false;
			}
			else
			{
				$(this).parents("div.formfield").removeClass('error');
			}			
		});
		if( !submit_it ) $('#error_message').fadeIn(250);
		return submit_it;
	}
}

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

