﻿/*
This file is used specifically for header menu
*/
var hoverBackgroundColor = "F4F7FC";
var hoverTextColor = "CC3333";
var grayBackground = "8A9099";

function fnPreloadImages(imgPath) {
    if (document.images) {
        var pic1 = new Image(1, 1);
        pic1.src = imgPath;
    }
}

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft-1, curtop]; // subtract 1px borderwidth 
}

function fnDisplayMenu(parent, mnuName) {
    var mnuElem = document.getElementById(mnuName);
    mnuElem.style.display = "block";
    var placement = findPos(parent);
    if (mnuElem.id == "mnuMovies") {
        mnuElem.style.left = parseInt(parent.offsetWidth) + parseInt(placement[0]) - parseInt(mnuElem.offsetWidth) + "px";
    }
    else {
        mnuElem.style.left = placement[0] + "px";
    }
    fnHighlightTD(mnuName);
}

function fnHideMenu(mnuName) {
    var mnuElem = document.getElementById(mnuName);
    mnuElem.style.display = "none";
    var allTopMenuLinks = $('.topMnuLink');
    allTopMenuLinks.css('color', '#FFFFFF');
    allTopMenuLinks.css('backgroundColor', '#8A9099');

    allTopMenuLinks = $('.topMnuLink_JPN');
    if (allTopMenuLinks) {
        allTopMenuLinks.css('color', '#FFFFFF');
        allTopMenuLinks.css('backgroundColor', '#8A9099');
    }
}


function fnHighlightTD(mnuName) {
    var elem = document.getElementById(mnuName + "Link");
    if (!elem) { elem = document.getElementById(mnuName + "1Link"); }
    if (!elem) { elem = document.getElementById(mnuName + "Link_JPN"); }
    //elem.style.color = hoverTextColor;
    //elem.style.backgroundColor = hoverBackgroundColor;    
}

function fnRemoveHighlight(mnuName) {
    var elem = document.getElementById(mnuName + "Link");
    if (!elem) { elem = document.getElementById(mnuName + "1Link"); }
    if (!elem) { elem = document.getElementById(mnuName + "Link_JPN"); }
    //elem.style.color = "#FFFFFF";
    //elem.style.backgroundColor = grayBackground;    
}

function fnUnderlineLink(linkElem) {
    linkElem.style.textDecoration = "underline";
    linkElem.style.cursor = "pointer";

}

function fnRemoveUnderline(linkElem) {
    linkElem.style.textDecoration = "none";
    linkElem.style.cursor = "auto";
}

function fnApplyBackgroundImage(linkElem, imgUrl) {
    var newImage = "url(" + imgUrl + ")";
    linkElem.style.backgroundImage = newImage;
}

function fnRemoveBackgroundImage(linkElem) {
    linkElem.style.backgroundImage = "";
    /*window.setTimeout(function (mnuName){
    linkElem.style.backgroundImage = "";
    }, 3);*/
}

function fnGetMenuTopPositionOld() {
    // Code to get the CSToolbar
    var csToolbar = document.getElementById("CSContainer");
    //alert(csToolbar);
    var csToolbarHeight = 0;
    if (csToolbar) {
        if (csToolbar.style.display != "none") {
            csToolbarHeight = csToolbar.offsetHeight
            alert(csToolbarHeight + " Jq " + $(csToolbar).height());
        }
    }
    //var csToolbarHeight = 0;
    var artLogoSpace = document.getElementById("HeaderRight");
    var headerMenuDiv = document.getElementById("HeaderBar");
    var popMenuTop = parseInt(csToolbarHeight) + parseInt(headerMenuDiv.offsetHeight) + parseInt(artLogoSpace.offsetHeight);
    return popMenuTop;
}

function fnNavigate2(url) {
    window.location.href = url;
}


