﻿var timer = null;

$(document).ready(function () {

    $(":button[name$='master_search_bt']").unbind('click').removeAttr('onclick').click(function (event) {
        var si = $.trim($(":text[name$='master_search_tb']").val());
        var cat = $.trim($("select[name$='master_search_ddl']").val());
        if (si.length > 0) {
            if (/^\d{8,}$/.test(si))
                window.location.href = "http://" + window.location.host + "/suche/" + si + cat + "/aktuellste/alle/1";
            else
                window.location.href = "http://" + window.location.host + "/suche/" + $.URLEncode(si.substring(0, 100), '*') + cat + "/relevanz/alle/1";
        } else {
            window.location.href = "http://" + window.location.host + cat + "/aktuellste";
        }

        return false;
    });

    $("input[hasfocus]").focus();

    $("textarea").bind('keypress paste', function (event) {
        var key = event.which;
        if (key >= 33 || key == 13 || key == 0) {
            var maxLength = ($(this).attr("maxlen")) ? $(this).attr("maxlen") : 3000;
            var length = this.value.length;
            if (length >= maxLength) {
                this.value = this.value.substr(0, maxLength);
                event.preventDefault();
            }
        }
    });

    $("img[image_zoom]").mouseenter(function () {
        $("#image_zoom_src").attr("src", $(this).attr("image_zoom")).load(function () {
            $("#image_zoom").show();
        });
    }).mousemove(function (e) {
        var corr = $(window).height() + $(window).scrollTop() - e.pageY - 5 - $("#image_zoom_src").height();
        if (corr > 0) corr = 0;
        $("#image_zoom").offset({ top: e.pageY + 5 + corr, left: e.pageX + 5 })
    }).mouseout(function () {
        $("#image_zoom").hide();
        $("#image_zoom_src").attr("src", "http://pic.exsila.com/pic_blank.gif");
    });

    /*$("select[query]").change(function () {
    return changeSelect(this);
    });*/
    $("select[marker]").change(function () {
        changeFilter(this);
        return true;
    });
    $("ul[marker] input").change(function () {
        changeFilter(this);
        return true;
    });

    $(".tradelist tr").hover(function () { In(this) }, function () { Out(this) });
    $(".acclist tr").not(":has(th)").hover(function () { In(this) }, function () { Out(this) });

    if ($("b[datetime]").length > 0) {
        window.setInterval(function () {
            $("b[datetime]").each(function (index, ele) {
                var num = $(ele).attr("datetime").split(",", 6);
                var time = ((new Date(num[0], num[1] - 1, num[2], num[3], num[4], num[5])).getTime() - (new Date()).getTime()) / 1000;
                if (time < 0) {
                    $("#" + $(ele).attr("hide")).css("display", "none");
                } else {
                    var hour = Math.floor(time / 3600);
                    time -= hour * 3600;
                    var minutes = Math.floor(time / 60);
                    time -= minutes * 60;
                    var seconds = Math.floor(time);
                    $(ele).text("in " + (hour < 10 ? "0" + hour.toString(10) : hour.toString(10)) + ":" + (minutes < 10 ? "0" + minutes.toString(10) : minutes.toString(10)) + ":" + (seconds < 10 ? "0" + seconds.toString(10) : seconds.toString(10)));
                }
            });
        }, 1000);
    }

    $("#master_hover_link").children().each(function (i, e) {
        var obj = $(e);
        var id = obj.attr("id").replace(/_hover/, "");
        $("#" + id).hover(function (event) {
            window.clearTimeout(obj.data("timeout"));
            obj.data("timeout", null);
            obj.show();
        }, function (event) {
            obj.data("timeout", window.setTimeout(function () {
                obj.hide();
            }, 1000));
        });
        obj.hover(function (event) {
            window.clearTimeout(obj.data("timeout"));
            obj.data("timeout", null);
            obj.show();
        }, function (event) {
            obj.data("timeout", window.setTimeout(function () {
                obj.hide();
            }, 1000));
        });
    });
});

function locationSubmit(str, id) {
    window.location = str + $.URLEncode($(":text[name$='" + id + "']").val());
}

/*function changeSelect(obj) {
    var val = $(obj).val();
    var app = $(obj).attr("query") != "true";
    var a = theForm.action.indexOf('/') > -1 ? theForm.action : window.location.pathname.replace(/\/$/, '');

    if (val == '--') {
        return false;
    } else {
        var found = false;
        $(obj).children().each(function () {
            if ($(this).val()) {
                var q = '/' + $(this).val();
                if (a.indexOf(q) > -1) {
                    theForm.action = a.substring(0, a.indexOf(q)) + (val ? '/' + val : '') + (app ? a.substring(a.indexOf(q) + q.length) : '');
                    found = true;
                    return false;
                }
            }
        });

        if (theForm.action.indexOf('/filter') > -1)
            theForm.action = theForm.action.replace(/\/(\d+)\/filter/, '/$1/filter')
        else
            theForm.action = theForm.action.replace(/\/\d+$/, '')

        if (!found)
            theForm.action = a + '/' + val;
    }

    if (timer)
        window.clearTimeout(timer);

    if ($(obj).attr("onchange")) {
        $(obj).removeAttr("onchange").unbind();
        timer = window.setTimeout("__doPostBack('" + $(obj).attr("name") + "', 0);", 1);
    }
    else
        timer = window.setTimeout("window.location.href = theForm.action;", 1);

    $(obj).blur();

    return false;
}*/

