// JavaScript Document

window.addEvent('domready', function() {
											TB_WIDTH = 0;
TB_HEIGHT = 0;
var TB_doneOnce = 0;			
						var TB_WIDTH = 469;
						var TB_HEIGHT = 300;
	
																			

									var closp = $$('.popupclose');
									var openp = $('popuplink');
																		if(closp) {
																			closp.each(function(element) {
	element.addEvent('click', function(el){
									
											 TB_remove2();
											 
											  });
	});
	}
	
	
	
	
	
	if(openp) {
	openp.addEvent('click', function(el){
									 showbox();
									 										  });
	}
	
	function showbox() {
		 
									     if (!$("TB_overlay")) {
										
        new Element('iframe').setProperty('id', 'TB_HideSelect').injectInside(document.body);
        $('TB_HideSelect').setOpacity(0);
        new Element('div').setProperty('id', 'TB_overlay').injectInside(document.body);
        $('TB_overlay').setOpacity(0);
        TB_overlaySize();
        new Element('div').setProperty('id', 'TB_load').injectInside(document.body);
        $('TB_load').innerHTML = "<img src='loading.gif' />";
        TB_load_position();
        
        $('TB_overlay').set('tween', {
            duration: 400
        });
        $('TB_overlay').tween('opacity', 0, 0.6);
        
    } 
	window.onscroll = TB_position;
	  TB_position();
            TB_showWindow();
	};
									 
									 
											
	
	
	
									 
									
																		

			 window.onresize = function(){
        TB_position();
        TB_load_position();
        TB_overlaySize();
    }								
												
	function TB_overlaySize(){
		
    // we have to set this to 0px before so we can reduce the size / width of the overflow onresize 
    $("TB_overlay").setStyles({
        "height": '0px',
        "width": '0px'
    });
    $("TB_HideSelect").setStyles({
        "height": '0px',
        "width": '0px'
    });
	
    $("TB_overlay").setStyles({
        "height": window.getScrollHeight() + 'px',
        "width": window.getScrollWidth() + 'px'
    });
    $("TB_HideSelect").setStyles({
        "height": window.getScrollHeight() + 'px',
        "width": window.getScrollWidth() + 'px'
    });
	
	
}

function TB_load_position(){
    if ($("TB_load")) {
        $("TB_load").setStyles({
            left: (window.getScrollLeft() + (window.getWidth() - 56) / 2) + 'px',
            top: (window.getScrollTop() + ((window.getHeight() - 20) / 2)) + 'px',
            display: "block"
        });
    }
}

function TB_remove2(){
    $("TB_overlay").onclick = null;
    document.onkeyup = null;
    document.onkeydown = null;
  
    
    
    $('TB_windowinline').set('tween', {
        duration: 250,
        onComplete: function(){
         //   $('TB_windowinline').dispose();
        }
    });
    $('TB_windowinline').tween('opacity', 1, 0);
    
    
    
    $('TB_overlay').set('tween', {
        duration: 400,
        onComplete: function(){
            $('TB_overlay').dispose();
        }
    });
    $('TB_overlay').tween('opacity', 0.6, 0);
    
    window.onscroll = null;
    window.onresize = null;
    
    $('TB_HideSelect').dispose();
 //   TB_init();
    TB_doneOnce = 0;
    return false;
}
	
	
	function TB_position(){
    $('TB_windowinline').set('morph', {
        duration: 75
    });

    $('TB_windowinline').morph({
		width: TB_WIDTH + 'px',
		left: (window.getScrollLeft() + (window.getWidth() - TB_WIDTH) / 2) + 'px',
		top: (window.getScrollTop() + (window.getHeight() - TB_HEIGHT) / 2) + 'px'
	});	
}

function TB_showWindow(){
    //$("TB_load").dispose();
   $("TB_windowinline").setStyles({display:"block",opacity:'0'});
    
    if (TB_doneOnce == 0) {
        TB_doneOnce = 1;
        
        $('TB_windowinline').set('tween', {
            duration: 250,
            onComplete: function(){
                if ($('TB_load')) {
                    $('TB_load').dispose();
                }
            }
        });
        $('TB_windowinline').tween('opacity', 0, 1);
        
    }
    else {
        $('TB_windowinline').setStyle('opacity', 1);
        if ($('TB_load')) {
            $('TB_load').dispose();
        }
    }
}
		
		
		
		
		var addtocart=$$('.addtocart');
addtocart.each(function(element) {
	element.addEvent('submit', function(el){				 
										el.stop();							
							var qty = element.qty.value;
							
							var err;
							var itemid = element.itemid.value;
							var qn='qtyerr'+itemid;
						
							var qerror=$(qn);
							var an='attriberr'+itemid;
							var aerror=$(an);
							
							if (qty<1){ 
							
							qerror.set('html','*');
							//var optdiv = new Element('div', {'class': 'errcode', 'html':'Enter Qty'}).inject(element); 
							err='T'; 
							}  else {
									qerror.set('html','');
								}
					
							//var log = $('log_res').empty().addClass('ajax-loading');
	if (err!='T') {
		this.set('send', {onComplete: function(response) { 
			//log.removeClass('ajax-loading');
			showbox();
			$('popupmain').set('html', response);
			var cartqtyupdate = $('cartlink').get('rel');
			$('cartdetails').set('html',cartqtyupdate);
			
		}});
		this.send();
						};		
												});

									 });
});
