// JavaScript Document
$(document).ready(function() {
  $(".top_links").mouseover(function(e) {
   	$(this).find(".drop_shadow").show();
  }).mouseout(function(e) {
	$(this).find(".drop_shadow").hide();
  });
  $(".service_one").unbind('mouseout');
  $(".service_one").mouseout(function(e) {
	$(this).find(".drop_shadow").show();
  })
  
  $(".top_links").click (function(e) {
		$(".top_links").each(function (i) {
			$(this).find(".drop_shadow").hide();
			$(this).find("a").removeClass('hover');
			
			$(this).mouseover(function(e) {
				$(this).find(".drop_shadow").show();
				$(this).find("a").addClass('hover');
			});
			$(this).mouseout(function(e) {
				$(this).find(".drop_shadow").hide();
				$(this).find("a").removeClass('hover');
			});
		});
		$(this).find(".drop_shadow").show();
		$(this).find("a").addClass('hover');
		
		$(this).mouseover(function(e) {
			$(this).find(".drop_shadow").show();
			$(this).find("a").addClass('hover');
		});
		$(this).mouseout(function(e) {
			$(this).find(".drop_shadow").show();
			$(this).find("a").addClass('hover');
		});
  });
	
	$(".info_box_area").mouseover(function(e) {
		$(this).addClass("info_box_area_hover");
		$(this).find(".bike_info_link").show();
	}).mouseout(function(e) {
		$(this).find(".bike_info_link").hide();
		$(this).removeClass("info_box_area_hover");
	});
  $(".gallery_button").click(function(e) {
	    var slide =  $(this).attr('alt');
            $('#gallery').cycle(slide-1); 
            return false; 
  });
  $(".gallery_button2").click(function(e) {
	    var slide =  $(this).attr('alt');
            $('#gallery2').cycle(slide-1); 
            return false; 
  });

  $(".collapse_menu_action").click(function(e)
  {
	$expanded = $(this).find(".expanded").css('display');

	//It is exapned so you need to collapse everything
	if ($expanded == "block") {
		$(this).find(".expanded").css('display', 'none');
		$(this).find(".collapsed").css('display', 'block');
		$(this).parent().parent().find(".product_links").slideUp(200);
		$(this).parent().parent().find(".service_listing").slideUp(200);
		
	} else if($expanded ==""|| $expanded=="none" ) {
		//Clicking on an unexpanded link should close all others first then open the new link
		$("#product_listing_wrapper .collapsible_menu_wrapper").find(".expanded").css('display', 'none');
		$("#service_listing_wrapper .collapsible_menu_wrapper").find(".expanded").css('display', 'none');
		$(".collapsible_menu_wrapper").find(".collapsed").css('display', 'block');
		$(".collapsible_menu_wrapper").find(".product_links").slideUp(200);
		$(".collapsible_menu_wrapper").find(".service_listing").slideUp(200);
		
		$(this).find(".expanded").css('display', 'block');
		$(this).find(".collapsed").css('display', 'none');
		$(this).parent().parent().find(".service_listing").slideDown(200);
		$(this).parent().parent().find(".product_links").slideDown(200);
	}
  });

  $('#gallery').cycle({
      fx:     'fade',
      randomizeEffects: 1,
      timeout: 10000,
      before:  onBefore
   });
  $('#gallery2').cycle({
      fx:     'fade',
      randomizeEffects: 1,
      timeout: 3000,
      before:  onBeforeDepartmentPhotos
   });

});
function displayProduct(obj,id)
{
	$.ajax({
	   type: "POST",
	   url: "./",
	   data: "fx=product_detail&id="+id,
	   success: function(data){
			$("#product_detail").html(data);
			$(".gallery_button").click(function(e) {
					var slide =  $(this).attr('alt');
						$('#gallery').cycle(slide-1); 
						return false; 
			  });
			
			  $('#gallery').cycle({
				  fx:     'fade',
				  randomizeEffects: 1,
				  timeout: 3000,
				  before:  onBefore
			   });
			$(".product_links").find("a").removeClass('hover');  
			$(obj).addClass('hover');			  
	   }
	});	
}

