//actions to be setup on each page load
Event.observe(window, 'load', function() {
	
  //setup internal links
  setupInternalLinks();

  //setup navigation descriptions
  setupNavigation();

  //setup external links
  externalLinks();

  //setup quick contact links
  quick_contact_actions();

  //set top links

  //fix css issues
  css_hax();

  //contact onclicks
  contact_loaders();

}); //end window load


//use js for some css hax
function css_hax()
{
	//windows firefox hax
	if(navigator.platform == "Win32" && navigator.appName == "Netscape")
	{
		if($('contact_container'))
		{
			$('contact_container').style.height = "550px;"
		}
		
	}//end windows firefox hax
	
	
}//end function csshax


//use ScrollTo for all internal page links
function setupInternalLinks()
{
	$$('a[href^=#]:not([href=#])').each(function(element) {
	    element.observe('click', function(event) {
	      new Effect.ScrollTo(this.hash.substr(1));
	      Event.stop(event);
	    }.bindAsEventListener(element))
	  });
	
}//end function setupInternalLinks


function contact_loaders()
{
	try
	{
		$('quick_contact_loader').style.display = "none";
		$('contact_loader').style.display = "none";
	
	
		$('quick_submit').observe('click', function(event){
			$('quick_contact_loader').style.display = "";
		}.bindAsEventListener($('quick_submit')));
	
	
		$('contact_submit').observe('click', function(event){
			$('contact_loader').style.display = "";
		}.bindAsEventListener($('contact_submit')));
	
	}catch(e){ /*just supress this*/ }
	
}//end function contact_loaders


// function to transition from a condensed view to detailed
// or vice versa using a slide animation
function ssTransition(id, from, to)
{
	Effect.SlideUp(from + "_" + id, {
		duration: 0.75,
		afterFinish: function(){
		
			Effect.SlideDown(to + "_" + id, {duration: 0.75});
			
		}
	});
	
	
}//end function ssTransition


// function to swap out thumbnail images for main
// images in the detailed view for the work page
function swapSS(id, lgImg)
{
	$("lg_ss_img_" + id).src = lgImg;
	
}//end function swapSS



// function to scroll to an id
function st(anchor)
{
	new Effect.ScrollTo($(anchor));
}// end function st


//function to fire after the quick-contact form is submitted
function quick_contact_wrapup()
{
	$('thanks_msg').hide();

    Effect.Fade('quick_wrapper',{
        afterFinish: function(){
            Effect.Appear('thanks_msg');
        }
    });
    
}//end function quick_contact_wrapup


//function to format number w/ commas and decimals
function formatNumber (number) 
{
  number = '' + number;
  numberParts = new Array();
  numberParts = number.split(".");

  number = numberParts[0];

  if (number.length > 3) {
    var mod = number.length%3;
    var output = (mod > 0 ? (number.substring(0,mod)) : '');
    for (i=0 ; i < Math.floor(number.length/3) ; i++) {
      if ((mod ==0) && (i ==0)) {
        output+= number.substring(mod+3*i,mod+3*i+3);
      } else {
        output+= ',' + number.substring(mod+3*i,mod+3*i+3);
      }
    }
		
    if (numberParts.length == 2) {
      output = output + "." + numberParts[1];
    } 			
    return (output);
  } else {
    if (numberParts.length == 2) {
      number = number + "." + numberParts[1];
    }	
    return number;
  }

}//end function formatNumber


