/**
 * @fileoverview  Special "Der Münchner" scripts
 *
 * @package     Süddeutsche
 * @subpackage  Special "Der Münchner"
 * @author      Szabi
 * @author      Murat Purc <m.purc@edelweiss72.de>
 * @requires    Prototype JavaScript Framework
 *
 */


// check for existing SZ core
if (typeof(sz) !== "object") {
    sz = {};
}


sz.TipOfTheDay = {

    linkColor: "#003399",

    init: function (linkColor) {
        if (linkColor) {
            this.linkColor = linkColor;
        }
        $("themaderwoche_nav").style.display  = "block";
        $("tippdestages_cont2").style.display = "none";
        $("tippdestages_cont3").style.display = "none";
        $("themaderwoche_nav1").style.color = this.linkColor;

        // register mouseover functions
        $("themaderwoche_nav1").onmouseover = function() {
            sz.TipOfTheDay.display('1');
        };
        $("themaderwoche_nav2").onmouseover = function() {
            sz.TipOfTheDay.display('2');
        };
        $("themaderwoche_nav3").onmouseover = function() {
            sz.TipOfTheDay.display('3');
        };

    },

    display: function(pIndex) {
        var strNavObj  = "themaderwoche_nav" + pIndex;
        var strContObj = "tippdestages_cont" + pIndex;
        $("themaderwoche_nav1").style.color = "#000000";
        $("themaderwoche_nav2").style.color = "#000000";
        $("themaderwoche_nav3").style.color = "#000000";
        $("tippdestages_cont1").style.display = "none";
        $("tippdestages_cont2").style.display = "none";
        $("tippdestages_cont3").style.display = "none";
        $(strNavObj).style.color = this.linkColor;
        $(strContObj).style.display = "block";
    }
}



/**
 * Class to handle events search form.
 *
 * Adds several event handler to form fields such as validation if date
 */
sz.EventSearchForm = {

    oValidator:    null,

    fromField:    "",

    toField:      "",

    message:      "",

    submitButton: "",


    register: function(fromField, toField, message, submitButton) {

        sz.EventSearchForm.fromField    = $(fromField);
        sz.EventSearchForm.toField      = $(toField);
        sz.EventSearchForm.message      = $(message);
        sz.EventSearchForm.submitButton = $(submitButton);
        sz.EventSearchForm.oValidator   = new sz.FormValidator();

        $(fromField).onblur = function() {
            if (this.value == "") {
                this.value = "TT.MM.JJJJ";
            } else {
//                var result = sz.EventSearchForm.checkField(this.id, "Von");
            }
        }
        $(fromField).onfocus = function() {
            if (this.value == "TT.MM.JJJJ") {
                this.value = "";
            }
        }

        $(toField).onblur = function() {
            if (this.value == "") {
                this.value = "TT.MM.JJJJ";
            } else {
//                var result = sz.EventSearchForm.checkField(this.id, "Bis");
            }
        }
        $(toField).onfocus = function() {
            if (this.value == "TT.MM.JJJJ") {
                this.value = "";
            }
        }

        $(submitButton).onclick = function() {
            var result = sz.EventSearchForm.checkField(sz.EventSearchForm.fromField, "Von");
            if (!result) {
                return false;
            }

            var result = sz.EventSearchForm.checkField(sz.EventSearchForm.toField, "Bis");
            if (!result) {
                return false;
            }

            var aFrom = $(sz.EventSearchForm.fromField).value.split(".");
            aFrom[1]  = parseInt(aFrom[1]) - 1;
            aFrom[2]  = parseInt(aFrom[2]);

            var aTo   = $(sz.EventSearchForm.toField).value.split(".");
            aTo[1]    = parseInt(aTo[1]) - 1;
            aTo[2]    = parseInt(aTo[2]);
            var oFromDate = new Date(aFrom[2], aFrom[1], aFrom[0]);
            var oToDate   = new Date(aTo[2], aTo[1], aTo[0]);

            if (oFromDate > oToDate) {
                sz.EventSearchForm.displayMsg("Von-Datum sollte vor Bis-Datum liegen");
                return false;
            }
            return true;
        }
    },

    checkField: function (field, text) {
        var aDate = $(field).value.split(".");
        if (aDate.length !== 3) {
            sz.EventSearchForm.displayMsg("Ungültiges " + text + "-Datum (1)");
            $(field).focus();
            return false;
        }
        if (aDate[2].length != 4) {
            sz.EventSearchForm.displayMsg("Das Jahr im " + text + "-Datum sollte 4-stellig sein.");
            $(field).focus();
            return false;
        }

        aDate[1] = parseInt(aDate[1]) - 1;
        aDate[2] = parseInt(aDate[2]);
        if (!sz.EventSearchForm.oValidator.isValidDate(aDate[0], aDate[1], aDate[2])) {
            sz.EventSearchForm.displayMsg("Ungültiges " + text + "-Datum (2)");
            $(field).focus();
            return false;
        }
        sz.EventSearchForm.displayMsg("");
        return true;
    },


    displayMsg: function (msg) {
        if (msg != "") {
            alert(msg);
        }
/*
        sz.EventSearchForm.message.innerHTML = msg;
        if (msg != "") {
            sz.EventSearchForm.message.style.display = "block";
        } else {
            sz.EventSearchForm.message.style.display = "none";
        }
*/
    }

}

