/**************************************************************

	Script		: Popup class
	Version		: 0.0.1
	Desc		: Provides basic popup class

**************************************************************/

/****** Enums ************************************************/
var RelativePositionType =
{
    element : 0,
    elementRight: 1,
    elementBottom: 2,
    mouse: 3,
    screenTopLeft: 4,
    screenCenter: 5,
    screenBottomRight: 6
};

/****** Popup Class ******************************************/
var Popup = new Class({
        Implements: [Options, Events],
        // Default options
        options: {
            relativePositionType: RelativePositionType.mouse,
            offset:
            {
                x: 0,
                y: 0
            },
            positionElementID: null // The element which positioning will relate to if the relative position type is one of the element types
        },
        
        // Constructor
        initialize: function(id, dragID, exitID, options){
                this.setOptions(options);
                this.popup = $(id);
                this.dragElement = $(dragID);
                this.exitElement = $(exitID);
                this.width;
                this.height;
                this.isPopupDragging = false;
                
                if ($defined(this.options.positionElementID))
                {
                    this.positionElement = $(this.options.positionElementID);
                }
        },
        
       
        
        // Shows the popup
        Show: function()
        {
            // var timeout = setTimeout(function(){this.OnExit;},   4000);
            // Set height and width
            this.width  = window.innerWidth  ? window.innerWidth  : document.documentElement.clientWidth;
            this.height = window.innerHeight ? window.innerHeight : document.documentElement.clientHeight;

            // Show the popup
            this.popup.style.position = "absolute";
            this.popup.style.display  = "block";
            
            // Set the popup position
            switch(this.options.relativePositionType)
            {
                case RelativePositionType.mouse:
                    var scrollLeft = window.getScrollLeft();
                    var scrollTop = window.getScrollTop();
                    var left = (window.getScrollLeft() + MousePosition.x + this.options.offset.x);
                    var top = (window.getScrollTop() + MousePosition.y + this.options.offset.y);
                    
                    // Make sure that the popup stays within the limits of the page.
                    if(this.popup.offsetWidth > (window.getWidth() - MousePosition.x))
                    {
                        left -= this.popup.offsetWidth;
                    }
                    
                    if (this.popup.offsetHeight > 100 )
                    {
                        top = window.getScrollTop() + 40;
                    }
                    
                    this.popup.style.left = left + 'px';
                    this.popup.style.top  = top + 'px';
                    break;
                    

            }
            
            // Attach the drag element to mouse moving
            if (this.dragElement != null)
            {
                this.dragElement.addEvent('mousedown', this.OnMouseIsDown.bind(this));
            }
            
/*
            drag_element['target']   = id;
            drag_element.onmousedown = popup_mousedown_window;
*/
            if (this.exitElement != null)
            {
                this.exitElement.addEvent('click', this.OnExit.bind(this));
            }
        },

        // Fired when the drag element is clicked
        OnMouseIsDown: function(e)
        {
            this.isPopupDragging = true;
            this.dragOldX = e.event.clientX
            this.dragOldY = e.event.clientY
            document.addEvent('mousemove', this.OnMouseMoving.bind(this));
            document.addEvent('mouseup', this.OnMouseIsUp.bind(this));
        },

        // Fired when the drag element is moving
        OnMouseMoving: function(e)
        {
            this.popup.style.left = (this.popup.offsetLeft + e.event.clientX - this.dragOldX)+"px";
            this.popup.style.top = (this.popup.offsetTop + e.event.clientY - this.dragOldY)+"px";
            this.dragOldX = e.event.clientX;
            this.dragOldY = e.event.clientY;
        },
        
        // Fired when dragging of the drag element ends
        OnMouseIsUp: function(e)
        {
            this.isPopupDragging = false;
            document.removeEvents('mousemove');
            document.removeEvents('mouseup');
        },
        
        
        // Fired when the exit element is clicked
        OnExit: function(e)
        {
//          var ie      = navigator.appName == "Microsoft Internet Explorer";
//          var element = document.getElementById(popup_target);

//          popup_mouseup(e);
          this.popup.style.display = 'none'; // this - refers to close button and not popup!
          
          document.addEvent('mousemove', this.OnMouseDown.bind(this));
          
            if (this.exitElement != null)
            {
                this.exitElement.removeEvent('click', this.OnExit);
            }
        },
        
        // Fired when mouse is down
        OnMouseDown: function(e)
        {
          MousePosition.x = e.client.x;
          MousePosition.y = e.client.y;
        }
});

