/* Declare a namespace for the site */
var Site = window.Site || {};

/* Create a closure to maintain scope of the '$'
   and remain compatible with other frameworks.  */
(function($) {
	// ----------------------------- //
	// tabs
	$(function() {
		var tabContainers = $("#vehicle-info > div").not("#representative").not(".print #vehicle-info > div");
		var tabLinks = $("#tabs li a");
		// if page loaded with hash
		var hash_set = false
		if (window.location.hash) {
			var theTabAnchor = window.location.hash;
			if (theTabAnchor == '#description' || theTabAnchor == '#calculator') {
				var newTabLocation = theTabAnchor;	
				tabContainers.hide().filter(theTabAnchor).show();
				tabLinks.filter('[href*='+ theTabAnchor +']').addClass('current');
				location.href = newTabLocation;
				var hash_set = true;
			}
		}
		if (hash_set == false)
		{
			tabContainers.hide().filter(':first').show();
			tabLinks.filter(':first').addClass('current');
		}
		//when clicked
		tabLinks.click(function () {
				tabContainers.hide();
				tabContainers.filter(this.hash).show();
				tabLinks.removeClass('current');
				$(this).addClass('current');
				return false;
		})
	});
	// ----------------------------- //
	// alt rows on <dl>
	$(function() {
		$("#description dt:odd").addClass('alt');
		$("#description dd:odd").addClass('alt');
	});
	// ----------------------------- //
	// accordion
    $(function () {
		$("#faqs div").hide();
		// when clicked
		$("#faqs h2 a").click(function(){
			if ($(this).is(".open")) {
				$(this).removeClass();
				$("#faqs div:visible").slideUp();
				$(this).blur();
				return false;
			} else {
				$("#faqs h2 a").removeClass("open");
				$(this).addClass("open");
				$("#faqs div:visible").slideUp();
				$(this).parent().next(":not(:visible)").slideDown();
				$(this).blur();
				return false;
			}			
		});
		// if page loaded with hash
		if (window.location.hash) {
			var theAnchor = window.location.hash;
			var theOpenItem = '#faqs li#' + theAnchor + ' div';
			$(theOpenItem).show();
			var newLocation = theAnchor;	
			location.href = newLocation;
		}
	});
	// ----------------------------- //
	// print
	$('a.print').click(function() {
		window.print();
		$(this).blur();
		return false;
	});
	// ----------------------------- //
	// Go back
	$(document).ready(function(){
	    $('a.back').click(function(){
	        parent.history.back();
	        return false;
	    });
	});	
	// ----------------------------- //
	//rollover
    $(function () {
		xOffset = -10;
		yOffset = 30;
		var rolloverImages = $("#results li > a").not(".no-image");
		// when rollover
		$(rolloverImages).hover(function(e){
			this.t = this.title;
			this.title = "";
			//imgSrc = $(this).children(':first-child').attr("src");
			//imgNewSrc = imgSrc.replace("-result", "-preview");	
                    imgNewSrc = $(this).children(':first-child').attr("lrg");

			$("body").append("<div id='preview'><img src='"+ imgNewSrc +"' alt="+ this.t +"' /></div>");
			$("#preview")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px")
				.fadeIn("fast");						
	    },
		function(){
			this.title = this.t;	
			$("#preview").remove();
	    });
	    // stick with the cursor	
		$(rolloverImages).mousemove(function(e){
			$("#preview")
				.css("top",(e.pageY - xOffset) + "px")
				.css("left",(e.pageX + yOffset) + "px");
		});			
	});
	// ----------------------------- //
	// gallery
	$("#gallery li a").click(function() {
		imageSrc = $(this).attr("href");
		imageAlt = $(this).attr("title");
		$("#main-image").attr("src", imageSrc);
		$("#main-image").attr("alt", imageAlt);
		return false;
	});	
	// ----------------------------- //
	// button hover fix for IE
	$("#content button").mouseover(function() {
		$(this).addClass("hover");
	}).mouseout(function() {
		$(this).removeClass("hover");
	});
	// ----------------------------- //
	// HTML5 placeholder support
	$(function() {
		$('input, textarea').placeholder();
	});
	// ----------------------------- //
	// when page has loaded
	$(window).bind("load", function() {		
		
	
	});

  $("select#vehicle_search_vehicle_manufacturer_id").change(function(){
    $.getJSON("/vehicle_models.json",{vehicle_manufacturer_id: $(this).val(), ajax: 'true'}, function(j){
      var options = '<option value="">All models</option>';
      for (var i = 0; i < j.length; i++) {
        options += '<option value="' + j[i].id+ '">' + j[i].name+ '</option>';
      }
      $("select#vehicle_search_vehicle_model_id").html(options);
    })
  })

	
})(jQuery);

