/**
 * order.js
 * @author Davy Hellemans <davy@netlash.com>
 */


/**
 * Updates the shopping cart
 * 
 * @return	void
 * @param	string productId
 * @param	int amount
 */
function updateCart(productId, amount)
{
	// set spinner
	var spinner = '<img src="/modules/core/layout/images/general/spinner.gif" id="spinner" alt="" title="" style="padding: 10px;" />';
	$('#shopping_cart').html(spinner);
	
	// execute ajax call
	$.ajax({
	url:			'/ajax.php?module=products&action=order&product_id=' + productId + '&amount=' + amount,
	type:			'get',
	dataType:		'json',
	success:		function(msg) {
						// update cart & hover
						if($('#shopping_cart').html() != null)
						{
							// update shopping cart
							$('#shopping_cart').html(msg.content[0]);
							
							// update hover
							updateCartHover();
						}
						
						// reload current url
						else window.location.reload(true);
					}
	});
}


/**
 * Updates the shopping carts hover table
 * 
 * @return	void
 */
function updateCartHover()
{
	// execute ajax call
	$.ajax({
	url:			'/ajax.php?module=products&action=hover',
	type:			'get',
	dataType:		'json',
	success:		function(msg) {
						$('#shopping-cart-table').html(msg.content[0]);
					}
	});
}

function updateCartInBackground(productId, amount)
{
	// execute ajax call
	$.ajax({
	url:			'/ajax.php?module=products&action=update&product_id=' + productId + '&amount=' + amount,
	type:			'get',
	dataType:		'json',
	success:		function(msg) {
						window.location.reload(true);
					}
	});
}


/**
 * Sets the number of stars
 *
 * @return	void
 * @param	int numStars
 */
function updateStars(numStars)
{
	// between 1 & 5
	if(numStars >= 1 && numStars <= 5)
	{
		// output
		var output = '';
	
		// loop 5 times
		for(var i = 1; i <= 5; i++)
		{
			if(i <= numStars) output = output + '<a href="javascript:updateStars(' + i + ');"><img src="/modules/core/layout/images/general/star.jpg" /></a> ';
			else output = output + '<a href="javascript:updateStars(' + i + ');"><img src="/modules/core/layout/images/general/star_grey.jpg" /></a> ';
		}
		
		// update output
		$('#score_stars').html(output);
		
		// update textfield
		$('#score').val(numStars);
	}
	
	// default to 3 stars
	else updateStars(3);
}


/**
 * Update shipping & get shipping location info
 * 
 * @return	void
 * @param	int id
 */
function updateShipping(id)
{
	// execute ajax call
	$.ajax({
	url:			'/ajax.php?module=products&action=update_shipping&shipping_id=' + id,
	type:			'get',
	dataType:		'json',
	success:		function(msg) {
						if (parseInt(msg.status.code) == 200)
						{
							if (id == -1)
							{
								$("#total_price").html(Math.round((parseFloat($('#subtotal_price').html().replace(',', '.')) + parseFloat($('#price_delivery').html().replace(',', '.'))) * 100) / 100);
//								$('#collection_link').css('display', 'none');
							}
							else
							{
								$("#total_price").html(Math.round((parseFloat($('#subtotal_price').html().replace(',', '.')) + parseFloat($('#price_collection').html().replace(',', '.'))) * 100) / 100);
								$('#collection_link').attr('href', msg.content.url);
								$('#collection_link').html(msg.content.name);
								$('#collection_link').css('display', 'inline');
							}
						}
					}
	});
}


/**
 * Shows the correct fieldsets
 * 
 * @return	void
 * @param 	string sType
 */
function showFieldsets(sType)
{
	// the same address
	if(sType == 'same')
	{
		$('#delivery_other_address').hide();
		$('#delivery_partena_office').hide();
	}

	// other addres
	else if(sType == 'other')
	{
		$('#delivery_other_address').show();
		$('#delivery_partena_office').hide();
	}
}


 function hookHumanizedBox() {
	$('#boxContinue').bind('click', function () { $('#overlay').hide(); $('#humanizedSuccesBox').hide(); });
	$('#boxCheckout').bind('click', function () { window.location = $(this).attr('rel'); });
}
		

