$(document).ready(function() {
						   
	Galleria.loadTheme('common/galleria/themes/classic/galleria.classic.min.js');
	$("#gallery").galleria({
		width: 480,
		height: 400
	});						   
						   
	
	$(".rating li").mouseover(function(){
		var parent = $(this).parent().attr("id");
		if(!$("#"+parent).hasClass("rated")) {
			var index = $("#"+parent+" li").index(this);
		$("#"+parent+" li:lt("+(index+1)+")").addClass("hover");
			$("#"+parent+" li").addClass("mover");
		}
	}).mouseout(function(){
		var parent = $(this).parent().attr("id");
		if(!$("#"+parent).hasClass("rated")) {
			$("#"+parent+" li").removeClass("hover").removeClass("mover");
		}
	}).click(function(){
		var parent = $(this).parent().attr("id");
		if(!$("#"+parent).hasClass("rated")) {
			$("#"+parent).addClass("procesando");
			var index = $("#"+parent+" li").index(this);
			$("#"+parent).addClass("rated");
			$("#"+parent+" li").removeClass("on");
			$("#"+parent+" li:lt("+(index+1)+")").addClass("rated");
			$.post("/valorar", { obj: parent.replace("sb-",""), val: (index+1) }, function(){
				$("#"+parent).removeClass("procesando");
			});
		}
	});
	
	
	$(".connect").bind("click", function() {
		FB.login(function(response) {
			if (response.authResponse) {
				if (response.status=="connected") {
					$.post("/login", { fbid: FB.getAuthResponse().userID }, function(){																				  
						self.location.reload();
					});
				}
			}
		}, {scope:"publish_stream,email,offline_access"});
		return false;
	});
	
	$("#agregarComentario input, #hazTuPregunta input").focus(function(){
		$(this).val("");
	});
	
	$("form.validar").submit(function(){
		var valid = true;
		$("input.obligatorio, select.obligatorio, textarea.obligatorio").each(function(){
			if($.trim($(this).val())==""){
				alert($(this).attr("title"));
				$(this).focus();
				valid = false;
				return false;
			}
		});
		return valid;
	});
	
	if($(".masproductos div.producto").length){
		var col_max = 0;
		var col_idx = 0;
		$(".masproductos div.producto").each(function(i){
			if($(this).height()>col_max){
				col_max = $(this).height();
			}
			if(i%4==3 || i==($(".masproductos div.producto").length-1)){
				$(".masproductos div.producto").eq((col_idx*4)+0).height(col_max);
				$(".masproductos div.producto").eq((col_idx*4)+1).height(col_max);
				$(".masproductos div.producto").eq((col_idx*4)+2).height(col_max);
				$(".masproductos div.producto").eq((col_idx*4)+3).height(col_max);
				col_max = 0;
				col_idx++;
			}
		});
	}
	
	
});


var ticker = setInterval('showTicker()', 4000);
var items = 4;
function showTicker(){	
	if(items>=$("#count_esquelas").val()) {
		$("div.twrap").animate({"top": "0px"}, "fast"); items = 3;
	}
	else {
		$("div.twrap").animate({"top": "-=30px"}, "slow");
	}
	items++;
}


var ticker2 = setInterval('showTicker2()', 4000);
var items2 = 1;
function showTicker2(){	
	if(items2>=$("#count_questions").val()) {
		$("div.cont_questions_wrap").animate({"top": "0px"}, "fast"); items2 = 1;
	}
	else {
		$("div.cont_questions_wrap").animate({"top": "-=120px"}, "slow");
	}
	items2++;
}
