var current_banner = 1;
var max_rotator = 3;
var item_num = 0;

function banner_animation() {
	current_banner++;
	 if (current_banner > max_rotator) current_banner = 1;
	 
	$(".banner").fadeOut("slow");
	$("#banner"+current_banner).fadeIn("slow");
}

function ad_type(s)
{
	if (s!="Seleccionar")
	{
		$("#placement").val(s);
		width = s.split("(");
		width = width[1].split(" x");
		$("#width").val(width[0]);
		height = s.split("x ");
		height = height[1].split(")");
		$("#height").val(height[0]);
	} 
}

function change_media_type()
{
		var val = $("#tipo_id").val();
		switch($("#tipo_id option[value='"+val+"']").text())
		{
			case "Website":
			case "Mobile":
				$(".views_name").html("Páginas visitadas al mes");
				$("#views_name").hide();
				$("#views_id").parent().show();
				break;
			case "Facebook":
				$(".views_name").html("Cantidad de fans");
				$("#views_name").show();
				$("#views_id").parent().hide();
				break;
			case "Google+":
			case "Twitter":
				$(".views_name").html("Cantidad de seguidores");
				$("#views_name").show();
				$("#views_id").parent().hide();
				break;
			case "Youtube":
				$(".views_name").html("Cantidad de visitas a vídeos");
				$("#views_name").show();
				$("#views_id").parent().hide();
				break;
		}
}

function media_type(s)
{
	if (s!="Seleccionar")
	{
		s1 = s.split("(");
		s2 = s1[1].split(")");

		$("#width").parent().hide();
		$("#height").parent().hide();
		$("#tipoe_id").parent().hide();
		$("#width").val(0);
		$("#height").val(0);


		switch(s2[0])
		{
			case "Facebook":
				$("#precio_caption").html("Precio por post");
				$("#placement").val("1 Facebook Post");
				break;
			case "Google+":
				$("#precio_caption").html("Precio por post");
				$("#placement").val("1 Google+ Post");
				break;
			case "Youtube":
				$("#precio_caption").html("Precio anuncio en vídeo");
				$("#placement").val("1 Anuncio en Youtube");
				break;
			case "Twitter":
				$("#precio_caption").html("Precio por tweet");
				$("#placement").val("1 Tweet");
				break;
			case "Website":
			case "Mobile":
				$("#placement").val("");
				$("#precio_caption").html("Precio por mes");
				$("#width").parent().show();
				$("#height").parent().show();
				$("#tipoe_id").parent().show()();
				break;
		}
	} 
}

$(function(){
	// Banner animation
	inter = setInterval("banner_animation()", 8000);
	 
	// Register Form validation
	$("#forma_registro").validate({rules:{contrasena2:{equalTo:"#contrasena"}}});	
	
	// Delete warning
	$(".delete_btn").click(function(){
		mess="Desea borrar este registro?";
		if (confirm(mess)) {
			return true;
		} else {
			return false;
		}
	});
	
	// Date Pickers
	$("#start_date").datepicker();
	$("#end_date").datepicker();
	
	// 404 images
	$("img").error(function(){
    	$(this).hide();
  	});
  	
  	// Ad types
  	$("#tipoe_id").change(function(){
  		ad_type($("#tipoe_id option[value='"+$(this).val()+"']").text());
  	});
  	
  	// Fancybox
  	$("a.fancy").fancybox({
		'hideOnContentClick': false
	});
	
	// Spaces according to Media
	$("#website_id").change(function(){
		media_type($("#website_id option[value='"+$(this).val()+"']").text());
	});
	
	// Views according to media type
	$("#tipo_id").change(function(){
		change_media_type();
	});
	change_media_type();
	
	// PP Items
	
	$(".chk").click(function(){
	
		$("#pp_items").html("");
		item_num=0;
		$('.chk').each(function(index) {

			id = this.id;
			id = id.substring(4);
			if($(this).is(":checked"))
			{
				item_num++;
				amount = $(this).val();
				$("#pp_items").append("<div id='it_"+id+"'><input type='hidden' name='item_name_"+item_num+"' value='Anuncio # "+id+"'><input type='hidden' name='amount_"+item_num+"' value='"+amount+"'></div>");
			}
			else
			{
				//$("#it_"+id).remove();
			}
			
		});
	
	});
});