var LEFT_ALIGNMENT = "LEFT";
var RIGHT_ALIGNMENT = "RIGHT";
var SLIDE_DOWN = "SLIDE_DOWN";
var NORMAL_DISPLAY = "NORMAL_DISPLAY";

function fnPositionMenu(elemWidth, divElem, elemStartPos, align, displayType) {
    var menus = $(divElem).find("ul");
    var totalWidth = $(menus[0]).width() + $(menus[1]).width() + $(menus[2]).width(); ;

    var menuPosArray = new Array();
    $(menus[2]).css("border-left", "0px");
    $(menus[0]).css("border-right", "0px");
    if (align == RIGHT_ALIGNMENT) {
        var elemRightPos = parseInt(elemWidth) + parseInt(elemStartPos);
        menuPosArray[0] = elemRightPos - parseInt(totalWidth) + "px";
        menuPosArray[1] = elemRightPos - parseInt(totalWidth) + parseInt($(menus[0]).width()) + "px";
        menuPosArray[2] = elemRightPos - parseInt(totalWidth) + parseInt($(menus[0]).width()) + parseInt($(menus[1]).width()) + "px";        
    }
    else if (align == LEFT_ALIGNMENT) {
        menuPosArray[0] = elemStartPos + "px";
        menuPosArray[1] = elemStartPos + parseInt($(menus[0]).width()) + "px";
        menuPosArray[2] = elemStartPos + parseInt($(menus[0]).width()) + parseInt($(menus[1]).width()) + "px";
    }

    for (pp = 0; pp < 3; pp++) {
        fnShowMenu(menus[pp], menuPosArray[pp], displayType);
    }
}

function fnShowMenu(elem, leftPos, displayType) {
    if (displayType == SLIDE_DOWN) {
        $(elem).css({ top: fnGetMenuTopPosition() + "px", left: leftPos, visibility: "visible", display: "none" }).slideDown(200);
    }
    else if (displayType == NORMAL_DISPLAY) {
        $(elem).css({ top: fnGetMenuTopPosition() + "px", left: leftPos, visibility: "visible", display: "block" });
    }
}

function fnGetMenuTopPosition() {
    var MENU_TOP_POSITION = 89;
    //var MENU_TOP_POSITION = $('.cellmainfpsearch').height();
    var csToolBar = document.getElementById("CSContainer");
    if (csToolBar)
    {
        //MENU_TOP_POSITION = 193;
        MENU_TOP_POSITION = MENU_TOP_POSITION + $('#CSContainer').height();
        if (! $.browser.msie) {
            MENU_TOP_POSITION = MENU_TOP_POSITION + 2;
        }
    }   
    return MENU_TOP_POSITION;
}

