$(document).ready(init);

function init()
{
	// Añadimos validación de campos al formulario de contacto
	$('#contact_form').validate({
		rules: {
			name: 'required',
			email: {
				required: true,
				email: true
			},
			message: 'required'
		},
		errorElement: 'span'
	});
}

/*
// ´Focus first element
$.fn.focus_first = function() {
  var elem = $('input:visible', this).get(0);
  var select = $('select:visible', this).get(0);
  if (select && elem) {
    if (select.offsetTop < elem.offsetTop) {
      elem = select;
    }
  }
  var textarea = $('textarea:visible', this).get(0);
  if (textarea && elem) {
    if (textarea.offsetTop < elem.offsetTop) {
      elem = textarea;
    }
  }
  
  if (elem) {
    elem.focus();
  }
  
  return this;
}
var inc=170;
var inc2=169;
var pos=0;
var pos2=0;
var tot=0;
var tot2=0;
var canal_actual=1;
var stop=true;

$(document).ready(function(){
	/////////////////Calcul de l'scroll de la home//////////////////////////////////////
	if($("#scroll_home li").length>0){
		tot=$("#scroll_home li").length;
		$("#scroll_home").css("width",parseInt(tot)*inc);
	}
	if($("#slide_canal li").length>0){
		tot2=$("#slide_canal li").length;
		$("#slide_canal").css("width",parseInt(tot2)*inc2);
	}
	
	/////////////////Buidament de camps de input en cerca i login//////////////////////////////////////
	$(".text_field:not(#cerca_text)").blur(function(){
		if($(this).val()=="") $(this).val("usuari");
	});
	$(".text_field:not(#cerca_text)").focus(function(){
		if($(this).val()=="usuari") $(this).val("");
	});
	$("#cerca_text").blur(function(){
		if($(this).val()=="") $(this).val("Cerca recepta");
	});
	$("#cerca_text").focus(function(){
		if($(this).val()=="Cerca recepta") $(this).val("");
	});
	//////////////////////////////////////
});
function left_scroll(){
	
	if(stop && pos<0){
		stop=false;
		pos+=inc;
		$("#scroll_home").animate({marginLeft:pos},250,function(){
			stop=true;
		});
	}
}

function right_scroll(){
	
	if(stop && pos>-(inc*(tot-3))){
		stop=false;
		pos-=inc;
		$("#scroll_home").animate({marginLeft:pos},250,function(){
			stop=true;
		});
	}
}
function left_canal(){
	
	if(stop && pos2<0){
		stop=false;
		pos2+=2*inc2;
		$("#slide_canal").animate({marginLeft:pos2},250,function(){
			canal_actual--;
			$("#canal_actual").html(canal_actual);
			stop=true;
		});
	}
}

function right_canal(){
	
	if(stop && pos2>-(inc2*(tot2-2))){
		stop=false;
		pos2-=2*inc2;
		$("#slide_canal").animate({marginLeft:pos2},250,function(){
			canal_actual++;
			$("#canal_actual").html(canal_actual);
			stop=true;
		});
	}
}
function show_acordio(i){

$("div[id*=popup-]").slideUp();
$(".show_hide .hide").hide();
$(".show_hide .show").show();

$("#popup-"+i).slideDown();
$("#acordio-"+i+" .show").hide();
$("#acordio-"+i+" .hide").show();

}
function login(){
	
	$("#login_box").slideUp('slow',function(){
		$.post("/ajax/login",{user:$("#user").val(), password:$("#password").val() },function(data){
			if(data=="error-1"){ //Error en login
				$("#login_box input[@type!=submit]").css("background","#ffff66");
				$("#login_box").slideDown();
			}
			else if(data=="error-2"){ //Faltan campos
				$("#login_box input[@type!=submit]").css("background","#ffff66");
				$("#login_box").slideDown();
			}
			else{ //Todo OK
				
				$("#login_box").parent().addClass("user_logged");
				$("#login_box").html(data);
				$("#login_box").slideDown();
			}
		});
	});
	
}
function show_tab(tab){
	$(".users_list").hide();
	$("#"+tab).show();
	$(".users_list").parent().removeClass("select");
	$("#"+tab).parent().addClass("select");
}

function add_ingredient(){
	//console.log($("#ingredients").val());
	val=$("#ingredients").val();
	if(val!="") $("#list_ingredients").append("<li><strong>"+val+"</strong> <span><a href='javascript://' onclick='remove_ingredient(this);'>X</a></span></li>");
	$("#ingredients").val("");	
	$("#ingredients").focus();
	
}

function remove_ingredient(item){
	$(item).parent().parent().remove();

}
function publica_recepta(){
	val="";
	$("#list_ingredients li").each(function(){
		val+="&$"+$(this).children("strong").html()
		console.log($(this).children("strong").html());
	});
	
	$("#cu_ingredients").val(val);
	 document.nova_recepta_1.submit();
}
function enter(e) {
  tecla = (document.all) ? e.keyCode : e.which;
  if (tecla==13) add_ingredient();
}
*/