// JavaScript Document

//<![CDATA[

var resultsCountPerPage = 3;
var xhr_suggestions = "";
var xhr_contents= "";
var xhr_products = "";
var xhr_faqs = "";
var xhr_links = "";
var rowNumber = 0;
var searchValue = "";

var isSuggestions;
var isProducts;
var isRecommendation;
var isResults;

var r1a;
var r2a;
var r3a;

var seeall = $('.seeMore.all').html();

var timeout;

var Browser = {
  Version: function() {
    var version = 999; // we assume a sane browser
    if (navigator.appVersion.indexOf("MSIE") != -1)
      // bah, IE again, lets downgrade version number
      version = parseFloat(navigator.appVersion.split("MSIE")[1]);
    return version;
  }
}

function rowMiniHandle( direction ) {
	var str = "";
	var noSuggestions = false;
	
if(!(jQuery(".row3a").is(":visible"))){
if(!(jQuery(".row2a").is(":visible"))){
  
  	if (direction == "up") {
		if (rowNumber == 0) rowNumber = 2;
		rowNumber--;
		miniHoverEffect(rowNumber);
	}
	if (direction == "down") {
		if (rowNumber == 1) rowNumber = -1;
		rowNumber++;
		miniHoverEffect(rowNumber);
	}
	
	typeMiniResultSearch(noSuggestions);
	
  }else{  
  	if (direction == "up") {
		if (rowNumber == 0) rowNumber = 3;
		rowNumber--;
		miniHoverEffect(rowNumber);
	}
	if (direction == "down") {
		if (rowNumber == 2) rowNumber = -1;
		rowNumber++;
		miniHoverEffect(rowNumber);
	}
	
	typeMiniResultSearch(noSuggestions);
	
  }
}else{
  
  if (direction == "up") {
		if (rowNumber == 0) rowNumber = 4;
		rowNumber--;
		miniHoverEffect(rowNumber);
	}
	if (direction == "down") {
		if (rowNumber == 3) rowNumber = -1;
		rowNumber++;
		
		miniHoverEffect(rowNumber);
	}
	

	typeMiniResultSearch(noSuggestions);
}
	
}

function enableMiniHovers(){
	jQuery("#miniSearch .productItem").mouseover(function(){
       jQuery(this).addClass("hover");
       jQuery("#miniSearch .productItem.hover a").addClass("targetUrl");
    });
		jQuery("#miniSearch .productItem").mouseout(function(){
       jQuery(this).removeClass("hover");
       jQuery(this).find("a.url").removeClass("targetUrl");
    });
   	jQuery("#miniSearch .result, #miniSearch .newsResult").mouseover(function(){
       jQuery(this).addClass("hover");
       jQuery("#miniSearch .result.hover .title a, #miniSearch .newsResult.hover .title a").addClass("targetUrl");
    });
    jQuery("#miniSearch .result, #miniSearch .newsResult").mouseout(function(){
       jQuery(this).removeClass("hover");
       jQuery(this).find(".title a").removeClass("targetUrl");
    });
    jQuery("#miniSearch .productItem").click(function(){
     var url= "http://" + location.host + jQuery("a.targetUrl").attr("href");
    window.location.replace(url);
    });  
    jQuery("#miniSearch .result, #miniSearch .newsResult").click(function(){
     var url= jQuery(".title a.targetUrl").attr("href");
    window.location.replace(url);
    });  

}

