﻿function searchresultPopup(data) {
    if (data.length >= 2) {
        abortAjaxCall(_callbackHandleSearchPopup);  // vorige request discarden

        _perf = new Date().getTime();
        _callbackHandleSearchPopup = ShopWebService._staticInstance.SearchPopupResult(sid, data, SearchPopupResult_callback, SearchPopupResult_failed, data);
        //_callbackHandleSearchPopup = ShopWebService._staticInstance.SearchPopupResultv2(sid, data, SearchPopupResult_callback, SearchPopupResult_failed, data);
    } else {
        resetSearchPopupView();
    }
}

var SearchDivZindex = '';

function resetSearchPopupView() {
    if (_btnIsSearchPopupVisible) {
        if (SearchDivZindex != '') {
            $get('divFade').style.zIndex = SearchDivZindex;
            SearchDivZindex = '';
        }
        fadeHide()
        $get('divSearchResultPopup').style.display='none';
        //$get('divSearchArea').style.background="url('images/main/SearchBackground_Plain.png')";
        $get('txtSearch').style.backgroundColor='white';
        $get('divSearchArea').style.backgroundRepeat='no-repeat';
        _btnIsSearchPopupVisible=false;
    }
}

function closeSearchResultPopup() {
    resetSearchPopupView();
    if (_pageid=='cart') {
        window.location.href=window.location.href;
    } else {
        //ShopWebService.LogSearch(sid, _strPopupCurrentSearch, _strPopupCurrentSearchHits, 0, LogSearch_callback, generic_callbackerror);
    }
}

function LogSearch_callback(result) {
    //
}

function SearchPopupResult_failed(err) {
    //
}

