$(function()
{
	$('#search_category').change(function()
	{
		$('#search_filters select').attr('disabled', true);
		$.post(cfg.vir_path +'index.php?m=get_ajax&p=get_subcats', {'id': $(this).val()}, function(responseText)
		{
			$('#serach_scategory').html(responseText);
			$('#serach_scategory').triggerHandler('change');
		});
		$('#search_filters select').attr('disabled', false);
	});
	$('#serach_scategory').change(function()
	{
		$('#search_filters select').attr('disabled', true);
		$.post(cfg.vir_path +'index.php?m=get_ajax&p=get_sizes', {'id': $(this).val()}, function(responseText)
		{
			$('#search_size').html(responseText);
		});
		$('#search_filters select').attr('disabled', false);
	});
	$('#search_size').change(function()
	{
		if ($(this).val() != 0) {
			$('#search_filters .inputSubmit').attr('disabled', false);
		}
		else {
			$('#search_filters .inputSubmit').attr('disabled', true);
		}
	});
	$('#search_filters .inputSubmit').click(function()
	{
		if ($('#serach_scategory').val() && $('#search_size').val()) {
			location.href = cfg.vir_path +'category-'+ $('#serach_scategory').val() +'/size-'+ $('#search_size').val() +'/';
		}
	});
	$.add2cart(0);
});

(function($)
{
	var $updatingCart = false;
	$.extend(
	{
		add2cart: function(source_id, $quantity, $color, $size, target_id)
		{
			/* Check this function is work, if is work stop it */
			if ($updatingCart) {
				return false;
			}

			/* Set updateCart true */
			$updatingCart = true;

			/* Check if null source id */
			if (source_id == 0) {
				updateCart();
				return false;
			}

			var source = $('#product_'+ source_id);
			var target = $(target_id != undefined && $(target_id).length > 0 ? target_id : '.cart');

			var shadow = $('#' + source_id + '_shadow');
			if(!shadow.attr('id'))
			{
				$('body').prepend('<div id="'+ source_id +'_shadow" style="background-color:#ddd;display:none;position: static;top:0px;z-index:9999;border:solid 1px darkgray">&nbsp;</div>');

				var shadow = $('#'+ source_id +'_shadow');
			}

			$.scrollTo({top: 0, left: 0}, {duration: 500});
			$(shadow).css({
				'width': source.css('width'),
				'height': source.css('height'),
				'top': source.offset().top,
				'left': source.offset().left, 
				'opacity': 0.5,
				'position': 'absolute'}).show();

			shadow.animate({
					'width': target.innerWidth(),
					'height': target.innerHeight(),
					'top': target.offset().top,
					'left': target.offset().left}, 
					500
				)
				.animate({opacity: 0}, 100, updateCart);

			function updateCart()
			{
				/* Remove shadow */
				if (shadow) {
					shadow.remove();
				}

				/* Check if exist target id, if exist stop function */
				if (target_id) {
					$updatingCart = false;
					return false;
				}

				$('.head_cart_items').hide();
				$('.head_cart_loading').show();

				$.post(cfg.vir_path +'index.php?m=get_ajax&p=update_cart', {'id': source_id, 'quantity': $quantity, 'color': $color, 'size': $size}, function(items)
				{
					if (items.quantity && items.price)
					{
						$('.head_cart_items > .items .bold').html(items.quantity);
						$('.head_cart_items > .cost .bold').html(items.price);
					}

					$('.head_cart_loading').hide();
					$('.head_cart_items').show();
					$updatingCart = false;
				}, 'json');
			}
		}
	});
})(jQuery);


function CreateBookmarkLink()
{
	title = document.title; 
	url = window.location.href;

	// Mozilla Firefox Bookmark
	if (window.sidebar) {
		window.sidebar.addPanel(title, url, "");
	}
	// IE Favorite
	else if (window.external) {
		window.external.AddFavorite(url, title);
	}
	// Opera Hotlist
	else if(window.opera && window.print) {
		return true;
	}
}
