$(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){
	    elements = ['price', 'priceNonSpecial', 'priceMsrp', 'notice', 'productNo', 'issues', 'shipping_costs'];
	    jdataIdent = ['price', 'priceNonSpecial', 'msrp', 'price_notice', 'product_no', 'issues', 'shipping_costs'];
	    
	    for(var i in elements)
	    {
			$element = $('#' + elements[i] + '_' + formIter);
			if($element.text().length > 0)
	        {
				$element.text(item[ jdataIdent[i] ]);
	        }
	    }
	    
	    $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)
{
	url = getAjaxUrl('/xml/html/bundles/', formIter);

	$.ajax({
	  url: url,
	  success: function(data) {
		 $('#' + bundleContainer).replaceWith(data);
	  }
	});	
}

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);
}

