﻿$(document).ready(function() {

    $(":button[name$='master_search_bt']").click(function(event) {
        var si = $.trim($(":text[name$='master_search_tb']").val());
        if(si.length > 0)
        {
            window.location.href = "/suche/" + $("select[name$='master_search_ddl']").val() + "/aktuellste/" + $.URLEncode(si.substring(0, 100)) + "/1";
        }
        else
        {
            window.location.href = "/" + $("select[name$='master_search_ddl']").val() + "/aktuellste";
        }
    });

    $("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[asin]").mouseenter(function() {
        $("#image_zoom_src").attr("src", "http://pictures.exsila.com/" + $(this).attr("ptype") + "/" + $(this).attr("asin") + "_L.jpg").load(function() {
            $("#image_zoom").css("display", "block");
        });
    }).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").css("display", "none");
        $("#image_zoom_src").attr("src", "http://pictures.exsila.com/pic_blank.gif");
    });

    $("select[query][onchange*='__doPostBack']").change(function() {
        __prePostBack(this);
    });
    $("select[removepage][onchange*='__doPostBack']").change(function() {
        __removePage();
    });

    $(".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);
    }
});

function locationSubmit(str, id) {
    window.location = str + $.URLEncode($(":text[name$='" + id + "']").val());
}

function __removePage() {
    var a = theForm.action;
    var s = a.search(/\/(\d+)$/)
    if (s != -1)
        theForm.action = a.substring(0, s);
}

function __prePostBack(obj) {
    var val = $(obj).val();
    var a = window.location.href;

    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 : '');
                    found = true;
                    return false;
                }
            }
        });
        if (!found) {
            var s = a.search(/(\d+)$/)
            if (s != -1)
                theForm.action = a.substring(0, s) + val;
            else
                theForm.action = a + '/' + val;
        }
    }
    
    return true;
}

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";
}


