// Начальные установки индикатора корзины
var box     = "eCartDiv"; // id перемещаемой области 
var dragbar = "dragbox"; // id области захвата


function setCart() {
   this.w = 120; // ширина индикатора
   this.h = 55; // высота индикатора
   this.l = 850; // положение относительно левого края
   this.t = 520; // положение относительно верхнего края
   return this; 

} // End function setCart


// Позиционирование индикатора
function Position() { 
   if( brw.ok ) { 
       obj.pixelLeft = cart.l + document.body.scrollLeft; 
       obj.pixelTop  = cart.t + document.body.scrollTop; 
    
   } // End if 

} // End function Position


// Тип браузера
function browse_check() {
   this.ver     = navigator.appVersion;
   this.agent   = navigator.userAgent;
   this.dom     = document.getElementById?1:0;
   this.opera5  = this.agent.indexOf( "Opera 5" )>-1;
   this.opera   = ( this.agent.indexOf( "Opera" )>-1 ) ? 1 : 0;
   this.firefox = ( this.agent.indexOf( "Firefox" )>-1 ) ? 1 : 0;
   this.ie5     = ( this.ver.indexOf( "MSIE 5" ) >-1 && this.dom && !this.opera5 ) ? 1 : 0;
   this.ie6     = ( this.ver.indexOf( "MSIE 6" ) >- 1 && this.dom && !this.opera5 ) ? 1 : 0;
   this.ie4     = ( document.all && !this.dom && !this.opera5 ) ? 1 : 0;
   this.ie      = this.ie4 || this.ie5 || this.ie6;
   this.ns6     = ( this.dom && parseInt( this.ver ) >= 5 ) ? 1 : 0;
   this.ns4     = ( document.layers && !this.dom ) ? 1 : 0;
   this.ok      = this.ie || this.opera;
   
   return this; 

} // End function browse_check
 
brw = new browse_check(); 
document.cookie = " check_esm=on; path=/";


// Конструктор индикатора
if( brw.ok ) { 
   cart = new setCart();
 
   html_ind = '<style type="text/css">\n#' + box + ' { left:-' + cart.w + 'px; top:-' + 
                cart.h + 'px; width:' + cart.w + 'px; height:'+cart.h + 
                'px; position:absolute; visibility:hidden; }\n</style>\n';
   html_ind += '<div id="' + box + '" style="z-index: 999; position: absolute;"><div style="width: ' + cart.w + 
               'px;" id="dragbox" onmousedown="initializedrag();"><img src="/esm/i/esm_title.gif" width="120" height="16" alt="eSales Manager - Корзина покупок" border="0"></div>';
   html_ind += '<iframe id="eCart" width="' + cart.w + '" height="' + cart.h + '" marginwidth="0" marginheight="0" scrolling="no" frameborder="0"></iframe></div>';
   document.write( html_ind ); 
   
   obj     = document.getElementById( box ).style; 
   obj_ind = document.getElementById( box ).document.getElementById( 'eCart' ); 

} // End if


// Разрешение экрана и размер окна браузера
function bwSize() {
   if( brw.ok ) { 
      this.w = document.body.offsetWidth - 20; 
      this.h = document.body.offsetHeight - 5; 

   } // End if
  
   if( self.screen ) { 
       this.sw = screen.width; 
       this.sh = screen.height; 
    
   } else if( self.java ) { 
       jkit = java.awt.Toolkit.getDefaultToolkit(); 
       scrsize = jkit.getScreenSize(); 
       this.sw = scrsize.width; 
       this.sh = scrsize.height; 
    
   }  // End if
  
   return this;
   
} // End function bwSize


// Первая загрузка корзины
function loadCart() { 

   rnd = parseInt( ( Math.random() * 1000000 + 1 ) ); 
   
   if( check_esm == "on" && cid != "" ) { 
      goods = "/cgi-bin/esm/add_to_cart.pl?" + rnd + "&cid=" + cid; 
      Position(); 
      obj.visibility = "visible"; 
      obj_ind.src = goods; 
      
   } else if( check_esm != "" && cid == "" ) { 
      goods = "about:blank"; 
      Position(); 
      obj.visibility = "hidden"; 
      obj_ind.src = "about:blank"; 

   } // End if
  
} // End function loadCart


