
if(typeof window.controls == "undefined")
    window.controls = {};

var currentMonthId = "";

function CalendarWidget(widgetId){
    this.hoverDay = null;
    this.previous_day = null;
    this.selected_date = "";
    this.id = widgetId;
}

function DayMouseOver(id, e, year, month, day)
{
    window.controls[id].DayMouseOver(e, year, month, day);
}

function DayMouseOut(id, e, year, month, day)
{
    window.controls[id].DayMouseOut(e, year, month, day);
}

function DayClick(id, e, year, month, day)
{
    window.controls[id].DayClick(e, year, month, day);
}

function AddEvent(id, element, e){
    window.controls[id].AddEvent(element, e);
}

CalendarWidget.prototype.AddEvent = function(element, e)
{
    var evt = e ? e : window.event;
    if(evt.keyCode == 13){
        document.forms[0].onsubmit = function () { return false; };
	    evt.cancelBubble = true;
	    if (evt.stopPropagation) evt.stopPropagation();
	    var date = $ektron("input[name=calendar-event]");
        date.attr("value", this.selected_date);
        var action = $ektron("input[name=calendar-action]");
        action.attr("value", "add");
        $ektron("#" + this.id + " .addEventButton").get(0).click();
        return false;
	}
    return true;
}

/*function DoAddEvent()
{
    var date = $ektron("input[name=calendar-event]");
    date.attr("value", selected_date);
}*/

CalendarWidget.prototype.DayClick = function(e, year, month, day)
{
    this.selected_date = (month<10?"0":"")+month+(day<10?"0":"")+day+year;

    var events = $ektron("#" + this.id + " #day-edit"+(month<10?"0":"")+month+"-"+(day<10?"0":"")+day+"-"+year);
    var other = $ektron("#" + this.id + " #day-editOther");

    if(currentMonth[this.id + "_cal"] == month){
        currentMonthId = this.id;
        $ektron("#" + this.id + " .event-add").slideDown(300,function(){
            $ektron("#" + currentMonthId + " .event-add").slideDown(300, function(){});
        });
    }else
        $ektron("#" + currentMonthId + " .event-add").slideUp(300, function(){});

    if(events.length > 0)
    {
        if(this.previous_day != null)
        {
            var id = this.id;
            $ektron(this.previous_day).slideUp(300,function(){
                if(currentMonth[id + "_cal"] == month)
                    events.slideDown(300,function(){});
                else
                    this.previous_day = null;
            });
        }
        else
            events.slideDown(300,function(){});

        this.previous_day = events.get(0);
    }else{

        if(this.previous_day != null)
        {
            var id = this.id;
            $ektron(this.previous_day).slideUp(300,function(){
                $ektron("#" + id + " #day-editOtherTitle").html("<h3>"+(month<10?"0":"")+month+"-"+(day<10?"0":"")+day+"-"+year+"</h3>");
                $ektron("#" + id + " #day-editOtherTitle").css("display", "block");
                if(currentMonth[id + "_cal"] == month)
                    other.slideDown(300,function(){});
                else
                    this.previous_day = null;
            });
        }
        else
        {
            $ektron("#" + this.id + " #day-editOtherTitle").html("<h3>"+(month<10?"0":"")+month+"-"+(day<10?"0":"")+day+"-"+year+"</h3>");
            $ektron("#" + this.id + " #day-editOtherTitle").css("display", "block");
            if(currentMonth[this.id + "_cal"] == month)
                other.slideDown(300,function(){});
        }
        this.previous_day = other;
    }
}

CalendarWidget.prototype.DayMouseOut = function(e, year, month, day)
{
    try{
        if(this.hoverDay != null)
        {
            $ektron("#" + this.hoverDay.id).hide();
            if($ektron.browser.msie)
                $ektron("#" + this.hoverDay.id).appendTo(this.lastParent.get(0));
        }
    }
    catch(err){
        alert("Error " + err);
    }
    this.hoverDay = null;
}

CalendarWidget.prototype.DayMouseOver = function(e, year, month, day)
{
    var events = $ektron("#" + this.id + " #day"+(month<10?"0":"")+month+"-"+(day<10?"0":"")+day+"-"+year);

    if(events.length > 0)
    {
        var target;
        if($ektron.browser.msie)
            target = $ektron(e.srcElement);
        else
            target = $ektron(e.target);

        var tdOffset = target.offset();
        var left = tdOffset.left - events.width()/2 + target.width()/2;

        if(left < 0)
        {
            var dayHeader = events.find(".day-header");
            left = tdOffset.left - 30 + target.width()/2;

            if($ektron.browser.version == 6)
                dayHeader.css("background-position", "00px");
            else
                dayHeader.css("background-position", "10px");
        }
        events.css("display", "block");
        events.css("z-index", "999");
        events.css("position", "fixed");
        events.css("left", left);

        events.css("top", tdOffset.top + target.height());

        this.lastParent = events.parent();
        if($ektron.browser.msie)
            events.appendTo("body");
        this.hoverDay = events.get(0);
    }
}

if( typeof Sys != "undefined" ){
   Sys.Application.notifyScriptLoaded();
}