/****** cMousePosition Class ******************************************/
var cMousePosition = new Class({
        Implements: [Options, Events],

        // Constructor
        initialize: function()
        {
            this.x = 0;
            this.y = 0;
            
            // Track the mouse position with OnMouseDown method
            document.addEvent('mousemove', this.OnMouseDown.bind(this))
        },
        
        // Fired when mouse is down
        OnMouseDown: function(e)
        {
          this.x = e.client.x;
          this.y = e.client.y;
        }
        
});
var MousePosition = new cMousePosition();
/*************************************************************/

function ShowReview(content, logo, logoText, logoLink, playLink, newwindow)
{
   {
      // Set review content
      if (content != '')
      {
        $('ReviewContent').set('text', content);
      }
      // Set review logo image
      if (logo != '')
      {
        $('ReviewLogo').set('src', logo);
      }
      // Set review logo text
      if (logoText != '')
      {
        $('ReviewLogoText').set('text', logoText);
      }
      // Set review Play now button link
      if (newwindow=="true") {
        LogoLink=logoLink;
        PlayLink=playLink;
        pop = window.open('/FeedTemplates/review.htm','review','width=300,height=280,left=250,top=150');
        //Tip(content, CLICKCLOSE,true, OFFSETY,-30,WIDTH,468);
        return false;
      }  
      else 
      {
          if (playLink != '')
          {
            $('ReviewPlayNowButton').removeEvents('click');
            $('ReviewPlayNowButton').addEvent('click', function(e) {popMax(playLink);});
          }
          if (logoLink != '')
          {
            $('ReviewLogoLink').removeEvents('click');
            $('ReviewLogoLink').addEvent('click', function(e) {popMax(logoLink);});
          }
          reviewPopup.Show();
      }    
   }    
}





function ShowFilter(filterColumnID)
{
  
  filterPopup.Show();
  // filterColumnID is for filtering
  if (filterColumnID != '' && filterColumnID != null)
  {
    var filter=filters[filterColumnID];
    $('FilterColumnID').set('value', filterColumnID);
    
    if(filter!=null){
        $('FilterContent').value = filter['FilterContent'];
    } else {
        $('FilterContent').value = "";
    }
     // Set the focus to the filter content input box
    $('FilterContent').focus();
    
    // Attach a keyboard listener for 'enter' strokes
    $('FilterContent').addEvent('keydown', function(event)
        {
            // If 'enter' was clicked with filter content, filter table
            if (event.key == 'enter')
            {
                feedTable.filter();
                filterPopup.OnExit();
                return false; // Prevent page refresh
            }
        });
        
     // If a the user clicks outside the filter popup, hide the filter.
    document.addEvent('mousedown', function(event)
        {
            // Hide popup if the target of the mousedown event was not a child of a filter popup.
            if ($('FilterPopup').hasChild(event.target) == false)
            {
                filterPopup.OnExit();
            }
        });
  }
}

function ShowListFilter(filterColumnID) {
  feedTable.createCheckFilterValues(filterColumnID);
  listFilterPopup.Show();
    $('ListFilterColumnID').set('value', filterColumnID);
      // Attach a keyboard listener for 'enter' strokes
    $('ListFilterPopup').addEvent('keydown', function(event)
        {
            // If 'enter' was clicked with filter content, filter table
            if (event.key == 'enter')
            {
                feedTable.listFilter();
                listFilterPopup.OnExit();
                return false; // Prevent page refresh
            }
        });
        
     // If a the user clicks outside the filter popup, hide the filter.
    document.addEvent('mousedown', function(event)
        {
            // Hide popup if the target of the mousedown event was not a child of a filter popup.
            if ($('ListFilterPopup').hasChild(event.target) == false)
            {
                listFilterPopup.OnExit();
            }
        });
 
    }