function mainmenu() {
    var hflag = false, f = false, a, b = false,
	objHover = {
	    sensitivity: 62,
	    interval: 370,
	    over: function() {
	        if (!hflag) {
	            var mnuElem = $(this).find("ul");
	            var placement = findPos(this);
	            var hyperLinkElem = $(this).find("a");
	            var divElem = $(this).find("div");
	            var leftPos = placement[0] + "px";
	            /*Movies Link OR Framed Art Link*/
	            if (hyperLinkElem[0].id == "mnu101Link") {
	                if (divElem) {
	                    fnPositionMenu(this.offsetWidth, divElem, placement[0] + 1, RIGHT_ALIGNMENT, SLIDE_DOWN); //Adding 1px borderwidth for placement 
	                    hflag = true;
	                }
	            }
	            /*Animals Link*/
	            else if (hyperLinkElem[0].id == "mnu622Link") {
	                if (divElem) {
	                    fnPositionMenu(this.offsetWidth, divElem, placement[0], LEFT_ALIGNMENT, SLIDE_DOWN);
	                    hflag = true;
	                }
	            }
	            else {
	                $(mnuElem).css({ top: fnGetMenuTopPosition() + "px", left: leftPos, visibility: "visible", display: "none" }).slideDown(200,
					function() {
					    hflag = true;
					})
	            }
	        }
	        else {
	            //$(this).find("ul").css({ top: fnGetMenuTopPosition() + "px", left: leftPos, visibility: "visible", display: "block" });
	            var placement = findPos(this);
	            var hyperLinkElem = $(this).find("a");
	            var divElem = $(this).find("div");
	            var leftPos = placement[0] + "px";
	            /*Movies Link Or Framed Art Link*/
	            if (hyperLinkElem[0].id == "mnu101Link") {
	                if (divElem) {
	                    fnPositionMenu(this.offsetWidth, divElem, placement[0] + 1, RIGHT_ALIGNMENT, NORMAL_DISPLAY); //Adding 1px borderwidth for placement 
	                }
	            }
	            /*Animals Link*/
	            else if (hyperLinkElem[0].id == "mnu622Link") {
	                if (divElem) {
	                    fnPositionMenu(this.offsetWidth, divElem, placement[0], LEFT_ALIGNMENT, NORMAL_DISPLAY);
	                }
	            }
	            else {
	                $(this).find("ul").css({ top: fnGetMenuTopPosition() + "px", left: leftPos, visibility: "visible", display: "block" });
	            }
	            hflag = true;
	        }
	        // Highlight the menu
	        var gradientImage = "url(" + $("#bgImagePath").html() + ")";
	        $(this).find("a").css({ "backgroundImage": gradientImage, "background-repeat": "repeat-x", color: hoverTextColor });
	    },
	    timeout: 50,
	    out: function() {
	        $("#navigationMenu li ul").css("display", "none");
	        hflag = true;
	        // Remove the highlight
	        $(this).find("a").css({ "backgroundImage": "", color: "#FFFFFF" });
	    }
	};

	if ($("#navigationMenu ul li.menuItem").length > 0){
		$("#navigationMenu ul li.menuItem").hoverIntent(objHover);
		$("#navigationMenu").hover(function() { }, function() { $("#navigationMenu li ul").fadeOut(100, function() { hflag = false; }) });
	}
	
}

$(document).ready(function() {
    mainmenu();
    if ($('#isMLSDomain').length > 0) {
        if ($('#isMLSDomain').html() == "true") {
            fnHandleLanguagesDropDown();
        }
    }

    $('.sub_popup_childClass').hover(
    function() {
        $(this).css('background-color', '#ccd0db');
        $(this).css('color', '#cc3333');
        //$(this).css('font-weight', 'bold');
    },
    function() {
        $(this).css('background-color', '');
        $(this).css('color', '#666666');
        //$(this).css('font-weight', 'normal');
    });
});
 


/* Search Suggest related JS */

/* AutoSuggest Javascript start here ... */

var htmlString;
var autoSuggestDiv = null;
var lastSearchTerm = null;
var userSearchTerm=null;

/* This variable says how many letter search you want.. */
var howmanycharsearch = 3;

/* This variable says about the with image or without image */
var isImage = 1;

$(document).ready(function() {

AutoSuggest(document.getElementById("txtSearch"), '#txtSearch');
          
    $('body').click(function() {
             $('.searchSuggest').hide();
         });
   
    $('.ssgItem').live('click', function() {
        goToCategory($(this));
    });
   
    $('#txtSearch').keydown(function(event) {
        if (event.keyCode == '13') {
           if ($('.ssg_Selected').length){  
           event.preventDefault();
           goToCategory($('.ssg_Selected'));
          } 
      }
    });
    $('#txtSearch').live('mouseover', function() {
    if ($('#txtSearch').val().length > 0 && userSearchTerm != null) {
            $('#txtSearch').val(userSearchTerm);
        }
    });
});

//Prabhu Added for IE flickering issue
$('.ssgItem').live('mouseover', function() {
    applyItemStyle($(this), 'over', 'focus');
});
$('.ssgItem').live('mouseout', function() {
    applyItemStyle($(this), 'out', 'nofocus');
});

