var IDOWA_ROOT = "http://www.idowa.de/";

function initInfobox() {
    $(".infobox .content").toggle();
    $(".infobox h2").click(function() {
        /*var target = $(this).parent().children(".content");
        var action = false;
        if(!action) {
            if(target.css("display") != "none") {
                action = true;
                var h = target.height();
                target.animate({
                    height: '1px'
                }, 500, function() {
                    target.hide();
                    target.height(h);
                    action = false;
                });
            } else {
                action = true;
                target.slideDown(500, function() {
                    action = false;
                });
            }
        }*/
        $(this).parent().children(".content").slideToggle();
    });
}

function initFieldValidation() {
    if(typeof validationMap != 'undefined') {
        map = $.evalJSON(validationMap);
        $("div.validationForm input").blur(function() {
            name = this.name;
            if(map.hasOwnProperty(name)) {
                $.getJSON("/validation?validationId=" + map[name] + "&value=" + this.value,
                    function(data) {
                        if(data.errorMessage != "") {
                            addFieldError(name, data.errorMessage)
                        } else {
                            removeFieldError(name);
                        }
                        printFieldError();
                    });
            }
        });
    }
}

var vFieldErrors = new Array();

function addFieldError(name, message) {
    msg = message;

    lablename = name;
    if(formname != '') {
        lablename = formname + "_" + lablename;
    }

    msg = msg.replace(/%1/, $("label[for='" + lablename + "']").text());
    $("input[name='" + name + "']").addClass("error")
    vFieldErrors[name] = msg;    
}

function removeFieldError(name) {
    $("input[name='" + name + "']").removeClass("error")
    vFieldErrors[name] = undefined;
}

function printFieldError() {
    errors = "<ul>";
    for(field in vFieldErrors) {
        if(vFieldErrors[field] != undefined) {
            errors += "<li><span>" + vFieldErrors[field] + "</span></li>";
        }
    }
    errors += "</ul>";
    $("div.validationForm div.errorMessage").html(errors);
}

function addPasswordValidation(pwd, repeat) {
    $("input[name='"+pwd+"'], input[name='"+repeat+"']").change(function() {
        if($("input[name='"+pwd+"']").attr("value") != $("input[name='"+repeat+"']").attr("value")) {
            addFieldError(pwd+"_"+repeat, "Das Passwort und die Wiederholung stimmen nicht &uuml;berein");
        } else {
            removeFieldError(pwd+"_"+repeat);
        }
        printFieldError();
    });
}

function setLinkWhenRawActual(content_id, html_id, value, target) {
    $.ajax({
        type: "GET",
        url: "/common/content/isContentActual",
        data: "content_id=" + content_id + "&ajax=true",
        success: function(msg) {
            if(msg == "true") {
                $.ajax({
                    type: "GET",
                    url: "/common/content/getSizePath",
                    data: "content_id=" + content_id + "&size_model_id=20&ajax=true",
                    success: function(msg2) {
                        ending = msg2.substring(msg2.lastIndexOf(".") + 1);
                        $("#" + html_id + " .link").html('<a href="' + IDOWA_ROOT + msg2 +'" target="' + target + '" class="' + ending + '">' + value + '</a>');
                    }
                });
            } else {
                window.setTimeout(function() {
                    setLinkWhenRawActual(content_id, html_id, value, target);
                }, 500);
            }
        }
    });
}

function initTabs() {
    $("ul.applyTabs").addClass("Tabs");
    var size = $("ul.Tabs li p.label").size();
    $("ul.Tabs li p.label").click(function() {
        var parent = $(this).parent();
        var index = $("ul.Tabs li.Tab").index(parent);
        var opend;
        $("ul.Tabs li p.label").each(function(i) {
            if(i == 0) {
                $(this).css("padding-left","0.83em");
                if(i == index) {
                    $(this).attr("class", "label firstActive");
                } else {
                    $(this).attr("class", "label firstInactive");
                }           
            } else {
                if(i == index + 1) {
                    $(this).attr("class", "label middleActiveInactive");
                }else if(i == index) {
                    $(this).attr("class", "label middleInactiveActive");
                } else {
                    $(this).attr("class", "label middleInactiveInactive");
                }
            }

            var p = $(this).parent();
            if(size == i + 1) {
                if(i == index) {
                    p.attr("class","Tab lastActive");
                } else {
                    p.attr("class","Tab lastInactive");
                }
            }

            if(p.find("div.content").css("display") != "none") {
                opend = p;
            }

        });

        var pCont = parent.find("div.content");
        if(opend != null) {            
            opend.find("div.content").css("display","none");
            pCont.css("display","inline");
            opend.height(opend.find("p.label").height())
        } else {
            pCont.css("display","inline");
        }
        parent.height(parent.find("div.content").height() + parent.find("p.label").height());
    });
    $("ul.Tabs li:first p").click();
}

