var Site = {
	
	drop_int:Number = 0,
	drop_cur:Object,
	
	photo_cur:Number = 0,
	photo_int:Number = 0,
	photo_duration:Number = 3500,
	
	home_parts:Array,
	home_partint:Number = 0,
	home_currentpart:Number = 0,
	home_currentimage:Number = 0,
	home_int:Number = 0,
	home_duration:Number = 2000,
	
	pos_int:Number = 0,
	
	init : function()
	{
		$(document).pngFix();
		
		$('a.droptrig, div.dropdown').bind('mouseover', Site.dropOver)
									.bind('mouseout', Site.dropOut);
									
		Site.drop_cur = $('a.droptrig.active');
		if (Site.drop_cur.size()) 
		{
			Site.showDrop();
			Site.pos_int = setTimeout(Site.positionDrop,500);
		}

		$('img.popup.close').live('click', Site.close_popup);
		
		// rotate homepage photos
		Site.home_parts = ['topright','left','bottomright'];
		if ($('#homepage').size()) 
		{
			Site.home_int = setTimeout(Site.change_homepage,Site.home_duration);
		}
		
		
		// start slideshow
		if ($('#gallery').size()) 
		{
			Site.photo_int = setInterval(Site.change_slideshow,Site.photo_duration);
		}
		
					
		if (typeof photos !== 'undefined' && photos.length) 
		{
			$('a.arrow').bind('click', Site.change_photo);
			$('a.playbutton').bind('click', Site.play_slideshow);
			$('a.pausebutton').bind('click', Site.pause_slideshow);
		}
		else
		{
			$('a.arrow,a.playbutton,a.pausebutton').addClass('disabled');
		}				
		
		$('#contact input').bind('focus', Site.clear_input).bind('blur', Site.replace_input);
	},
	
	close_popup: function(){
		
		$(this).parent().fadeOut(200);
		
	},
	
	change_homepage: function(){
		
		
		var partnum, photonum;
		/*
		do
		{
			partnum = Math.floor(Math.random()*3);
		}
		while(partnum == Site.home_currentpart);
		*/
		Site.home_currentpart++;
		var randpart = Site.home_parts[Site.home_currentpart%Site.home_parts.length];
		
		Site.home_currentimage++;
		photonum = (Site.home_currentimage%7)+1;
		
		var imagepath = "/images/homepage/"+randpart+"/photo-"+photonum+".png";
		
		var objimg = new Image();
		$(objimg).bind('load', function()
		{
			var newimg = $('<img src="'+imagepath+'" />');
			newimg.hide().insertAfter($('#homepage div.'+randpart+' img')).end().fadeIn(1200).prev().remove();
			Site.home_int = setTimeout(Site.change_homepage,Site.home_duration);
			
		}).attr('src',imagepath);
		
	},
	
	change_photo: function(auto){
		
		var nextphoto = ($(this).hasClass('next') || auto===true) ? (Site.photo_cur+1)%photos.length : (Site.photo_cur+photos.length-1)%photos.length;
		window.log(nextphoto);
		if (auto!==true && Site.photo_int) 
		{
			clearInterval(Site.photo_int);
			Site.photo_int = 0;
			$('a.playbutton,a.pausebutton').toggleClass('disabled');
		}
		
		var objimg = new Image();
		$(objimg).bind('load',function()
		{
			var newimg = $('<img class="photo" src="/foleyadmin/thumb/'+photos[nextphoto]+'/600px/" />');
			newimg.hide().insertAfter($('img.photo')).end().fadeIn(500).prev().remove();
		});
		$(objimg).attr('src',"/foleyadmin/thumb/"+photos[nextphoto]+"/600px/");
		
		Site.photo_cur = nextphoto;
		
		return false;
		
	},
	
	change_slideshow: function(){
		
		Site.change_photo(true);
	},
	
	play_slideshow: function(){
		
		if (!Site.photo_int) 
		{
			window.log('play');
			Site.change_slideshow();
			Site.photo_int = setInterval(Site.change_slideshow,Site.photo_duration);
			$('a.playbutton,a.pausebutton').toggleClass('disabled');
		}
		return false;
	},
	
	pause_slideshow: function(){
		
		if (Site.photo_int) 
		{
			window.log('pause');
			clearInterval(Site.photo_int);
			Site.photo_int = 0;
			$('a.playbutton,a.pausebutton').toggleClass('disabled');
		}
		return false;
	},
	
	dropOver: function(){
		
		if(Site.pos_int) clearInterval(Site.pos_int);
		
		if(Site.drop_int) clearTimeout(Site.drop_int);
		Site.drop_cur = $(this);
		if (Site.drop_cur.attr('rel')) 
		{
			Site.showDrop();
		}
	},
	
	dropOut: function(){
		
		Site.drop_int = setTimeout(Site.hideDrop,400);
	},
	
	positionDrop: function(){
		
		if (Site.drop_cur.size()) 
		{
			var id = Site.drop_cur.attr('rel');
			var xpos = Math.round(Site.drop_cur.parent().position().left-25);
			$('#'+id).show().css({'left':xpos+'px'});
		}
	},
	
	showDrop: function(){
		
		$('div.dropdown,div.toptab').hide();
		Site.positionDrop();
		
		Site.drop_cur.prev().show();
	},
	
	hideDrop: function(){
		
		$('div.dropdown,div.toptab').not('.active').fadeOut(100);
		$('div.toptab.active, div.dropdown.active').show();
		
	},
	
	clear_input: function(){
		
		$(this).data('default',$(this).val()).val('');
	},
	
	replace_input: function(){
		
		if ($(this).val()=='') 
			$(this).val($(this).data('default'));
	},
	
	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;
	}
};

window.log=function(){log.history=log.history||[];log.history.push(arguments);if(this.console){console.log(Array.prototype.slice.call(arguments));}};

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

