// JavaScript Document

var crumbs;

var current_dropdown;
var dropdown_int;
var dropdown_duration = 40;
var dropdown_finish = 0;
var dropdown_start = -36;
var initial_dropdown;
var menu_array = new Array();

var fade_duration = 1;
var photo_duration = 5000;
var homelink;
var swap_int;
var photo_num = 0;
var photos = [];
var z_photo = 500;

function init_deleers()
{
	if($('homephotos')) photos = $('homephotos').value.split('|');
	if(document.getElementById('homephotos'))
	{
		var billboard = document.getElementById('billboard');
		var swap_int = setInterval(swapPhoto,photo_duration);
		
		for(i=0;i<photos.length;i++)
		{
			//alert(photos[i]);
			billboard.appendChild(Builder.node('img',{id:photos[i], src:'/images/billboard/'+photos[i]}));
			$(photos[i]).hide();
		}
	}
	
	$$('a[href=#]').each(function(a){ a.onclick=function(){return false;}});
	
	
	// init functions here
	var a = document.getElementsByTagName('A');
	var aNum = a.length;
	//crumbs = document.getElementById("breadcrumbs");
	for( var i=0;i<aNum;i++ )
	{
		if( a[i].rel && a[i].rel.indexOf('lightbox')==-1 )
		{
			menu_array.push(a[i]);
			a[i].submenu = document.getElementById('sub_'+a[i].rel);
			if( a[i].submenu )
			{
				a[i].submenu.atag = a[i];
				a[i].oldclass = a[i].className;
				a[i].submenu.oldclass = a[i].submenu.className;
				if(a[i].submenu.className.indexOf('over')==-1) 
				{
					a[i].submenu.style.top = dropdown_start+'px';
				}
				else
				{
					current_dropdown = a[i].submenu;
					initial_dropdown = a[i].submenu;
				}
			}
			
			a[i].onmouseover = function()
			{
				for( var i=0;i<menu_array.length;i++ )
				{
					menu_array[i].submenu.style.top = dropdown_start+'px';
					menu_array[i].className = menu_array[i].oldclass;
				}
				
				if(BrowserDetect.browser=='Explorer' && BrowserDetect.version <= 6)
				{}
				else
				{
					this.className = this.oldclass + ' over';
				}
				
				if(this.submenu)
				{
					if( current_dropdown )
					{
						clearInterval(dropdown_int);
						current_dropdown.style.top = dropdown_start+'px';
					}
					if(current_dropdown != this.submenu)
					{
						current_dropdown = this.submenu;
						menuOver();
					}
					else
					{
						current_dropdown.style.top = dropdown_finish+'px';
					}
				}
			}
			a[i].onmouseout = function()
			{
				if(current_dropdown != initial_dropdown)
				{
					this.className = this.oldclass;
					if(this.submenu)
					{
						menuOut();
					}
				}
			}
			
			if( a[i].submenu )
			{
				a[i].submenu.onmouseover = function()
				{
					if(BrowserDetect.browser=='Explorer' && BrowserDetect.version <= 6)
					{}
					else
					{
						this.atag.className = this.atag.oldclass + ' over';
					}
					clearInterval(dropdown_int);
					if( crumbs ) crumbs.style.zIndex = 120;
					this.style.top = dropdown_finish+'px';
				}
				a[i].submenu.onmouseout = function()
				{
					if(current_dropdown != initial_dropdown || !current_dropdown)
					{
						this.atag.className = this.atag.oldclass;
						clearInterval(dropdown_int);
						if( crumbs ) crumbs.style.zIndex = 210;
						this.style.top = dropdown_start+'px';
						current_dropdown = initial_dropdown;
						menuOver();
					}
				}
			}
		}
	}
	
	// init gallery
	var thumbs = new Array();
	var num = 0;
	var current_num = 0;
	var current_thumb;
	
	if($('gallery_image'))
	{
		for( i=0; i<$('gallery_thumbs').childNodes.length; i++ )
		{
			if($('gallery_thumbs').childNodes[i].tagName == "IMG")
			{
				var thumb = $('gallery_thumbs').childNodes[i];
				thumbs.push(thumb);
				thumb.num = num++;
				if(!current_thumb) 
				{
					current_thumb = thumb;
					thumb.className = 'current';
				}
				
				// set click action
				thumb.onclick = function()
				{
					current_num = this.num;
					changeImage(this);
				}
				
				// preload other images
				var preload = new Image();
            	preload.src = thumb.src.replace('thumb','med');
			}
			
			var prev = $('gallery_prev');
			var next = $('gallery_next');
			
			prev.onclick = function()
			{
				current_num = (current_num==0) ? thumbs.length-1 : current_num-1;
				changeImage(thumbs[current_num]);
				return false;
			}
			
			next.onclick = function()
			{
				current_num = (current_num==thumbs.length-1) ? 0 : current_num+1;
				changeImage(thumbs[current_num]);
				return false;
			}
			
			function changeImage(thumb)
			{
				current_thumb.className = '';
				current_thumb = thumb;
				thumb.className = 'current';
				
				$('gallery_image').parentNode.href = thumb.src.replace('thumb','large');
				Lightbox.prototype.updateImageList();
				
				$('gallery_image').fade( 
				{
					duration: 0.3, 
					from: 1.0, 
					to: 0.01,
					queue: 'end',
					afterFinish: (function()
										{
											$('gallery_image').src = thumb.src.replace('thumb','med');
											$('gallery_image').appear({duration: 0.3});
										})
				});
			}
		}
	}
}

