$(document).ready(function() {
	$('#editManufacturer').click(function(){
		var $form = $('#datos_productor');
		if($form.hasClass('disable')){
			$('input:disabled', $form).attr('disabled', '');
			$('textarea:disabled', $form).attr('disabled', '');
			$form.removeClass('disable');
			$(this).val('Salvar cambios');
		}
		else{
			$form.submit();
		}
		return false;
	});
	
	$("#add_category").click(function () {
		$("#categorias").slideToggle();
		return false;
	});
	
	$(".ico_info").click(function () {
		$(this).parent().next().slideToggle();
		$("form .info").hide();
		return false;
	});
	
	$(".ico_delete").click(function () {
		$(this).parent().remove();
		return false;
	});
	
	$(".edit_entrega").click(function () {
		
		$("#edit_entrega_form").slideToggle();
		
		return false;
	});
	
	$("#vieworders").click(function () {
		var href = $(this).attr('href');
		window.loadUrlIn(href, 'vieworders_container');
		return false;
	});
	
	$("#viewreviews").click(function () {
		var href = $(this).attr('href');
		window.loadUrlIn(href, 'viewreviews_container');
		return false;
	});
	
	$(".cat_slide").click(function () {
		$("#categorias ul").hide();
		$(this).parent().next().slideToggle();
		
		return false;
	});
});
function loadUrlIn(url, id){
	var $container = $('#'+id);

	$.get(url, function(data){
		$container.fadeOut(function(){
			var wasEmpty = $container.is(":empty");
			$container.empty();
			$container.append(data);
			if(wasEmpty)
				$container.slideDown();
			else
				$container.fadeIn();
		});
	});
}

function redirect(url, key, value)
{
	if(typeof(key) === 'undefined' && typeof(value) === 'undefined')
	{
		document.location = url;
	}
	else
	{
		/*
		 * Example:
		 * 		- url = www.example.com/key1/value1/key2/value2/
		 * 		- key = "key1"
		 * 		- value = "toto"
		 */
		if(key !== null)
		{
			// index = 15
			var index = url.indexOf('/'+key+'/');
			
			var url1;
			// case 1: key is already present in URL
			if(index !== -1){
	
				// iafter = 21 = 15 + 4 ("key1".length) + 2 ("//".length)
				var iafter = index+key.length+2;
				
				// url1 = www.example.com/key1/
				url1 = url.substr(0, iafter);
				
				// index2 = 27 = 21 (iafter) + 6 ("value1".length)
				var index2 = url.indexOf('/', iafter);
				if(index2 !== -1){
					// url2 = /key2/value2
					var url2 = url.substring(index2, url.length);
				}
			}
			// case 2: key is not present in URL
			else{
				// url1 = www.example.com/key3/ (let's say key = "key3"...)
				url1 = url + '/'+key+'/';
			}
			
			// case 1: url = www.example.com/key1/
			// case 2: url = www.example.com/key3/
			url = url1;
		}
		else
		{
			// if there is no "/" at the end of url, add it
			if(url.lastIndexOf('/') != (url.length-1))
				{url += '/';}
		}
		
		// case 1: url = www.example.com/key1/toto
		// case 2: url = www.example.com/key3/toto
		// case key == null: url = www.example.com/toto
		url += value;
		if(url2){
			// only case 1: url = www.example.com/key1/toto/key2/value2
			url += url2;
		}
		
		// redirect
		document.location = url;
	}
}
function loadCategoryImage(id)
{
	var src = $("#categoryDescription img:first").attr("src");
	if(src)
		{$("#"+id).attr("src", src);}
	else
		{$("#"+id).attr("src", "/images/product_noimage.jpg");}
}
