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');	


	// Resize BG Container
	resize_bg = function(){
		var body_height = $("body").height();
		var window_height = $(window).height();
		var new_height = body_height;
		if(window_height > body_height){
			new_height = window_height;
		}
		$(".container_bg").css("height", new_height);
	};
	
	$(window).resize(function(){
		resize_bg();
	});	
	
	resize_bg();
	
	// preload images
	var images = new Array(43);
	images[0] = new Image();
	images[0].src = "_public/img/nav/weddings_hover.png";
	images[1] = new Image();
	images[1].src = "_public/img/nav/about_hover.png";
	images[2] = new Image();
	images[2].src = "_public/img/nav/press_hover.png";
	images[3] = new Image();
	images[3].src = "_public/img/nav/events_hover.png";
	images[4] = new Image();
	images[4].src = "_public/img/nav/testimonials_hover.png";
	images[5] = new Image();
	images[5].src = "_public/img/nav/investment_hover.png";
	images[6] = new Image();
	images[6].src = "_public/img/nav/contact_hover.png";	
	
	$(".navlink").mouseenter(function(){
		var img = $(this).attr("id");
		$(this).find("img").attr("src", "_public/img/nav/" + img +"_hover.png");
	});
	
	$(".navlink").mouseleave(function(){
		var img = $(this).attr("id");
		$(this).find("img").attr("src", "_public/img/nav/" + img +".png");
	});	
	
	
});

$(window).load(function(){
	resize_bg();
});

