$(function() {
	if($('a.iframe').length > 0) {
		$('a.iframe').fancybox({
			'width' : 600,
			'height' : 500
		});
	}
	$("#home_marquee .items").jcarousel({
			auto: 6,
			wrap: 'last',
      scroll: 1,
      initCallback: carousel_controls,
			itemVisibleInCallback: carousel_movenav,
      buttonNextHTML: null,
      buttonPrevHTML: null
  });
	$(".product_details .items").jcarousel({
			//auto: 5,
			wrap: 'none',
      scroll: 1,
			initCallback: slide_controls
  });
	$('#product_nav a').click(function() {
		if($(this).siblings('ul').length > 0) {
			$(this).parents('li').toggleClass('open');
			return false;
		}
	});
	$('#product_tabs').tabs();
	$.each($.browser, function(i, val) {
	  if(i=="msie" && val==true && ($.browser.version=="8.0")) {
			var height = 1;
			$('#product_tabs .ui-tabs-panel').each(function() {
				if($(this).height() > height) {
					height = $(this).height();
				}
			});
			$('#product_tabs .ui-tabs-panel').height(height+'px');
		}
	});
	if($('#search_results').length > 0) {
	  $('#search_results').tabs();
	  // Move to the first tab with results
	  var search_counts = [$('#search_results #product_results .product').length, $('#search_results #application_results .application').length, $('#search_results #service_results .service').length, $('#search_results #article_results .article').length]
	  var largest = Math.max.apply(Math, search_counts);
	  var search_index = 0;
	  $.each(search_counts, function(i,v) {
	    if(v == largest) {
	      search_index = i;
	    }
	  });
	  var tab_set = $('#search_results').tabs();
    tab_set.tabs('select', search_index);
	  $('body.search .sub_content li').eq(search_index).addClass('selected');
  	$('body.search .sub_content li a').click(function() { 
  		var tabs = $('#search_results').tabs();
      tabs.tabs('select', $(this).parents('li').index());
  		$(this).parents('li').siblings('li').removeClass('selected');
  		$(this).parents('li').addClass('selected');
      return false;
  	});
	}
	$('#product_tabs .rating_select').voltage_switch();
	$('.application_detail .related_products .product').click(function() {
		var url = $(this).children('.title').children('a').attr('href');
		window.location = url;
		return false
	});
	$(".product_details .main_photo img").click(function() {
		var index = parseInt($(this).attr('rel'))-1;
		var images = new Array();
		var z = 0;
		$('.product_details .items.jcarousel-list li').each(function() {
			image = $('img', this);
			src = image.attr('src').replace('small', 'big');
			images[z] = {
				'href': src,
				'title': image.parents('a').attr('title')
			};
			z++;
		});
		$.fancybox(images, {
			'padding'				: 10,
			'transitionIn'	: 'fade',
			'transitionOut'	: 'none',
			'type'          : 'image',
			'changeFade'    : 0,
			'titlePosition'	: 'over',
			'index'					: index
		});
	});
	$(".product_details .main_photo .zoom").click(function() {
	  $(this).siblings('img').trigger('click');
	  return false;
	});
	if($("body.documents .sub_content .nav select").length > 0) {
		$("body.documents .sub_content .products .nav select").prepend('<option value="" selected="selected"> </option>');
		$("body.documents .sub_content .nav select, body.documents .filter select").uniform();
		$("body.documents .sub_content .nav select").change(function() {
			if($(this).val() != '') {
				$(this).parents('form').submit();
			}
		});
		$('body.documents .filter select').change(function() {
			if($(this).val() == '') {
				$('.document_results table tr').show();
			} else {
				var val = $(this).val().replace(' ', '_');
				$('.document_results table tr').hide();
				$('.document_results table tr.' + val).show();
			}
		});
	}
	if($("body.regions").length > 0) {
		$('body.regions #region_id').uniform();
		$('body.regions #region_id').change(function() {
			window.location = '/regions/' + $(this).val();
		});
	}
	$('.product_details a.update_cart').click(function() {
		link = $(this);
		$.get($(this).attr('href'), function(data) {
			if(link.hasClass('add')) {
				link.attr('class', 'remove update_cart');
				link.html('Remove from quote cart');
			} else {
				link.attr('class', 'add update_cart');
				link.html('Add to quote cart');
			}
		});
		return false;
	});
	if($("body.contacts").length > 0) {
		$('body.contacts #new_contact select').uniform();
	}
	$('.cart_form .update_cart a').click(function() {
		link = $(this);
		$.get($(this).attr('href'), function(data) {
			var product = link.parents('.update_cart').parents('.info').parents('.product')
			product.fadeTo(0);
			product.remove();
			if($('.cart_form .product').length == 0) {
				window.location.reload();
			}
		});
		return false;
	});
	$('.cart_form .remove a').click(function() {
		var href = $(this).attr('href');
		var answer = confirm("Are you sure?");
		if (answer){
			window.location = href;
		}
		else{
			return false;
		}
	});
	setTimeout('fade_notice()', 2500);
	$('a[rel=external]').click(function() {
		window.open($(this).attr('href'));
		return false;
	});
	if($("body.job_applications #new_job_application").length > 0) {
		$("#new_job_application select").uniform();
	}
});

function fade_notice() {
	$('#flash').fadeOut('slow',0);
}

$.fn.voltage_switch = function() {
	var selector = $(this);
	var ratings = $(this).siblings('.ratings');
	$('select', selector).change(function() {
		var id = $(this).val();
		ratings.children('.voltage_rating').hide();
		ratings.children('#voltage_rating_' + id).show();
	});
	$('select', selector).each(function() {
	  $('option:selected', this).removeAttr('selected');
	  $(this).trigger('change');
	});
}

function slide_controls(carousel) {
	var scroller = $(carousel.container);
  $('.item a', scroller).bind('click', function() {
		var main_img = $(this).attr('rel');
		var index = $(this).parents('li').attr('jcarouselindex');
		var title = $(this).attr('title');
		$(".product_details .main_photo img").attr('src', main_img);
		$(".product_details .main_photo img").attr('rel', index);
		$(".product_details .main_photo img").attr('title', title);
		$(".product_details .main_photo .caption p").text(title);
    return false;
  });
}

function carousel_controls(carousel) {
	var scroller = $(carousel.container).siblings('.scroll_nav');
  $('a', scroller).bind('click', function() {
		carousel.stopAuto();
    carousel.scroll($.jcarousel.intval(jQuery(this).attr('rel')));
    return false;
  });
	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});
}

function carousel_movenav(carousel,index) {
	var pointer = $(index).attr('jcarouselindex');
	var scroller = $(carousel.container).siblings('.scroll_nav');
	$('a.current', scroller).removeClass('current');
	$('a[rel='+ pointer +']', scroller).addClass('current');
}