
document.getById = function(strId) {
    if (document.getElementById)
        return document.getElementById(strId);
    else if (document.all)
        return document.all[strId];
    else if (document[strId])
        return document[strId];
    else if (document.layers && document.layers[strId])
        return document.layers[strId];
}

if (String && String.prototype) {
    String.prototype.trim = function() {
        return this.replace(/^\s+|\s+$/g, "");
    }
}

function Cookies() {
    var arrCookies = new Object();
    if (document.cookie && document.cookie.length != 0) {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; ++i) {
            var arrCookie = cookies[i].split('=');
            if (arrCookie.length != 2)
                continue;
            arrCookies[unescape(arrCookie[0])] = unescape(arrCookie[1]);
        }
    }

    this.getCookie = function(strName) {
        return arrCookies[strName];
    }

    this.setCookie = function(strName, strValue, expDate) {
        var strCookie = escape(strName) + "=" + escape(strValue) + "; ";
        if (expDate)
            strCookie += "expires=" + expDate.toGMTString() + "; ";
        strCookie += "path=/";
        document.cookie = strCookie;
        arrCookies[strName] = strValue;
    }

    this.deleteCookie = function(strName) {
        var oDate = new Date();
        oDate.setDate(oDate.getDate() - 1);
        document.cookie = escape(strName) + "=; expires=" + oDate.toGMTString() +
            "; path=/";
        arrCookies[strName] = null;
    }
}

document.cookies = new Cookies();

function setContentSize(iCont) {
    var iSize = 10;
    switch (iCont) {
        case 1:
            iSize = 10;
            break;
        case 2:
            iSize = 12;
            break;
        case 3:
            iSize = 14;
            break;
        case 4:
            iSize = 16;
            break;
    }

    var oContent = document.getById("page_content");
    if (oContent) {
        oContent.style.fontSize = iSize + "px";
        var expDate = new Date();
        expDate.setFullYear(expDate.getFullYear() + 1);
        document.cookies.setCookie("ContentSize", iSize, expDate);
    }
}

function swapImage(imgObj, strImg) {
    imgObj.old = imgObj.src;
    imgObj.src = strImg;
}

function restoreImage(imgObj) {
    imgObj.src = imgObj.old;
}

//formats a string for inclusion in URLs for SEO
//maintained in JS for consistency, web-crawlers
//will not pick up.
function urlSafeString(str) {

    var str_ret = '';
    var str_last_chr = '';
    var str_len = str.length;

    str = str.toLowerCase();

    var aCharCode = "a".charCodeAt(0);
    var zCharCode = "z".charCodeAt(0);

    var charCode0 = '0'.charCodeAt(0);
    var charCode9 = '9'.charCodeAt(0);

    for (var i = 0; i < str_len; ++i) {
        if ((str.charCodeAt(i) >= aCharCode && str.charCodeAt(i) <= zCharCode) || (str.charCodeAt(i) >= charCode0 && str.charCodeAt(i) <= charCode9)) {
            str_last_chr = str.charAt(i);
            str_ret += str_last_chr;
        }

        if (str.charAt(i) == ' ' && str_last_chr != '_') {
            str_last_chr = '_';
            str_ret += str_last_chr;
        }
    }

    return str_ret;
}

