//<![cdata[
    
/* *******************************************************************************
'*  File Name:       CalendarDetails.js
'*  Created By:      Carlos Muniz
'*  Description:     Client side logic used to display and edit the details of an event
'*********************************************************************************/    

var window_width;
var window_height;
var JSONobj;
var editEvntRuning = false;


    /* *******************************************************************************
    '*  Function name:   EditEvent
    '*  Created By:      Carlos Muniz
    '*  Description:     Displays an edit window for the event
    '*  Input parameters:None
    '*  Return Value:    None
    '*********************************************************************************/    
function EditEvent(EventId)
{
    loadEditModal(JSONobj, "#EditingMask", "#EventEdit"); 
}
    /* *******************************************************************************
    '*  Function name:   loadModal
    '*  Created By:      Carlos Muniz
    '*  Description:     Load values into the modal display window
    '*  Input parameters:JSONobj with parameter, maskid, windowId
    '*  Return Value:    None
    '*********************************************************************************/    
function loadModal(JSONobj, mask, modalWindow)
{

    $("#DtlEventTitleArea").html(JSONobj.Title);
    $("#DtldpStartDate").html(JSONobj.StartDateTime);
    $("#DtldpEndDate").html(JSONobj.EndDateTime);

    $("#DtlEventDescriptionArea").html(JSONobj.Description);
    if ($.trim(JSONobj.Location.toString()) == "")
    {
        $("#DtlEvntLocationArea").parent().css("display", "none");

    } else {
        $("#DtlEvntLocationArea").html(JSONobj.Location);
        $("#DtlEvntLocationArea").parent().css("display", "table-row");
    }
    
    if ($.trim(JSONobj.Price.toString()) == "" || JSONobj.Price.toString() == "0")
    {
        $("#DtlEvntPriceArea").parent().css("display", "none");

    } else {
        $("#DtlEvntPriceArea").html(JSONobj.Price);
        $("#DtlEvntPriceArea").parent().css("display", "table-row");
    }
    if ($.trim(JSONobj.PointOfContact.toString()) == "")
    {
        $("#DtlEvntPointOfContactArea").parent().css("display", "none");

    } else {
        $("#DtlEvntPointOfContactArea").html(JSONobj.PointOfContact);
        $("#DtlEvntPointOfContactArea").parent().css("display", "table-row");
    }
    
    if ($.trim(JSONobj.GalleryId.toString()) == "" || JSONobj.GalleryId.toString() == "0")
    {
        $("#DtlEvntGalleryIdArea").parent().css("display", "none");

    } else {
        $("#DtlEvntGalleryIdArea").html(JSONobj.GalleryId);
        $("#DtlEvntGalleryIdArea").parent().css("display", "table-row");
    }
    if ($.trim(JSONobj.UpdateUser.toString()) == "" || JSONobj.UpdateUser.toString() == "0")
    {
        $("#DtlEvntUpdateUserArea").parent().css("display", "none");

    } else {
        $("#DtlEvntUpdateUserArea").html(JSONobj.UpdateUser);
        $("#DtlEvntUpdateUserArea").parent().css("display", "table-row");
    }
 
    if ($.trim(JSONobj.DateUpdated.toString()) == "")
    {
        $("#DtlEvntDateUpdatedArea").parent().css("display", "none");

    } else {
        $("#DtlEvntDateUpdatedArea").html(JSONobj.DateUpdated);
        $("#DtlEvntDateUpdatedArea").parent().css("display", "table-row");
    };
    //alert (getCookie('AUTH_USER'));
    if ($("#IsUserCalendarAdmin").val()=='TRUE')
    {
       $('#dtlEditEventButton').css ("display", "inline");
       $('#EditCopyEventButton').css ("display", "inline");
       $('#EditDeleteEventButton').css ("display", "inline");
       
       
    } else {
       $('#dtlEditEventButton').css ("display", "none");
       $('#EditCopyEventButton').css ("display", "none");
       $('#EditDeleteEventButton').css ("display", "none");
    }  
    $('#EditCreateEventButton').css ("display", "none");    
    
    addClickHandlersDetail();
    // Geth the height and widht of the modal window
    var modal_height = $(modalWindow).outerHeight();
    var modal_width = $(modalWindow).outerWidth();
    //calculate top and left offset needed for centering
    var top = (window_height-modal_height)/2;
    var left = (window_width-modal_width)/2;
        
    // Apply new top and left css values
    $(modalWindow).css({
        'top':top, 
        'left':left
    });
    show_modal(modalWindow,mask,.8);
}
    /* *******************************************************************************
    '*  Function name:   loadEmptyModal
    '*  Created By:      Carlos Muniz
    '*  Description:     Load a version of the modal display window where all the values are blank
    '*  Input parameters:maskid, windowId
    '*  Return Value:    None
    '*********************************************************************************/    
