$(document).ready(function(){
	//Constructores de clases
	// Agregar clases especificas para inputs.
	$("input[type='text']").addClass('input_txt');
	$("input[type='image']").addClass('input_img');
	$("input[type='password']").addClass('input_txt');
	$("input[type='button']").addClass('input_btn');
	$("input[type='submit']").addClass('input_btn');	
	$("input[type='radio']").addClass('input_rad');	
	$("input[type='checkbox']").addClass('input_chk');
	$("textarea").addClass('input_tar');
	$("select").addClass('input_sel');
	// Transversal, Input Super Cleaner
	$(".clean").each(function(){
		// TODO to native js		
		var value = $(this).val();
		$(this).focusin(function(){if($(this).val() == value){$(this).val("");};});
		$(this).focusout(function(){if($(this).val() == ''){$(this).val(value);};});
	});
	// Funcion para generar diferenciaciones en elementos de lista y tablas
	/* TODO(pnoel) this is a critical bug for JS applications that use references by id and classnames: ad.texteditor.js. See bug #10
	
	$("ul").each(function(){
		// TODO use django cycle and indexes
		$(this).find('li:first').addClass('fst');
		$(this).find('li:odd').addClass('evn');
		$(this).find('li:last').addClass('lst');
	});
	$("table").each(function(){
		// TODO idem
		$(this).find('tr:first').addClass('fst');
		$(this).find('tr:odd').addClass('evn');
		$(this).find('tr:last').addClass('lst');
	});
	$("table tr").each(function(){
		// TODO idem
		$(this).find('td:first').addClass('fst');
		$(this).find('td:odd').addClass('evn');
		$(this).find('td:last').addClass('lst');
	});
	$("form").each(function(){
		// TODO idem (django cycle only works for iterations, figure a workaround)
		$(this).find('p:first').addClass('fst');
		$(this).find('p:odd').addClass('evn');
		$(this).find('p:last').addClass('lst');
	});*/

	$('.short_txt textarea').keyup(function() {
		var lenght = this.value.length;
		if (lenght >= 600) {
		this.value = this.value.substring(0, 600);
		}
		$('.short_msg').text(600 - lenght);
	});	
	
	$('.incontext a[href="#about"]').click(function(){
		$(".incontext .hide").slideToggle();
	});
	
// Message Load
	$("#bottle").animate({'top':'0px'},'500').delay('5000').animate({'top':'-100px'},'500');
	$("#bottle .options a").click(function(){$("#bottle").stop().animate({'top':'-100px'},'500');});

// Filters: Add Filter
	$("#filter form a").click(function(){
		// TODO(raliste) ajax_search and sammyzize it
		var clase = '.'+$(this).parent().attr('class');
		var size = $(clase).size();
		if( $(this).attr('class') == 'remove' ){
			$(this).parent().slideUp(function(){$(this).remove();});
			$(clase).eq(0).find('a').show();
			$(clase).eq(0).find('small').remove();
			return false;
		}
		else if ( size <= 3) {
			var value = $(this).parent().clone().hide();
			var para = $(value).find('label').attr('for');
			
			$(value).find('label').attr('for', para);
			$(value).find('select').attr('id', para);
			$(value).find('a').html('remove?').attr('class','remove');			
			
			$(this).parent().after(value);
			$(value).slideDown();
			return false;
		}
		else{
			$(this).hide().before('<small>Just reached the max!</small>');
			return false;
		}
	})

	// Catalog Grid View: show meta info
    $('#catalog_objects.grid li').hover(function() {
		$(this).find('.meta').slideToggle();
	});	
	
// Catalog Tittle: show more items
     $('#showmore').hover(function() {
              var elemento = $(this).find('ul');
              var t = setTimeout(function() {
                    $(elemento).slideDown();
              }, 500);
              $(this).data('timeout', t);
          }, function() {
               clearTimeout($(this).data('timeout'));
    });
    $("#showmore a").click(function(){
    	$('#showmore ul').slideToggle();
    });
    $("#showmore ul").mouseleave(function(){
    	$(this).slideUp();
    });
/* 	$("#id_publish_at").datepicker(); */
	
	
// Scrollable
	//$(".scrollable").scrollable();
	
	
// Social Bar behavior and shit
	jQuery.event.add(window, "load", viewport);
	jQuery.event.add(window, "resize", viewport);
	
	function viewport(){
	    var resolution = $(window).width();
		if( resolution <= '1180' ){
			$(".actibox").removeClass('vertical');
			$(".actibox").addClass('horizontal');
		}
		else{
			$(".actibox").removeClass('horizontal');
			$(".actibox").addClass('vertical');
		}
	}
	if(navigator.userAgent.match(/iPad/i)) {
		$(".actibox").removeClass('vertical');
		$(".actibox").addClass('horizontal');
	}
	
	$(window).scroll(function(){		
		var size = $(".actibox.fixed").parent().height();
		var actipos = $(window).scrollTop();
		var donde = parseInt(size - actipos);
		if( donde <= 0){
			$(".actibox.vertical.fixed").fadeOut();
		}
		else{
			$(".actibox.vertical.fixed").fadeIn();
		}
	});
	
	
	
/* Request forms behavior TODO: finish this! */
	$("#request .btn").click(function(){
		var pos = $(this).parent().position();
		var scroll = pos.top+100;
		$('body').animate({scrollTop: scroll});
		$(this).addClass('open');
		$(this).parent().find('form').slideDown();
		return false;
	});
	$("#request form .btns a").click(function(){
		$('body').animate({scrollTop: '0'}, 700);
		$(this).parent().parent().fadeOut();
		$(this).parent().parent().parent().find('.btn').removeClass('open');
	});
	
	$("#troncal .active").append('<span></span>');

// Bookmarks
	$('#bookmark_button').click(function() {
		var $this = $(this);
		if($this.hasClass('logged_out')) {
			return true;
		}
		if($this.hasClass('selected')) {
			$this.removeClass('selected');
		} else {
			$this.addClass('selected');
		}
		$.get(this.href, {t: Math.random()}, function() {

		});

		return false;
	});
	
// Legacy -->	
	
// Efectos	
	// Efecto de transparencia
	$(".trans").append('<div class="trans_bck"></div>');
	$(".trans .trans_bck").css({'opacity':0.60});
	
	// Contratulations
	var keys=[];var konami='38,38,40,40,37,39,37,39,66,65';
	$(document).keydown(function(e){keys.push(e.keyCode);
	if(keys.toString().indexOf(konami)>=0){window.open('http://www.youtube.com/watch?v=dQw4w9WgXcQ', '123');
	keys=[];}});
	
// Funciones
	// Funcion para popup
	$(".popup").click(function(){
		var url = $(this).attr('href');
		window.open(url,'qwe123', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=670,height=420,left = 630,top = 320');
		return false;
	});
	// Funcion para Tabs
	/*$(".tabs .tbox").hide();
	$(".tabs .tbox").eq(0).show();
	$(".tabs .tmenu li").eq(0).addClass('active');
	$(".tabs .tmenu li a").click(function(){
		var tab = '#' + $(this).attr('href').substring(1);
		$(this).parent().parent().find('li').removeClass('active');
		$(this).parent().addClass('active');
		$(this).parent().parent().parent().find('.tbox').hide();
		$(this).parent().parent().parent().find(tab).show();
		return false;
	});*/
	$('.tabs .tmenu li a').click(function(e) {
	    var parent = $(this).parent();
        if(parent.hasClass('active'))
    	    parent.removeClass('active');
    	    
        parent.addClass('loading_small');
        
        var self = this; // TODO(eng, pnoel): we need to change this to a special div so JS can load the spinner properly
        window.setTimeout(function() {
            window.location.href = self.href;
        }, 10);
        
        return false;
	});
});

