﻿JSGridViewReservation.prototype.PopUpHeight = 530;
JSGridViewReservation.prototype.PopUpWidth = 790;
function JSGridViewReservation(objectId, id, parentHtmlElement, enviorment, columnNum, rowNum, lenght, x, y, width, height, reservationDateTimeStr, isConfirmed, isUpLimit, isDownLimit, noMoveText, shopperName, shopperGuid, publicMode, colorNumber, groupId, noGroupMoveText) 
{
    this._PublicMode = publicMode;
    this._Id = id;
    this._ObjectId = objectId;
    this._ParentHtmlElement = parentHtmlElement;
    this._ColumnNum = columnNum;
    this._RowNum = rowNum;
    this._Lenght = parseInt(lenght);
    this._X = x;
    this._Y = y;
    this._Width = width;
    this._Height = height;
    this._Eviorment = enviorment;
    this._ReservationDateTimeStr = reservationDateTimeStr;
    this._IsConfirmed = isConfirmed;
    this._IsUpLimit = isUpLimit;
    this._IsDownLimit = isDownLimit;
    this._MainCss;
    this._MainSelectedCss;
    this._ContentCss;
    this._ContentSelectedCss;
    this._H1Css;
    this._H1SelectedCss;
    this._ClickMeCss = 'headerText';
    this._InnerHeight;
    this._IsActive=false;
    this._NoMoveText = noMoveText;
    this._ShopperName = shopperName;
    this._ShopperGuid = shopperGuid;
    this._colorNumber = colorNumber;
    this._groupId = groupId;
    this._noGroupMoveText = noGroupMoveText;
    
    this.ReservetionDiv = document.createElement("div");
    
    this.defineDomElementClasses();
    this.defineDomElement();
    this.DefineDomElementEvents();
    this.drowHtmlElement();
}

JSGridViewReservation.prototype.defineDomElementClasses = function() 
{
    var cssMainPart;
    var cssDownLimitPart;
    var cssUpLimitPart;

    if (this._IsDownLimit) {
        cssDownLimitPart = " downLimit";
    }
    else {
        cssDownLimitPart = "";
    }

    if (this._IsUpLimit) {
        cssUpLimitPart = " upLimit";
    }
    else {
        cssUpLimitPart = "";
    }

    this._MainCss = "reservation color" + this._colorNumber + cssDownLimitPart;
    this._MainSelectedCss = this._MainCss + " over";
    this._ContentCss = "text";
    this._ContentSelectedCss = this._ContentCss + " over";
    this._H1Css = cssUpLimitPart;
    this._H1SelectedCss = this._H1Css + " over";
}

JSGridViewReservation.prototype.defineDomElement = function() 
{
    this.ReservetionDiv.className = this._MainCss;
    this.RepositionDoomElement();
}

JSGridViewReservation.prototype.RepositionDoomElement = function() 
{
    this.ReservetionDiv.style.zIndex = "1";
    var height = this._Height + 8;
    this._InnerHeight = height - 23;
    if (this._IsDownLimit) 
    {
        height -= 9;
    }
    this.ReservetionDiv.style.top = this._Y + "px";
    this.ReservetionDiv.style.left = this._X + "px";
    this.ReservetionDiv.style.width = this._Width + "px";
    this.ReservetionDiv.style.height = height + "px";
    this.UpdateText();
}

JSGridViewReservation.prototype.RerenderDomElement = function(newDateTimeStr) 
{
    this._ReservationDateTimeStr = newDateTimeStr;
    this.RepositionDoomElement();
}

JSGridViewReservation.prototype.SetDataAndPos = function(newX, newY, xInPx, yInPx, newHeight) 
{
    this._ColumnNum = newX;
    this._RowNum = newY;
    this._X = xInPx;
    this._Y = yInPx;
    this._Height = newHeight;
    
    this.RepositionDoomElement();
}

JSGridViewReservation.prototype.UpdateText = function() 
{
    if (this._IsActive) {
        h1Css = this._H1SelectedCss;
        contextCss = this._ContentSelectedCss;
    }
    else {
        h1Css = this._H1Css;
        contextCss = this._ContentCss;
    }

    this.ReservetionDiv.innerHTML = '';

    var content = document.createElement('div');
    content.setAttribute('class', contextCss);
    content.style.height = this._InnerHeight + 'px';
    content.style.width = (this._Width - 2) + 'px';

    //this.ReservetionDiv.innerHTML = '<h1 class="' + h1Css + '"></h1>\n';
    var header = document.createElement('h1');
    header.setAttribute('class', h1Css);
    this.ReservetionDiv.appendChild(header);

    var contentText = document.createElement("span");
    contentText.innerHTML = this._ShopperName;
    content.appendChild(contentText);

    //this.ReservetionDiv.appendChild(content); // adding this way bugs out in IE7, reservation becomes transperent, use workouround with innerHTML
    var tempParent = document.createElement('div');
    tempParent.appendChild(content);
    this.ReservetionDiv.innerHTML += tempParent.innerHTML;
    contentText = this.ReservetionDiv.getElementsByTagName('span')[0];
    if (!this._PublicMode) {
        var shopperHref = 'Shoppers.aspx?id=' + this._ShopperGuid + '&keepThis=true&TB_iframe=true&height=360&width=480&modal=true';
        var linkEventHanlder = new JSGridViewReservationLinkHandler(contentText, shopperHref);
    }

    var clickMeDiv = document.createElement("div");
    clickMeDiv.className = this._ClickMeCss;
    clickMeDiv.innerHTML = this._ReservationDateTimeStr;

    if (!this._PublicMode) {
        var reservationHref = 'Reservation.aspx?id=' + this._ObjectId + '&parentNoRefresh=true&keepThis=true&TB_iframe=true&height=' + this.PopUpHeight + '&width=' + this.PopUpWidth + '&modal=true';
        var eventHandler = new JSGridViewReservationLinkHandler(clickMeDiv, reservationHref);
    }
    this.ReservetionDiv.appendChild(clickMeDiv);
}

