
$(function() {
//breadcrumb tabs
	$("ul.mogBreadCrumb li.mogBcItem a.mogBcLink").click(function() {
		$(this).parent().addClass("expanded").removeClass("hover").siblings().removeClass("expanded");return false;
		}).hover(function() {
		if(!$(this).parent().hasClass("expanded")) $(this).parent().addClass("hover");
		}, function() {
		$(this).parent().removeClass("hover");
	});

	//categories classes
	$("ul.mogPGroups li:nth-child(3n)").addClass("third");
	$("ul.mogPGroups li:nth-child(3n)").after("<li class='liner'>&nbsp;</li>");
	//categories breadcrumb classes
	$("div.mogBCtab ul.mogPGroups li.liner").remove();
	$("div.mogBCtab ul.mogPGroups li:nth-child(6n)").addClass("sixth");
	$("div.mogBCtab ul.mogPGroups li:nth-child(6n)").after("<li class='breadLiner'>&nbsp;</li>");

	//help popup

	$("a.sHelpLink").click(function(){
		$('#helpPop').fadeIn('fast');
		$('select').css("visibility", "hidden");
	});
	$("a.helpClose").click(function(){
		$('#helpPop').fadeOut('fast');
		$('select').css("visibility", "visible");
	});

//product info tabs
	$("ul.mogProdTabs li").click(function() {
		$(this).addClass("selected").removeClass("hover").siblings().removeClass("selected");return false; 

		}).hover(function() {
		if(!$(this).hasClass("selected")) $(this).addClass("hover");
		}, function() {
		$(this).removeClass("hover");
	});
	$("ul.mogProdTabs li#detailsTab").click(function() {
		$("div.prodTabDetails").addClass("visible").siblings().removeClass("visible");
	});
	$("ul.mogProdTabs li#crTab").click(function() {
		$("div.prodTabCr").addClass("visible").siblings().removeClass("visible");
	});
	$("ul.mogProdTabs li#relatedTab").click(function() {
		$("div.prodTabRelated").addClass("visible").siblings().removeClass("visible");
	});

//product tabs from search	
	$("ul.mogProdTabs li#schProductsTab").click(function() {
		$("div#mogSchProd").addClass("visible").siblings().removeClass("visible");
		$("div.prodListRes").show();
	});
	$("ul.mogProdTabs li#schProductGroupsTab").click(function() {
		$("div#mogSchProdGroup").addClass("visible").siblings().removeClass("visible");
		$("div.prodListRes").hide();
	});
	
//cross reference search result tabs
	$("ul.mogProdTabs li#crAllTab").click(function() {
		$("div#crAllResult").addClass("visible").siblings().removeClass("visible");
	});
	$("ul.mogProdTabs li#crCompTab").click(function() {
		$("div#crCompResult").addClass("visible").siblings().removeClass("visible");
	});
	$("ul.mogProdTabs li#crICTab").click(function() {
		$("div#crIcResult").addClass("visible").siblings().removeClass("visible");
	});
//help tips
	$("a.mogCurHelp").click(function(){
		$("span.helpPopup").toggle();
		return false;
	});
	$("a.hpClose").click(function(){
		$("span.helpPopup").hide();
		return false;
	});
//info tips
	$("a.infoTip").click(function(){
		$("div.infoPop").hide(),
		$(this).siblings("div.infoPop").toggle();
		return false;
	});
	$("div.infoPop a.hpClose").click(function(){
		$("div.infoPop").hide();
	});
//emty product attribute value
	$("div.mogSResultItem table tr td.val").each(function(){
		  var value = $.trim($(this).text())
		  if(value == ""){
			$(this).html("-");
		  }
	});	
//search refinement
/*	$("td.mogColumn ul").each(function() {
	    $("li:gt(4)", this).wrapAll("<li><ul class='moreOptions'>"); 
	  });
	$("a.moreOpt").click(function(){
		$(this).toggleClass("collapse");
		$(this).text($(this).text() == 'More options' ? 'Less options' : 'More options');
		$(this).parent().find('ul.moreOptions').toggle();
  		return false;
	});
	
	$("div.searchRefinement span").click(function(){
		$(this).toggleClass("expand").toggleClass("colapse"),
		$("div.mogRefineSearch").toggleClass("visible");
	});
	$("table.refinementOptions tr td:first-child").addClass("first");
	$("table.refinementOptions tr td:last-child").addClass("last");
	
	$("a.moreAttr").click(function(){
		$(this).toggleClass("collapse");
		$(this).text($(this).text() == 'Show all attributes' ? 'Less attributes' : 'Show all attributes');
		$("table.addRefOpt").toggleClass("visTable");
		return false;
	});
*///category tree list
//$("ul.mogCatListing li.mclItem:nth-child(even)").addClass("even");
//$("ul.mogCatListing li:odd").removeClass("even").addClass("odd");
/*equal column height
	$.fn.setAllToMaxHeight = function(){
		return this.height( Math.max.apply(this, $.map( this , function(e){ return $(e).height() }) ) );
	}
	$("div.searchRefinement span").one("click", function(){
		$('.mogColumn').setAllToMaxHeight();
	});*/
//Search options
	$('input[name=dontKnow]').attr('checked', false);
	$('input#dontKnow').click(function(){
		$('div#mogComModSelect').toggleClass("hidden");
		$('div#mogPtNumSearch').toggleClass("hidden");
	});
//Search results dropdown
	$('div.mogViewResultDrop').click(function(){
		$('div#mogResultsDrop').toggleClass("visible");
	});
/*Cross reference search options*/
	//$("div.crCboxes input:checkbox:checked").attr("checked", "");    
	$("div.crCboxes input#comp").click(function(){
		$("fieldset.competitor").toggleClass("visible");
	});
	$("div.crCboxes input#icMan").click(function(){
		$("fieldset.icMan").toggleClass("visible");
	});
	/*disabling search field*/
	$( "input#icMan, input#comp" ).click(
		function(){
		var icMan = $("input#icMan").attr('checked');
		var comp = $("input#comp").attr('checked');
		if((icMan == true) || (comp == true)){
			$(".mogSbox").addClass("dis");
			$(".mogSbox").attr("disabled", "disabled");
		}
		else{
			$(".mogSbox").removeClass("dis");
			$(".mogSbox").removeAttr("disabled");
		}

	});
	
});
// Search input value
$(function() {
	$('input#searchBy').defaultValue('Search Pulse products by Keyword or Part number');
	$('input#schWithin').defaultValue('Search Within Results');
	$('input#mogPartNumber').defaultValue('Enter a competitor part number or IC manufacturer part number');
});

