﻿function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}


var submitted = false;
function doSubmit(searchItem) {
    if (submitted == false) {
        submitted = true;
        if (searchItem == "")
            document.location.href = "/";
        else
            document.location.href = "/" + searchItem.replace(/ /g, "_").replace(/&/, "_ampersand_") + "?action=search";
    }
}


//reduce font size to fit in container
// note: div width and height must be set; set span font-size; for ie: set div overflow to hidden;
function reduceFontSize(divID, spanID) {
    var i = 1
    while ((document.getElementById(spanID).offsetWidth > document.getElementById(divID).offsetWidth || document.getElementById(spanID).offsetHeight > document.getElementById(divID).offsetHeight) && i < 50) {
        i++;
        //alert(document.getElementById(spanID).offsetWidth);
        //alert(document.getElementById(spanID).style.fontSize);
        var fontSize = document.getElementById(spanID).style.fontSize.replace("px", "");
        fontSize--;
        if (fontSize > 50)
            fontSize--;
        document.getElementById(spanID).style.fontSize = fontSize + "px";
        //alert(document.getElementById("test").style.fontSize);
        //alert(document.getElementById("test").offsetWidth);
    }
}

var distanceScalar; // bizlist slider


// ON READY
$(document).ready(function() {
    // bizlist slider
    if (document.getElementById('listSlider'))
        $("#listSlider").slider({ max: 100,
            min: 0,
            value: distanceScalar,
            change: function(event, ui) {
                submitDistanceScalar(ui.value);
            }

        });


    window.onunload = function() {
        submitted = false;
    }

});





// submit new distanceScalar
function submitDistanceScalar(val) {
    // submit new distanceScalar
    var href = currentURL;
    if (href.indexOf("distanceScalar=") > 0) {

        var pos = href.indexOf("&", href.indexOf("distanceScalar="))
        if (pos == -1)
            href = href.substring(0, href.indexOf("distanceScalar=") - 1)
        else
            href = href.substring(0, href.indexOf("distanceScalar=") - 1) + href.substring(pos, href.length);
        //alert(href);

    }
    if (href.indexOf("?") == -1) {
        document.location.href = href + "?distanceScalar=" + val;
    } else {
        document.location.href = href + "&distanceScalar=" + val;
    }
}



function deleteAddress(uid, mapid) {
    var txt = 'האם למחוק את הכתובת?';

    $.prompt(txt, {
        opacity: 0.2,
        zIndex: 10001,
        buttons: { מחק: true, ביטול: false },
        callback: function(v, m, f) {

            if (v) {

                $.post("/ajax/deleteUserAddress.asp", { uid: uid, mapid: mapid });
                $('#' + uid + mapid).hide('slow', function() { $(this).remove(); });
            }
            else { }

        }
    }); //callback

}

function focusMapOnBiz(i) {
    map.openInfoWindowHtml(eval("markerLocation" + i), eval("htmlText" + i));
}



$(document).ready(function() {
    $("#ajaxErrMsg").ajaxError(function(e, xhr, settings, exception) {
        $(this).append('error in: ' + settings.url + '<br/>' + 'error: ' + exception);
    });

    $.ajaxSetup({
        type: "POST",
        cache: false
    });
});