function fnMouseOver(item) {
    var $item = $(item);
    applyItemStyle($item, 'over', 'focus');
}
function fnMouseOut(item) {
    var $item = $(item);
    applyItemStyle($item, 'out', 'nofocus');
}

function AutoSuggest(elem,jqueryobj) {
    var me = this;
    this.elem = elem;
    var oElem = elem;
    this.div = null;
    this.highlighted = -1;
    var searchBox = $(jqueryobj);
    var KEYUP = 38;
    var KEYDN = 40;
    var ESC = 27;
    var BACKSPACE = 8;

    elem.setAttribute("autocomplete", "off");

    elem.onkeydown = function(ev) {
        var key = me.getKeyCode(ev);
        switch (key) {
            case KEYUP:
                if (me.highlighted > 0) {
                    me.highlighted--;
                }
                else {
                    $('#txtSearch').val(userSearchTerm);
                    me.highlighted = -1;
                }
                if ($('.searchSuggest').css('display') != 'none') {
                    me.changeHighlight(key);
                }
                break;

            case KEYDN:
                if (me.highlighted < $('.ssg_results .ssgItem').length - 1) {
                    me.highlighted++;
                }
                if ($('.searchSuggest').css('display') != 'none') {
                    me.changeHighlight(key);
                }
                break;
        }
    };

    
    elem.onkeyup = function(ev) {
        
        var key = me.getKeyCode(ev);
        switch (key) {
            case ESC:
                me.hideDiv();
                break;
            case 40:
                break;
            case 38:
                break;
            case 37:
                break;
            case 39:
                break;
            case 13:
                break;  
            default:
                if (this.value.length > 0) {
                    me.createDiv();
                }
                else {
                    $('.searchSuggest').hide();
                    me.hideDiv();
                }
                break;
        }
    };

    
    this.createDiv = function() {
    
        var url = "/ADC.NET/root/Pages/Gallery/SearchSuggest.aspx?SearchValue=";
        var inputLength = oElem.value.length;
        userInput = oElem.value;
        var isItNumeric=false;
        var MakeAjaxCall=false;  

        if (inputLength < 0) {
            this.highlighted = -1;
            $('.searchSuggest').hide();
            me.hideDiv();
            return;
        }

        if (inputLength > 0) {
            
            this.highlighted = -1;
            userInput=RemoveNonAlphaNumericTerms(userInput);
            inputLength=userInput.length;
            var customerZoneIDandLanguageID= "&CustomerZoneID=" + GetCustomerZoneID() + "&langIso=" + GetLangCode() + "&endecagroup=2";  
            var searchSuggestURL = url + userInput + customerZoneIDandLanguageID;
            isItNumeric= isnumericSS(userInput);
            MakeAjaxCall=MakeAjaxCallToEndeca();
             if (howmanycharsearch > 1) {
                if (autoSuggestDiv == null || autoSuggestDiv == " ") {
                    if (inputLength == howmanycharsearch || inputLength > howmanycharsearch + 1 && ajaxCallNotMade(userInput)) {
                         userSearchTerm=userInput;
                        if(!isItNumeric && MakeAjaxCall)
                        {
                        me.callAjaxAutoSuggest(searchSuggestURL);
                        lastSearchTerm = userInput.substring(0, howmanycharsearch);
                        }
                    }
                }
                else {
                    if (inputLength == howmanycharsearch && (!isItNumeric) && MakeAjaxCall ) {
                         userSearchTerm=userInput;
                        me.callAjaxAutoSuggest(searchSuggestURL);
                        me.refineResults(userInput);
                    }
                }
                if (inputLength < howmanycharsearch) {
                    $('.searchSuggest').hide();
                    me.hideDiv();
                }
                if (inputLength > howmanycharsearch && (!isItNumeric) && MakeAjaxCall) {
                   // me.refineResults(userInput);
                   userSearchTerm=userInput; 
                   me.callAjaxAutoSuggest(searchSuggestURL); 
                }
            }
            else {
                if(!isItNumeric && MakeAjaxCall)
                {  
                  userSearchTerm=userInput;
                 me.callAjaxAutoSuggest(searchSuggestURL);
                } 
            }
        }
    }
   
    this.hideDiv = function() {
        if (autoSuggestDiv) {
            document.body.removeChild(autoSuggestDiv);
            autoSuggestDiv = null;
            this.highlighted = -1;
        }
    }

    this.changeHighlight = function(key) {
        $('.ssg_results').find('.ssgItem').each(function(i) {
            if (i == highlighted) {
                applyItemStyle($(this), 'over', 'focus');
            }
            else {
                if ($(this).hasClass('ssg_Selected'))
                    applyItemStyle($(this), 'out', 'nofocus');
            }
            if (key == 13) {
                goToCategory($(this));
            }
        });

    };
    this.callAjaxAutoSuggest = function(searchSuggestURL) {
        makeAjaxCallToTealeafSS('Request');
        $.ajax({
            url: searchSuggestURL,
            processData: false,
            cache: false,
            type: "GET",
            beforeSend: function() {
                //return validateLogin($loginContainer);
            },
            success: function(callback) {
                var flag = me.callBackAutoSuggest(callback);

                //var currentSearchValue = '=' + $("#txtSearch").val() + '$';
                //if (searchSuggestURL.match(currentSearchValue) && flag && searchBox.val().length > 0) {
                if (flag && searchBox.val().length > 0) {
                      makeAjaxCallToTealeafSS('Results');
                    if (autoSuggestDiv == null || autoSuggestDiv == " ") {
                        autoSuggestDiv = me.fnDOMElementCreate("DIV", "autoSuggestDiv", "searchSuggest");
                        this.div = autoSuggestDiv;
                        autoSuggestDiv.innerHTML = htmlString;
                        document.body.appendChild(autoSuggestDiv);
                    }
                    else {
                        autoSuggestDiv.innerHTML = htmlString;
                    }
                    var temp = me.fnDOMElementCreate("UL", "ssgmani", "ssgmani");
                    temp.innerHTML = $(".ssg_results").html();
                    autoSuggestDiv.appendChild(temp);
                    $(".ssgmani").hide();

                    
                    var oText = document.getElementById("txtSearch");

                    if (oText.value.length > howmanycharsearch) {
                        if ($(".ssgmani").length < 0) {
                            me.callAjaxAutoSuggest(searchSuggestURL);
                        }
                      //  me.refineResults(oText.value);
                    }
                    if (htmlString.length > 1) {
                        me.positionSearchSuggest();
                        if ($('.ssg_results').html().length > 0) {
                            $('.searchSuggest').slideDown('normal');
                        }
                    }
                }
            },
            error: function(textStatus, errorThrown) {
                //alert('error');
            }
        });
    }
    this.callBackAutoSuggest = function(data) {
        htmlString = '';
        var temp = data;
        var startpos = temp.indexOf("<div", 0);
        var endpos = temp.indexOf(">", startpos);
        var startpos = temp.indexOf("</div>", endpos);
        var tempstr = temp.substring(startpos, endpos + 1);
        var itemPosition=0; // to implement.. CR
        var tempboldcnt = searchBox.val().length;
        if (tempstr != "nodata") {
             
            var jsonobj = eval('(' + tempstr + ')');
            htmlString = '<ul class="ssg_results">';
            for (var i = 0; i < jsonobj.searchsuggest.items.item.length; i++) {
                if (jsonobj.searchsuggest.items.item[i].itemcount != '0') {
                     //htmlString += '<li class="ssgItem ssgCurrent" onmouseover="fnMouseOver(this);" onmouseout="fnMouseOut(this);">';
                    //Prabhu Added for IE flickering issue
                    htmlString += '<li class="ssgItem ssgCurrent">'; 
                    if (isImage == 1)
                        htmlString += '<div class="ssg_galleryThumb floatLeft" style="background-image:url(' + jsonobj.searchsuggest.items.item[i].imagelocation + ');"></div>';
                    htmlString += '<div class="ssgTextBox floatLeft">';
                    
                    var temp = jsonobj.searchsuggest.items.item[i].categoryname;
                    var result=GetHighlightedString(temp,searchBox.val());                      
                    htmlString += '<div class="ssg_heading_hide" n="' + temp + '">' + result + '</div>';
                    result = autoEllipseText(temp, '35');
                    var result = GetHighlightedString(result, searchBox.val());   
                    htmlString += '<div class="ssg_heading" n="' + temp + '">' + result+ '</div>';

                    var txt = jsonobj.searchsuggest.items.item[i].itemcount;
                    var itemCount = txt;
                    var re1 = '.*?';
                    var re2 = '(\\d+)';

                    var p = new RegExp(re1 + re2, ["i"]);
                    var m = p.exec(txt);
                    if (m != null) {
                        var int1 = m[1];
                        itemCount = int1;
                    }
                    if (itemCount == '1')
                        itemCount = me.insertCommaToCategoryCount(itemCount) + ' item';
                    else
                        itemCount = me.insertCommaToCategoryCount(itemCount) + ' items';

                    htmlString += '<div class="ssg_byline">' + itemCount + '</div>';

                  //  var sessionID = me.getCookie('CustSessionID');
                 //   var ui = "?ui=" + sessionID;
                    var t = jsonobj.searchsuggest.items.item[i].galleryurl;
                    var t1 = t.indexOf('?', 0);
                    //var gUrl = t.substring(0, t1) + ui;
                    var gUrl = t.substring(0, t1);
                    htmlString += '<div class="ssgURL">' + gUrl + '</div>';
                    htmlString += '</div><div class="clear" style="font-size:0;line-height:0;margin:0;font-weight:normal;height:0px;"></div></li>';
                }
            }
            htmlString += '</ul>';
            return true;
        }
        else {
            $('.searchSuggest').hide();
            me.hideDiv();
        }
    }
    this.getKeyCode = function(ev) {
        if (ev)			//Moz
        {
            return ev.keyCode;
        }
        if (window.event)	//IE
        {
            return window.event.keyCode;
        }
    };
    this.getEventSource = function(ev) {
        if (ev)			//Moz
        {
            return ev.target;
        }

        if (window.event)	//IE
        {
            return window.event.srcElement;
        }
    };
    this.cancelEvent = function(ev) {
        if (ev)			//Moz
        {
            ev.preventDefault();
            ev.stopPropagation();
        }
        if (window.event)	//IE
        {
            window.event.returnValue = false;
        }
    }
    this.fnDOMElementCreate = function(tag, id, classname) {
        var robj = null;
        robj = document.createElement(tag);
        if (id)
            robj.id = id;
        if (classname)
            robj.className = classname;
        return robj;
    }
    this.insertCommaToCategoryCount = function(number) {
        number = '' + number;
        if (number.length > 3) {
            var mod = number.length % 3;
            var output = (mod > 0 ? (number.substring(0, mod)) : '');
            for (i = 0; i < Math.floor(number.length / 3); i++) {
                if ((mod == 0) && (i == 0))
                    output += number.substring(mod + 3 * i, mod + 3 * i + 3);
                else
                    output += ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
            }
            return (output);
        }
        else {
            return number;
        }
    }
    this.positionSearchSuggest = function() {
        searchBoxOffset = $('#txtSearch').offset();
        searchBoxTopPos = searchBoxOffset.top;
        searchBoxLeftPos = searchBoxOffset.left;
        searchBoxHeight = $('#txtSearch').height();
        searchSuggestTopPos = searchBoxTopPos + searchBoxHeight + 14;
        searchSuggestLeftPos = searchBoxLeftPos-8;
        $('.searchSuggest').css('top', searchSuggestTopPos - 3);
     	$('.searchSuggest').css('left', searchSuggestLeftPos);
    }
    this.getCookie = function(c_name) {
        if (document.cookie.length > 0) {
            c_start = document.cookie.indexOf(c_name + "=");
            if (c_start != -1) {
                c_start = c_start + c_name.length + 1;
                c_end = document.cookie.indexOf(";", c_start);
                if (c_end == -1) c_end = document.cookie.length;
                return unescape(document.cookie.substring(c_start, c_end));
            }
        }
        return "";
    }
    this.refineResults = function(userInput) {
        if ($('.ssgmani').length > 0) {
            $('.ssg_results').empty();
            $('.ssgmani').find('.ssg_heading').each(function() {
                var headingText = $(this).attr("n");
                if (headingText.toLowerCase().indexOf(userInput.toLowerCase()) > 0) {
                    $(this).parents('li').addClass('ssgCurrent');
                    $(this).parents('li').show();
                    
                    var boldText = headingText;
                    var boldHtml = "<b>" + boldText.substring(0, userInput.length) + "</b>" + boldText.substring(userInput.length, boldText.length);
                    var result=GetHighlightedString(boldText,userInput);  
                    //end                     
                   
                    $(this).html(result);
                    var temp = "<li class='ssgItem ssgCurrent'>" + $(this).parents('li').html() + "</li>";
                    $('.ssg_results').append(temp);
                }
                else {
                    $(this).parents('li').removeClass('ssgCurrent');
                    $(this).parents('li').fadeOut('fast');
                }
            });
            if ($('.ssg_results').html().length > 0) {
                $('.searchSuggest').slideDown('normal');
            }
        }
    }
}