function loadEmptyModal($target, mask, modalWindow)
{
    var $selectedDate = new Date(document.getElementById("hselectedYear").value, document.getElementById("hselectedMonth").value,document.getElementById("hselectedDay").value, 0,0,0 );
    $selectedDate.setDate($target[0].innerHTML);   

    
    $('#EditEventTitleArea').val("");

    $("#EditdpStartDate").datepicker('setDate', ($selectedDate.getMonth() + 1) + "/" + $selectedDate.getDate() + "/" + $selectedDate.getFullYear());
    $("#EditdpEndDate").datepicker('setDate', ($selectedDate.getMonth() + 1) + "/" + $selectedDate.getDate() + "/" + $selectedDate.getFullYear());
        
    $("#EditEventDescriptionArea").val("");
    
    $("#EditEvntLocationArea").html("");
    $("#EditEvntLocationArea").parent().css("display", "table-row");
    
    $("#EditEvntPriceArea").html(0);
    $("#EditEvntPriceArea").parent().css("display", "table-row");

    $("#EditEvntPointOfContactArea").html("");
    $("#EditEvntPointOfContactArea").parent().css("display", "table-row");
    
    $("#EditEvntGalleryIdArea").html(0);
    $("#EditEvntGalleryIdArea").parent().css("display", "table-row");

    $("#EditEvntUpdateUserArea").html("");
    $("#EditEvntUpdateUserArea").parent().css("display", "table-row");
 

    $('#EditCreateEventButton').css ("display", "block");
    
    $('#EditEventButton').css ("display", "none");
    $('#EditDeleteEventButton').css ("display", "none");
    
    //isUserCalendarManager
    //addClickHandlersCalendar ();
    addClickHandlersDetail();
    // Geth the height and widht of the modal window
    var modal_height = $(modalWindow).outerHeight();
    var modal_width = $(modalWindow).outerWidth();
    //calculate top and left offset needed for centering
    var top = (window_height-modal_height)/2;
    var left = (window_width-modal_width)/2;
        
    // Apply new top and left css values
    $(modalWindow).css({
        'top':top, 
        'left':left
    });
    show_modal(modalWindow,mask,1);
}
    /* *******************************************************************************
    '*  Function name:   loadEditModal
    '*  Created By:      Carlos Muniz
    '*  Description:     Load values into the modal edit window
    '*  Input parameters:JSONobj with parameter, maskid, windowId
    '*  Return Value:    None
    '*********************************************************************************/    

