
var preloadimgs= new Array();
window.addEvent('load', function() { 

  //images preload
  $each($$('#menu img,.itemImageSize img,.itemBasketBuy'),function(item) { 
      preloadimgs[preloadimgs.length-1]=new Image(); 
      preloadimgs[preloadimgs.length-1].src= ($(item).getAttribute('src')+'').replace(/\.png$/i,'-over.png'); 
  });  

  //fix footer
  var a=quirkgetPageSize(); 
  if(a[1]<=a[3])
    $('content').setStyle('height', Math.abs(a[3]-400) );

  //main menu buttons
  $$('#menu img').addEvent('mouseover',function() {
       if($(this).getAttribute('src').indexOf('-over.png')<0)
        $(this).setAttribute('src',($(this).getAttribute('src')+'').replace(/\.png/i,'-over.png'));
  }).addEvent('mouseout',function() { 
        $(this).setAttribute('src',($(this).getAttribute('src')+'').replace(/-over\.png/i,'.png'));
  }); 
  
   //goods size buttons
   itemImageSizeEvents();

   //BUY good Button
   $$('.itemBasketBuy').addEvent('mouseover',function(e) {
       if($(this).getAttribute('src').indexOf('-over.png')<0)
            $(this).setAttribute('src',($(this).getAttribute('src')+'').replace(/\.png/i,'-over.png')); 
   }).addEvent('mouseout',function(e) { 
       $(this).setAttribute('src',($(this).getAttribute('src')+'').replace(/-over\.png/i,'.png'));
   });
   $$('.itemBasket input[type=submit], .itemBasketBuy').addEvent('click',function(e) {return itemBasketBuyEventBody(this,e);});

});

function adronbasket_additem(id,num,s) {
    s=s.replace(/\./g,'');
    num=(num+'');
    if(!s) {
        SqueezeBox.fromElement($$('.itemImageSize')[0], {handler: 'adopt', onOpen:function() { $$('#sbox-content').setStyle('background-color','black');$$('#sbox-window').setStyle('background-color','white');itemImageSizeEvents(1); },size: {x: 300, y: 200}});         
        return false;
    } 
    var myAjax = new Ajax('/basket.php', {method: 'get',data:'_buy['+id+'.'+s+']='+num+'&ajaxbuy=1&_rnd='+(Math.random()*1000),onComplete:function(data) {    
        var j=Json.evaluate(data);  
        $$('.cart .carttext').setStyle('display','block'); 
        $('cartitemcount').setHTML(j.count);
        $('carttotal').setHTML(j.total); 
        SqueezeBox.fromElement($$('.cart2')[0], {handler: 'adopt', onOpen:function() { $$('#sbox-content').setStyle('background-color','black');$$('#sbox-window').setStyle('background-color','white'); $$('#sbox-content .cart2').setStyle('margin-top','90px').setStyle('margin-left','50px'); },size: {x: 300, y: 200}}); 
    }}).request();
    return true;
}

function adronbasket_delitem(obj) {    
    var myAjax = new Ajax( '/basket.php' , {method: 'get',data:'_del['+$(obj).getProperty('itemid')+']=1&ajaxbuy=1',onComplete:function(data) {
        var j=Json.evaluate(data);  
        $$('.cart .carttext').setStyle('display','block'); 
        $('cartitemcount').setHTML(j.count);
        $('carttotal').setHTML(j.total); 
        document.location.reload();
        //$$(obj).getParent().getParent().getParent().dispose(); 
    }}).request();
    return false;
}