Event.observe( window, 'load', init_deleers, false);

function swapPhoto()
{
	//alert(photo_num);
	var num = ++photo_num%photos.length;
	$(photos[num]).setStyle({ zIndex: z_photo++ });
	new Effect.Appear(photos[num], { duration: fade_duration });
	var nextnum = (num+1)%photos.length;
	$(photos[nextnum]).hide();
}

function menuOver()
{
	current_dropdown.className = current_dropdown.oldclass+' over';
	clearInterval(dropdown_int);
	if( crumbs ) crumbs.style.zIndex = 120;
	dropdown_int = setInterval(showSubmenu,dropdown_duration);
}

function menuOut(sm)
{
	clearInterval(dropdown_int);
	dropdown_int = setTimeout(hideSubmenu,125);
}

function showSubmenu()
{
	var pos = parseInt(current_dropdown.style.top.replace('px',''));
	if(pos < dropdown_finish)
	{
		if(current_dropdown) current_dropdown.style.top = Math.floor(pos-dropdown_finish)/2+'px';
	}
	else
	{
		if(current_dropdown) current_dropdown.style.top = dropdown_finish+'px';
		clearInterval(dropdown_int);
	}
}

function hideSubmenu()
{
	//alert(current_dropdown.atag.innerHTML);
	clearInterval(dropdown_int);
	if( crumbs ) crumbs.style.zIndex = 210;
	current_dropdown.style.top = dropdown_start+'px';
	current_dropdown = initial_dropdown;
	menuOver();
}

/* form validation */

function css_validation()
{
	form = document.forms[0];
	var submit_it = true;
	for( i=0;i<form.length;i++ )
	{
		if( form[i].className && form[i].className.indexOf('REQUIRED') > -1 )
		{
			form[i].oldclass = form[i].parentNode.parentNode.className;
			if( form[i].value == '' )
			{
				form[i].parentNode.parentNode.className = form[i].oldclass+" error";
				submit_it = false;
			}
			else
			{
				form[i].parentNode.parentNode.className = form[i].oldclass;
			}
		}
	}
	if( !submit_it ) document.getElementById('errormsg').innerHTML = '<strong>Please review the highlighted fields and resubmit.</strong>';
	return submit_it;
}