//function to setup nav events
function setupNavigation()
{
	//code to setup rollover descriptions on main menu
	//
	//
	// var d 			= $('nav_description');
	// var home_msg 	= "new and featured projects along with the latest blog topics";
	// var about_msg 	= "find out who brian webb is and what he can do for you";
	// var work_msg	= "take a look at projects brian has completed to date";
	// var service_msg = "see the services brian offers, and which could work for you";
	// var blog_msg 	= "delve into the mind of brian webb";
	// var contact_msg = "contact brian about a future project"
	// 
	// Event.observe($('nav_home'), 'mouseover', function(){ applyNavMessage(d, home_msg);});
	// Event.observe($('nav_home'), 'mouseout', function(){ killNavMessage(d); });
	// 
	// Event.observe($('nav_about'), 'mouseover', function(){ applyNavMessage(d, about_msg);});
	// Event.observe($('nav_about'), 'mouseout', function(){ killNavMessage(d); });
	// 
	// Event.observe($('nav_work'), 'mouseover', function(){ applyNavMessage(d, work_msg);});
	// Event.observe($('nav_work'), 'mouseout', function(){ killNavMessage(d); });
	// 
	// Event.observe($('nav_service'), 'mouseover', function(){ applyNavMessage(d, service_msg);});
	// Event.observe($('nav_service'), 'mouseout', function(){ killNavMessage(d); });
	// 
	// Event.observe($('nav_blog'), 'mouseover', function(){ applyNavMessage(d, blog_msg);});
	// Event.observe($('nav_blog'), 'mouseout', function(){ killNavMessage(d); });
	// 
	// Event.observe($('nav_contact'), 'mouseover', function(){ applyNavMessage(d, contact_msg);});
	// Event.observe($('nav_contact'), 'mouseout', function(){ killNavMessage(d); });

}//end function setupNavigation


function quick_contact_actions()
{
	//
	Event.observe($('quick_contact_name'), 'focus', function(){
		if($('quick_contact_name').value == "Name"){$('quick_contact_name').value=""};
	});
	Event.observe($('quick_contact_name'), 'blur', function(){ 
		if($('quick_contact_name').value == ""){$('quick_contact_name').value = "Name"};
	});
	
	//
	Event.observe($('quick_contact_email'), 'focus', function(){
		if($('quick_contact_email').value == "Email"){$('quick_contact_email').value=""};
	});
	Event.observe($('quick_contact_email'), 'blur', function(){ 
		if($('quick_contact_email').value == ""){$('quick_contact_email').value = "Email"};
	});
	
	//
	Event.observe($('quick_contact_subject'), 'focus', function(){
		if($('quick_contact_subject').value == "Subject"){$('quick_contact_subject').value=""};
	});
	Event.observe($('quick_contact_subject'), 'blur', function(){ 
		if($('quick_contact_subject').value == ""){$('quick_contact_subject').value = "Subject"};
	});
	
	//
	Event.observe($('quick_contact_message'), 'focus', function(){
		if($('quick_contact_message').value == "Message"){$('quick_contact_message').value=""};
	});
	Event.observe($('quick_contact_message'), 'blur', function(){ 
		if($('quick_contact_message').value == ""){$('quick_contact_message').value = "Message"};
	});
	
}//end function quick_contact_actions


//function to get rel="external" links
//and make them target="_blank"
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }

}//end function


function setTopLinks()
{	

	$$('a[href=#top]').each(function(element) {
	    element.observe('click', function(event) {
	      st('top');
	      Event.stop(event);
	    }.bindAsEventListener(element))
	  });
	
}//end function setTopLinks


//var to watch for a running nav animation
var mouseoverRunning = false;


//function to display the nav desc
function applyNavMessage(object, message)
{
	object.hide();
	object.innerHTML = message;
	mouseoverRunning = true;
	Effect.Appear(object, {duration: 0.5, afterFinish: function(){mouseoverRunning = false;}});
	
}//end function applyNavMessage


//function to remove the nav desc
function killNavMessage(object)
{
	window.setTimeout(function(){
		if(mouseoverRunning == false)
		{
			Effect.Fade(object, {duration: 0.5});
		}
	}, 500);
	
}//end function killNavMessage

function mark_for_destroy(element, css_class)
{
	$(element).next('.should_destroy').value = 1;
	$(element).up(css_class).hide();
	
}//end function mark_for_destroy