//----------------------------------------------------------------
// Copyright© 2008 New Vintage Software Inc.
//----------------------------------------------------------------

function LL_AJAX_CLIENT(IsDebug) {

  this.NoAjax = (!GetAJAXConnection());
  this.DebugMode = 0;
  var BaseURL = location.protocol + "//" + location.hostname + "/"  
  
//  alert(location.protocol)
  
  if (location.hostname == "nvs") BaseURL = BaseURL + "www.rittenhouse.ca/"
  
  var BaseInclude = BaseURL + "settings/java/ajax/"    
  var LastURL = "";

  var LL_Includes = Array("LLShipratePopup.js","LLQuickView.js","LLCartPreview.js");  
  for (var iii in LL_Includes) document.write("<SCRIPT type=\"text/javascript\" src=\"" + BaseInclude + LL_Includes[iii] + "\"></SCRIPT>");
  
  
  var MaxRequests = 3;
  var MaxTimeout = 5000;

  var ActiveRequests = 0;
  var RequestedViews = Array(0);
  this.Views = Array(0);  
  
  this.QueryString = new LoadQueryString()
  if (this.QueryString["noajax"]=="true") this.NoAjax = true

  this.DragDiv = null;
  this.DragDeltaX =0; 
  this.DragDeltaY =0; 
  this.oldonselectstart = null;



  this.Request = function(URL, Params, Callback_Function, Timeout_Function) {     
  
    if (RequestedViews[URL] && !Params) return
    if (ActiveRequests > MaxRequests || URL < 1) return     
    RequestedViews[URL] = true
    ActiveRequests++;
    
    LastURL = URL;  
    var FullURL = BaseURL + URL;
    if (Params) FullURL += "?" + Params;
    if (this.DebugMode) alert("Posting URL: " + FullURL);  
 
    var Request = GetAJAXConnection();                          
    var Timeout = setTimeout(function() {LLAPP.TimeoutCallBack(Request,Timeout_Function,LastURL); },MaxTimeout) 
    Request.open("GET", FullURL, true);                   
    Request.onreadystatechange = function() {LLAPP.ReceivedCallBack(Request,Timeout,Callback_Function,LastURL); };     // pass the request and timer object
    Request.send(""); 
  }



  this.ReceivedCallBack = function(Request, Timeout, CallBack, URL) {
      if (Request.readyState != 4) return;

      clearTimeout(Timeout)

      RequestedViews[URL] = false
      ActiveRequests--;
      if (ActiveRequests < 0) ActiveRequests = 0;

      if (Request.responseText.length > 0) {
          if (this.DebugMode) alert("Received: " + Request.responseText.length + " bytes for " + URL);
          this.Views[URL] = Request.responseText
          //      try {

          if (CallBack) CallBack(Request.responseText);
          //      } catch(failed) {
          //        debugger
          //        alert("Error: unable to execute callback:" + CallBack + "\n" + failed.message);
          //      }            
      } else {
          if (this.DebugMode) alert("Error: Server returned an empty string");
      }
  }

  this.TimeoutCallBack = function(request,Timeout){ 
 
    request.abort(); 
    if (this.DebugMode) alert("Timeout Error: no result received from server before timeout.");        
    try {
      if (Timeout) Timeout();
    } catch(failed) {
      alert("Error: unable to execute callback:" + Timeout  + "\n" + failed.message);
    }
  }
  
  
  
  function GetAJAXConnection() {
    var request = false;
    try { request = new XMLHttpRequest();
    } catch (trymicrosoft) {
      try { request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (othermicrosoft) {
        try { request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (failed) {
           request = false;
    }}}         
    return request
  }




  //-----------------------------------------------------------------------------------------------------------
  // FrameBuster           --- called if body has a LL_FrameBuster attribute
  //-----------------------------------------------------------------------------------------------------------
      this.FrameBuster = function() { if (top!=self.parent) top.location=self.parent.location; }


  //-----------------------------------------------------------------------------------------------------------
  // LoadQueryString
  //-----------------------------------------------------------------------------------------------------------
      function LoadQueryString() {
        if (!location.search) return;       
        var qs = (location.search.slice(1)).split("&")
        for (var i in qs) {
          var qsx = qs[i].split("=")
          this[qsx[0]] = qsx[1];              
      }}          



  // this.VirtualizeBody = function(BodyDiv)   {                                      // virtualizes a new div as the page body
  //    window.onload = function() {LLAPP.Initalize();}  //??? still needed?????// 
  //    window.onbeforeunload = function() {LLAPP.beforeUnload();}
  //  }


  //-----------------------------------------------------------------------------------------------------------
  // Navigate
  //-----------------------------------------------------------------------------------------------------------
     this.Navigate = function(URL) {
       if (window.location) {
         window.location = BaseURL + URL;
       } else {
         window.navigate(BaseURL + URL);
       }
     }

  //-----------------------------------------------------------------------------------------------------------
  // Window Width
  //-----------------------------------------------------------------------------------------------------------  
      this.windowWidth = function() {  
        if(typeof( window.innerWidth ) == 'number' ) {     //Non-IE
          return window.innerWidth;
        } else if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {    //IE 6+ in 'standards compliant mode'
          return document.documentElement.clientWidth;
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {      //IE 4 compatible
          return document.body.clientWidth;
        }
      }  
            
  //-----------------------------------------------------------------------------------------------------------
  // Window Height
  //-----------------------------------------------------------------------------------------------------------  
      this.windowHeight = function() {
        if(typeof( window.innerHeight ) == 'number' ) {     //Non-IE
          return window.innerHeight;
        } else if(document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {    //IE 6+ in 'standards compliant mode'
          return document.documentElement.clientHeight;
        } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {      //IE 4 compatible
          return document.body.clientHeight;
        }
      }  

  //-----------------------------------------------------------------------------------------------------------
  // Scroll X
  //-----------------------------------------------------------------------------------------------------------  
     this.scrollX = function() {
       if( typeof( window.pageYOffset ) == 'number' ) {       //Netscape compliant
         return window.pageXOffset;
       } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {    //DOM compliant
         return document.body.scrollLeft;
       } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) { //IE6 standards compliant mode     
         return document.documentElement.scrollLeft;
       } else {
         return 0;
       }
     }
  
  //-----------------------------------------------------------------------------------------------------------
  // Scroll Y
  //-----------------------------------------------------------------------------------------------------------  
     this.scrollY = function() {     
       if( typeof( window.pageYOffset ) == 'number' ) {       //Netscape compliant
         return window.pageYOffset;
       } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {    //DOM compliant
         return document.body.scrollTop;
       } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) { //IE6 standards compliant mode     
         return document.documentElement.scrollTop;
       } else {
         return 0;         
       }
     }


  //-----------------------------------------------------------------------------------------------------------
  // Set Widget Opacity
  //-----------------------------------------------------------------------------------------------------------  
      this.setWidgetOpacity = function(node,val) {
        if (node.filters) {
          try { node.style.filter="alpha(opacity=" + (val*100) + ")"} catch (e) {}
        } else {
          node.style.opacity = val;
        }
      }


  //-----------------------------------------------------------------------------------------------------------
  // Center Widget
  //-----------------------------------------------------------------------------------------------------------  
      this.centerWidget = function(node,offsetX,offsetY) {
        node.style.left= this.scrollX() + (this.windowWidth() - offsetX) /2;      
        node.style.top= this.scrollY() + (this.windowHeight() - offsetY) / 2;      
      }
 
  //-----------------------------------------------------------------------------------------------------------
  // Popup Widget 
  //-----------------------------------------------------------------------------------------------------------  
      this.popupWidget = function(innerHTML) {
        var Node = document.createElement("DIV");        
        Node.innerHTML = innerHTML;
        document.body.appendChild(Node)      
        Node.style.zindex = 1000
        Node.style.position = "absolute"        
        return Node;                               
      }
 

  //-----------------------------------------------------------------------------------------------------------
  // On Start Drag
  //-----------------------------------------------------------------------------------------------------------  
     this.onStartDrag = function(Node,evt) { 
       evt = new Evt(evt);      
       this.DragDiv = Node;
       this.setWidgetOpacity(this.DragDiv,.8);
       this.DragDeltaX = evt.x - parseInt(this.DragDiv.style.left);
       this.DragDeltaY = evt.y - parseInt(this.DragDiv.style.top);              
       
       this.oldonselectstart = document.body.onselectstart;
       document.body.onselectstart = function() {return false;};
       document.body.unselectable = "on";
       document.body.style.MozUserSelect = "none";   
       
       Evt.addEventListener(document,"mousemove",LLAPP.onDragMove,false);
       Evt.addEventListener(document,"mouseup",LLAPP.onDragRelease,false);
       
     }

  //-----------------------------------------------------------------------------------------------------------
  // On Drag Move 
  //-----------------------------------------------------------------------------------------------------------  
     this.onDragMove= function(evt) {       
       evt = new Evt(evt);       
       LLAPP.DragDiv.style.left = evt.x - LLAPP.DragDeltaX;          
       LLAPP.DragDiv.style.top = evt.y - LLAPP.DragDeltaY;
       evt.consume();
       
 
     }

  //-----------------------------------------------------------------------------------------------------------
  // On Drag Release
  //-----------------------------------------------------------------------------------------------------------  
     this.onDragRelease= function(evt) {
       evt = new Evt(evt);
       LLAPP.setWidgetOpacity(LLAPP.DragDiv,1);  

       document.body.onselectstart = LLAPP.oldonselectstart;
       document.body.unselectable = "off";
       document.body.style.MozUserSelect = "text";   
              
       Evt.removeEventListener(document,"mousemove",LLAPP.onDragMove,false);   // ??? how to address ???
       Evt.removeEventListener(document,"mouseup",LLAPP.onDragRelease,false);   
       
     }

}




 
//----------------------------------------------------------
    var LLAPP = new LL_AJAX_CLIENT();     
    
  //  LLAPP.DebugMode = 1;
//----------------------------------------------------------





function Evt(evt) {
this.evt = (!!evt) ? evt : window.event; 
this.source = this.evt.target ? this.evt.target : this.evt.srcElement;
this.x = this.evt.pageX ? this.evt.pageX : this.evt.clientX;
this.y = this.evt.pageY ? this.evt.pageY : this.evt.clientY;
}

Evt.prototype.toString = function () {
return "Evt [ x = " + this.x + ", y = " + this.y + " ]";
};

Evt.prototype.consume = function () {
if (this.evt.stopPropagation) {
this.evt.stopPropagation();
this.evt.preventDefault();
} else if (this.evt.cancelBubble) {
this.evt.cancelBubble = true;
this.evt.returnValue = false;
}
};

Evt.addEventListener = function (target,type,func,bubbles) {
if (document.addEventListener) {
target.addEventListener(type,func,bubbles);
} else if (document.attachEvent) {
target.attachEvent("on"+type,func,bubbles);
} else {
target["on"+type] = func;
}
};

Evt.removeEventListener = function (target,type,func,bubbles) {
if (document.removeEventListener) {
target.removeEventListener(type,func,bubbles);
} else if (document.detachEvent) {
target.detachEvent("on"+type,func,bubbles);
} else {
target["on"+type] = null;
}
};





