function htmlEscape(str) {

    str = str.replace(/\&/g, "&amp;");
    str = str.replace(/\"/g, "&quot;");
    str = str.replace(/\>/g, "&gt;");
    str = str.replace(/\</g, "&lt;");
    return str;
}

function nl2br(str) {
    return str.replace(/\n/g, "\n<br />");
}

var __g_arrDays = new Array(0, "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
var __g_curPage = 1;
var __g_curBounds = 0;
var __g_curDay = 0;

function getDealsNextPage() {
    __g_curPage++;
    getDailyDeals(__g_curDay, __g_curBounds, true);
}

function getDealsPrevPage() {
    if (__g_curPage > 1)
        __g_curPage--;
    getDailyDeals(__g_curDay, __g_curBounds, true);
}

function getDailyDeals(day_of_week, arrBounds, bPreservePage) {

    var obEstab = new Establishment();
    var obDeals = false;

    if (!bPreservePage)
        __g_curPage = 1;
    if (day_of_week != __g_curDay)
        __g_curDay = day_of_week;

    if (window.__g_mapObject) {

        var top = 0, left = 0, bottom = 0, right = 0;

        if (arrBounds && arrBounds.length == 4) {

            top = arrBounds[0];
            left = arrBounds[1];
            bottom = arrBounds[2];
            right = arrBounds[3];
        }
        else {

            var oBounds = __g_mapObject.getBounds();
            bottom = oBounds.getSouthWest().lat();
            left = oBounds.getSouthWest().lng();
            top = oBounds.getNorthEast().lat();
            right = oBounds.getNorthEast().lng();
        }

        obDeals = obEstab.getDealsByBounds(top, left, bottom, right, day_of_week, __g_curPage);
    }
    else {
        var obDeals = obEstab.getDailyDeals(SGL_HOTWINGS_CITY_ID, day_of_week, 6);
    }

    var arr_deals = obDeals.daily_deals;
    var obElement = document.getById("daily_deals_place");
    if (!obElement)
        return;

    if (!arr_deals) {
        obElement.innerHTML = "<p>Unfortunately we were unable to locate any wing establishments in the selected area.</p>" +
            "<p>If you know of a missing establishment in your area, please <a href=\"" + SGL_JS_GLOBALURL + "/add-listing/\">Add a Listing</a> " +
            "and help the community out!</p>";
        return;
    }

    var strHtml = "";

    var iStart = 0;
    var iEnd = 0;
    var iCount = 0;

    if (window.__g_mapObject && arr_deals.length != 0) {
        iStart = parseInt(arr_deals[0].start);
        iEnd = parseInt(arr_deals[0].end);
        iCount = parseInt(arr_deals[0].cnt);
        strHtml = "<div align=\"right\">Showing " + iStart + " to " + iEnd + " of " + iCount + "</div>";
    }
    for (var i = 0; i < arr_deals.length; ++i) {
        var iter = i + 1;
        var strClass = (iter % 2 == 0) ? "panel_list_row_even" : "panel_list_row_odd";
        strHtml += "<div class=\"" + strClass + "\" id=\"deal_" + arr_deals[i].establishment_id + "\"><table cellspacing=\"0\" cellpadding=\"0\"><tr><td class=\"panel_list_left_col\">" +
            "<a id=\"deal_anchor_" + arr_deals[i].establishment_id + "\" href=\"" + SGL_JS_GLOBALURL + "/vendor/" + arr_deals[i].establishment_id + "_" + urlSafeString(arr_deals[i].name) + "/\"";
        if (window.__g_mapObject) {
            strHtml += " onmouseover=\"mapHighlight(" + arr_deals[i].establishment_id + ", true);\" onmouseout=\"mapHighlight(" + arr_deals[i].establishment_id + ", false);\"";
        }
        strHtml += ">" + arr_deals[i].safe_name + "</a></td>";
        if (arr_deals[i].today_price)
            strHtml += "<td class=\"panel_list_right_col\">$" + arr_deals[i].str_price_per_wing +
            ((arr_deals[i].is_price_per_pound && arr_deals[i].is_price_per_pound != 0) ? "/lb" : "") + "</td>";
        else
            strHtml += "<td></td>";
        strHtml += "</tr></table></div>";
    }
    if (window.__g_mapObject && iCount != 0) {
        var obLinkPlace = document.getById("daily_deals_link_place");
        if (obLinkPlace) {
            obLinkPlace.innerHTML = "";
        }

        if (iEnd == iCount && iEnd - iStart < 5) {
            strHtml += "<p>Can't find what you're looking for? <a href=\"" + SGL_JS_GLOBALURL + "/add-listing/\">Add It</a> and help the community grow!</p>";
        }

        strHtml += "<div><table><tr>";
        if (iStart != 1) {
            strHtml += "<td align=\"left\"><a href=\"javascript:void(0);\" onclick=\"getDealsPrevPage();\"><img src=\"" + SGL_JS_IMAGESDIR + "/arrow_left.gif\" alt=\"Prev\" width=\"25\" height=\"25\" border=\"0\" /></a></td>";
        }
        if (iEnd != iCount) {
            strHtml += "<td align=\"right\"><a href=\"javascript:void(0);\" onclick=\"getDealsNextPage();\"><img src=\"" + SGL_JS_IMAGESDIR + "/arrow_right.gif\" alt=\"Next\" width=\"25\" height=\"25\" border=\"0\" /></a></td>";
        }
        strHtml += "</tr></table></div>";
    }

    obElement.innerHTML = strHtml;

    var obLink = document.getById("daily_deals_link");
    if (obLink) {
        var strUrl = __g_arrDays[day_of_week].toLowerCase() + "-wing-deals";
        obLink.href = SGL_JS_WEBROOT + "/" + strUrl + "/";
        obLink.innerHTML = "SHOW ALL " + __g_arrDays[day_of_week].toUpperCase() + " DEALS";
    }

    if (window.__g_mapObject && reloadDailyDealMarkers) {
        reloadDailyDealMarkers(arr_deals);
    }
}

function getClientDailyDeals(iServerDayOfWeek) {
    var oDate = new Date();
    if (oDate.getHours() < 4)
        oDate.setDate(oDate.getDate()-1);

    //get always for 'by bounds' functionality
    var obDays = document.daily_deals.deals_by_day;
    obDays.options[oDate.getDay()].selected = true;
    getDailyDeals(oDate.getDay() + 1);
}

function getTopRanked(city_id) {

    var obEstab = new Establishment();
    var obRanks = obEstab.getTopRanked(city_id, 5);
    var arr_ranks = obRanks.top_ranked;

    var obElement = document.getById("top_ranked_place");
    if (!obElement)
        return;

    if (!arr_ranks) {
        obElement.innerHTML = "Unable to retrieve top ranked establishments for selected city!";
        return;
    }

    var strHtml = "";
    for (var i = 0; i < arr_ranks.length; ++i) {
        var iter = i + 1;
        var strClass = (iter % 2 == 0) ? "panel_list_row_even" : "panel_list_row_odd";
        var strIconClass = (iter < 4) ? "panel_icon_top_3" : "panel_icon_other";
        strHtml += "<div class=\"" + strClass + "\"><table cellspacing=\"0\" cellpadding=\"0\"><tr><td class=\"panel_icon_pos\"><div class=\"" + strIconClass + "\"><div>" + iter + "</div></div></td>" +
            "<td class=\"panel_text_pos\"><div class=\"panel_text_spacer\"></div><div>" +
            "<a href=\"" + SGL_JS_GLOBALURL + "/vendor/" + arr_ranks[i].establishment_id + "_" + urlSafeString(arr_ranks[i].name) + "/\">" + arr_ranks[i].safe_name + "</a>" +
            "</div><div>" + arr_ranks[i].num_comments + " comments</div><div style=\"clear:both;\"></div></td></tr></table></div>";
    }
    obElement.innerHTML = strHtml;
}

function getFinderCityAreas(city_id, obAreasSel) {

    var obEstab = new Establishment();
    var obAreas = obEstab.getCityAreas(city_id);
    var arr_areas = obAreas.areas;

    if (!obAreasSel)
        return;

    obAreasSel.options.length = 0;

    if (!arr_areas || city_id == -1) {
        return;
    }

    obAreasSel.options[0] = new Option("Select an Area", "0");

    for (var i = 0; i < arr_areas.length; ++i) {
        var obOption = new Option(arr_areas[i].safe_name, arr_areas[i].city_areas_id);
        obAreasSel.options[obAreasSel.length] = obOption;
    }
}

function toggleCityPanel() {
    var obCityPanel = document.getById("change_city_panel");
    if (obCityPanel) {
        obCityPanel.style.visibility = (obCityPanel.style.visibility == "visible") ? "hidden" : "visible";
    }
}