function hookOverlay(show) {
	if($('#overlay').length == 0) $('#container').prepend('<div id="overlay"></div>');
	$('#overlay').height($(document).height());
	
	// Persistent overlay opacity for IE6
	$('#overlay').css("filter", "alpha(opacity=40)");

	if(show) $('#overlay').show();
	else $('#overlay').hide();
}


/**
 * Shows the humanizedBox
 * 
 * @param text
 * @param events
 * @return
 */
function showHumanizedBox()
{
	hookOverlay(true);
	$('#humanizedSuccesBox').fadeIn('slow');
	hookHumanizedBox();

	// Explicitly declare the width and height of modal boxes
	var modalWidth = $('#humanizedSuccesBox').width();
	$('#humanizedSuccesBox').css('width', modalWidth);
	var modalHeight = $('#humanizedSuccesBox').height();
	$('#humanizedSuccesBox').css('height', modalHeight);

	// add the already scrolled height
	$('#humanizedSuccesBox').css('top', (($(window).height() - modalHeight) / 2) + $(document).scrollTop());
	$('#humanizedSuccesBox').css('left', (($('#container').width() - modalWidth) / 2));

	setTimeout("$('#humanizedSuccesBox').fadeOut('slow'); $('#overlay').hide();", 2000);
}


// bind the events
jQuery(document).ready(function($) {

	// hook humanized box
	hookOverlay(false);
	hookHumanizedBox();
	
	// bind the click event
	$('a.ajax').bind('click', function(evt) {
		evt.preventDefault();
		var productId = $(this).attr('rel');
		updateCart(productId, 1);
		showHumanizedBox();
	});
	
	// bind the onchange event
	$('input.ajax').bind('change', function(evt) {
		var productId = $(this).attr('name');
		var amount = parseInt($(this).val());
		if(isNaN(amount) || !isFinite(amount)) amount = 0;
		updateCartInBackground(productId, amount);
	});

	// bind the onmouseover event
	$('#mijn_winkelwagentje').bind('mouseover', function(evt){

		// Explicitly declare the width and height of modal boxes
		var modalWidth = $('#shopping-cart-table').width();
		$('#shopping-cart-table').css('width', modalWidth);
		var modalHeight = $('#shopping-cart-table').height();
		$('#shopping-cart-table').css('height', modalHeight);

/*		// center the modal
		$('#shopping-cart-table').css('top', (($(window).height() - modalHeight) / 2) + $(document).scrollTop());
		$('#shopping-cart-table').css('left', (($('#container').width() - modalWidth) / 2));
*/
		var position = $('#mijn_winkelwagentje').position();
		$('#shopping-cart-table').css('top', position.top + 40); // 40 margin on this h3
		$('#shopping-cart-table').css('left', position.left - modalWidth - 15);
		

		$('#shopping-cart-table').show();
	});
	
	$('#mijn_winkelwagentje').bind('mouseout', function(evt){
		$('#shopping-cart-table').hide();
	});

	// shipping events
	if ($('select#collection_points').length > 0)
	{
		$('select#collection_points').bind('change', function(evt) {
			updateShipping($(this).val());
		});

		$('input#afhaalpunt').bind('click', function(evt) {
			updateShipping($('select#collection_points').val());
		});

		$('select#collection_points').bind('click', function(evt) { $('input#afhaalpunt').click(); });

		$('input#levering').bind('click', function(evt){
			updateShipping(-1);
		});

//		$(document).ready(function() { updateShipping($('select#collection_points').val()); });
	}

	// show/hide dropdown
	$('#afhaalpunt').click(function(){
		$('#collection_points').css('display', 'inline').next().css('display', 'inline');
	});
	$('#levering').click(function(){
		$('#collection_points').css('display', 'none').next().css('display', 'none');
	});

	// set the default stars
	updateStars($('#score').val());
});