encodeSlash=function(input){
	return input.replace(/\//g,"%252F");
}

//autocomplete
$(document).ready(function () {
	
	function findValueCallback(event, data, formatted) {
		$("#searchBy").attr("value",data.name);
	}
	
	$(":text, textarea").result(findValueCallback).next().click(function() {
			$(this).prev().search();
	});

	$("#searchBy").autocomplete("/nodecorate/autocompleteSearch",{        
		minChars: 0,
		max: 22,
		width: 555,
		scroll: true,
		matchContains: false,
		selectFirst: true,
		dataType:"json", 
		formatItem: function(data,i,max,value,term){
			if (value == "lastOne"){
				return "<img src=\"/images/autocomplete_magnifier.png\"/><strong>Search for:</strong> '" + data + "'";
			}else if (data.record_type =="product"){
				return "<img src=\"/productThumbnail/" + data.name_image + "\" width=\"28\" height=\"18\"/>" + "<span>" + data.name + "</span>";
			}else {
				return "<img src=\"/showImage/"+ data.name +"\"/>" + "<span>" + data.name + "</span>";
			}
		}, 
		parse: function(data){
			var acList = new Array();
				for(var i=0;i<data.productList.length && i<10;i++){
					acList[acList.length] = { data:data.productList[i], value:data.productList[i].name, result:data.productList[i].name };
				}
				for(var i=0;i<data.categoryList.length && i<5;i++){
					acList[acList.length] = { data:data.categoryList[i], value:data.categoryList[i].name, result:data.categoryList[i].name };
				}
				acList[acList.length] = { data:data.q, value:"lastOne", result:"" };
				return acList;
		}	
	}).result(function(event, data, formatted) {
		if (data.record_type == "product"){
			location.href="/product/" + encodeSlash(data.name);
		} else if (data.record_type == "category"){
			location.href="/categoryList/" + data.id;
		} else {
			location.href="/productSearch/" + encodeSlash(data);
		}
	});
;
//search from

	$('#productSearchForm').submit(function () {
		if ($("#searchBy").val() != null && $("#searchBy").val()!= "" &&  $("#searchBy").val().indexOf('*')==-1) {
			return true;
		}else {
			$('#searchBy').val('Search Pulse products by Keyword or Part number');
			return false;
		}
	
	});
	
	$('#productFinderForm').submit(function () {
		if ($("#productFinderSearchBy").val()!='undefined' && $("#productFinderSearchBy").val() != null && $("#productFinderSearchBy").val()!= "" &&  $("#productFinderSearchBy").val().indexOf('*')==-1) {
			location.href="/productSearch/" + encodeSlash(jQuery("#productFinderSearchBy").val());
		}
	});
	
	$('#productFinderSearchButton').click(function(){
		if ($("#productFinderSearchBy").val()!='undefined' && $("#productFinderSearchBy").val() != null && $("#productFinderSearchBy").val()!= "" &&  $("#productFinderSearchBy").val().indexOf('*')==-1) {
			location.href="/productSearch/" + encodeSlash(jQuery("#productFinderSearchBy").val());
		}

	});

});