function getAddressSuggest(txt, prevMapID, ismobile, uid) {
    txt = txt.replace(/ /g, "_");
    txt = heb2hex(txt);
    $('#addressSuggestResults').html('<img src="/images/ajax-loader.gif" width="20px" />מאתר כתובת...');
    //alert("/ajax/addressSuggest.asp?newLocation="+txt+"&prevMapID="+prevMapID+'&uid='+uid);
    $.ajax({
        url: "/ajax/addressSuggest.asp?newLocation=" + txt + "&prevMapID=" + prevMapID + '&uid=' + uid,
        dataType: "xml",
        success: function(xml) {
            //alert($(xml).text());
            var cunt = $(xml).find('cunt').text();
            var resultType = $(xml).find('resultType').text();
            if (cunt == 0) {
                var out = '<div>הכתובת לא נמצאה</div>';
                $('#addressSuggestResults').html(out);
            }
            else if (cunt == 1 && resultType != 3) {
                var mapid = $(xml).find('addr').attr("id");
                var pointRadius = $(xml).find('addr').attr("pointRadius");
                var type1 = $(xml).find('addr').attr("type");
                if (type1 < 3 && pointRadius > 1000) {
                    //alert("be more specific");
                    var out = '<div>';
                    out += "הכניסו כתובת מדוייקת או לחצו  ";
                    out += '<a href="/changeLocation.asp?ismobile=' + ismobile + '&newLocation=' + mapid + '&uid=' + uid + '&qs=' + currentQS + '" style="color:navy;" >המשך</a> '
                    out += 'לגלישה מוגבלת באתר ';
                    out += '</div>';
                    $('#addressSuggestResults').html(out);
                    $('#exampsArea').load('/ajax/getMoreExamps.asp?mapid=' + mapid);
                }

                else
                    document.location.href = '/changeLocation.asp?ismobile=' + ismobile + '&newLocation=' + mapid + '&uid=' + uid + '&qs=' + currentQS;
            }
            else {
                if (resultType == 3) {
                    var out = '<div>האם התכוונת ל...</div>';
                }
                else {
                    var out = '<div>נמצאה יותר מאפשרות אחת לחיפוש</div>';
                }
                $(xml).find("addr").each(function() {
                    var mapid = $(this).attr("id");
                    var addr = $(this).find("name").text();
                    //alert(mapid);
                    //alert(addr);
                    out += '<div><a class="h5 hover" href="/changeLocation.asp?ismobile=' + ismobile + '&newLocation=' + mapid + '&uid=' + uid + '&qs=' + currentQS + '">' + addr + '</a></div>';
                });
                //alert(out);
                $('#addressSuggestResults').html(out);
            }
        }
    });
}



/*subcategories - show submenus*/
var lastOpenedSubcat = "";
function showSubcat(id) {
    if (!$.browser.msie || 1 == 1) {
        if (lastOpenedSubcat == id) {
            document.getElementById('bizlistMoreOptionsOpened').innerHTML = "";
            lastOpenedSubcat = "";
        }
        else {
            $('#moreOptionsSubcat').slideUp(function() {
                $('#bizlistMoreOptionsOpened').html(document.getElementById(id).innerHTML);
                $('#bizlistMoreOptionsOpened').find('.subcatTitle').html('<img src="/images/shortcut2.png" style=\"cursor:pointer;\" title=\"חזור\" onclick=\"hideSubcat();\" />' + "&nbsp;" + $('#bizlistMoreOptionsOpened').find('.subcatTitle').html());
                $('#bizlistMoreOptionsOpened').slideDown();
            });
        }
    }
    else {
        $('#bizlistMoreOptionsOpened').html(document.getElementById(id).innerHTML);
        $('#bizlistMoreOptionsOpened').find('.subcatTitle').html('<img src="/images/shortcut2.png" style=\"cursor:pointer;\" title=\"חזור\" onclick=\"hideSubcat();\" />' + "&nbsp;" + $('#bizlistMoreOptionsOpened').find('.subcatTitle').html());
        document.getElementById("moreOptionsSubcat").style.display = "none";
        document.getElementById("bizlistMoreOptionsOpened").style.display = "block";
    }
}

function hideSubcat() {
    if (!$.browser.msie || 1 == 1) {
        $('#bizlistMoreOptionsOpened').slideUp(function() {
            $('#moreOptionsSubcat').slideDown();
        });
    }
    else {
        document.getElementById("bizlistMoreOptionsOpened").style.display = "none";
        document.getElementById("moreOptionsSubcat").style.display = "block";
    }
}


function setActiveBiz(i) {
    img = eval("icon" + i).image;
    img = img.replace(/20x20white/, "20x20orange");
    eval("marker" + i).setImage(img);

    if (document.getElementById('bizlistBizNameLink' + i))
        document.getElementById('bizlistBizNameLink' + i).style.color = "#dc6241";
}