function checkHeight(cons1) {

  var scHeight = document.documentElement.clientHeight; 
  var suggestionBar = 70;
  var sB = jQuery("#topmenuLv1-search-box");
  var searchBox = sB.offset();
  var cons = 0.8;
  if(jQuery("#cw-preview-box").is(":visible")){
   jQuery("#miniSearchWrapper").css("top","124px");
  }
  var stdHeight = (scHeight - searchBox.top)*cons; 
  var stdHeight = Math.round(stdHeight);
  var height2 = stdHeight;  // available height
  var height = stdHeight - suggestionBar - 10; // size of scroll part
  var h3 = jQuery("#miniSearch .scrollerPart .wrapp").height();
  var trueHeight = 70 + h3; 

  //alert(height + " > " + trueHeight);
  
  
        jQuery("#miniSearch .jScrollPaneContainer, #miniSearch .scrollerPart").css("height", "auto");
        jQuery("#miniSearch .jScrollPaneContainer, #miniSearch .scrollerPart").css("max-height", "none");
        jQuery("#miniSearch .scrollerPart").css("padding-right","0px");
        
  
     
      if(cons1 == 2){
      
          height2 = suggestionBar + height;
      
       if (Browser.Version() <= 8){
          jQuery("#miniSearch .scrollerPart").css("max-height", height + "px");
          jQuery("#miniSearch").css("position","absolute");
       }else{ 
        
          if(height > trueHeight){
          
          jQuery('.jScrollPaneTrack, .jScrollArrowUp, .jScrollArrowDown').hide();
          $('#miniSearch .scrollerPart').jScrollPaneRemove();      
          }else{ 
          
          jQuery("#miniSearch .jScrollPaneContainer").css("max-height", height + "px");
          jQuery("#miniSearch .scrollerPart").css("max-height", height + "px");
          jQuery("#miniSearch .jScrollPaneContainer").css("height", height + "px");
          jQuery("#miniSearch .scrollerPart").attr("style","height:" + h3 + "px"); 
          
          jQuery("#miniSearch .scrollerPart").jScrollPane(
          		{
			       showArrows: true,
			       horizontalGutter: 30,
			       verticalGutter: 30
		        }
          );  
          
          jQuery("#miniSearch a.jScrollArrowDown, #miniSearch a.jScrollArrowUp, #miniSearch .jScrollPaneDrag").mousedown(function(){
          jQuery(this).addClass("active");
          });
          jQuery("#miniSearch a.jScrollArrowDown, #miniSearch a.jScrollArrowUp, #miniSearch .jScrollPaneDrag").mouseup(function(){
          jQuery(this).removeClass("active");
          });
          jQuery("#miniSearch a.jScrollArrowDown, #miniSearch a.jScrollArrowUp, #miniSearch .jScrollPaneDrag").mouseout(function(){
          jQuery(this).removeClass("active");
          });        
          
           jQuery("#miniSearch .jScrollPaneTrack, #miniSearch .jScrollPaneDrag, #miniSearch a.jScrollArrowDown, #miniSearch a.jScrollArrowUp").css("width", "15px");
           jQuery("#miniSearch .scrollerPart").css("padding-right","15px");
        
          }    
        }
       }
}

function hideMiniSearch(){
  jQuery("#miniSearchWrapper").hide();
  hideSeeAllLink();
} 

function showSeeAllLink(){
  jQuery("#miniSearch .seeMore.all").show();	
}
function hideSeeAllLink(){
 jQuery("#miniSearch .seeMore.all").hide();
}

function firstShowSuggestionBox() {
  $("#miniSearchWrapper").show();
  $("#miniSearch .miniSearchSuggerstions").show();
  showSeeAllLink();
}

function showMiniSuggestionBox() {
		  
	$("#miniSearch .miniSearchSuggerstions").show();
	$("#miniSearch .miniSearchSuggerstions").css("display","block");
	showSeeAllLink();
}

function hideMiniSuggestionBox() {
		  
	$("#miniSearch .miniSearchSuggerstions").hide();
	$("#miniSearch .miniSearchSuggerstions").css("display","none");
	checkHeight(2);
}

function miniSuggestion(show, data) {
	var str = $("input#search-text").val();
	if( show == true && str != "" ) {
		showMiniSuggestionBox();
	} else {
		hideMiniSuggestionBox();
	}
	   jQuery(".row1a, .row2a, .row3a").show();
	   
	if(r3a.length <= 0 ){
     jQuery(".row3a").hide();
    if(r2a.length <= 0) jQuery(".row2a").hide();
  }
}

function checkResults(){
	
	if(isProducts == false && isRecommendation == false && isResults == false && isVideos == false){
      hideSeeAllLink();
      $('.seeMore.message').show();
      $('.seeMore.message').css("text-align", "left");
      
  }else{
      $('.seeMore.message').hide();
      showSeeAllLink();
  }
}
	