function SearchPopupResult_callback(result, search) {
    var strHTML='';
    var blnIsOdd=true;
    var strTRStyle='';
    var intHashSize=0;
    var blnShowCountOnly=false;

    var start = new Date().getTime();
    
    _strPopupCurrentSearch = search;
    _strPopupCurrentSearchHits = intHashSize;

    if (result==null) {
        resetSearchPopupView();
    } else {
    
        if (result.error!='') {
            //alert(result.error);
            return;
        }
    
        intHashSize = result.rows;
    
        strHTML += '<div style="background-color:white;padding:0px;"><table cellspacing="0" cellpadding="0" style="margin-left:8px;margin-right:8px;width:584px;background-color:white;" border="0">';

        if (result.showdetail==true) {
            blnShowCountOnly = false;
            if (intHashSize > 8) {
                intHashSize=8;  //clip op 8 artikelen
            }
        } else {
            blnShowCountOnly = true;
        }

        if (intHashSize==0) {
            strHTML += '<tr><td>' + result.nodatatext + '</td></tr>';
            $get('divMoreResults').style.display='none';
        } else {
            $get('spnMoreResults').innerHTML=result.rows.toString();
            $get('divMoreResults').style.display='block';
            if (blnShowCountOnly) {
                strHTML += '<tr><td>' + result.detailtext + '</td></tr>';
            } else {
                for (i=0;i<intHashSize;i++) {
                    if (blnIsOdd) {
                        strTRStyle = 'background-color:#ffffff;';
                    } else {
                        strTRStyle = 'background-color:#f0f0f0;';
                    }

                    if (i<1000) {
                        strHTML += '<tr style="';
                        strHTML += strTRStyle;
                        strHTML += '"><td style="width:80px;height:80px;text-align:left;border-right:1px solid #d0d0d0;background-color:#ffffff;">';
                        strHTML += '<img style="border:1px solid white;cursor:pointer;" id="cardimgpopup_' + CWEncode(result.data[i].Artikelcode);
                        strHTML += '" src="';
                        strHTML += _thumbnailStub;
                        strHTML += result.data[i].ImageName;
                        strHTML += '.jpg" '
                        strHTML += 'onclick="return openBigImage(' + "'" + CWEncode(result.data[i].Artikelcode) + "'" + ');" ';
                        strHTML += '/></td>';
                        strHTML += '<td style="width:504px;cursor:pointer;vertical-align:top;padding-left:4px;border-right:1px solid #d0d0d0;padding-top:8px;';
                        if (i==0) {
                            strHTML += 'border-top:1px solid #d0d0d0;';
                        }
                        if (i==intHashSize-1) {
                            strHTML += 'border-bottom:1px solid #d0d0d0;';
                        }
                        strHTML += '" ';
                        strHTML += 'onmouseover="return hilightPopupRow(this,event);" ';
                        strHTML += 'onmouseout="return restorePopupRow(this,event);" ';
                        //                        strHTML += 'onclick="return clickPopupRow(this,event,' + "'" + result.data[i].Artikelcode + "'" + ');" ';
                        
                        //strHTML += 'onclick=\'return clickPopupRow(this,event,\\\"' + CWEncode(result.data[i].Artikelcode) + '\\\",\\\"' + CWEscape(result.data[i].seo_url) + '\\\");\' ';
                        strHTML += 'artcode="' + CWEncode(result.data[i].Artikelcode) + '" ';
                        strHTML += 'seourl="' + CWEscape(result.data[i].seo_url) + '" ';
                        strHTML += 'onclick="return clickPopupRow(this,event)" ';
                        strHTML += '>';
                        strHTML += '<div style="position:absolute;margin-left:0px;margin-top:42px;">';
                        strHTML += '<table cellspacing="0" cellpadding="0"><tr>';
                        strHTML += '<td style="width:10px;">&nbsp;</td>';
                        
                        if (_showCartButtonAndQTY==false) {
                            //
                        } else {
                            if ((result.data[i].UwPrijs==0) && (result.data[i]['Price0Label'] && result.data[i].Price0Label != '')) {
                                strHTML += '<td colspan="3" style="width:400px;text-align:right;">' + result.data[i].Price0Label + '</td>';
                            } else {
                                strHTML += '<td style="width:390px;text-align:right;padding-right:4px;">';
                                strHTML += '<span style="font-weight:bold;color:#404040;margin-right:4px">&euro; ' + result.data[i].DisplayPrijs + '</span>';
                                strHTML += '</td>';
                                strHTML += '<td><input type="text" id="txtAantalPopup_' + CWEncode(result.data[i].Artikelcode) + '" style="width:50px;text-align:right;marging-right:6px;" value="' + result.data[i].MinBestelEenheid + '" /></td>';
                                strHTML += '<td>';
                                if (result.data[i].MaxRight>1) {
                                    strHTML += '<img src="skins/' + _shopid + '/icons/Small/icon_cart.png" onclick="return addtocartfromsearchpopup_wrapper(\'' + CWEncode(result.data[i].Artikelcode) + '\',this,0,true,\'txtAantalPopup_\',' + result.data[i].MinBestelEenheid + ');" />'
                                } else {
                                    strHTML += '<img src="skins/' + _shopid + '/icons/Small/icon_cart_disabled.png" />'
                                }
                                strHTML += '</td>';
                            }                
                        }

                        strHTML += '</tr></table>';
                        strHTML += '</div>';

                        if (result.data[i].VoorraadIndicatie != -1 && result.data[i].HasVariants == 0) {
                            strHTML += '<div style="text-align:right;width:490px;"><img style="border:1px solid #c0c0c0;" src="/skins/' + _shopid + '/stock/stock_' + result.data[i].VoorraadIndicatie + '.png" /></div>';
                        } else {
                            strHTML += '<div style="text-align:right;width:490px;">&nbsp;</div>';
                        }

                        strHTML += '<span style="font-weight:bold;color:#404040;">' + CWHtmlEncode(result.data[i].Artikelcode);
                        if (result.data[i].ArtikelcodeKlant!='') {
                            strHTML += '/' + result.data[i].ArtikelcodeKlant;
                        }
                        strHTML += ' - ' + result.data[i].Description + '</span><br>';
                        if (result.data[i].ShortDescription==null || !_blnShowBigDescriptionsInSearchPopup) {
                            strHTML += '<span style="color:#808080;font-weight:7px;">&nbsp;</span>';
                        } else {
                            strHTML += '<span style="color:#808080;font-size:7pt;font-weight:7px;">' + result.data[i].ShortDescription + '</span>';
                        }
                        strHTML += '</td>';
                        strHTML += '</tr>';
                    } else {
                        strHTML += '<tr><td colspan="2">';
                        strHTML += result.data[i].Description;
                        strHTML += '</td><tr>';
                    }
                    
                    blnIsOdd = !blnIsOdd;
                }
            }
        }

        strHTML += '</table></div>';
        
        $get('divSearchResultContent').innerHTML=strHTML;
        $get('divSearchResultPopup').style.display='block';
        $get('divSearchArea').style.background = '';
        
        if (SearchDivZindex = '')
            SearchDivZindex = $get('divFade').style.zIndex;
        fadeShow();
        $get('divFade').style.zIndex = '8100';

        _btnIsSearchPopupVisible=true;
    }
    
    var diff = new Date().getTime() - start;
    var fulldiff = new Date().getTime() - _perf;
    
    //window.status = 'Total front-to-back:' + fulldiff + 'ms, CLIENT:' + diff + 'ms, SERVER:' + result.took + 'ms (step1:' + result.step1 + ', step2:' + result.step2 + ', step3:' + result.step3 + ')';
}

function hilightPopupRow(obj, evt) {
    _strPreviousPopupColor=obj.style.backgroundColor;
    obj.style.backgroundColor='#ffe0a0';
    return false;
}

function restorePopupRow(obj, evt) {
    obj.style.backgroundColor=_strPreviousPopupColor;
    return false;
}


function clickPopupRow(obj, evt) {
    var tag;
    if (evt.target) {
        tag=evt.target.tagName;
    }
    if (evt.srcElement) {
        tag=evt.srcElement.tagName;
    }
    
    if (tag=='INPUT' || tag=='IMG') {
        return false;
    }

    // Log het doorklikken naar detail (id=1)
    ShopWebService.LogSearch(sid, _strPopupCurrentSearch, 1, 1, clickPopupRow_callback, generic_callbackerror, obj);
}

function clickPopupRow_callback(result, obj) {
    return showdetail(obj.attributes["artcode"].value, obj.attributes["seourl"].value);
}

