/**
 * @author rossatkinson
 */
Millwork = {
	
	thumbs_current : String,
	thumbs_slide : Number,
	thumbs_x : Number,
	
	photo_current : Number = 0,
	photo_slide : Number,
	total_slides : Number = 0,
	
   init : function()
   {
		if($('#subpageL').height() < $('#subpageR').height()) $('#subpageL').find('.inset').height($('#subpageR').height()-150);
		
		$('#list1a').accordion(({ header: 'a.slider' }));	
		
		// expand dropdowns (i.e. FAQ's)
		$("a.expand").click( function(expand)
		{
			$("#"+$(this).attr("rel")).toggle();
			//this.ancestors()[0].toggleClassName('expanded');
			return false;
		});
		
		
		// sidebar stretch actions
		$("#sidebar","#sidebar_shadow").height($("#content").height());
		
		
		// dropdown menu options
		$("#main_menu > li").each( 
			function()
			{
				var mainwidth = $('#main_menu').width();
				var menu = $(this);
				var submenu = $(this).find("ul");
				
				var subwidth = 0;
				submenu.children().each( function()
				{ 
					subwidth += (this.tagName == "IMG") ? 15 : $(this).width();
					//$(this).css({opacity:0.85});
				});
				submenu.width(subwidth);
				//alert(submenu.height());
				while(submenu.height()>34) submenu.width(submenu.width()+1);
				
				// determine the offset
				var offset = (-subwidth/2)+(menu.width()/2);
				var overhang = ((747-mainwidth)+menu.position().left+(subwidth/2))-715;
				//alert(overhang);
				//if(((747-mainwidth)+menu.position().left+(subwidth/2)) > 730)
				if(overhang>0)
				{
					//alert("align left: "+overhang);
					var left = (mainwidth-menu.position().left)-subwidth+4;
				}
				else
				{
					//alert("align center: "+overhang);
					var left = offset;
				}
				
				submenu.css("left",left+"px");
				if(!submenu.hasClass('active')) submenu.hide();
			}
		);
		
		$("#main_menu > li").hover( 
			function()
			{
				var ul = $(this).find("ul");
				if(!ul.hasClass('active'))
				{
					$("#main_menu").find("ul.active").hide();
					$(this).find("ul").show();
				}
				$(this).find("a.main").addClass("hover");
			},
			function()
			{
				var ul = $(this).find("ul");
				if(!ul.hasClass('active')) ul.hide();
				$("#main_menu").find("ul.active").show();
				$(this).find("a.main").removeClass("hover");
			}
		);
		
		$('a.thumb').each(function(index) {
			Millwork.total_slides++;
		});
		$('a.thumb').bind('click', Millwork.this_photo);
		$('img.button.prev').bind('click', Millwork.prev_photo);
		$('img.button.next').bind('click', Millwork.next_photo);
	},
	
	change_photo : function(thumb)
	{
		var href = thumb.attr('href');
		var rel = thumb.attr('rel');
		$('img.current').removeClass('current');
		thumb.children('img').addClass('current');
		Millwork.photo_current = rel;
		$('#photo450').stop().hide().attr('src',href).load(function() {
			$(this).fadeIn();
		});
		return false;
	},
	
	this_photo : function()
	{
		var thumb = $(this);
		Millwork.change_photo(thumb);
		return false;
	},
	
	prev_photo : function()
	{
		if(window.console) window.console.log(Millwork.photo_current-1);
		Millwork.change_photo($('#thumb_'+((parseInt(Millwork.photo_current)==0) ? parseInt(Millwork.total_slides)-1 : parseInt(Millwork.photo_current)-1)));
		return false;
	},
	
	next_photo : function()
	{
		if(window.console) window.console.log(Millwork.photo_current+1);
		Millwork.change_photo($('#thumb_'+(parseInt(Millwork.photo_current)+1)%parseInt(Millwork.total_slides)));
		return false;
	},
	
	validate_form : function(form)
	{
		if(!form) form = document.forms[0];
		var submit_it = true;
		$(form).find(".REQUIRED").each(function(req)
		{
			if(this.value == '')
			{
				$(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;
	}
};

$(function() { Millwork.init(); });


