﻿// JScript File

function ShowMessageBox(Title,Message,Link,LinkText,ElementToPositionOn)
{
    CloseMessageBox();
    var newdiv = document.createElement('div');
    newdiv.className = "MessageBoxDiv";
    newdiv.id = "MessageBoxDiv";
    
    var top = document.createElement('div');
    top.className = "MessageBoxTop";
    newdiv.appendChild(top);
    
    var body = document.createElement('div');
    body.className = "MessageBoxBody";
    newdiv.appendChild(body);
    
    var header = document.createElement('div');
    header.id = 'MessageBoxHeader';
    header.className = "MessageBoxHeader";
    header.innerHTML = Title;
    body.appendChild(header);
    
    var message = document.createElement('div');
    message.className = "MessageBoxMessage";
    message.innerHTML = Message;
    body.appendChild(message);
    
    var bottom = document.createElement('div');
    bottom.className = "MessageBoxBottom";
    newdiv.appendChild(bottom);
    
    if(Link != null)
    {
      var link = document.createElement('a');  
      link.href = Link;
      link.innerHTML = LinkText;
      link.className = 'MessageBoxLink';
      body.appendChild(link);
    }
    
    document.body.appendChild(newdiv);
    
    if (ElementToPositionOn !=null)
    {
        PositionDivOnElementTop(newdiv, ElementToPositionOn);
    }
    else
    {
        CenterDivOnMouse(newdiv);
    }
  
    var floatingBehavior = new AjaxControlToolkit.FloatingBehavior($get(newdiv.id));
    floatingBehavior.set_handle($get(header.id));
    floatingBehavior.initialize();
   
    MethodQueue.add( 'CloseMessage.Callback', 5000, true, function() { CloseMessageBox();} );
    
    
}



function CloseMessageBox()
{
    var div = document.getElementById('MessageBoxDiv');
    if(div != null) document.body.removeChild(div);
   
}

function RemoveArticlePanel(pid)
{
   var div = document.getElementById(pid);
   var maindiv = document.getElementById('pnl_main');
   if(div != null) maindiv.removeChild(div);
   
}

function CheckDelete(ID, pid)
{
    CloseConfirmBox();
    var newdiv = document.createElement('div');
    newdiv.className = "MessageBoxDiv";
    newdiv.id = "MessageBoxConfirm";
    
    var top = document.createElement('div');
    top.className = "MessageBoxTop";
    newdiv.appendChild(top);
    
    var body = document.createElement('div');
    body.className = "MessageBoxBody";
    newdiv.appendChild(body);
    
    var header = document.createElement('div');
    header.id = 'ConfirmHeaderDiv';
    header.className = "MessageBoxQuestionHeader";
    header.innerHTML = "Confirm Removal?";
    body.appendChild(header);
    
    var message = document.createElement('div');
    message.className = "MessageBoxMessage";
    message.innerHTML = "Are you sure you wish to remove this article from your fashionfile?";
    body.appendChild(message);
    
    var bottom = document.createElement('div');
    bottom.className = "MessageBoxBottom";
    newdiv.appendChild(bottom);

    var yeslink = document.createElement('a');  
    yeslink.innerHTML = 'Yes';
    yeslink.className = 'MessageBoxYesLink';
    yeslink.href = 'javascript:ConfirmDelete('+ID+', \''+pid+'\');';
    
    var nolink = document.createElement('a');  
    nolink.innerHTML = 'No';
    nolink.className = 'MessageBoxNoLink';
    nolink.href = 'javascript:CloseConfirmBox();';
    
      
    body.appendChild(yeslink);
    body.appendChild(nolink);
    
    document.body.appendChild(newdiv);
    
    var floatingBehavior = new AjaxControlToolkit.FloatingBehavior($get(newdiv.id));
    floatingBehavior.set_handle($get(header.id));
    floatingBehavior.initialize();
    
    CenterDivOnMouse(newdiv);
   
  
}

function ConfirmDelete(ID, pid)
{
    UserProfileService.DeleteStoredArticle(ID);
    RemoveArticlePanel(pid);
    CloseConfirmBox();
    window.setTimeout('GetArticleCount()',300);;
}

function CloseConfirmBox()
{
    var div = document.getElementById('MessageBoxConfirm');
    if(div != null) document.body.removeChild(div);
   
}

function PositionDivOnElementTop(DivToAlign,Element)
{
    var pos = findPos(Element);
    var newleft = pos[0] - DivToAlign.offsetWidth + 65;
    if (newleft < 0) newleft = 0;
    var newtop  = pos[1] - DivToAlign.offsetHeight +10;
    if (newtop < 0) newtop = 0;
    if (newleft < 0) newy = 0;
    
     DivToAlign.style.top = newtop + "px";
     DivToAlign.style.left = newleft +"px";
     DivToAlign.style.position = "absolute";  

}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function CenterDivOnMouse(DivToAlign)
{
    
    var newleft = mLocation.x - DivToAlign.offsetWidth;
    var newtop  = mLocation.y - DivToAlign.offsetHeight;
    if (newtop < 0) newtop = 0;
    if (newleft < 0) newy = 0;
    
     DivToAlign.style.top = newtop + "px";
     DivToAlign.style.left = newleft +"px";
     DivToAlign.style.position = "absolute";  

}

var IE = document.all?true:false; //Test to see if IE
if (!IE) { document.captureEvents(Event.MOUSEMOVE); }//If not IE

document.onmousemove = findCoords;
        
        function Point(x,y) {  this.x = x; this.y = y; }
        mLocation = new Point(0,0);


