var base_url = null;
var imported_scripts = new Array();

$(document).ready(function() {
	
	base_url = $("base").attr("href");

/**
 * Check if a value exists in an array
 *
 * @param str val
 * @param array array
 * @return true on success, false on failure
 */ 
	in_array = function(val, array){
		for(x in array){
			if(val == array[x]){ 
				return true;
			}
		}
		return false;
	};	

/**
 * Display a message to the user
 *
 * @param str message
 * @return void
 */ 	
	flash_message = function(message, position){
		if($("#flash").attr("id")){
			$("#flash").html(message);
		}else{
			$("body").append("<div id=\"flash\">" + message + "</div>");
		}
		$("#flash").slideDown('fast').delay(5000).slideUp('fast');
	};

/**
 * Import a spine controller and init
 *
 * Pass a spine folder and the script will init the 
 * base controller (which should be the name of the folder) and
 * use the element passed to build the spine controller on.
 *
 * @param str js
 * @param element el
 * @return void
 */ 
	import_spine = function(js, el){
		script = base_url + "_public/spine/" + js + "/" + js + ".js";
		if(!in_array(script, imported_scripts)){
			$LAB .script(base_url + "_public/spine/" + js + "/" + js + ".js").wait(function(){
				eval(js).init({"el": el});
				imported_scripts.push(script);
			});
		}else{
			alert("Script has already been loaded");
			eval(js).init({"el": el});
		}
	}
	
/**
 * Import any JavaScript file
 *
 * @param str js
 * @param element el
 * @return void
 */ 
	import_script = function(js){
		script = base_url + "_public/spine/" + js + "/" + js + ".js";
		if(!in_array(script, imported_scripts)){
			$LAB .script(base_url+js).wait(function(){
				imported_scripts.push(script);
			});
		}
	}
	
/**
 * Obtain a template and pass to an element
 *
 * << Ex JSON >>
 * URL: "ajax/locations_near_zip",
 * Template: "_public/spine/search/templates/results.htm",  
 * Element: $("#zips") 
 *
 * @param json request
 * @return void
 */ 	
	template_it = function(request){
		$(request[0]['Element']).prepend("<img id='loader' style='position:absolute;top:50%;left:50%' src='_public/img/ajax-loader.gif'>").fadeIn();
		$.get(request[0]['URL'], function(data){
			data = jQuery.parseJSON(data);
			$.get(request[0]['Template'], function(tmpl){
				request[0]['Element'].html($.tmpl(tmpl,data));
				request[0]['Element'].children().closest("#loader").fadeOut();
			});
		});
	}		
	
	$(window).scroll(function(){
		$('#flash').animate({top:$(window).scrollTop()+"px" },{queue: false, duration: 350}); 
	});
	$('#flash').delay(1000).slideDown('fast').delay(5000).slideUp('fast');	
	
	
	// Header Routines
	$(".tab").click(function(){
		self.location = $(this).find("a").attr("href");
	});	
	$(".tab").mouseenter(function(){
		$(this).find(".tab_overlay").stop().fadeTo(250, .8);
		$(this).find(".text").stop().animate({
			color: "#000"
		}, 250);
	});
	$(".tab").mouseleave(function(){
		$(this).find(".tab_overlay").stop().fadeTo(250, .3);
		$(this).find(".text").stop().animate({
			color: "#fff"
		}, 250);	
	});		

});

