$(document).ready(function(){

	/* SCROLLABLE */
	var number = 0;
	function change_number(direction,amount,height) {
		//check if we should increment or decrement
		if (direction == "down") {
			if (number >= height) {
				// don't allow number to go beyond current height of .scrollable_inside
			} else {
				number = parseInt(number) + parseInt(amount);
			}
		} else if (direction == "up") {
			if (number <= 0) {
				// don't allow number to go below 0
			} else {
				number = parseInt(number) - parseInt(amount);
			}
		}
	}

	$(".scroll_down").click(function(){
		amount = $(this).attr("rel");
		height = $(".scrollable_inside").height()
		height_wrap = $(".scrollable").height()
		height = parseInt(height) - parseInt(height_wrap);
		change_number('down',amount,height);
		$(".scrollable").animate({scrollTop:number}, "slow");
		return false;
	});
	$(".scroll_up").click(function(){
		amount = $(this).attr("rel");
		height = $(".scrollable_inside").height()
		height_wrap = $(".scrollable").height()
		height = parseInt(height) - parseInt(height_wrap);
		change_number('up',amount,height);
		$(".scrollable").animate({scrollTop:number}, "slow"); 
		return false;
	});

	// search form, found in header
	$("#search").focus(function(){
		if(this.value == "Search Alpine Connected"){
			this.value = "";
		}
	});
	$("#search").blur(function(){
		if(this.value == ""){
			this.value = "Search Alpine Connected";
		}
	});

	// search by model number, found on homepage
	$("#search_model").focus(function(){
		if(this.value == "Enter Model Number"){
			this.value = "";
		}
	});
	$("#search_model").blur(function(){
		if(this.value == ""){
			this.value = "Enter Model Number";
		}
	});

	// Homepage BLOG CALLOUT
	// get height of H2's in the blog_callout on the homepage
	// addClass, if need be
	$(".title span").each(function() {
		height = ($(this).height());
		if(height <= '16'){
		}
		else {
			$(this).parent("a").parent("li").addClass("multi_line");
		}
	});
	$(".title a").click(function() {
		$(this).parent("li").parent("ul").siblings("ul").removeClass("open");
		$(this).parent("li").parent("ul").toggleClass("open");
		return false;
	});
	// minor footer adjustments (CSS3 in IE 6 and 7)
	$("#footer_nav li:last-child").css({'padding-right': '10px'});

	$("#search_model_form").submit(function(){
		$("#search_model_btn").click();
		return false;
	});
	$("#search_model").autocomplete(BASE_URL+"products/search");
	$("#search_model_btn").click(function(){
		$.get(BASE_URL+"products/lookup", { q : $("#search_model").val() }, function(r){
			if (r != 0)
			{
				// Found product, go to it
				location.href = BASE_URL+'products/view/'+r;
			}
			else
			{
				// Not found
				$("#search_model").val();
				$("#search_not_found").html("The model number specified could not be found.");
				$("#search_not_found").show();
			}
		});
	});

	
/**********************************************************************
PRODUCTS PAGES
**********************************************************************/
	// Photo related menus on the right side, used in conjunction with SCROLLABLE, above
	$(".related_photos").click(function(){
		// nav item
		$(this).parent("li").addClass("current");
		$(".product_views").parent("li").removeClass("current");
		// UL
		// restart and reset scrollable
		$(".scrollable").animate({scrollTop:0}, "fast");
		number = 0;
		$("#related_photos").fadeIn("slow").css({'z-index' : '1'});
		// hide competing UL
		$("#views").css({'z-index' : '0', 'display' : 'none'});
		return false;
	});

	// Clicking on product views
	$(".view_larger").click(function(){
		id = $(this).attr('href');
		$(".product_image").css({'z-index' : '0', 'display' : 'none'});
		$("#view_"+id).fadeIn("slow").css({'z-index' : '1'});
		return false;
	});

	$(".product_views").click(function(){
		//nav item
		$(this).parent("li").addClass("current");
		$(".related_photos").parent("li").removeClass("current");
		// UL
		// restart and reset scrollable
		$(".scrollable").animate({scrollTop:0}, "fast");
		number = 0;
		$("#views").fadeIn("slow").css({'z-index' : '1'});
		// hide competing UL
		$("#related_photos").css({'z-index' : '0', 'display' : 'none'});
		return false;
	});
	
	// Product navigation, the buttons and UL's across the bottom
	$("#product_nav a").click(function() {
		// switch class="current" for navigation item
		$('#product_nav a').removeClass('current');
		$(this).addClass('current');
		// asign value of the link target
		var thisTarget = $(this).attr('href');
		// show target UL, hide other UL
		$('.description_container div').removeClass('selected');
		$(thisTarget).addClass('selected');
		return false;
	});
	
	// replace target="_blank"
	$("[rel='external']").click(function(){
		this.target = "_blank";
	});	

	// fake File inputs
	$("#fileupload").blur(function(){
		var fake_text = $(this).attr('value'); 
		$("#fakin_it").attr("value",fake_text);
	});	

	function process1() {
		document.getElementById("fakin_it").value = (document.getElementById("fileupload").value);
	}


	
	if (window.location.hash == '#faq')
	{
		$("#faq_link").click();		
	}

	$(".postlink").each(function(){
		$(this).attr("target", "_blank");
	});

});