function changeFilter(obj) {
    var found = false;
    var number = false;
    var select = true;
    var checked = false;
    var name = $(obj).attr("name");
    var val = $(obj).val();
    if (obj.nodeName == 'INPUT') {
        select = false;
        checked = $(obj).is(":checked");
        obj = $(obj).parents("[marker]");
    }
    var a = theForm.action.indexOf('/') > -1 ? theForm.action : window.location.pathname.replace(/\/$/, '');

    var marker = $(obj).attr("marker");
    if (marker.indexOf('_from') > -1 || marker.indexOf('_to') > -1) {
        marker = marker.replace(/_from|_to/, '');
        number = true;
    }
    if (a.indexOf('/filter') > -1) {
        // Es ist bereits ein filter vorhanden
        if (isNaN(val)) {
            var q = new RegExp('(/filter(?:/[a-z]+/(?:\\d*-?\\d*|(?:[a-z]+-?)+))*)/' + marker + '/((?:[a-z]+-?)+)');
            if (q.test(a)) {
                if (!select) {
                    val = checked ? RegExp.$2 + '-' + val : RegExp.$2.replace(new RegExp(val + "-?"), '');
                    theForm.action = (a.replace(q, (val ? '$1/' + marker + '/' + val.replace(/-$/, '') : '$1'))).replace(/\/filter$/, '');
                    found = true;
                }
            }
        } else {
            var q = new RegExp('(/filter(?:/[a-z]+/(?:\\d*-?\\d*|(?:[a-z]+-?)+))*)/' + marker + '/(\\d*)-?(\\d*)');
            if (q.test(a)) {
                if (select) {
                    if (number) {
                        if ($(obj).attr("marker").indexOf('_from') > -1) {
                            if (RegExp.$3 && parseInt(val) <= parseInt(RegExp.$3))
                                val = (val > -1 ? val : '') + '-' + RegExp.$3;
                            else if (val > -1)
                                val = val + '-';
                            else
                                val = '';
                        } else {
                            if (RegExp.$2 && parseInt(val) >= parseInt(RegExp.$2))
                                val = RegExp.$2 + '-' + (val > -1 ? val : '');
                            else if (val > -1)
                                val = '-' + val;
                            else
                                val = '';
                        }
                        theForm.action = (a.replace(q, (val ? '$1/' + marker + '/' + val : '$1'))).replace(/\/filter$/, '');
                    } else
                        theForm.action = (a.replace(q, (val > -1 ? '$1/' + marker + '/' + val : '$1'))).replace(/\/filter$/, '');
                } else {
                    val = checked ? (RegExp.$2 | (1 << val)) : (RegExp.$2 & ~(1 << val));
                    theForm.action = (a.replace(q, (val > 0 ? '$1/' + marker + '/' + val : '$1'))).replace(/\/filter$/, '');
                }
                found = true;
            }
        }
    } else {
        var s = a.search(/\/(\d+)\/?$/);
        if (s != -1)
            a = a.substring(0, s);

        var t = a.search(/\/(alle|punkte|chf)\/?$/);
        if (t != -1)
            a = a + '/1/filter';
        else {
            var u = a.search(/(aktuellste|guenstigste|meist-gekaufte|meist-angebotene|neu-verfuegbare|relevanz|distanz)\/?$/);
            if (u != -1)
                a = a + '/alle/1/filter';
            else
                a = a + '/aktuellste/alle/1/filter';
        }
    }

    if (!found) {
        if (select) {
            if (number) {
                if ($(obj).attr("marker").indexOf('_from') > -1)
                    theForm.action = a + '/' + marker + '/' + val + '-';
                else
                    theForm.action = a + '/' + marker + '/-' + val;
            } else
                theForm.action = a + '/' + marker + '/' + val;
        } else {
            if (isNaN(val))
                theForm.action = a + '/' + marker + '/' + val;
            else
                theForm.action = a + '/' + marker + '/' + (1 << val);
        }
    }

    if (timer)
        window.clearTimeout(timer);

    timer = window.setTimeout("window.location.href = theForm.action;", 1);

    $(obj).blur();
}

function popup(url, x, y) {
    var str = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=" + x + " ,height=" + y;
    window.open(url, "popup", str);
}

function Out(item) {
    if ($(item).hasClass("red"))
        item.style.backgroundColor = "#FFD1D1";
    else
        item.style.backgroundColor = "#FFF";
}

function In(item) {
    if ($(item).hasClass("red"))
        item.style.backgroundColor = "#FFB8B8";
    else
        item.style.backgroundColor = "#eee"; //"#DCF6DB";
}

function changeTab(id, i) {
    $("#" + id + " div.paging").hide().eq(i).show();
    $("#" + id + " li").removeClass('active').eq(i).addClass('active');
}