function findCoords(e) {
   if( !e ) 
   { e = window.event; } 
   if( !e || ( typeof( e.pageX ) != 'number' && typeof( e.clientX ) != 'number' ) ) 
   { return [ 0, 0 ]; }
   
   if( typeof( e.pageX ) == 'number') 
   {  var posX = e.pageX; 
      var posY = e.pageY; 
      mLocation.x = posX;
      mLocation.y = posY;
   } 
   else 
   {
      var posX = e.clientX; var posY = e.clientY;
      if( !( ( window.navigator.userAgent.indexOf( 'Opera' ) + 1 ) || ( window.ScriptEngine && ScriptEngine().indexOf( 'InScript' ) + 1 ) || window.navigator.vendor == 'KDE' ) ) {
         if( document.documentElement && ( document.documentElement.scrollTop || document.documentElement.scrollLeft ) ) {
            posX += document.documentElement.scrollLeft; posY += document.documentElement.scrollTop;
         } else if( document.body && ( document.body.scrollTop || document.body.scrollLeft ) ) {
            posX += document.body.scrollLeft; posY += document.body.scrollTop;
         }
      }
      mLocation.x = posX;
      mLocation.y = posY;
   }
}

function CenterDivOnWindow(DivToAlign, x, y)
{       
        if (x == null) x = -1;
        if (y == null) y = -1;
        var scrollLeft = (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
        var scrollTop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
        var clientBounds = CommonToolkitScripts.getClientBounds();
        var clientWidth = clientBounds.width;
        var clientHeight = clientBounds.height;
     
        var isIE6 = (Sys.Browser.agent == Sys.Browser.InternetExplorer && Sys.Browser.version < 7);
        
        if(x < 0)
        {
            DivToAlign.style.left = scrollLeft+((clientWidth-DivToAlign.offsetWidth)/2)+'px';
        }
        else
        {
            if(isIE6)
            {
                DivToAlign.style.position = 'absolute';
                DivToAlign.style.left = (x + scrollLeft) + 'px';
            }
            else
            {
                DivToAlign.style.position = 'fixed';
                DivToAlign.style.left = x + 'px';
            }
        }
        if(y < 0)
        {
            DivToAlign.style.top = scrollTop+((clientHeight-DivToAlign.offsetHeight)/2)+'px';
        }
        else
        {
            if(isIE6)
            {
                DivToAlign.style.position = 'absolute';
                DivToAlign.style.top = (y + scrollTop) + 'px';
            }
            else
            {
                DivToAlign.style.position = 'fixed';
                DivToAlign.style.top = y + 'px';
            }
        }
        
    
   
   /* var ypos = 0;
      var xpos = 0;
      if (window.pageYOffset) {
        ypos = window.pageYOffset;
        xpos = window.pageXOffset;
      }
      else {
        ypos = document.documentElement.scrollTop;
        xpos = document.documentElement.scrollLeft;
      }

      var top = ypos + (screen.availHeight/2) - 350;
      var left = xpos + (screen.availWidth/2) - 150;

    divMain.style.top = top + "px";
      divMain.style.left = left + "px";
      divMain.style.display = "block";   
    */

}

var MethodQueue = 
{
    delays : [],
    timerIDs : [],
    add : function(id, delay, resetIfExist, func )
    {
        var queue = MethodQueue.delays[delay]; // get the queue for this delay
        
        if( null == queue ) queue = [];
        if( null == queue[id] ) // first time registration
        {
            queue[id] = { id: id, code: func, reset: false }; // register the function in the delay queue
        }
        else
        {
            // same id, same delay already exist. If reset defined, then store the new function and discard previous one
            if( resetIfExist ) { queue[id] = { id: id, code: func, reset: true }; }
            else { return; }
        }
        
        MethodQueue.delays[delay] = queue; // register the queue in the delay
        
        if( MethodQueue.timerIDs[delay] == null )
        {
            MethodQueue.timerIDs[delay] = window.setTimeout( function()
            {
                MethodQueue.execute(delay);                
            }, delay );
        }
    },
    execute : function( delay )
    {
        var delayQueue = MethodQueue.delays[ delay ];
        
        for( var key in delayQueue )
        {
            var item = delayQueue[key];
            if( item != null )
            {
                if( typeof item.code == "function" )
                {
                    if( !item.reset )   // if this method was not reset, then execute it
                    {
                        delayQueue[key] = null; // remove this item, incase the code tries to register again, it will get this key free
                        item.code();    // execute the code, note it might register the timer callback again
                        delete item.code;                         
                    }
                    else
                    {
                        item.reset = false;
                    }
                }
                
            }
        }
        
        // find out if there's any callback left in the queue.
        // it might happen that, when a callback was called, it
        // registered in the same delay queue again
        var allCallbackCalled = true;
        for( var key in delayQueue )
        {
            var item = delayQueue[key];
            if( item != null )
            {
                if( typeof item.code == "function" )
                {
                    allCallbackCalled = false;
                }
            }
        }
        
        if( allCallbackCalled ) // if all callbacks in the queue fired, we can remove this queue and timer
        {        
            delete delayQueue;
            MethodQueue.delays[ delay ] = null;
            delete MethodQueue.delays[ delay ];
            
            window.clearTimeout( MethodQueue.timerIDs[delay] );
            MethodQueue.timerIDs[delay] = null;
            delete MethodQueue.timerIDs[delay];
        }
        else
        {
            // re-register the timer
            MethodQueue.timerIDs[delay] = window.setTimeout( function()
            {
                MethodQueue.execute(delay);                
            }, delay );
        }
    },
    exist : function( id, delay )
    {
        var queue = MethodQueue.delays[delay]; 
        
        if( null == queue ) queue = [];
        return ( null != queue[id] );        
    },
    remove : function( id, delay )
    {
        var queue = MethodQueue.delays[delay]; 
        
        if( null == queue ) return;
        else queue[id] = null;
        
        MethodQueue.delays[delay] = queue;
    }
}