function itemBasketBuyEventBody(obj,e,buynow) {
        var nam=$$(obj).getParent().getElement('input[name^=_buy]');
        var val=0,size='';
        if(nam){val=nam.getProperty('value');nam=(nam.getProperty('name')+'').replace(/[^0-9]+/g,'');}
        else nam=0;
        if(val<1)val=1;  
        var selectorstr='.itemImageSize img';
        if(buynow)  
           selectorstr='#sbox-content .itemImageSize img';
        var tmpimgs=$$(selectorstr);
        for( var s=0;s<tmpimgs.length;s++)
            if(parseInt(tmpimgs[s].getAttribute('buysize-set')))
                size+=','+(tmpimgs[s].getAttribute('alt')+'').replace(/^([^\s]+).*/g,'$1');  
        if(buynow)
            SqueezeBox.close(); 
        adronbasket_additem(nam,val,size);        
        if(e)
            new Event(e).stop();      
        return false;
}

function itemImageSizeEvents(buynow) {
   var selectorstr='.itemImageSize img';
   if(buynow)  
       selectorstr='#sbox-content .itemImageSize img';
   $$(selectorstr).addEvent('mouseover',function(e) {
       if($(this).getAttribute('src').indexOf('-over.png')<0 && $(this).getAttribute('src').indexOf('-out.png')<0)
            $(this).setAttribute('src',($(this).getAttribute('src')+'').replace(/\.png/i,'-over.png')); 
   }).addEvent('mouseout',function(e) { 
       if(!parseInt($(this).getAttribute('buysize-set')))
            $(this).setAttribute('src',($(this).getAttribute('src')+'').replace(/-over\.png/i,'.png'));
   }); 
   if(buynow)  
      $$(selectorstr).addEvent('click',function(e) {
            if($(this).getAttribute('src').indexOf('-out.png')<0) {
                if($(this).getAttribute('src').indexOf('-over.png')<0)
                    $(this).setAttribute('src',($(this).getAttribute('src')+'').replace(/\.png/i,'-over.png')); 
                $(this).setAttribute('buysize-set',1); 
                itemBasketBuyEventBody($$('.itemBasketBuy')[0],0,1);
            } else {
                return false;
            } 
       }); 
   else { 
      $$(selectorstr).addEvent('click',function(e) {
           if(!parseInt($(this).getAttribute('buysize-set')) && $(this).getAttribute('src').indexOf('-out.png')<0) {
                if($(this).getAttribute('src').indexOf('-over.png')<0)
                    $(this).setAttribute('src',($(this).getAttribute('src')+'').replace(/\.png/i,'-over.png')); 
                $(this).setAttribute('buysize-set',1); 
           } else if(parseInt($(this).getAttribute('buysize-set')) && $(this).getAttribute('src').indexOf('-out.png')<0) {
                $(this).setAttribute('src',($(this).getAttribute('src')+'').replace(/-over\.png/i,'.png'));     
                $(this).setAttribute('buysize-set',0); 
                //$(this).setProperty('buysize-set',0); 
           }         
       });  
   }
   
   
}

function quirkgetPageSize(){
    
    var xScroll, yScroll;
    
    if (window.innerHeight && window.scrollMaxY) {    
        xScroll = document.body.scrollWidth;
        yScroll = window.innerHeight + window.scrollMaxY;
    } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
        xScroll = document.body.scrollWidth;
        yScroll = document.body.scrollHeight;
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        xScroll = document.body.offsetWidth;
        yScroll = document.body.offsetHeight;
    }
    
    var windowWidth, windowHeight;
    if (self.innerHeight) {    // all except Explorer
        windowWidth = self.innerWidth;
        windowHeight = self.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
        windowWidth = document.documentElement.clientWidth;
        windowHeight = document.documentElement.clientHeight;
    } else if (document.body) { // other Explorers
        windowWidth = document.body.clientWidth;
        windowHeight = document.body.clientHeight;
    }    
    
    // for small pages with total height less then height of the viewport
    if(yScroll < windowHeight){
        pageHeight = windowHeight;
    } else {
        pageHeight = yScroll;
    }

    // for small pages with total width less then width of the viewport
    if(xScroll < windowWidth){    
        pageWidth = windowWidth;
    } else {
        pageWidth = xScroll;
    }


    arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
    return arrayPageSize;
}