function removeActiveBiz(i) {
    img = eval("icon" + i).image;
    img = img.replace(/20x20orange/, "20x20white");
    eval("marker" + i).setImage(img);

    if (document.getElementById('bizlistBizNameLink' + i))
        document.getElementById('bizlistBizNameLink' + i).style.color = "#5F5F5F";
}

function hoverMapIcons(i) {
    try {
        for (j = 0; j < eval("cat" + i + "Icons").length; j++) {
            setActiveBiz(eval("cat" + i + "Icons")[j]);
        }
    } catch (e) {
    }
}

function outMapIcons(i) {
    try {
        if (eval("cat" + i + "Icons"))
            for (j = 0; j < eval("cat" + i + "Icons").length; j++) {
            removeActiveBiz(eval("cat" + i + "Icons")[j]);
        }
    } catch (e) {
    }
}


// BizList Map nailing
var origineY;
$(document).ready(function() {
    if (document.getElementById("scroller") != null)
        origineY = $("#scroller").offset().top;
});

var mapnailed = false;
function nailMap() {
    if (mapnailed == false) {
        window.onscroll = function() {
            if (window.XMLHttpRequest) {
                if (document.documentElement.scrollTop > origineY || self.pageYOffset > origineY) {
                    $('#scroller').css('position', 'fixed');
                    $('#scroller').css('top', '0');
                } else if (document.documentElement.scrollTop < origineY || self.pageYOffset < origineY) {
                    $('#scroller').css('position', 'absolute');
                    $('#scroller').css('top', origineY + 'px');
                }
            }
        }
        mapnailed = true;
        document.getElementById("nailMapIcon").src = "/images/pin5.gif";
        document.getElementById("nailMapIcon").onmouseover = "";
    }
    else {
        window.onscroll = function() { };
        mapnailed = false;
        $('#scroller').css('position', 'absolute');
        $('#scroller').css('top', origineY + 'px');
        document.getElementById("nailMapIcon").src = "/images/pin4.gif";
    }
}


function heb2hex(str) {
    var key1 = str;
    key1 = key1.replace(/א/g, "E0");
    key1 = key1.replace(/ב/g, "E1");
    key1 = key1.replace(/ג/g, "E2");
    key1 = key1.replace(/ד/g, "E3");
    key1 = key1.replace(/ה/g, "E4");
    key1 = key1.replace(/ו/g, "E5");
    key1 = key1.replace(/ז/g, "E6");
    key1 = key1.replace(/ח/g, "E7");
    key1 = key1.replace(/ט/g, "E8");
    key1 = key1.replace(/י/g, "E9");
    key1 = key1.replace(/ך/g, "EA");
    key1 = key1.replace(/כ/g, "EB");
    key1 = key1.replace(/ל/g, "EC");
    key1 = key1.replace(/ם/g, "ED");
    key1 = key1.replace(/מ/g, "EE");
    key1 = key1.replace(/ן/g, "EF");
    key1 = key1.replace(/נ/g, "F0");
    key1 = key1.replace(/ס/g, "F1");
    key1 = key1.replace(/ע/g, "F2");
    key1 = key1.replace(/ף/g, "F3");
    key1 = key1.replace(/פ/g, "F4");
    key1 = key1.replace(/ץ/g, "F5");
    key1 = key1.replace(/צ/g, "F6");
    key1 = key1.replace(/ק/g, "F7");
    key1 = key1.replace(/ר/g, "F8");
    key1 = key1.replace(/ש/g, "F9");
    key1 = key1.replace(/ת/g, "FA");
    return key1;
}


function sendSiteFeedback() {
    $.ajax({
        type: "POST",
        url: "/ajax/siteFeedback.asp",
        data: $("#siteFeedbackForm").serialize(),
        success: function(msg) {
            alert(msg);
            if (msg == "thank u")
                $.fancybox.close();
        }
    });
}