function ajaxMiniSearchAll(howMany, showSuggestions , suggestionAgain) {
	resultsCount = 0;
	
	//alert( howMany + "," + showSuggestions + "," + suggestionAgain );
	
	if(suggestionAgain==undefined){
  suggestionAgain = true;
  }
	start = resultsCount * resultsCountPerPage;
	//if (cw != "") cw = "&cw-gsa-prefix=" + cw;
	if(xhr_contents != "") {
		xhr_contents.abort();
	}    
	xhr_contents = jQuery.ajax({
		type: "GET",
		url: "/search/ajax-search-all-mini.jhtml",
		dataType: "html",
		data: "startItem="+ start +"&itemCount=" + howMany + "&queryValue=" + encodeURI($("input#search-text").val()) + "&needSuggestion=" + suggestionAgain,
		contentType: "application/x-www-form-urlencoded;charset=utf-8",
		beforeSend: function(data){
         hideSeeAllLink();
			},
		success: function(data){
				if (!(data == "")) $('#miniSearch .scrollerPart').html(data);			
			},
			
		complete: function(data){
		
				if (!(data == "")) {
					$('#miniSearch').show(); 
					firstShowSuggestionBox();
				
        
        if(suggestionAgain == true){
        	
					if(!(r1a == '')){
           $('.row1a').show();
           $('.row1a').html(r1a);
          }else{
            $('.row1a').hide();
          }
					if(!(r2a == '')){
					 $('.row2a').show();
           $('.row2a').html(r2a);
          }else{
            $('.row2a').hide();
          }
          if(!(r3a == '')){
           $('.row3a').show();
           $('.row3a').html(r3a);
          }else{
            $('.row3a').hide();
          }
                     
          if(r1a == '' && r2a == '' && r3a == ''){
           hideMiniSuggestionBox();
          }else{
          miniSuggestion(showSuggestions, data);
          }      
            if(isSuggestions == false){
               jQuery(".miniSearchSuggerstions").hide();
            }else if(isSuggestions == true){
               jQuery(".miniSearchSuggerstions").show();  
            }
             
          }
  
        checkHeight(2);
        checkResults();  
				  
        $('.recommendedLinks .title a').ThreeDots({ max_rows: 1 });	
      	$('.result .title a').ThreeDots({ max_rows: 1 });	
      	$('.result .description').ThreeDots({ max_rows: 2 });															
      	$('.newsResult .title').ThreeDots({ max_rows: 1 });
      	$('.newsResult .description').ThreeDots({ max_rows: 2 });
					
				checkHeight(2);		
				}
			}
	});  
}

function typeMiniResultSearch(noSuggestAgain, show) {

if(noSuggestAgain == undefined) noSuggestAgain = true;
if(show == undefined) show = true;

          
	if (jQuery.trim($("input#search-text").val()) != ""){
					$('#submitButton').attr("href","/search/index.jhtml?searchValue=" + encodeURI($("input#search-text").val()) + "");
          $('.seeMore.all a').attr("href","/search/index.jhtml?searchValue=" + encodeURI($("input#search-text").val()) + "");
          
          if(noSuggestAgain == false){
          //alert(noSuggestAgain);
             	ajaxMiniSearchAll(resultsCountPerPage, show, false);
          }else if ((noSuggestAgain == true)){
          //alert(show);
          		ajaxMiniSearchAll(resultsCountPerPage, show);  
          }
				
	} else {
		hideMiniSuggestionBox();
	}
}

function takeValue( i ) {
	var str = $(".row"+i+"a").text();
	$("#topmenuLv1-search-box input").val( str );
					$('#submitButton').attr("href","/search/index.jhtml?searchValue=" + encodeURI(str) + "");
          $('.seeMore.all a').attr("href","/search/index.jhtml?searchValue=" + encodeURI(str) + "");
          $('.seeMore.products a').attr("href","/search/index.jhtml?type=products&searchValue=" + encodeURI(str) + "");
	hideMiniSuggestionBox();
}