// Добавление в корзину
function toCart( eIntID, eQuantity ) {
   rnd = parseInt( ( Math.random() * 1000000 + 1 ) ); 
   cid = check_esm="";
    
   esm_cookie = document.cookie + ";";
    
   re = new RegExp( "check_esm=([^;]*);" );
   if( re.exec( esm_cookie ) ) { check_esm = RegExp.$1; }
    
   re = new RegExp( "esm_cid=([^;]*);" );
   if( re.exec( esm_cookie ) ) { cid = RegExp.$1; }
   
   if( ( brw.ok && check_esm == "on" ) && eQuantity ) {
      goods="/cgi-bin/esm/add_to_cart.pl?" + rnd + "&cid=" + cid + "&quantity=" + eQuantity + "&goods_id=" + eIntID;
      //Position(); 
      obj.visibility = "visible"; 
      obj_ind.src = goods; 
      
   } else if( check_esm != "on" || brw.ns4 || brw.ns6 ) { 
      page = new bwSize();
      goods = "/cgi-bin/esm/cart.pl?" + rnd + "&quantity=" + eQuantity + "&goods_id=" + eIntID;
      wleft = ( page.sw - ( page.sw - 100 ) ) / 2; 
      wtop = ( page.sh - ( page.sh - 50 ) ) / 2;
      property = "left=" + wleft + ",top=" + wtop + ",width=" + ( page.sw - 100) + 
                 ",height=" + ( page.sh - 100 ) + 
                 ",toolbar=0,menubar=0,scrollbars=1,resizable=0";
      esm_win = window.open( goods, "esm_order" , property ); 
     
   } // End if
   
} // End function toCart


// перетаскивание 
function drag_drop(){
   if( brw.ok && dragapproved ){
      new_y = tempy + event.clientY - offsety;
      if( document.body.offsetHeight + document.body.scrollTop - cart.h < new_y ) { 
         crossobj.style.pixelTop = document.body.offsetHeight + document.body.scrollTop - cart.h;
      } else if( new_y < document.body.scrollTop ) {
         crossobj.style.pixelTop = document.body.scrollTop;
      } else {
         crossobj.style.pixelTop = new_y;
      } // End if
      new_x = tempx + event.clientX - offsetx;
      if( document.body.offsetWidth + document.body.scrollLeft - cart.w < new_x ) { 
         crossobj.style.pixelLeft = document.body.offsetWidth + document.body.scrollLeft - cart.w;
      } else if( new_x < document.body.scrollLeft ) {
         crossobj.style.pixelLeft = document.body.scrollLeft;
      } else {
         crossobj.style.pixelLeft = new_x;
      } // End if
      
      topOffset = crossobj.style.pixelTop - document.body.scrollTop;
      return false;
      
   } // End if
   
} // End function drag_drop


function initializedrag() {
   if( brw.ok && crossobj ) {
      
      var firedobj = event.srcElement;
      var topelement = document.compatMode && document.compatMode != "BackCompat" ? "documentElement" : "body";
      while( firedobj.tagName != topelement.toUpperCase() && firedobj.id != dragbar ) {
         firedobj = firedobj.parentElement;
         
      } // End while
   
      if( firedobj.id == dragbar ) {
         offsetx = event.clientX;
         offsety = event.clientY;
         
         tempx = crossobj.style.pixelLeft ? parseInt( crossobj.style.pixelLeft ) : 0;
         tempy = crossobj.style.pixelTop ? parseInt( crossobj.style.pixelTop ) : 0;
         
         dragapproved = true;
         document.onmousemove = drag_drop;
         
      } // End if
      
   } // End if
   
} // End function initializedrag

var shiftMinimize = 0;
var topOffset     = 0;


function initMove() {
   crossobj = document.all[ box ];
   crossobj.onmouseup = new Function( "dragapproved=false" );
   topOffset = crossobj.style.pixelTop;
   setInterval( "doMove()", 30 );
   
} // End function initMove


function doMove() {
   var shiftPage = document.body.scrollTop;
   if( shiftPage != shiftMinimize ){
      dif = ( shiftPage - shiftMinimize ) >> 2;
      if( dif ) shiftMinimize += dif; 
      else shiftMinimize = shiftPage;
      crossobj.style.pixelTop = shiftMinimize + topOffset;
      
   } // End if

} // End function doMove


cid = check_esm= "";
esm_cookie = document.cookie + ";";
 
re = new RegExp( "check_esm=([^;]*);" );
if( re.exec( esm_cookie ) ) { check_esm = RegExp.$1; }
 
re = new RegExp( "esm_cid=([^;]*);" );
if( re.exec( esm_cookie ) ) { cid = RegExp.$1; }

if( brw.ok ) { 
   loadCart(); 
   onresize = Position; 
   if( !brw.opera ) { 
      window.onload = initMove;
      document.getElementById( dragbar ).style.cursor = 'hand';
      
   } // End if

   
} // End if



