/* function appends a javascript file into the header of the page's DOM
* Instead of an arbitrary and lengthy timeout to ensure the script's functions will be 
* ready, we can use the completed upload event to trigger
*/
var filesadded = "";
function includeJs(filename,callback){
	/* test to see if the script's already been loaded and skip to the callback if true */
	if (filesadded.indexOf("["+filename+"]") == -1){
	  //get the page's head object
	  var head = document.getElementsByTagName('head')[0];
	  //create an build the new scripting object to be injected
	  var script = document.createElement('script');
	  
	  script.type = 'text/javascript';
	  script.src = filename;
	  //if we're dealing with IE
	  if (script.readyState){
		  //check the status of the script looking for a completed load
		  script.onreadystatechange= function () { 
			  //check to see if the state has changed to indicate a successful load
			  if (script.readyState == "loaded" || script.readyState == "complete"){ 
				  //some scripts don't need a callback as they're self executing so avoid a callback if it's not already defined
				  if (callback != null){
					  //the callback is the collection of operations waiting for the script to be uploaded.
					  callback();
				  }
			  }
		  }
	  } else {
		  //Mozilla based browsers use the onload event for upload completion
		  script.onload = function () { if (callback != null){ callback(); }}
	  }
	  //List of files added in the form "[filename1],[filename2],etc"
	  filesadded+="["+filename+"]"; 
	  //attach/upload the js file to the document head
	  head.appendChild(script);
	}else{
		if (callback != null){
			//the callback is the collection of operations waiting for the script to be uploaded.
			callback();
	  	}
	}
}

// JavaScript Document
includeJs('/resources/js/jquery.js',function(){	
	jQuery(document).ready(function(){
	jQuery("#people-list ul.accordian_p li a").click(function(e){
		obj = $(this);
		if ($(this).parent().find("span").css("display") == "none"){
			$(this).parent().find("span").slideDown('slow');
		}else{
			$(this).parent().find("span").slideUp('slow');
		}
		e.preventDefault();	
	});
	
	jQuery("ul.faq li a").click(function(e){
		obj = $(this);
		if ($(this).parent().find("span").css("display") == "none"){
			$(this).parent().find("span").slideDown('slow');
			$(this).find("em").html('click to close');
		}else{
			$(this).parent().find("span").slideUp('slow');
			$(this).find("em").html('click to open');
		}
		e.preventDefault();	
	});
	
	/*jQuery("#navigation > li").hover(
		function(){
			$(this).find("ul").fadeIn('slow');
		},
		function(){
			$(this).find("ul").fadeOut('slow');
		}
	);*/
	if ($('a[rel^="lightbox"]')){
		includeJs('/resources/js/jquery.lightbox.min.js',function(){	
			$('a[rel^="lightbox"]').lightBox({imageLoading: '/css/loading.gif',
			imageBtnClose: '/css/closelabel.gif',
			imageBtnPrev: '/css/controller-prev.gif',
			imageBtnNext: '/css/controller-next.gif',
			containerResizeSpeed: 350
			});
		});
	}
	
	/*if($(".validate")){
		includeJs('/resources/js/jValidate.js',function(){		
			$(".validate").blur(function() {
				$(this).validate.init(this);
			});
			$(".validate").focus(function() {
				$(this).validate.init(this);
			});
			duplicate_originals = new Array();
			$(this).find(".no_duplicate").each(function() {duplicate_originals[this.id] = this.value;});
			
			$("form").submit(function(e){
				var approve = true;
				$(this).find(".validate").each(function() {
					$(this).validate.init(this);
					//approve = false;
				});
				$(this).find(".error").each(function() {approve = false});
				if (!approve){
						e.preventDefault();
				}
			});
		});
	}*/
	$("#top_shareholders td:last-child").css("width","58px");
	$("#top_shareholders td:last-child").css("text-align","right");

	$(".msg").each(function(){
		$(this).fadeOut(12000);
	});
	$("#mailinglist #name").focus(function(){
		if ($(this).val() == 'Name'){
			$(this).val("");
		}
	});
	$("#mailinglist #name").blur(function(){
		if ($(this).val() == ''){
			$(this).val("Name");
		}
	});
	$("#mailinglist #email").focus(function(){
		if ($(this).val() == 'Email'){
			$(this).val("");
		}
	});
	$("#mailinglist #email").blur(function(){
		if ($(this).val() == ''){
			$(this).val("Email");
		}
	});
	
	$("#mailinglist #btn-submit").click(function(e){
		if ($("#mailinglist #name").val() != 'Name' && $("#mailinglist #email").val() != 'Email'){
			$("#mailinglist").submit();	
		}
		e.preventDefault();
		
	});
	

});	
});	