$(document).ready(function(){

	

	/* ************* INITIALIZE ***************/
	var segment1 = ""; 
	var segment2 = "";
	var segment3 = "";
	var segment4 = ""; 
	var segment5 = "";
	var tooltip=new Boolean(false);

	if ($.url.segment(2))
	{
		var selected_menu_item = $.url.segment(2).toLowerCase();
	}
	if (!$.url.segment(2) || selected_menu_item == "catalog")
	{
		var selected_menu_item = "";
		button_action($("#catalog_button"),'catalog'); 
	}
	else
	{
		if (selected_menu_item == "revit")
		{
			button_action($("#revit_button"),"revit");
			$('#please-pick').hide();
			
		}
		else if (selected_menu_item == "photometrics")
		{
			button_action($("#photometrics_button"),"photometrics");
			$('#please-pick').hide();
			
		}
		else if (selected_menu_item == "installation")
		{
			button_action($("#installation_button"),"installation");
			$('#please-pick').hide();
			
		}
		else if (selected_menu_item == "lamping")
		{
			window.open($("#lamp_button").attr("href"));
			$('#please-pick').hide();
		}
		else if (selected_menu_item == "diffusers")
		{
			button_action($("#diffusers"),"diffusers");
			$('#please-pick').hide();
			
		}
		else if (selected_menu_item == "finishes")
		{
			button_action($("#finishes"),"diffusers");
			$('#please-pick').hide();
			
		}
		else
		{
			button_action($("#catalog_button"),'catalog');
		}
		
	}
	
	/* ************* MAIN NAV BUTTONS ***************/
	// For nav items that do a direct download with no further actions.
	
	$('a.one_file').click(function(){
        window.open(this.href);
		$('#please-pick').slideUp();
        return false;
    });
	$("#catalog_button").click(function(){
		button_action($(this),"catalog");
		$('#please-pick').slideUp();
		return false; 
	});
	$("#revit_button").click(function(){
		button_action($(this),"revit");
		$('#please-pick').slideUp();
		return false; 
	});		
	$("#photometrics_button").click(function(){
		button_action($(this),"photometrics");
		$('#please-pick').slideUp();
		return false;
	});
	$("#dwg_button").click(function(){
		button_action($(this),"dwg");
		$('#please-pick').slideUp();
		return false;
	});		
	$("#installation_button").click(function(){
		button_action($(this),"installation");
		$('#please-pick').slideUp();
		return false;

	});
	$("#diffusers").click(function(){
		button_action($(this),"diffusers");
		$('#please-pick').slideUp();
		return false; 
	});
	$("#finishes").click(function(){
		button_action($(this),"finishes");
		$('#please-pick').slideUp();
		return false; 
	});
	/* 
	* Removes content, does animation, sets the selected menu item, adds new content
	* 
	* @param object button_object. The <a/> button that was clicked. Requires A button have href to activate
	* @param string selected_item. An arbitrary name for the button. This is used in later functions for evaluating actions
	* @return void
	*/
	function button_action(button_object,selected_item)
	{
		
		$('#catalog_button, #revit_button, #dwg_button, #photometrics_button, #installation_button, #diffusers, #finishes').removeClass('activeSlide');
		$(button_object).addClass('activeSlide');
		$("#resource_content").fadeOut("fast",function(){
			if(selected_item == 'finishes' || selected_item == 'diffusers'){
				tooltip = true;
			}
			$("#resource_content").html("");
			get_content(button_object.attr("href"),'#resource_content',"#cats","append"); 
			$("#resource_content").fadeIn();
			selected_menu_item = selected_item;
		 
			return true;
		});
	}

	/*
	 * Activate divs
	 * 
	 * This function can refreshe the buttons on the page
	 * we need to call this after ajax, because many of these buttons
	 * will have been replaced with new items.
	 * 
	 * @return void
	*/
	function activate_divs(){
		$("img").fullsize({iconOffset:2});  
		
		// unbinding past clicks, otherwise we'll have exponentially more action with every click. 
		$(".category_link").unbind('click');
		$(".products_link").unbind('click');
		$(".series_link").unbind('click');
		$("ul.lamping li").unbind('click');
		$(".oah_item").unbind('click');

		/* CATEGORIES */
		$(".category_link").click(function(){
			$("#oah_div").fadeOut("fast").remove();
			$("#lamping_div").fadeOut("fast").remove();
			$("#series_div").fadeOut("fast").remove();
			
			var div = $(this).parents("#cats");
			get_content($(this).attr('href'),div,"#subcats","after");
			
			$(this).parent().siblings().children().removeClass('active');
			$(this).addClass('active');
			return false; 
		});
		/* PRODUCT TYPES */
		$(".products_link").click(function(){
			$("#oah_div").fadeOut("fast").remove();
			$("#lamping_div").fadeOut("fast").remove();
			$("#series_div").fadeOut("fast").remove();
			
			var div = $(this).parents("#subcats");
			segment1 = $(this).attr('id').toLowerCase(); 
			entry_id = $(this).attr('rel');
			switch (selected_menu_item)
			{
				case "catalog":
				window.open("/images/uploads/catalog_pages/OCL_"+segment1+".pdf");
				break;
				case "dwg":				
				window.open("/images/uploads/dwg/OCL_"+segment1+".dwg");
				break;
				case "installation":
					get_content("/resources/installation-ajax/"+entry_id,div,"#series_div","after");
				break;
				default: 
				get_content($(this).attr('href'),div,"#series_div","after");
			
			}
			
			$(this).parent().siblings().children().removeClass('active');
			$(this).addClass('active');
			
			return false; 
		});
		/* SERIES */
		$(".series_link").click(function(){
			$("#oah_div").fadeOut("fast").remove();
			$("#lamping_div").fadeOut("fast").remove();
			
			segment2 = $(this).attr('rel'); 

			switch (selected_menu_item)
			{
				case "installation":
					//get_content("/resources/installation-ajax"+entry_id,div,"#lamping_div","after");
					
					window.open("/images/uploads/installation/"+segment2.toLowerCase()+".pdf");
					break;
				case "photometrics":
					var div = $(this).parents("#series_div");
					//get_content($(this).attr('href'),div,"#lamping_div","after");
					get_content("/resources/photometrics-ajax/"+entry_id,div,"#lamping_div","after");
					break;
				default: 
				var div = $(this).parents("#series_div");
				get_content($(this).attr('href'),div,"#lamping_div","after");
			}
			
			$(this).parent().siblings().children().removeClass('active');
			$(this).addClass('active');
			
			return false; 
		});
		
		// need to add this, because the code that comes in has no A links
		// due repurposing existing PHP output to suit this purpose
		$("ul.lamping li").css("cursor","pointer"); 
		
		/* LAMPING */
		$("ul.lamping li").click(function(){
			$("#oah_div").fadeOut("fast").remove();
			
			var size = $(this).parent().attr("id"); 
			var trimmed_size = size.replace(/sz_/, '');
			var div = $(this).parents("#lamping_div");
			segment3 = $(this).children(".code").attr("rel");
			switch (selected_menu_item)
			{
				case "photometrics":
					log("segment 3" + segment3);
					if (segment3 !="")
					{
						window.open("/images/uploads/photometrics/OCL-"+segment3.toLowerCase()+".IES");
					}
					break; 
				default: 
				get_content("/resources/voltage-ajax/"+segment1+"/"+segment2+"/"+trimmed_size,div,"#oah_div","after");
			}

			log("/resources/voltage-ajax/"+segment1+"/"+segment2+"/"+trimmed_size);
			
			$(this).parent().siblings().children().removeClass('active');
			$(this).siblings().removeClass('active');
			$(this).addClass('active');
	
			return false; 
		});
		
		/* OAH */
		$(".oah_item").click(function(){
			segment4 = $(this).attr("rel");
			switch (selected_menu_item)
			{
				case "revit":
					window.open("/images/uploads/revit/"+segment2.toLowerCase()+"-"+segment3.toLowerCase()+"-"+segment4.toLowerCase()+".pdf");
				break; 
				default: 
			}

			$(this).parent().siblings().children().removeClass('active');
			$(this).addClass('active');
			
			return false; 
		});
	}
	
	/* 
	* Get_content
	* AJAX, loads data from provided href
	* 
	* @param string href. The location to get data from
	* @param object div. Current div object to append data to
	* @param string next. Name of the div that might be next after this one that will be replaced
	* @param string in_or_out. [append,after] Pass in whether the item should be appended to, or placed after current div
	* @return void
	*/
	function get_content(href,div,next,in_or_out){
		var append_or_after = in_or_out || "append";
		
		var content;
		$.get(href, function(data){
		    content= data;
			append_to_div(content,div,next,in_or_out);
		});
		

	}
	/* 
	* append_to_div
	* Appends or Adds data to current div
	* 
	* @param string data. HTML to add
	* @param object div. Current div object to append data to
	* @param string next. Name of the div that might be next after this one that will be replaced
	* @param string in_or_out. [append,after] Pass in whether the item should be appended to, or placed after current div
	* @return void
	*/
	function append_to_div(data,div,next,in_or_out) {
		
		if ($(next).length > 0)
		{
			$(next).replaceWith(data);
			$("#" + $(data).attr("id")).hide();
			$("#" + $(data).attr("id")).fadeIn("normal", function callback(){
				if(tooltip == true){
					$('.diffuser-finish').tooltip({
						track: true,
						delay: 0,
						showURL: false,
						showBody: " - ",
						fade: 250
					});
				}
			}); 
			
		}
		else
		{
			if (in_or_out == "append")
			{
				$(div).append(data);
				$("#" + $(data).attr("id")).hide();
				$("#" + $(data).attr("id")).fadeIn("normal", function callback(){
					if(tooltip == true){
						$('.diffuser-finish').tooltip({
							track: true,
							delay: 0,
							showURL: false,
							showBody: " - ",
							fade: 250
						});
					}
				}); 

			}
			else
			{
				$(div).after(data);
				$("#" + $(data).attr("id")).hide();
				$("#" + $(data).attr("id")).fadeIn("normal", function callback(){
					if(tooltip == true){
						$('.diffuser-finish').tooltip({
							track: true,
							delay: 0,
							showURL: false,
							showBody: " - ",
							fade: 250
						});
					}
				}); 

				
			}
		}
		activate_divs();
		
	}
	/*
	* Log
	*
	* logs messages to error console
	* @param string msg. The messge you wanna pass out
	* @return void
	*/
	function log(msg){
		if(window.console) {
			window.console.log(msg);
			return this;
		} 
		else
		{
			return this;
		}
	}
	
});