$(document).ready(function() {
	$('input:checkbox[name^="filter["]').click(function(){
		submitFilterForm( $(this) );	
	});
	$('select[name^="filter["],select[name^="order_by["]').change(function(){
		submitFilterForm( $(this) );	
	});
});

function submitFilterForm($elem)
{
	$elem.parents('form:first').submit();
}

function processVatID()
{
	$idContainer = $('#bdi_vatIDContainer');
	if( $('#order_person_company').val().length > 0 )
	{
		$idContainer.show();
	}
	else
	{
		$idContainer.hide();
	}
}

function setProductVariation(jdata, formIter, selIds, selItems)
{
	$.each(jdata, function(indexItem,item){
		
		var elements = { 
		  'price': 'price', 
		  'priceNonSpecial': 'priceNonSpecial',
		  'priceMsrp': 'msrp', 
		  'notice': 'price_notice',
		  'productNo': 'product_no', 
		  'issues': 'issues',
		  'shipping_costs': 'shipping_costs'
		};
		price_display = item['price_display'];
		
		$.each(elements, function(elementId, jdataIdent)
		{ 
			var namespace = (elementId.substr(0,5) == 'price' ? '_' + price_display + '_' : '_');
			
			$element = $('#' + elementId + namespace + formIter);
			if($element.text().length > 0)
	        {
				$element.text(item[ jdataIdent ]);
	        }
		});
		
		$.each( ['default','special','msrp'], function(){
			$displayElement = $('#display_' + this + '_' + formIter);
			
			if(this == price_display)
	        {
				$displayElement.show();
	        }
			else
			{
				$displayElement.hide();
			}
		});
	    		
	    $shipping_costs_text = $('#shipping_costs_text_' + formIter);
	    if(parseFloat(item.shipping_costs.replace(',','.')) > 0)
	    {
	    	$shipping_costs_text.show();
	    }
	    else
	    {
	    	$shipping_costs_text.hide();
	    }
	    
	    i = 0;
	    $.each(item.variations, function(varKey, varArr){
	    	$select = $('#' + selIds[i]);
	    	$select.children('option').remove();
	    	$.each(varArr, function(key, option){
	    		$select.append('<option value="' + key + '">' + option + '</option>');
	    	});
	    	$select.children('option[value=' + selItems[i] + ']').attr("selected","selected") ;
	    	i++;
	    });
   });	
	
} 

function getProductVariation(formIter)
{
	url = getAjaxUrl('/xml/json/product/', formIter);

	$.getJSON(url, function(jdata ){
		setProductVariation(jdata, formIter, selIds, selItems);		
    });	
}

function getProductBundle(formIter, bundleContainer)
{
	var url = getAjaxUrl('/xml/html/bundles/', formIter);
	
	var $container = $('#' + bundleContainer);
	$container.find('div[id$="_toLoad"]').hide();
	$container.find('div[id$="_loading"]').show();
	
	$.ajax({
	  url: url,
	  success: function(data) {
		 $container.replaceWith(data);
		 /*
		 $.each(selIds, function(varKey, varId){
			$select = $('#' + varId);
	    	$select.children('option[value=' + selItems[varKey] + ']').attr("selected","selected") ;
	    });
	    */
	  }
	});	
}

function getAjaxUrl(path, formIter)
{
	$form = $('#order_' + formIter + '_vv_1').parents('form:first');

	baseUrl = (typeof bdiShopBaseUrl != 'undefined' && bdiShopBaseUrl != '') ? bdiShopBaseUrl : '/home';
	qstr = baseUrl + path;
	productId = $form.find('input[name="product[product_id]"]').val();
	qstr += '?product_id=' + productId;

	$selects = $form.find('select[name="product[variations][' + productId + '][]"]');
	selIds = new Array($selects.length);
	selItems = new Array($selects.length);

	$selects.each(function (index) {		
		selIds[index] = $(this).attr('id');
		selItems[index] = $(this).children('option:selected').val();

		var_index = index + 1;
		qstr += '&variation_' + var_index + '=' + selItems[index];
    });

	return qstr;	
}

function getBookmarkLink(link, bookmarkTitle)
{	
	var url = window.location.href;
	var bookmarkUrl = url.substring(0, url.length - 1); // cut last character of url

	if (window.sidebar) { // For Mozilla Firefox Bookmark
		window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,'');
	} else if( window.external || document.all) { // For IE Favorite
		window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
	} else if(window.opera) { // For Opera Browsers
		link.attr('href',bookmarkUrl);
		link.attr('title',bookmarkTitle);
		link.attr('rel','sidebar');
		link.click();
	} 
	
	return false; // this will prevent the anchor tag from going the user off to the link
}


function changeNavigationItem(category_id)
{
	$('#bdi_navigationStartList a').removeClass('activeListElement').addClass('noactiveListElement');
	$('#bdi_navigationStartList a#listElem_' + category_id).removeClass('noactiveListElement').addClass('activeListElement');

	$('#bdi_navigationStartImage div').removeClass('activeImage').addClass('noactiveImage');
	$('#bdi_navigationStartImage div#image_' + category_id).removeClass('noactiveImage').addClass('activeImage');
}

function setProductQuantity(identifier, quantity)
{
	$('#' + identifier).val(quantity);
}