JSGridViewReservation.prototype.getParentHtmlElement = function() 
{
    return this._ParentHtmlElement;
}

JSGridViewReservation.prototype.DefineDomElementEvents = function(eventHeandler) 
{
    var eventHandler = new JSGridViewReservationDomEventHandler(this);

    this.ReservetionDiv.onclick = function(e) 
    {
        eventHandler.handleLeftClick(e); 
    }
    
    /*this.ReservetionDiv.ondblclick = function(e) {
        eventHandler.handleLeftDoubleClick(e); 
    }*/

    this.ReservetionDiv.onmousedown = function(e) 
   {
        eventHandler.handleMouseDown(e);
        return false;
    }

    /*this.ReservetionDiv.onmouseup = function(e) 
    {
    eventHandler.handleMouseUp(e);
    }
    
    this.ReservetionDiv.onmousemove = function(e) 
    {
    eventHandler.handleMouseMove(e);
    } */

    this.ReservetionDiv.ondragstart = function(e) 
    {
        return false;
    }

    this.ReservetionDiv.onmouseout = function(e) 
    {
        eventHandler.handleMouseOut(e);
    }
}

JSGridViewReservation.prototype.drowHtmlElement = function() 
{

    this._ParentHtmlElement.appendChild(this.ReservetionDiv);
}

JSGridViewReservation.prototype.setActive = function() 
{
    this._IsActive=true;
    this.ReservetionDiv.className = this._MainSelectedCss;
    this.UpdateText();
}

JSGridViewReservation.prototype.setInactive = function() 
{
    this._IsActive=false;
    this.ReservetionDiv.className = this._MainCss;
    this.UpdateText();
}


//----------------------------------------------------------------------------------------------------
//-------------------------------------------EVENT HANDLING-------------------------------------------
//----------------------------------------------------------------------------------------------------
function JSGridViewReservationDomEventHandler(source) {
    this._Source = source;

    this.handleLeftClick = function(e) {
        if (e == null) {
            e = window.event;
        }
    }
    
    // handles dragging the reservation on the grid
    this.handleMouseDown = function(e) {
        if (e == null) {
            e = window.event;
        }

        if (this._Source._Eviorment.UtilsFunc.isLeftButton(e) && !this._Source._PublicMode) {
            if (this._Source._IsUpLimit || this._Source._IsDownLimit) {
                jAlert(this._Source._NoMoveText);
            } else if (this._Source._groupId != null) {
                jAlert(this._Source._noGroupMoveText);
            } else {
                this._Source.ReservetionDiv.style.zIndex = "2";
                this._Source._Eviorment.ReservationLeftMousePressed(this._Source, e);
            }
        }
    }

    this.handleLeftDoubleClick = function(e) 
    {
        if (e == null) 
        {
            e = window.event;
        }

        e.cancelBubble = true;
        if (e.stopPropagation) 
        {
            e.stopPropagation();
        }
    }

    this.handleMouseDrag = function(e) 
    {
        if (e == null) 
        {
            e = window.event;
        }
    }

    this.handleMouseOut = function(e) 
    {
        if (e == null) 
        {
            e = window.event;
        }
    }
}

//----------------------------------------------------------------------------------------------------
//-------------------------------------------EVENT HANDLING-------------------------------------------
//----------------------------------------------------------------------------------------------------
//
// Set thickbox link on element, pointing to href. Handle onmousedown, onmouseover, onmouseout to simulate link and cancel reservation mouse event
// @element - dom element to be a link
// @href - location to go to when clicked
function JSGridViewReservationLinkHandler(element, href) 
{
    element.onmousedown = function(e) 
    {
        var pageLink;

        if (e == null) {
            e = window.event;
        }
        e.cancelBubble = true;

        if (e.stopPropagation) {
            e.stopPropagation();
        }

        tb_show(null, href, null);
    }

    element.onmouseover = function(e) 
    {
         if (e == null) {
             e = window.event;
         }
    }

    element.onmouseout = function(e) 
    {
        if (e == null) {
            e = window.event;
        }
    }
}