function miniHoverEffect() {
	switch (rowNumber) {
			case 0:
				$(".row1a").removeClass("rowHover");
				$(".row2a").removeClass("rowHover");
				$(".row3a").removeClass("rowHover");
				$("#topmenuLv1-search-box input").val( searchValue );
			break;
			case 1:
				$(".row1a").addClass("rowHover");
				$(".row2a").removeClass("rowHover");
				$(".row3a").removeClass("rowHover");
				str = $(".row1a").text();
				$("#topmenuLv1-search-box input").val( str );
			break;
			case 2:
				$(".row1a").removeClass("rowHover");
				$(".row2a").addClass("rowHover");
				$(".row3a").removeClass("rowHover");
				str = $(".row2a").text();
				$("#topmenuLv1-search-box input").val( str );
			break;
			case 3:
				$(".row1a").removeClass("rowHover");
				$(".row2a").removeClass("rowHover");
				$(".row3a").addClass("rowHover");
				str = $(".row3a").text();
				$("#topmenuLv1-search-box input").val( str );
			break;
		}
}

function mouseHoverEffect(){
	switch (rowNumber) {
			case 0:
				$(".row1a").removeClass("rowHover");
				$(".row2a").removeClass("rowHover");
				$(".row3a").removeClass("rowHover");
			break;
			case 1:
				$(".row1a").addClass("rowHover");
				$(".row2a").removeClass("rowHover");
				$(".row3a").removeClass("rowHover");
			break;
			case 2:
				$(".row1a").removeClass("rowHover");
				$(".row2a").addClass("rowHover");
				$(".row3a").removeClass("rowHover");
			break;
			case 3:
				$(".row1a").removeClass("rowHover");
				$(".row2a").removeClass("rowHover");
				$(".row3a").addClass("rowHover");
			break;
		}
}


jQuery(document).ready(function(){ 
jQuery("#miniSearchWrapper").css("visibility","visible");

if(jQuery("input#search-text").is(":visible")){  
}else{
  jQuery("#miniSearchWrapper").hide();
}

      searchFocusOut();       
				$('.seeMore.all').show();
				
jQuery("#search-text").keyup(function(e){

  var KeyID = (window.event) ? event.keyCode : e.keyCode;
  
  if(KeyID == 38 || KeyID == 40 || KeyID == 13){
  
	switch(KeyID) {
		case 38: //Arrow Up			
			rowMiniHandle("up");
		break;
		case 40: //Arrow Down			
			rowMiniHandle("down")
		break;
		case 13: //Enter
			var url = "/search/index.jhtml?searchValue=" + encodeURI($('input#search-text').val());
			location.href  = url;
		break;
		default:
			searchValue = $("#search-text input").val();
		break;
	}
	
	}else{
     	searchValue = $("#search-text input").val();
		 	if(timeout) { clearTimeout(timeout); }
			timeout = setTimeout("typeMiniResultSearch(true)", 250);
  }
}); 

jQuery("#miniSearch").mouseover(function(){
enableMiniHovers();
}); 
    	
});

//]]>

/* -----------------------------------------------------------------------------
GSA functions for Webtrends
All data needed for GSA notifications for Webtrends is in the gsaWt object.
----------------------------------------------------------------------------- */

var gsaWt = createDebugObject( {
	
	// used to set up debug messages level
	objectName : "gsaWt",
	// set to true on first ( each ) GSA search request
	results : false,
	// last searched phrase
	phrase : "",
	// last found results count
	count : 0,
	// last searched category, values : all, products, customerService
	category : "all",
	
	// WT notifications --------------------------------------------------------
	
	/**
	 * Apply WT notification call on click on selected links.
	 */
	applyUnload : function() {
		var o = this;
		jQuery( window ).unload( function() {
			gsaWt.i( "Unloading page ..." );
			jQuery( window ).unbind( "unload" );
			o.notify();
		} );
	},
	
	/**
	 * WT notification call.
	 */
	notify : function() {
		if( ! this.results ) {
			gsaWt.w( "No search done so far. Can't call WT notification." );
			return;
		}
		if( typeof( wt ) == "undefined" ) {
			gsaWt.w( "WT is not enabled. Can't call WT notification." );
			return;
		}
		wt.track(
			"DCS.dcsuri", "/utils/search/searchResult_virt.jhtml",
			"WT.ti", "SearchResult",
			"WT.oss", escape( this.phrase ),
			"WT.oss_r", this.count,
			"DCS.ext.searchCategory", escape( this.category )
		);
	}
	
} );


