var screenshotPreview = function(){
	/* CONFIG */
	
	xOffset = 225;
	yOffset = 10;
	// these 2 variable determine popups distance from the cursor
	// you might want to adjust to get the right result
	
	/* END CONFIG */
	$(".screenshot").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		$("body").append("<p id='screenshot'><img src='"+ this.rel +"' alt='url preview' />"+ c +"</p>");								 
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
		},
	function(){
		this.title = this.t;	
		$("#screenshot").remove();
	});	
	$(".screenshot").unbind('mousemove').bind('mousemove',function(e){
		$("#screenshot")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};
	
$(document).ready(function(){
	$('a.img_lightbox').lightBox(); //Execute jQuery's lightbox on all relevant links
		
	$("#biaf_category_box ul li a").click(function(event){
		if ($(this).parent("li").hasClass("submenu"))
		{
			var box_child = $(this).next("ul");
			if (box_child.is(":hidden")) {
				box_child.slideDown(300);
			}
			else {
				box_child.slideUp(300);
			}
				
			event.preventDefault();
		}
	});
	
	/*************
		For the cross reference drop-downs on the category view pages
	**************/
	$(".cr_layer_manufacturer").live('change',function(){
		var handle = $(this); //a reference variable for this particular drop down
			
		var MFG_VALUE = $(this).val();
		var CAT_VALUE = handle.closest(".cr_dropdown_section").find(".cr_layer_categories").attr("value");
		
		if (MFG_VALUE == '') { //If they didn't chose a valid manufactuer, we'll turn off the model selection
			handle.closest(".cr_dropdown_section").find(".cr_layer_model").html("<option value=''>Select a Model</option>").attr('disabled', 'disabled');
			return;
		}
		handle.closest(".cr_dropdown_section").find(".cr_ajax_loader").css("display", "inline"); //show the ajax-loader
		
		$.post("cr_ajax.php?"+Math.random(), {type_AJ : "cr_model", category_AJ: CAT_VALUE, mfg_AJ: MFG_VALUE}, function(data){
			handle.closest(".cr_dropdown_section").find(".cr_ajax_loader").css("display", "none"); //hide the ajax-loader
			handle.closest(".cr_dropdown_section").find(".cr_layer_model").removeAttr('disabled').html(data); //enable the box and append the data we've recieved
		},"html");
	});
	
	$(".cr_layer_model").live('change',function(event){
		var go = $(this).val();
		if (go!="") window.location = go; //go to the location specified by the chosen option
	});
	
	/*************
		Heatmap javascript code
	***************/
	var offset = $("#mainWrapper").offset();
	var offset_x = offset.left;
	var width_x = $("#mainWrapper").outerWidth();
	
	$(document).bind('click.heatmap', function(e){
		//We're going to send the coordinates of the click, as well as the current page
		var _x = e.pageX,
			_y = e.pageY,
			_page = document.location.toString();
		
		//We only want to register clicks within the content area
		if (_x < offset_x) return;
		_x -= offset_x;
		if (_x > width_x) return;
		 
		$.get("click.php",
			{x : _x,
			 y : _y,
			 page : _page}
		);
	});
		
	var autocomplete_cache = {},
		lastXHR;

	$("#top_search_input").autocomplete({
		minLength : 2,
		source: function(request, response) {
			var term = request.term;
			if (term in autocomplete_cache) {
				response(autocomplete_cache[term]);
				return;
			}
			lastXHR = $.get("search_ajax.php?action=search_autocomplete", request, function(data, status, XHR) {
				autocomplete_cache[term] = data;
				if (XHR === lastXHR) {
					response(data);
				}
			}, "json");
		}
	});

	var bf_search_type = 'finder_xr';
	
	var search_type_switch = function(type)
	{
		if (bf_search_type == type)
			return;
			
		switch (type) {
			case "finder_xr":
				$("#search_form").appendTo($("#mainSearch"));
				$("#subSearch").slideUp(500);
				/* $("#bf_hr").css("display", "none"); */
				$("#bf_hr").hide(); 
			break;
			
			case "finder_v":
				$("#search_form").appendTo($("#subSearch"));
				$("#subSearch").slideDown(500);
				/* $("#bf_hr").css("display", ""); */
				$("#bf_hr").show(); 
			break;
		}
		
		bf_search_type = type;
	}
	
	//"Battery finder" javascript
	$(".bf_search_dropdown").live('change', function(event){
		var id_val = $(this).val();
		var action_val = $(this).attr("name");
		var link_val = $(this).attr("rel");
		
		if (!$(this).is(".static")) {
			$(this).nextAll(".bf_search_dropdown").attr("disabled", "disabled").children(":not(.static)").remove();
		}
		else {
			$(this).nextAll("div").children(".bf_search_dropdown").attr("disabled", "disabled").children(":not(.static)").remove();
		}
		
		if (id_val == '')
			return;
			
		postdata = Array();
		postdata.push({"name" : "val_id", "value" : id_val});
		
		if (link_val) {
			var linked = $("select[name="+link_val+"]");
			postdata.push({"name" : "second_id", "value" : linked.val()});
		}

		postdata.push({"name" : "action", "value" : action_val});
		
		$("#finder_load").stop().fadeIn(200);
		
		$.post("search_ajax.php", postdata, function(data){ //change filename as appropriate
			var change_box = $("select[name="+data.affect_box+"]");
			
			$("#finder_load").stop().fadeOut(200);
			
			if (data.html) {
				change_box.removeAttr("disabled");
				change_box.children(":not(.static)").remove();
				change_box.children(".static").after(data.html);
			}
			if (data.url) {
				window.location = data.url;
			}

			if (data.finder) {
				$(".finder").hide();
				$("#" + data.finder).css('display', 'inline');
				
				search_type_switch(data.finder);
			}
		},"json");
	});
});
