document.observe("dom:loaded", function() {

	// Add event to all headers
	$$('div#info_tabs_headers a').each(function(header){
	
		header.observe('click', function(header_click) {
		
			// Disable link
			header_click.stop();
			
			// Hide all info tabs
			$$('div.info_tabs').each(function(info_tab) {
				info_tab.setStyle({ display: 'none' });
			});
			
			// Show clicked tab
			$$('div.' + header.className).each(function(tab) {
				tab.setStyle({ display: 'block' });
			});
			
			// Remove all other active
			$$('div#info_tabs_headers li').each(function(header_parent){
				header_parent.removeClassName('active');
			});

			// Add active
			header.parentNode.addClassName('active');
			
		});
	
	});
	
	// Quantity
	$$('div#product_quantity a').each(function(q_item) {

		q_item.observe('click', function() {

			var id = q_item.id;
			
			// Get quantity
			var quantity = $("quantity").value;
			
			// Add
			if ( id == 'product_quantity_add' )
				quantity++;
			else if ( id == 'product_quantity_subtract' && quantity > 1 )
				quantity--;
				
			// Place value
			$("quantity").value = quantity;
			
			// Some ajax for the price, quantity discount support
			quantity_ajax();
		
		});
	
	});
	
	$("quantity").observe('blur', function() {
		var quantity = $("quantity").value;
		quantity_ajax();
	});
	
	quantity_ajax = function() {
	
		var products_id = $("products_id_hidden").value;
		var quantity = $("quantity").value;
		var attribute = 0;
		
		// Get chosen attribute if available
		/*if ( $$('#attributes select').first() )
		{
			attribute = $$('#attributes>select').first().value;
		}*/
		// Get all attributes
		var attribute = '';
		$$('#attributes select').each(function(obj) {
			attribute += '&attribute[]=' + obj.value;
		});
	
		var url = 'product_info_ajax.php?method=quantity_discount&products_id='+products_id+'&quantity='+quantity+attribute;
		//alert(url);
		new Ajax.Request(url, {
		  method: 'get',
		  onSuccess: function(response) {
			if (response.responseText != 'error')
				$("productsPrice").innerHTML = response.responseText;
		  }
		});
	}
	
	// Fix select box in ie
	/*var attributes_select = "qwe";
	
	$$('#product_actions .box_border select').each(function(select) {
		attributes_select = select;
	});*/
	
	/*var test =  $('attributes').childElements();
	test.each(function(node){
	  alert(node.nodeName);
	});*/
	
	if ( $$('#attributes select') )
	{
	
		$$('#attributes select').each(function(obj) {
		
			obj.observe('change', function() {
		
				quantity_ajax();
				
				if ( $('product_price_ordinary') )
				{
				
					var showOrdinaryPrice = true;
					$$('#attributes select').each(function(obj) {
						var optionHTML = obj.options[obj.selectedIndex].innerHTML;
						var pattern = /\(.*?\)$/;
						if ( optionHTML.match(pattern) )
						{
							showOrdinaryPrice = false;
						}
					});
					
					if ( showOrdinaryPrice == false )
					{
						$('product_price_ordinary').setStyle({ display: 'none' });
					}
					else
					{
						$('product_price_ordinary').setStyle({ display: 'block' });
					}
					
				}
			
			});
		
		});
	
	}

	
	//var attributes_select = $('attrcomb');
	//if ( !attributes_select )
		//var attributes_select = $('id[12]'); // might be wrong
		
	//var attributes_select = $$('#attributes select').first();
	var attributes_select = '';
	// DISABLED
	if ( $(attributes_select) && 1 == 2 )
	{
	
		var attrcomb_width = $(attributes_select).getWidth() + 'px';
		var attrcomb_expanded = false;
		
		// Open
		$(attributes_select).observe('mouseover', function(obj) {
		
			$(attributes_select).setStyle({ width: 'auto' });

		});
		$(attributes_select).observe('click', function(obj) {
		
			attrcomb_expanded = true;
		
		});
		
		// Close
		$(attributes_select).observe('mouseleave', function(obj) {
		
			if ( attrcomb_expanded == false )
			{
				$(attributes_select).setStyle({ width: attrcomb_width });
			}
		
		});
		$(attributes_select).observe('change', function(obj) {
		
			$(attributes_select).setStyle({ width: attrcomb_width });
			attrcomb_expanded = false;
		
		});
		$(attributes_select).observe('blur', function(obj) {
		
			$(attributes_select).setStyle({ width: attrcomb_width });
			attrcomb_expanded = false;
		
		});
		
	}
		
});

/*$(document).ready(function() {

	$('div#info_tabs_headers a').each(function() {
	
		$(this).click(function() {
		
			// Hide all info tabs
			$('div.info_tabs').each(function() {
				$(this).hide();
			});
			
			// Get real id
			//var info_tab_id = $(this).attr('id').replace("header", "info_tab");
			
			// Show clicked tab
			//$('div#' + info_tab_id).show();
			$('div.' + $(this).attr('class')).show();
			
			// Remove all other active
			$('div#info_tabs_headers a').each(function() {
				$(this).parent().removeClass('active');
			});
			
			// Add active
			$(this).parent().addClass('active');
			
			return false;
			
		});
	
	});
	
	$('div#product_quantity a').each(function() {
	
		$(this).click(function() {
		
			var id = $(this).attr('id');
			
			// Get quantity
			var quantity = $("input[name$='quantity']").val();
			
			// Add
			if ( id == 'product_quantity_add' )
				quantity++;
			else if ( id == 'product_quantity_subtract' && quantity > 1 )
				quantity--;
				
			// Place value
			$("input[name$='quantity']").val(quantity);
		
		});
	
	});
	
	$('a[rel="lightbox"]').lightBox(); // Select all links that contains lightbox in the attribute rel

});*/