function updateSearchResult(page,sortby,cat,brand,type)
{
	$.ajax({
	   type: "POST",
	   url: "./",
	   data: "fx=search_usedbikes&page="+page+"&sort="+sortby+"&cat="+cat+"&b="+brand+"&type="+type+"",
	   success: function(data){
			$("#search_results_ajax").html(data);
	   }
	});
}

function getBrand(id)
{
	$.ajax({
	   type: "POST",
	   url: "./",
	   data: "fx=get_brand&id="+id,
	   success: function(data){
			$("select#select_brand").html(data);
			$("select#select_brand").attr('disabled',false);
			$("select#select_type").attr('disabled',true);
			updateSearchResult(1,'ASC',id,0,0);
	   }
	});
}

function getType(id)
{
	$.ajax({
	   type: "POST",
	   url: "./",
	   data: "fx=get_type&id="+id,
	   success: function(data){
			$("select#select_type").html(data);
			$("select#select_type").attr('disabled',false);
			updateSearchResult(1,'ASC',0,id,0);
	   }
	});
}
function displayUsedBikeDetails(id,page,sortby,cat,brand,type)
{
	$.ajax({
	   type: "POST",
	   url: "./",
	   data: "fx=display_usedbike_details&id="+id+"&page="+page+"&sort="+sortby+"&cat="+cat+"&b="+brand+"&type="+type+"",
	   success: function(data){
			$("#search_results_ajax").html(data);
	   }
	});
}
function displayService(id)
{
	$.ajax({
	   type: "POST",
	   url: "./",
	   data: "fx=service_detail&id="+id,
	   success: function(data){
			$("#service_listing_wrapper").html(data);
			$(".collapse_menu_action").click(function(e)
			{
				$expanded = $(this).find(".expanded").css('display');
				
				//It is exapned so you need to collapse everything
				if ($expanded == "block") {
					$(this).find(".expanded").css('display', 'none');
					$(this).find(".collapsed").css('display', 'block');
					$(this).parent().parent().find(".service_listing").slideUp(200);
					
				} else if($expanded ==""|| $expanded=="none" ) {
					//Clicking on an unexpanded link should close all others first then open the new link
					$("#service_listing_wrapper .collapsible_menu_wrapper").find(".expanded").css('display', 'none');
					$(".collapsible_menu_wrapper").find(".collapsed").css('display', 'block');
					$(".collapsible_menu_wrapper").find(".service_listing").slideUp(200);
					
					$(this).find(".expanded").css('display', 'block');
					$(this).find(".collapsed").css('display', 'none');
					$(this).parent().parent().find(".service_listing").slideDown(200);
				}
			});
	   }
	});	
}

function onBefore(currSlideElement, nextSlideElement, options, forwardFlag) {
	var curr=0;
	var next = 0;
	if(currSlideElement.alt)
	{
		ctr = currSlideElement.alt;
		next = nextSlideElement.alt;
	}
	else {
		ctr = currSlideElement.title;
		next = nextSlideElement.title;
	}
	
	$("#gbb_lit_button"+ctr).hide();
	$("#gbb_unlit_button"+ctr).show();
	
	$("#gbb_lit_button"+next).show();
	$("#gbb_unlit_button"+next).hide();
}

function onBeforeDepartmentPhotos(currSlideElement, nextSlideElement, options, forwardFlag) {
	var curr=0;
	var next = 0;
	if(currSlideElement.alt)
	{
		ctr = currSlideElement.alt;
		next = nextSlideElement.alt;
	}
	else {
		ctr = currSlideElement.title;
		next = nextSlideElement.title;
	}
	
	$("#gbb2_lit_button"+ctr).hide();
	$("#gbb2_unlit_button"+ctr).show();
	
	$("#gbb2_lit_button"+next).show();
	$("#gbb2_unlit_button"+next).hide();
}

function showImage(obj,id)
{
	$('.view_bike_results_graphic').find('img').css('display','none'); 
	$('#usedbike_image_'+id).css('display','block'); 
	$('.usedbike_thumbnail').each(function (i) { $(this).removeClass('redborder'); }); 
	$(obj).addClass('redborder');
}
