/* 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 li a").click(function(e){
		obj = jQuery(this);
		obj.parent().parent().parent().find("img").each(function(){
			var img_path = jQuery(this).attr("src").split("/");
			img_path[img_path.length-1] = (img_path[img_path.length-1].match(/thumb_/))?img_path[img_path.length-1]:"thumb_"+img_path[img_path.length-1];
			img_path = img_path.join("/")
			jQuery(this).attr("src",img_path);
		});
		obj.parent().find("img").attr("src",obj.parent().find("img").attr("src").replace("thumb_",""));
		jQuery("#info-text").fadeOut("fast",function(){
			$(this).html(obj.parent().find(".info").html()).fadeIn("fast");
		});	
		e.preventDefault();	
	});
	
	if ($("#navigation ul li.selected ul").css("display") == "block"){
		$("#main").css("padding-top","30px");
		$("#main h2").css("border-top","1px solid #8d9194");
		$("#main h2").css("padding-top","14px");
		$("#main h2").css("padding-bottom","10px");
	}	
	
	//if ($('.ticker')){
	

	//}
	
	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');
		}
	);*/
	includeJs('http://iguana2.com/ijax/ijax_v1.js',function(){
		$(function() {

  //cache the ticker
  var ticker = $(".ticker");

  //hide the scrollbar
  ticker.css("overflow", "hidden");

  //animator function
  function animator(currentItem) {

    //work out new anim duration
    var distance = currentItem.width(),
	duration = (distance - Math.abs(parseInt(currentItem.css("marginTop")))) / 0.025;

    //animate the first child of the ticker
    currentItem.animate({ marginLeft: -distance }, duration, "linear", function() {

      //move current item to the bottom		
      currentItem.appendTo(currentItem.parent()).css("marginLeft", 0);

	//recurse
	animator(currentItem.parent().children(":first"));
    });
  };
		function show_quote(stock, quote, news){
  			Ijax.set_elem(stock.code+'_code', stock.code);
  			Ijax.set_elem(stock.code+'_name', stock.name);
  			Ijax.set_price_elem(stock.code+'_last', quote.mkt_price);
  			Ijax.set_change_elem(stock.code+'_change', quote.change);
  			Ijax.set_volume_elem(stock.code+'_volume', quote.volume);

  			Ijax.set_elem(stock.code+'_code2', stock.code);
  			Ijax.set_elem(stock.code+'_name2', stock.name);
  			Ijax.set_price_elem(stock.code+'_last2', quote.mkt_price);
  			Ijax.set_change_elem(stock.code+'_change2', quote.change);
  			Ijax.set_volume_elem(stock.code+'_volume2', quote.volume);
		}

		function show_index_quote(index, quote){
			Ijax.set_elem(index.code+'_code', index.code);
  			Ijax.set_elem(index.code+'_name', index.name);
  			Ijax.set_index_price_elem(index.code+'_last', quote.last);
  			Ijax.set_index_change_elem(index.code+'_change', quote.change);

			Ijax.set_elem(index.code+'_code2', index.code);
  			Ijax.set_elem(index.code+'_name2', index.name);
  			Ijax.set_index_price_elem(index.code+'_last2', quote.last);
  			Ijax.set_index_change_elem(index.code+'_change2', quote.change);
  			if(index.code == 'XMM'){
  				animator(ticker.children(":first"));
  			}
		}

		function load_ir(){
  			Ijax.initialize('cortona');
  			Ijax.set_fmt_style('no_change_html', 'n/c')
  			Ijax.set_fmt_style('no_volume_html', '-')

 			Ijax.quote('CRC', show_quote);
  			//Ijax.quote('CRCO', show_quote);
  			Ijax.index_quote('XJO', show_index_quote);
  			Ijax.index_quote('XMM', show_index_quote);
  			//animator(ticker.children(":first"));
		}


	load_ir();
  //start the ticker
  
});
	});
	
	if ($('#price_figure').length){
		includeJs('http://iguana2.com/ijax/ijax_v1.js',function(){	
			function show_quote(stock, quote){
  				jQuery("#price_figure").html(quote.mkt_price);
				//Ijax.set_price_elem('price_figure', quote.mkt_price);
  				
  			}
  			
			Ijax.initialize('cortona');
			Ijax.quote('CRC', show_quote);
		});
	}
	
	if ($('a[href$=jpg],a[href$=jpeg],a[href$=gif],a[href$=png]').length){
		includeJs('/resources/js/jquery.prettyPhoto.js',function(){	
			$('a[href$=jpg],a[href$=jpeg],a[href$=gif],a[href$=png]').prettyPhoto({theme: 'light_rounded',show_title: false});
		});
	}
	
	/*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();
		
	});
	

});	
});	