function loadEditModal(JSONobj, mask, modalWindow)
{
    var dateObj;
    var timecomp;
    var elemStr;
    $('#EditEventTitleArea').val(JSONobj.Title);

    //*************************************************
    //  Start Date
    //*************************************************

    $("#EditdpStartDate").datepicker('setDate', JSONobj.StartDateTimeMonth  + "/" + JSONobj.StartDateTimeDay + "/" + JSONobj.StartDateTimeYear);
    
    //dateObj = new Date(JSONobj.StartDateTime);
    // Set hour drop down for start date selector
    timecomp =JSONobj.StartDateTimeHour;
    if (timecomp > 12)
        {
            timecomp = timecomp - 12;
        }
    elemStr = "EditEventSDHour"+timecomp;
    document.getElementById(elemStr).selected=true;
    
    //Set minute drop down for Start date selector
    timecomp = Math.round (JSONobj.StartDateTimeMinute/15) * 15;
    elemStr = "EditEventSDMinute"+timecomp;
    document.getElementById(elemStr).selected=true;
    
    //**************************************************
    // End Date
    //*************************************************
    $("#EditdpEndDate").datepicker('setDate', JSONobj.EndDateTimeMonth  + "/" + JSONobj.EndDateTimeDay + "/" + JSONobj.EndDateTimeYear);

    //dateObj = new Date(JSONobj.EndDateTime);
    // Set hour drop down for End date selector
    timecomp =JSONobj.EndDateTimeHour;
    if (timecomp > 12)
        {
            timecomp = timecomp - 12;
        }
    elemStr = "EditEventEDHour"+timecomp;
    document.getElementById(elemStr).selected=true;
    
    //Set minute drop down for Start date selector
    timecomp = Math.round (JSONobj.EndDateTimeMinute/15) * 15;
    elemStr = "EditEventEDMinute"+timecomp;
    document.getElementById(elemStr).selected=true;
        
    $("#EditEventDescriptionArea").val(JSONobj.Description);
    
    if ($.trim(JSONobj.Location.toString()) == "")
    {
        $("#EditEvntLocationArea").parent().css("display", "none");

    } else {
        $("#EditEvntLocationArea").html(JSONobj.Location);
        $("#EditEvntLocationArea").parent().css("display", "table-row");
    }
    
    if ($.trim(JSONobj.Price.toString()) == "" || JSONobj.Price.toString() == "0")
    {
        $("#EditEvntPriceArea").parent().css("display", "none");

    } else {
        $("#EditEvntPriceArea").html(JSONobj.Price);
        $("#EditEvntPriceArea").parent().css("display", "table-row");
    }
    if ($.trim(JSONobj.PointOfContact.toString()) == "")
    {
        $("#EditEvntPointOfContactArea").parent().css("display", "none");

    } else {
        $("#EditEvntPointOfContactArea").html(JSONobj.PointOfContact);
        $("#EditEvntPointOfContactArea").parent().css("display", "table-row");
    }
    
    if ($.trim(JSONobj.GalleryId.toString()) == "" || JSONobj.GalleryId.toString() == "0")
    {
        $("#EditEvntGalleryIdArea").parent().css("display", "none");

    } else {
        $("#EditEvntGalleryIdArea").html(JSONobj.GalleryId);
        $("#EditEvntGalleryIdArea").parent().css("display", "table-row");
    }
    if ($.trim(JSONobj.UpdateUser.toString()) == "" || JSONobj.UpdateUser.toString() == "0")
    {
        $("#EditEvntUpdateUserArea").parent().css("display", "none");

    } else {
        $("#EditEvntUpdateUserArea").html(JSONobj.UpdateUser);
        $("#EditEvntUpdateUserArea").parent().css("display", "table-row");
    }
 
    if ($.trim(JSONobj.DateUpdated.toString()) == "")
    {
        $("#EditEvntDateUpdatedArea").parent().css("display", "none");

    } else {
        $("#EditEvntDateUpdatedArea").html(JSONobj.DateUpdated);
        $("#EditEvntDateUpdatedArea").parent().css("display", "table-row");
    };
   //alert (getCookie('AUTH_USER'));
    if ($("#IsUserCalendarAdmin").val()=='TRUE')
    {
       $('#EditEditEventButton').css ("display", "block");
    } else {
       $('#EditEditEventButton').css ("display", "none");
    }
    
    //isUserCalendarManager
    //addClickHandlersCalendar ();
    addClickHandlersDetail();
    // Geth the height and widht of the modal window
    var modal_height = $(modalWindow).outerHeight();
    var modal_width = $(modalWindow).outerWidth();
    //calculate top and left offset needed for centering
    var top = (window_height-modal_height)/2;
    var left = (window_width-modal_width)/2;
        
    // Apply new top and left css values
    $(modalWindow).css({
        'top':top, 
        'left':left
    });
    show_modal(modalWindow,mask,1);
}
    /* *******************************************************************************
    '*  Function name:   show_modal
    '*  Created By:      Carlos Muniz
    '*  Description:     Display modal window
    '*  Input parameters:modal window id, maskid
    '*  Return Value:    None
    '*********************************************************************************/    

function show_modal(modal_id,mask, opacity)
{
    //set the display to block and opacity to 0 so we can use fadeto
    $(mask).css({
        'display':'block', 
        opacity:0
    });
        
    // fade in the mask to opacity 0.8
    $(mask).fadeTo(500, opacity);
        
    //Show the modal window
    $(modal_id).fadeIn(500);
}

    /* *******************************************************************************
    '*  Function name:   constructQueryString
    '*  Created By:      Carlos Muniz
    '*  Description:     Construct the parameters expectd by the ajax routine
    '*  Input parameters:None
    '*  Return Value:    None
    '*********************************************************************************/
function constructEventQueryString($EventId, action)
{
    var return_string = "";
    return_string = "SelectedEvent=" + $EventId + "&";
    return_string = return_string + "action=" + action;
        
    return return_string;
}

    /* *******************************************************************************
    '*  Function name:   constructUpateEventQueryString
    '*  Created By:      Carlos Muniz
    '*  Description:     Construct the parameters expectd by the ajax routine for the update operation
    '*  Input parameters:None
    '*  Return Value:    Query String
    '*********************************************************************************/
