var base_url = get_parameter('base:url');
var loader = '<img src="' + base_url + 'themes/default/images/ajax-loader.gif" alt="" />';

$(document).ready(function() {
	$("#dialog").dialog({
		bgiframe: false,
		autoOpen: false,
		width: 'auto',
		modal: true,
		draggable: true,
		close: function() {
			//
		}
	});
});

function detailChange(type, value)
{
	var pid    = parseInt(get_parameter('product:id'));
	var style  = -1;
	var length = -1;
	var size   = -1;
	var color  = -1;
	
	// Set default value or get value
	switch (type)
	{
		case 'style':
			if ($("#lengthval").val() != undefined) $("#lengthval").val(0);
			if ($("#sizeval").val() != undefined) $("#sizeval").val(0);
			if ($("#colorval").val() != undefined) $("#colorval").val(0);
			$("#product-status").html('');
			break;
		
		case 'length':
			if ($("#sizeval").val() != undefined) $("#sizeval").val(0);
			if ($("#colorval").val() != undefined) $("#colorval").val(0);
			$("#product-status").html('');
			break;
			
		case 'size':
			if ($("#colorval").val() != undefined) $("#colorval").val(0);
			$("#product-status").html('');
			break;
			
		case 'color':
			if ($("#quantityval").val() != undefined) $("#quantityval").val(0);
			// Get value
			if ($("#styleval").val() != undefined) style = $("#styleval").val();
			if ($("#lengthval").val() != undefined) length = $("#lengthval").val();
			if ($("#sizeval").val() != undefined) size = $("#sizeval").val();
			if ($("#colorval").val() != undefined) color = $("#colorval").val();
			// Get status
			if (style != 0 && length != 0 && size != 0 && color != 0)
			{
				$("#product-status").html($("#image-loader").html());
				var url = base_url + 'ajax_product_check_status/' + pid + '/' + style + '/' + length + '/' + size + '/' + color;
				$.get(url, function callback(response) {
					if (response == '')
					{
						$("#canbesell").val('');
						response = get_parameter('frontend:product:in:stock');
					}
					else
						$("#canbesell").val(response);
					$("#product-status").html(response);
				});
			}
			
			break;
	}
}

function showSizeChart()
{
	$("#dialog").dialog('open');
}

