jQuery(document).ready(function(){
	
	$j = jQuery.noConflict();
	
	$j("#slideshow").cycle({
	  fx: 'fade',
	  speed: 1500,
    timeout: 5000
	});
		
	$j(".scroll_container").cycle({
	  fx: 'scrollUp',
	  speed: 700,
    timeout: 5000
	});
	
	$j("div.grey_button a").ellipsis();
	$j("ul#secondary_nav li a").ellipsis();
	
	$j("div.dialog").dialog({
	  autoOpen: false,
	  modal: true,
	  draggable: false,
	  resizable: false
	});
	
	$j("a.dialog_link").live('click', function() {
	  var id = $j(this).attr('id');
	  $j('#dialog_' + id).dialog('open');
	  return false;
	});
	
	$j("#lid_container").cycle({
	  fx: 'scrollHorz',
	  speed: 650,
    timeout: 0,
    next: '#lid_next',
    prev: '#lid_prev',
    after: updateLidNumbers
	});
	
	function updateLidNumbers() {
	  var scroller = $j('.' + this.className + ':visible');
	  var start = scroller.attr('id').split("lid_group_")[1];
	  var count = scroller.children().length;
	  $j(".lid_start").text(((start - 1) * 2) + 1);
	  $j(".lid_end").text(((start - 1) * 2) + count);
	}
	
	$j("#base_container").cycle({
	  fx: 'scrollHorz',
	  speed: 650,
    timeout: 0,
    next: '#base_next',
    prev: '#base_prev',
    after: updateBaseNumbers
	});
	
	function updateBaseNumbers() {
	  var scroller = $j('.' + this.className + ':visible');
	  var start = scroller.attr('id').split("base_group_")[1];;
	  var count = scroller.children().length;
	  $j(".base_start").text(((start - 1) * 5) + 1);
	  $j(".base_end").text(((start - 1) * 5) + count);
	}
	
	$j("a.browser_thumb").click(function() {
	  $j("#product_partial").load($j(this).attr('href'));
	  return false;
	});
		
	// clicking alternate view thumbnail shows full size version
	$j("a.product_alternate_view").click(function() {
	  var image = $j("#ajax_product_image_container").find('img');
	  image.attr('src', $j(this).attr('href'));
	  return false;
	});
	
	// shows category title / description in sidebar on hover
	$j(".range_container_second").hover(
	  function() {
	    var title = $j(this).find(".category_title").html();
	    var description = $j(this).find(".category_description").html();
	    $j("#show_title").html(title);
	    $j("#show_description").html(description);
	  },
	  function() {
	    var parent_title = $j("#parent_title").html();
	    var parent_description = $j("#parent_description").html();
	    $j("#show_title").html(parent_title);
	    $j("#show_description").html(parent_description);
	  }
	);
	
	// clicking div links to child's a href
	$j(".range_container_second, .internal_type_container").click(function() {
	  var path = $j(this).find("a").attr('href');
	  window.location = path;
	});
	
	$j(".top_second, .internal_type_container").hover(
	  function() {
	    $j(this).css({ border: "1px solid #be1c07" });
	  },
	  function() {
	    $j(this).css({ border: "1px solid #ccc" });
	  }
	);
	
	// select current menu item  
  $j("div.main_nav_option").each( function() {
      var link = $j(this).find('a');
      if (location.pathname.indexOf(link.attr('href')) > -1) {
        $j("div.main_nav_option").removeClass("on");
        $j(this).addClass("on");
        if(location.pathname == '/') {
          $j("#left_end").addClass("left_on");
        }
      }
    }
  );
  
  $j.ajaxSetup({ 
    'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")} 
  })
  
});

jQuery(document).ajaxSend(function(event, request, settings) {
  if (typeof(AUTH_TOKEN) == "undefined") return;
  settings.data = settings.data || "";
  settings.data += (settings.data ? "&" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
});