function Idowa() {
}
Idowa.addFanzyZoom = function (selector) {
    $.getScript("/common/javascript/jquery.fancyzoom.min.js", function() {
        $(document).ready(function() {
            $(selector).fancyZoom();
        });
    });
}

Idowa.addHelp = function (selector) {
    $(selector).each(function () {
        var helptext = this.title;
        this.title = "";
        if(helptext != "") {
            $(this).append("<img class=\"help\" src=\"/common/resources/images/icons/help_16x16.gif\" alt=\"Hilfe\"/>");
            $(this).find("img.help").mouseenter(function(e) {
                Idowa.showTooltip($(this), helptext);
            }).mouseout(function (e) {
                Idowa.hideTooltip($(this));
            });
        }
    });
}

Idowa.showTooltip = function(object, text) {
    var tooltip = $("div#tooltip");
    if(tooltip.size() == 0) {
        $("body").append("<div id=\"tooltip\"></div>");
        tooltip = $("div#tooltip");
        tooltip.css('opacity','0');
        tooltip.css('display','inline');
    }
    tooltip.html(text);
    tooltip.css('top', object.offset().top - tooltip.outerHeight());
    tooltip.css('left',object.offset().left + object.outerWidth());
    tooltip.css('display','inline').fadeTo(1000, 0.7);
}

Idowa.hideTooltip = function(object) {
    var tooltip = $("div#tooltip");
    tooltip.hide();
}

Idowa.showSideBox = function(content) {
    sb = $("div.sideBox");
    br = $("div#BannerRechts");
    if(sb.size() == 0) {
        br.prepend("<div class=\"sideBox\"></div>");
        sb = $("div.sideBox");
    }
    sb.css("left", (br.offset().left + br.outerWidth(true)) + "px")
    sb.html(content);

    ready = true
    $(window).bind("scroll" , function(e) {
        Idowa.setSideBoxPosition(sb);
    });
    Idowa.setSideBoxPosition(sb);
    
    sb.fadeIn(5000);
}

Idowa.hideSideBox = function() {
    sb = $("div.sideBox");
    sb.fadeOut();
}

Idowa.setSideBoxPosition = function(sb) {
    st =  $(window).scrollTop();
    st = st + sb.outerHeight() / 2 + $(window).height() / 2;
    sb.stop().animate({
        "top": st + "px"
    },1000);
}

Idowa.setImageWhenReady = function(content_id, size_model_id, img_id) {
    $.ajax({
        type: "GET",
        url: "/common/content/isContentActual",
        data: "content_id=" + content_id + "&ajax=true",
        success: function(msg) {
            if(msg == "true") {
                $.ajax({
                    type: "GET",
                    url: "/common/content/getSizePath",
                    data: "content_id=" + content_id + "&size_model_id="+size_model_id+"&ajax=true",
                    success: function(msg2) {
                        $("#" + img_id).attr('src',IDOWA_ROOT + msg2);
                    }
                });
            } else {
                window.setTimeout(function() {
                    Idowa.setImageWhenReady(content_id, size_model_id, img_id);
                }, 500);
            }
        }
    });
}

Idowa.loadJQueryUI = function() {
    fileref=document.createElement("link")
    fileref.setAttribute("rel", "stylesheet")
    fileref.setAttribute("type", "text/css")
    fileref.setAttribute("href", "/common/css/blitzer/jquery-ui.css")
    document.getElementsByTagName("head")[0].appendChild(fileref)

    $.ajaxSetup({
        async: false
    });
    $.getScript("/common/javascript/jquery-ui.js");
    $.getScript("/common/javascript/ui.datepicker-de.js");
    $.ajaxSetup({
        async: true
    });

    $(function() {
        $("input.datepicker").each(function() {
            val = this.value;
            $(this).datepicker($.datepicker.regional['de']);
            $(this).datepicker('option',$.extend({
                changeYear: true
            }));
            if(val.length > 0) {
                tiles = val.split(".");
                $(this).datepicker('setDate', new Date(tiles[2], tiles[1] - 1, tiles[0]));
            }
        });
    });
}

Idowa.loadMailbox = function() {
    $.getScript("/common/javascript/client.js", function() {
        Client.loadMailbox();
    });
    
}


$(document).ready(function() {
    initInfobox()
    initFieldValidation();
    initTabs();
});