var sCurrentOpenedPopuplayer = '';

/**
 * Displays or hides a element by passed id
 */
function togglePopupLayer(elementId) {
    if (!$(elementId)) {
        return;
    }

    if (sCurrentOpenedPopuplayer !== '' && sCurrentOpenedPopuplayer !== elementId) {
        // close previous opened layer
        togglePopupLayer(sCurrentOpenedPopuplayer);
    }

    if (!$(elementId).style.display || $(elementId).style.display == "none") {
        $(elementId).style.display = "block";
        sCurrentOpenedPopuplayer = elementId;
    } else {
        $(elementId).style.display = "none";
        sCurrentOpenedPopuplayer = '';
    }
}



/**
 * Form validator class. Provides several functions beeing used to check content
 * of form elements form.
 *
 * @todo:  Is in design phase
 * @class  FormValidator
 */
sz.FormValidator = Class.create({

    strip:   false,

    /**
     * Constructor
     *
     * @param  {Object}  options  Options as json object
     */
    initialize: function(options) {
        if (typeof(options) !== "undefined") {
            if (typeof(options.strip)  !== "undefined") {
                this.strip = options.strip;
            }
        }
    },

    isNumeric: function(elem, options) {
        var val = this._getValue(elem, options);
        return !isNaN(val);
    },

    isNotNumeric: function(elem, options) {
        var val = this._getValue(elem, options);
        return isNaN(val);
    },

    isValidDate: function(day, month, year, options){
        var obj = new Date(year, month, day);
//        alert(day + " = " + obj.getDate() + "\n" +month + " = " + obj.getMonth() + "\n" + year + " = " + obj.getFullYear());
        return ((day == obj.getDate()) && (month == obj.getMonth()) && (year == obj.getFullYear()));
    },


    _getValue: function(elem, options) {
        var obj = (typeof(elem) == "string") ? $(elem) : elem;
        if (!obj || !obj.value) {
            return null;
        }

        var val   = obj.value;
        var strip = this.strip;

        if (typeof(options) !== "undefined") {
            if (typeof(options.strip)  !== "undefined") {
                strip = options.strip;
            }
        }

        if (strip) {
            val = val.strip();
        }

        return val;
    }

});



/**
 * Zeichen zählen
 */
function CheckLen(Target) {
    var maxlength = "250"; //die maximale Zeichenlänge
    StrLen=window.document.forms["referal"].elements["ref_message"].value.length;

    if (StrLen == 1 && Target.value.substring(0,1)==" ") {
        Target.value="";
        StrLen=0;
    }
    if (StrLen > maxlength ) {
        Target.value=Target.value.substring(0,maxlength);
        CharsLeft=0;
    } else {
        CharsLeft = maxlength-StrLen;
    }
    document.getElementById("strchars").innerHTML = "(max. "+CharsLeft+" Zeichen)";
}