function addBag(bagorwish)
{
	// Nếu add product to wishlist, check member is logged
	if (bagorwish == 'wish')
	{
		var loggedin = get_parameter('logged:in');
		if (loggedin != '')
		{
			// Set redirect page to current url
			var url = base_url + 'ajax_product_redirect/';
			$.post(url, {curl : get_parameter('current:url')}, function callback(response) {
				window.location = get_parameter('common:user:url:account:profile:login');
				return false;
			});
			
			return false;
		}
	}
	
	var style     = -1;
	var length    = -1;
	var size      = -1;
	var color     = -1;
	var quantity  = $("#quantityval").val();
	var canbesell = $("#canbesell").val();
	
	// Product can not sell, show when user click addtobag while this product is not selling...
	if (canbesell != '')
	{
		alert(canbesell);
		return false;
	}
	// Get value
	if ($("#styleval").val() != undefined) style = $("#styleval").val();
	if ($("#lengthval").val() != undefined) length = $("#lengthval").val();
	if ($("#sizeval").val() != undefined) size = $("#sizeval").val();
	if ($("#colorval").val() != undefined) color = $("#colorval").val();
	// Check and Submit
	if (style == 0 || length == 0 || size == 0 || color == 0 || quantity == 0)
	{
		if (style == 0) return alert(get_parameter('frontend:product:style:empty')) + false;
		if (length == 0) return alert(get_parameter('frontend:product:length:empty')) + false;
		if (size == 0) return alert(get_parameter('frontend:product:size:empty')) + false;
		if (color == 0) return alert(get_parameter('frontend:product:color:empty')) + false;
		if (quantity == 0) return alert(get_parameter('frontend:product:quantity:empty')) + false;
	}
	// Set value to submit
	$("#stylepost").val(style == -1 ? '' : style);
	$("#lengthpost").val(length == -1 ? '' : length);
	$("#sizepost").val(size == -1 ? '' : size);
	$("#colorpost").val(color == -1 ? '' : color);
	$("#quantitypost").val(quantity == -1 ? '' : quantity);
	$("#addbagorwish").val(bagorwish);
	
	// Submit to server
	$("#product-status").html(loader);
	var url = base_url + 'ajax_add_cart/';
	$.post(	url,
		{
			rowid        : parseInt(get_parameter('row:id')),
			addbagorwish : $("#addbagorwish").val(),
			productid    : $("#productid").val(),
			stylepost    : $("#stylepost").val(),
			lengthpost   : $("#lengthpost").val(),
			sizepost     : $("#sizepost").val(),
			colorpost    : $("#colorpost").val(),
			quantitypost : $("#quantitypost").val(),
			price        : $("#price").val(),
			pricespecial : $("#pricespecial").val()
		},
		function callback(response) {
			// Check if member do not login
			if (response == 'not:logged:in')
			{
				window.location = get_parameter('common:user:url:account:profile:login');
				return false;
			}
			
			var styletext  = '';
			var lengthtext = '';
			var sizetext   = '';
			var colortext  = '';
			var infoText   = '';
			var end        = '';
			
			// Get extra info
			if (style != -1) styletext = $("#styleval option[value='0']").text() + ': ' + $("#styleval option[value='" + style + "']").text() + ', ';
			if (length != -1) lengthtext = $("#lengthval option[value='0']").text() + ': ' + $("#lengthval option[value='" + length + "']").text() + ', ';
			if (size != -1) sizetext = $("#sizeval option[value='0']").text() + ': ' + $("#sizeval option[value='" + size + "']").text() + ', ';
			if (color != -1) colortext = $("#colorval option[value='0']").text() + ': ' + $("#colorval option[value='" + color + "']").text() + ', ';
			if (quantity != -1) quantitytext = $("#quantityval option[value='0']").text() + ': ' + $("#quantityval option[value='" + quantity + "']").text();
			
			switch (response)
			{
				case 'cart:success':
					infoText = get_parameter('frontend:product:add:cart:finish') + ': ' + get_parameter('product:title') + '<br />';
					end      = '<br /><br />' + get_parameter('frontend:product:add:cart:continue');
					end += '<a href="' + get_parameter('common:user:cart:url') + '">&nbsp;<strong>' + get_parameter('frontend:product:view:cart') + '</strong></a>';
					// Update cart total products
					$("#cart_total").html(parseInt($("#cart_total").html()) + parseInt(parseInt(quantity) - parseInt(get_parameter('product:quantity'))));
					// Reset attribute
					reset_attribute(style, length, size, color, quantity);
					break;
				
				case 'cart:duplicate':
					infoText = get_parameter('frontend:product:add:cart:duplicate') + ': ' + get_parameter('product:title') + '<br />';
					end      = '<br /><br />' + get_parameter('frontend:product:add:cart:continue:duplicate');
					end += '<a href="' + get_parameter('common:user:cart:url') + '">&nbsp;<strong>' + get_parameter('frontend:product:view:cart') + '</strong></a>';
					break;
				
				case 'wish:success':
					infoText = get_parameter('frontend:product:add:cart:finish') + ': ' + get_parameter('product:title') + '<br />';
					end      = '<br /><br />' + get_parameter('frontend:product:add:cart:continue');
					end += '<a href="' + get_parameter('common:user:url:wishlist') + '">&nbsp;<strong>' + get_parameter('frontend:product:view:wishlist') + '</strong></a>';
					// Reset attribute
					reset_attribute(style, length, size, color, quantity);
					break;
				
				case 'wish:duplicate':
					infoText = get_parameter('frontend:product:add:cart:duplicate') + ': ' + get_parameter('product:title') + '<br />';
					end      = '<br /><br />' + get_parameter('frontend:product:add:wishlist:continue:duplicate');
					end += '<a href="' + get_parameter('common:user:url:wishlist') + '">&nbsp;<strong>' + get_parameter('frontend:product:view:wishlist') + '</strong></a>';
					break;
			}
			
			$("#product-status").html(infoText + styletext + lengthtext + sizetext + colortext + quantitytext + end);
		}
	);
}

function reset_attribute(style, length, size, color, quantity)
{
	// Reset attribute
	if (style != -1) $("#styleval").val(0);
	if (length != -1) $("#lengthval").val(0);
	if (size != -1) $("#sizeval").val(0);
	if (color != -1) $("#colorval").val(0);
	if (quantity != -1) $("#quantityval").val(0);
}

