//Filling SELECTs
$(function()
{
	build_promotion_products(promote_products, affiliate_pageID);
	
	build_selects(product_selects, product_data);
	
	if( typeof(products_by_dosage) == "object" )
		build_main_prodct_table(products_by_dosage, main_table_titles, main_table_defines);
});

function get_list_params(list_params)
{
	var output = new Object();
	output.productID = new Object();

	for (var key in list_params)
	{
		output['productID'][key] = list_params[key]['productID'];
	}

	return $.param(output);
}

function build_promotion_products(promote_products, affiliate_pageID)
{
       $('.fproduct').each(function(i){
		$(this).bind('click', function() {
			window.location = basedomain + promote_products[i].link.replace(/{refID}/gi, affiliate_pageID);
		}); // make box clickable
		$(this).find('.fn').text(promote_products[i].display_name);
		$(this).find('a.url').attr('href',basedomain + promote_products[i].link.replace(/{refID}/gi, affiliate_pageID));
		$(this).find('.currency').text(currency);
		$(this).find('.price .primary').text(promote_products[i].data.price[0]);
		$(this).find('.price .decimal').text(promote_products[i].data.price[1]);
		$(this).find('.photo').attr('src', product_images + promote_products[i].data.homepage_banner_img_src);
		$(this).find('.photo').attr('alt', promote_products[i].display_name);
       });
}

function build_selects(list_data, product_data)
{
	var product_key = null;
	var qty_key = null;
	var product;
	var qty;
	var listId;
	var affiliate_pageID_param = '';
	var quantityID;

	for (product_key in list_data)
	{
		listId = list_data[product_key]['elm_id'];
		product = product_data[list_data[product_key]['productID']];

		if (product)
		{
			//Removing previous options.
			$('#' + list_data[product_key]['elm_id']).children('option').remove();
			//Activating select's button.
			$('.btn_' + listId).bind("click", { listId: listId, Param2: 2 }, function(event){
				quantityID = $('#' + event.data.listId).val();
				if (affiliate_pageID && parseInt(affiliate_pageID) != NaN) affiliate_pageID_param = '&affiliate_pageID=' + affiliate_pageID;
				else affiliate_pageID = '';
				window.location = cart_url + '?quantityID=' + quantityID + affiliate_pageID_param;
			});

			var count = 0;
			for (qty_key in product['qty']) {
			       count++;
			}
			middle = Math.round(count/2);
			var index = 0
			for (qty_key in product['qty'])
			{
				index++;
				qty = product['qty'][qty_key];
				if (qty)
				{
					//Adding options from fetched data.
					$('<option value="' + qty.quantityID  + '" '+ (index == middle ? 'selected="selected"' : '') +'>' + qty.text  + '</option>').appendTo($('#' + listId));
				}
			}
			//seting price per pill
			$('.ppp_' + listId).text(currency+qty.price_pp);
		}
	}
}

function build_main_prodct_table(product_data, main_table_titles, main_table_defines)
{
	var affiliate_pageID_param = '';
	
	for (product_id in product_data)
	{
		var prices_table = $(document.createElement('table'));
		prices_table.addClass( "prices-table" );
		var prices_table_title = $(document.createElement('tr'));
		
		for( word in main_table_titles)
		{
			prices_table_title.append('<th>'+main_table_titles[word].word+'</th>');			
		}
		prices_table.append(prices_table_title);
		
		var index = 0
		for (qty_key in product_data[product_id]['qty'])
		{
			var product_row = $(document.createElement('tr'));
			
                        if (index%2 == 1){
                            product_row.addClass('odd');
                        }
                        
			index++;
			qty = product_data[product_id]['qty'][qty_key];
			if (qty)
			{
				if (affiliate_pageID && parseInt(affiliate_pageID) != NaN) affiliate_pageID_param = '&affiliate_pageID=' + affiliate_pageID;
				else affiliate_pageID = '';
				var location_url = cart_url + '?quantityID=' + qty.quantityID + affiliate_pageID_param;
				
				product_row.append('<td>'+qty.quantity+' '+qty.quantity_type+'</td>');
				product_row.append('<td>'+currency+qty.price+'</td>');
				product_row.append('<td>'+((qty.extra_quantity > 0)? qty.extra_quantity+" "+qty.quantity_type : '-') +'</td>');
				product_row.append('<td>'+currency+qty.price_pp+'</td>');
				product_row.append('<td>\n\
										<a href="javascript:window.location=\''+location_url+'\'">\n\
											<div class="button">\n\
												<span>'+main_table_defines['buy-now']+'</span>\n\
											</div>\n\
										</a>\n\
									</td>');
			}
			prices_table.append(product_row);
		}
		
		$("#prices_table").append(prices_table);
		
		$('#ppp_main_product').text(currency+qty.price_pp);
	}
}