function applyItemStyle($item, action, param) {
    if (action == 'over') {
        $item.addClass('ssg_Selected');
        $item.find('.ssg_byline').addClass('ssgTextHover');
        $item.find('.ssgTextBox').addClass('ssgTextBoxSelected');
        $item.find('.ssg_heading').hide();
        $item.find('.ssg_heading_hide').show();
        if (param == 'focus')
            $('#txtSearch').val($item.find('.ssg_heading_hide').attr('n'));

    }
    else if (action == 'out') {
        $item.removeClass('ssg_Selected');
        $item.find('.ssg_byline').removeClass('ssgTextHover');
        $item.find('.ssgTextBox').removeClass('ssgTextBoxSelected');
        if (document.all) {
            $item.find('.ssg_heading').css('width', '203px');
            $item.find('.ssg_heading_hide').css('width', '203px');
        }
        else {
            $item.find('.ssg_heading').css('width', '206px');
            $item.find('.ssg_heading_hide').css('width', '206px');
        }
        $item.find('.ssg_heading').show();
        $item.find('.ssg_heading_hide').hide();
    }
}


function goToCategory($item) {
    theCategory = $item.find('.ssg_heading').text();
    theURL = $item.find('.ssgURL').text();
    $('#txtSearch').val(theCategory);
    $('.searchSuggest').slideUp('normal', function() {
        location.href = theURL;
    });
    makeAjaxCallToTealeafSS(theCategory);
    makeAjaxCallToEndecaLogging(); 
}

