﻿var load_complete;
function report_err(message, title, src, keepon) {
    if (!keepon) process_load_off();
    alert(message);
}

function process_load(msg, src) {
    if (!msg) msg = "Loading...";
    if (!src) src = $(document.body);
    var div = $("divpop");

    div.update("<center>" + msg + "<br><img src='/img/des/loading.gif' /></center>");
    div.setStyle({ paddingTop: "150px" });
    div.absolutize();
    div.clonePosition(src);
    process_load_on();
}
function process_load_off() {
    var d = $("divpop");
    d.update("");
    d.hide(); // Effect.DropOut('divpop');
}

function centerize(obj) {
    var viewPort = document.viewport.getDimensions();
    var owidth = obj.getWidth();
    var centerX = (viewPort.width - owidth) / 2;
    obj.style.left = Math.floor(centerX) + 'px';
}

function addtocart(frm) {
    return popup_div("/ajax/cart?" + $(frm).serialize(), 460, 628 + 68);
}

function popup_div(url, ht, wdt) {
    var div = $("divpop");
    div.absolutize();
    div.setOpacity(1);

    var viewPort = document.viewport.getDimensions();
    var offsets = document.viewport.getScrollOffsets();
    var centerX = viewPort.width / 2 + offsets.left - wdt / 2;
    var centerY = viewPort.height / 2 + offsets.top - ht / 2;

    //div.setStyle({ padding: "0", width: (wdt + "px"), height: (ht + "px"), position: "absolute", overflow: "hidden",
   //     left: Math.floor(centerX) + 'px', top: Math.floor(centerY) + 'px'
    // });
    div.setStyle({ padding: "0", width: '100%', position: "absolute", overflow: "hidden",
        left:'0px', top: Math.floor(centerY) + 'px'
    });
    process_load_on();
    
    div.update("<center>Loading Results...<br><img src='/img/des/loading.gif' /></center>");
    new Ajax.Request(url, { method: 'get', onSuccess: function (t) { div.update(t.responseText); } });
    
    return false;
}
//var _process_load_on;
function process_load_on() {
    Effect.Appear('divpop');
}
var record_item;
function load_complete() {
    adjust_height();
    var u = '/ajax/visit.aspx?u=' + encodeURIComponent(document.location);
    if (record_item) u += "&i=" + record_item;
    new Ajax.Request(u, { method: 'get', onSuccess: process_visit_result });

}
function adjust_height() {
    var t = $("tbl_body");
    var h = document.viewport.getDimensions().height;
    var th = t.getHeight()
    if (th >= h) return;
    t.setStyle({ height: (h) + 'px' });
}
function process_visit_result(t) {
    t = t.responseText
    if (isNaN(t) || t == "0") return;
    t = parseInt(t);
    $("spn_crt_status").update('(' + t + ' Item' + ((t > 1) ? 's' : '') + ')');
}
function cacheURL(u, div) {
    if (!(div.innerHTML.blank())) return true;
    div.update(new Element("img", { src: "/img/des/load.gif" }));
    new Ajax.Request(u, { method: 'get', onSuccess: function(t) { div.update(t.responseText); } });
    return false
}
var _TabberInitialize
function TabberInitialize() {
    $$("table.tabs").each(function (t) {
        if (t.hasClassName('noauto')) return;
        t.addClassName('noauto');
        t.select("th").each(function (th, thIndex) {
            if (thIndex == 0) {
                th.addClassName("active");
                _TabberInitialize = th;
            }
            th.onmouseover = function () { th.addClassName("active"); };
            th.onmouseout = function () { if (_TabberInitialize != th) th.removeClassName("active"); };
            th.onclick = function () {
                if (_TabberInitialize) _TabberInitialize.removeClassName("active");
                _TabberInitialize = th;
                t.select("td.content div").each(function (d, dIndex) { (dIndex == thIndex) ? d.show() : d.hide(); });
            }
        });
        t.select("td.content div").each(function (d, i) { if (i > 0) d.hide(); });
    })
}
function ImgMomoize() {
    $$('img[lowsrc]', 'input[data-lowsrc]').each(
        function (i) {
            if (i.hasClassName('processed')) return;
            i.addClassName('processed');
            var src = i.src;
            var lsrc = i.lowsrc || i.getAttribute('data-lowsrc') || src;
            i.observe('mouseover', function () { i.src = lsrc; });
            i.observe('mouseout', function () { i.src = src; });
        }
    );
}

