(function(){
    $.fn.setContact = function() {
        return this.each(function(){
            em = $(this).text().replace(/\*/g,'_').replace(/\/-\//,'@').replace(/\//g,'.');
            $(this).html('<a href="mailto:'+em+'">'+em+'</a>');
        });
    }
})( jQuery );

function storesNext() {
    $("a.storesnext").click(function() {
        $(this).parent().parent().hide();
        $(this).parent().parent().next().show();
        return false;
    });
}


function storesPrev() {
    $("a.storesprev").click(function() {
        $(this).parent().parent().hide();
        $(this).parent().parent().prev().show();
        return false;
    });
}

function initStores() {
    $("#storesresult").hide();

    $("a.stores").click(function() {
        storeSource = $(this).attr('href').replace(/#/,'#st');

        $("#storesresultlist").html($(storeSource).html());
        $("#storesresult").show();
        return false;
    });
    $('dd.contact').setContact();
    

    var previewCpt = 0;
    var previewCurrent = -1;
    var storesCurrent = '';

    $("#storesnewsearch").click(function() {
        $("#storessearch").attr('value','').data('current','');
        $("#storesresultlist").html('');
        $("#storespreview").html('');
        $("#storespreviewcount").html('');
        $("#storesresult").hide();
        return false;
    });

    $("#storessearch").attr('data-current','').click(function() {
        if ($(this).data('current')!='')
            $(this).attr('value',$(this).data('current')).data('current','');
    }).keyup(function(event) {
        if (event.keyCode==13) {
            str = $(this).val();
            if (str.length>2) {
 
 
                $.post('/stores.php', { // Added Slash
                    citystore: str,
                    vast: 1
                }, function(results) {
                    $("#storesresultlist").html(results);
                    $("#storesresult").show();
                    $("#storespreview").empty().hide();
                    $("#storessearch").attr('value', str);

                    $("a.storesnext").click(function() {
                        $(this).parent().parent().parent().parent().parent().hide();
                        $(this).parent().parent().parent().parent().parent().next().show();
                        return false;
                    });
                    $("a.storesprev").click(function() {
                        $(this).parent().parent().parent().parent().parent().hide();
                        $(this).parent().parent().parent().parent().parent().prev().show();
                        return false;
                    });
                });
 
 
            }
           
        } else if (event.keyCode==38) {
            if (previewCurrent>-1) {
                previewCurrent--;
                $("#storespreview div").css('background-color','#ffffff');
                if (previewCurrent==-1) {
                    $(this).attr('value',$(this).data('current')).data('current','');
                } else {
                    $("#storespreview div").slice(previewCurrent).first().css('background-color','#ffff00');
                    previewVal = $("#storespreview div").slice(previewCurrent).first().text().replace(/,.*/,'');
                    $("#storessearch").attr('value',previewVal);
                }

            }
        } else if (event.keyCode==40) {
            if (previewCurrent<previewCpt) {
                if ($(this).data('current')=='') {
                    $("#storessearch").data('current',$(this).val());
                }
                previewCurrent++;
                $("#storespreview div").css('background-color','#ffffff');
                $("#storespreview div").slice(previewCurrent).first().css('background-color','#ffff00');
                previewVal = $("#storespreview div").slice(previewCurrent).first().text().replace(/,.*/,'');
                $("#storessearch").attr('value',previewVal);
            }
        } else {
            str = $(this).val();
            if (str != storesCurrent) {
                storesCurrent = str;
                eval('rx=/^'+str+'/i');
                cpt=0;
                out = '';
                $("#storespreview").html('');
                $("#storespreviewcount").html('');
                if (str.length>0) {
                    $.post('/stores.php', { // Added slash
                        keystore: str
                    }, function(data) {
                        //console.log(data);
                        $("#storespreview").show();
                        $("#storespreview").html(data);
                        $("#storespreview div").css('cursor','pointer').click(function() { // Removed 'b' selector
                            citysel = $(this).find('b').text(); // Added 'b' selector
//							alert("city: " + citysel);
                            $.post('/stores.php', { // Added slash
                                citystore: $(this).find('b').text() // Added 'b' selector
                            }, function(results) {
                                $("#storesresultlist").html(results);
                                $("#storesresult").show();
                                $("#storespreview").empty().hide();
                                $("#storessearch").attr('value', citysel);

                                $("a.storesnext").click(function() {
                                    $(this).parent().parent().parent().parent().parent().hide();
                                    $(this).parent().parent().parent().parent().parent().next().show();
                                    return false;
                                });
                                $("a.storesprev").click(function() {
                                    $(this).parent().parent().parent().parent().parent().hide();
                                    $(this).parent().parent().parent().parent().parent().prev().show();
                                    return false;
                                });
                            });
                        });

                    });


                /*
                    results = jQuery.grep(storesData,function(a) {
                        if (a.search(rx)>=0) {
                            return true;
                        }
                        return false;
                    });
                    jQuery.each(results,function(i,a) {
                        if (cpt<4) {
                            out = out + '<div>'+a.replace(rx,'<b>'+str+'</b>')+', <span>City</span></div>';
                            cpt++;
                        }
                    });
                    $("#storespreview").html(out);
                    $("#storespreview").mouseleave(function(){
                        $("#storespreview div").css('background-color','#ffffff');
                    });
                    $("#storespreview div").hover(function() {
                        $("#storespreview div").css('background-color','#ffffff');
                        $(this).css('background-color','#ffff00');
                    }).click(function() {
                        $("#storesresultlist").html($("#datastores").html());
                        $("#storesresult").show();
                    });
                    $("#storespreviewcount").html(results.length+' result'+(results.length>1?'s':''));
                    previewCpt = cpt-1;
                    previewCurrent = -1;
                    */
                }
            }
        }
    });
}