function constructUpateEventQueryString($EventId, action)
{
    var return_string = "";
    return_string = "SelectedEvent=" + $EventId + "&";
    return_string = return_string + "action=" + action + "&";
    return_string = return_string + "title=" + escape($('#EditEventTitleArea').val()) + "&";
    return_string = return_string + "startdatetime=" +  escape($("#EditdpStartDate").val() + " " + $("#EditEventSDHour").val() + ":" + $("#EditEventSDMinute").val() + ":00 " + $("#EditEventSDAMPM").val()) + "&";
    return_string = return_string + "enddatetime=" +  escape($("#EditdpEndDate").val() + " " + $("#EditEventEDHour").val() + ":" + $("#EditEventEDMinute").val() + ":00 " + $("#EditEventEDAMPM").val()) + "&";
    return_string = return_string + "description=" +  escape($("#EditEventDescriptionArea").val());
  //  return_string = return_string + "location=" + $("#EditEvntLocationArea").html() + "&";
  // return_string = return_string + "Price=" + $("#EditEvntPriceArea").html(JSONobj.Price) + "&";
  //  return_string = return_string + "Contact=" + $("#EditEvntPointOfContactArea").html() + "&";
  //  return_string = return_string + "GalleryId=" + $("#EditEvntGalleryIdArea").html();
        
    return return_string;
}

    /* *******************************************************************************
    '*  Function name:   addClickHandlersDetail
    '*  Created By:      Carlos Muniz
    '*  Description:     Set event handlers for detail events
    '*  Input parameters:None
    '*  Return Value:    None
    '*********************************************************************************/    

function addClickHandlersDetail ()
{
    $('#calendarData tr td').unbind();
    $('#dtlcloseModal').unbind();
    $('#EditEvent').unbind();
    $('#dtlEditEventButton').unbind();
    $('#EditdpStartDate').unbind();
    $('#EditdpEndDate').unbind();
    $('#EditCreateEventButton').unbind();
    $('#EditDeleteEventButton').unbind();
    $('#EditcloseModal').unbind();
    $('#EditCopyEventButton').unbind();
    
    $('#calendarData tr td').click(function(event)
    {
        event.preventDefault();
        var $target = $(event.target);
        if ($target.is('a'))
            { 
                var eventId = $target.attr('id');
                SelectEvent(eventId);
            } else { 
                if ($target.is('td'))
                    {
                        if ($("#IsUserCalendarAdmin").val()=='TRUE')
                            {
                                loadEmptyModal($target, "#EditingMask", "#EventEdit");
                            }
                    }
                
            }
    });

    $('#dtlcloseModal').click(function(e)
    {
        e.preventDefault();
        $('#ModalMask').fadeOut(500);
        $('#EventDetail').fadeOut(500);
    });
    $('#EditEvent').click(function(e)
    {
        e.preventDefault();
        EditEvent();
    });
    $('#dtlEditEventButton').click(function(e)
    {
        e.preventDefault();
        EditEvent();
    });
    
    $('#EditdpStartDate').datepicker({
        showButtonPanel: true,
        changeMonth: true,
	changeYear: true
    });
    $('#EditdpStartDate').datepicker( "option", "dateFormat", "mm/dd/yy" );
    $('#EditdpEndDate').datepicker({
        showButtonPanel: true,
        changeMonth: true,
	changeYear: true
    });
    $('#EditdpEndDate').datepicker( "option", "dateFormat", "mm/dd/yy" );
    
    $('#EditEventButton').click(function(e)
    {
        e.preventDefault();
        UpdateEvent();
        $('#EditingMask').fadeOut(500);
        $('#EventEdit').fadeOut(500);
        
        $('#ModalMask').fadeOut(500);
        $('#EventDetail').fadeOut(500);
        
        CurrMonth();
    });
    
    $('#EditDeleteEventButton').click(function(e)
    {
    });
    $('#EditcloseModal').click(function(e)
    {
        e.preventDefault();
        $('#EditingMask').fadeOut(500);
        $('#EventEdit').fadeOut(500);
    });
    $('#EditCreateEventButton').click(function(e)
    {
        e.preventDefault();
        InsertEvent();
              
        $('#EditingMask').fadeOut(500);
        $('#EventEdit').fadeOut(500);
        
        $('#ModalMask').fadeOut(500);
        $('#EventDetail').fadeOut(500);
        
        CurrMonth();
    });
    
    $('#EditCopyEventButton').click(function(e)
    {
        e.preventDefault();
        InsertEvent();
        
        $('#EditingMask').fadeOut(500);
        $('#EventEdit').fadeOut(500);
        
        $('#ModalMask').fadeOut(500);
        $('#EventDetail').fadeOut(500);
        
        CurrMonth();
    });
    
}
//]]>