function MoMoize(slctor, clss, mclss) {
    $$(slctor).each(function(elem) {
        elem.observe('mouseover', function() { this.removeClassName(clss).addClassName(mclss); });
        elem.observe('mouseout', function() { this.removeClassName(mclss).addClassName(clss); });
    });
}
function Labelize() {
    $$('input.inside').each(
        function(i) {
            i.observe('blur', function() { LabelizeAdjust(this); });
            i.observe('focus', function() { LabelizeAdjust(this, true); });
            try {
                LabelizeAdjust(i, false);
            } catch (e) { alert(e); }

            var f = i.up("form");
            if (f) f.observe("submit", function() { if (i.value == i.title) i.value = ""; });
        });
}

function LabelizeAdjust(o, f) {
    if (f) o.removeClassName("inside").value = (o.value == o.title) ? "" : o.value;
    else {
        if (o.value.blank()) o.value = o.title;
        if (o.value == o.title) o.addClassName("inside");
        else o.removeClassName("inside");
    }
}

function toCurrency(c) {
    return '$' + parseFloat(c).toFixed(2);
}
function isValidEmail(e) {
    var r = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9\-\.])+\.([A-Za-z]{2,4})$/;
    return r.test(e);
}

var _navdrop_src;
var _navdrop_id;
var _navdrop_vals = new Array();

function navdrop(src, cid, css) {
    if(!css)css='over';
    if (!load_complete) return;
    if (_navdrop_src) _navdrop_src.removeClassName(css);
    _navdrop_src = src;
    _navdrop_id = cid;
    _navdrop_src.addClassName(css);
    if (!_navdrop_src.onmouseout) src.onmouseout = function() {
        _navdrop_src = null;
        setTimeout(function() { navdrop_off(src,css); }, 75);
    }
    if (_navdrop_vals[cid] == null) {
        _navdrop_vals[cid] = "";
        new Ajax.Request('/ajax/cats?i=' + cid, { onSuccess: function(t) {
            _navdrop_vals[cid] = t.responseText;
            navdrop_obj();
        }
        });
    } else
        setTimeout(navdrop_obj, 100);
}
function navdrop_obj() {
    var src = _navdrop_src;
    if (!src) return;
    if (_navdrop_vals[_navdrop_id] == "") return;
    var des = $("divpop");
    des.setStyle({ width: null, height: null, padding: 0});
    des.update(_navdrop_vals[_navdrop_id]);
    var div = des.down("div");
    if (!div) return;

    div.onmouseout = src.onmouseout;
    div.onmouseover = function() { _navdrop_src = src; }

    var tbl = src.up('table');
    var tstart = tbl.cumulativeOffset()[0];
    var tend = tbl.getWidth();
    var sstart = src.cumulativeOffset()[0];
    var send = src.getWidth();
    var top = src.cumulativeOffset()[1] + src.getHeight();

    var dwidth = div.getWidth();
    if (dwidth <= 0) dwidth = parseInt(div.getStyle('width').replace('px', ''));
    
    //if (dwidth == 0) dwidth = 500;
    //alert('div data;' + div.getStyle('width') + ',' + div.getWidth() + ':' + dwidth + div.innerHTML);
    //document.forms[0].k.value = "{d:" + dwidth + ",s:" + tstart + ",e:" + tend + ",t:" + top + "}";
    var loff = 8;
    var left = sstart + (send - dwidth) / 2;
    if (left < tstart + 5) left = tstart + loff;
    if (left > sstart) left = tstart - loff;
    if (left + dwidth > tstart + tend - loff) left = tstart + tend - loff - dwidth;
    if (sstart + send >= tstart + tend) left = tstart + tend - dwidth + 6;
    des.setStyle({ left: left + 'px', top: top + 'px' });
    des.show();
}

function navdrop_off(src,css) {
    if (_navdrop_src == src) return;
    src.removeClassName(css);
    if (_navdrop_src != null) return;
    process_load_off();
}

function orderwait(tbl) {
    var fun = function () {
        var d = new Date();

        var hours =  19 - (d.getUTCHours());
        if (hours > 12 || hours < 0) return (tbl.hide() && false);


        var minutes = 60 - d.getUTCMinutes();

        var display = "";
        if (hours > 0) {
            display += hours + "&nbsp;Hour";
            if (hours > 1) display += "s";
            if (minutes > 0) display += ",&nbsp;";
        }

        if (minutes > 0) display += minutes + "&nbsp;Minutes" 
        tbl.down('div.order-within-clock').update(display);

        return true;
    };
    if(fun())window.setInterval(fun, 1000 * 60 );
}
function initiateNav() {
    navcats.each(function (cid) { new Ajax.Request('/ajax/cats?i=' + cid, { onSuccess: function (t) { _navdrop_vals[cid] = t.responseText; } }) });
}
document.observe('dom:loaded', function() {
    load_complete = true;
    TabberInitialize();
    Labelize();
    MoMoize(".momo", "momo", "momo-over");
    ImgMomoize();
    initiateNav();
});