function autoEllipseText(text, limit) {

    var temp;
    if (text.length - 1 > limit) {
        temp = text.substring(0, limit-3) + '...';
    }
    else {
        temp = text;
    }
    return temp;
}

function ajaxCallNotMade(userInput) {
    retval = false;
    var currentInput = userInput.substring(0, howmanycharsearch);
    if (lastSearchTerm != currentInput) {
        retval = true;
    }
    return retval;
}

function GetHighlightedString(categoryname,userinput) {
        userinput=TrimSS(userinput);
        var tempToSearch=categoryname.toLowerCase(); // to take care of case issue.
        var mysearchTemp=userinput;
        var mytemppos = tempToSearch.indexOf(mysearchTemp.toLowerCase());
        var finalstring=categoryname;
        var str1,str2,str3;
        str1=tempToSearch.substring(0,mytemppos);
               
        str2=tempToSearch.substring(mytemppos, mytemppos+userinput.length);
                
       if(userinput.toLowerCase()==str2) 
       {
            finalstring=str1 + '<b>';
            finalstring=finalstring+str2+'</b>';
       }
       else
       {
         finalstring= str1+str2;
       } 
 
        str3=tempToSearch.substring(mytemppos+userinput.length);
        finalstring=finalstring+str3;
        return finalstring;
  } 
 
function LTrimSS( str ) {
    var whitespace = new String(" \t\n\r");
    var s = new String(str);
    if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
        j++;
        s = s.substring(j, i);
    }
    return s;
}
function RTrimSS( str ) {
  var whitespace = new String(" \t\n\r");
  var s = new String(str);
  if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
    var i = s.length - 1;       // Get length of string
    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
      i--;
      s = s.substring(0, i+1);
  }
  return s;
}
function TrimSS( str ) {
  return RTrimSS( LTrimSS( str ) );
} 

// Added to track search suggest in Tealeaf
function makeAjaxCallToTealeafSS(dataToSend){
		if(dataToSend=="Request")
		{
		dataToSend=dataToSend + "=SearchRequest";
		}
		
		else if(dataToSend=="Results")
		{
		dataToSend=dataToSend + "=SearchResults";
		}
		else
		{
		dataToSend="Action=SearchSuggestClicked" + dataToSend ;
		}
		
		$.ajax({
        url: '/ADC.NET/Root/Pages/Checkout/tealeaf.aspx',
        processData: false,
        data: dataToSend,
        type: "GET"
    });
}

function RemoveNonAlphaNumericTerms(SearchTerm)
{
SearchTerm = SearchTerm.replace(/[^a-zA-Z 0-9]+/g,'');
return SearchTerm;
}

function isnumericSS( theVal )
{
  var numericExpression = /^[0-9]+$/;
  if(theVal.match(numericExpression))
   return true;
  else 
   return false;
}

function MakeAjaxCallToEndeca()
{
    var lang = GetCookieDictionary('ap','langIso');
    if(lang.length > 0)
    {
        lang=lang.toLowerCase(); 
    }   
    if(lang=='en' || lang=='fr' || lang=='de')
    {
        return true; 
    }
   else
        return false;    
}

function setUserSearchString($item)
 { 
  var itemPosition=$item.find('.itemPosition').text();             
  if(itemPosition=="1")
   {      
   $('#txtSearch').val(userSearchTerm);
   }     
 }

function makeAjaxCallToEndecaLogging(){		
		var sessionID = getCookie('CID');
		var customerZoneIDandLanguageID= "&CustomerZoneID=" + GetCustomerZoneID() + "&langIso=" + GetLangCode() + "&endecagroup=2"; 
		dataToSend="SearchStringForLog=" + userSearchTerm + "&UI=" + sessionID + customerZoneIDandLanguageID;				
		$.ajax({
        url: '/ADC.NET/root/Pages/Gallery/SearchSuggest.aspx',
        processData: false,
        data: dataToSend,
        type: "GET"
    });
}

function GetCustomerZoneID()
{
 var customerZoneID="1"; // default it to one. 
 customerZoneID=GetCookieDictionary('apc','CustomerZoneID');// this is retrived from cookie 
 return customerZoneID;         
}

function GetLangCode()
{
 var LanguageCode="en"; // default it to three 
 LanguageCode=GetCookieDictionary('ap','langIso');// this is retrived from cookie
 return LanguageCode;         
}  

/* End of Search Suggest related JS */

