
/* Dropdownize a menu with sublists
----------------------------------------------------------------------------------------------------*/
(function($) {
  $.fn.dropdownize = function(options) {
    // build main options before element iteration
    var opts = $.extend({}, $.fn.dropdownize.defaults, options);
    $.fn.dropdownize.options = opts;
    // iterate and reformat each matched element
    return this.each(function() { 
      var childs = $(this);
      var menu = childs.closest('ul');
      childs
        .bind('mouseover', function(e){
          var target = $(this),
          submenu = $(opts.sub_element, target),
          side = parseInt($(document).width()-opts.content_width)/2,
          dist = target.position().left-side;
          target.addClass(opts.hover_class);
          if (submenu.width()+dist >= opts.sensitive_width) submenu.css({left: 'auto', right: opts.offset_x});
          submenu.removeClass('accessible');
        })
        .bind('reset', function(e){
          $(opts.sub_element, menu).addClass('accessible');
          $(this).removeClass(opts.hover_class);
        });
        
        $('body .cabecera, body .container_int, .mod_superdestacado_multi').bind('mouseover', function(){
          childs.trigger('reset');
        })
        
        $('> li > a', menu).bind('mouseover', function(){
          childs.trigger('reset');
        })
      
    });
  };
  // plugin defaults
  $.fn.dropdownize.defaults = {sub_element: "ul", content_width: 996, hover_class: 'over', sensitive_width: 920, offset_x: '3px'};
})(jQuery);


/* Made tabs -- Modificado i3
----------------------------------------------------------------------------------------------------*/
(function($) {
$.fn.tabify = function(container){
  var containers = $(container);
  return this.each(function(i, e){
    var menu = $(this);
    var container = $('> div', containers.eq(i));
    container.not(':first').hide();
    $('li:first', menu).addClass('active');
    $('li a', menu).bind('click', function(e){
      e.preventDefault();
      var index = $('li a', menu).index( this );
      $('li', menu).removeClass('active');
      $(this).parent().addClass('active');
      container.hide();
      container.eq(index).show();
    })
  })  
}
})(jQuery);
/*(function($){ 
     $.fn.extend({  
         tabify: function() {
			function getHref(el){
				hash = $(el).find('a').attr('href');
				if(hash)
					return hash.substring(0,hash.length-4);
				else
					return false;
				}
		 	function setActive(el){
				$(el).addClass('active');
				if(getHref(el))
					$(getHref(el)).show();
				else
					return false;
				$(el).siblings('li').each(function(){
					$(this).removeClass('active');
					$(getHref(this)).hide();
				});
			}
			return this.each(function() {
				var self = this;
				
				$(this).find('li a').each(function(){
					$(this).attr('href',$(this).attr('href') + '-tab');
				});
				
				function handleHash(){
					if(location.hash)
						setActive($(self).find('a[href=' + location.hash + ']').parent());
				}
				if(location.hash)
					handleHash();
				setInterval(handleHash,100);
				$(this).find('li').each(function(){
					if($(this).hasClass('active'))
						$(getHref(this)).show();
					else
						$(getHref(this)).hide();
				});
            }); 
        } 
    }); 
})(jQuery);*/

/* SuperDestacado con tabs
----------------------------------------------------------------------------------------------------*/
(function($) {
  $.fn.highlighter = function(options) {
    var opts = $.extend({}, $.fn.highlighter.defaults, options);
    $.fn.highlighter.options = opts;
    
    // iterate and reformat each matched element
    return this.each(function() {
      var target = $(this);
      opts.target = target;
      var list_items = $(opts.list_items+' a', target);
      var viewport = $(opts.viewport, target);
      var banners = $(opts.full_items, opts.viewport);
      
      var other_banners = banners.not(':first');
/*      other_banners.hide();*/
      $('.info',other_banners).css('right', '-250px');
      $(list_items[0]).parent().addClass('active');
      
      list_items
        .bind('click', function(e, target){
          e.preventDefault();
          //reset setInterval if user click on tab
          if (typeof(target) == 'undefined' ) {
            window.clearInterval(opts.autoplay_timer);
          }
          
          if($(this).closest('li').is('.active') !== true){
            var new_banner = $(banners[list_items.index(this)]);
            
            list_items.parent().removeClass('active');
            $(this).parent().addClass('active');
            
            $('.info', banners.not(':hidden')).animate({right: '-250px'},'slow');
            banners.has(':visible').fadeOut('normal', function(){
              new_banner.fadeIn('normal',function(){
                $('.info', new_banner).animate({right: '0px'},'slow');
              });
            });
          }
        })
        .bind('mouseover', function(){
            window.clearInterval(opts.autoplay_timer);
        })
        .bind('mouseout', function(){
            opts.autoplay_handler();
        })
      
      if (opts.autoplay === true){
        opts.autoplay_handler = function(){
          opts.autoplay_timer =  window.setInterval(function(){
            var next = $('a', $(opts.list_items+'.active', opts.target).next());
            if(next.length > 0){
              next.trigger('click', true);
            }else{
              $('a', $(opts.list_items+':first', opts.target)).trigger('click', true);
            }
          }, opts.delay);
        }
        opts.autoplay_handler();
      }

    });
  };
  
  // plugin defaults
  $.fn.highlighter.defaults = {viewport: ".visor", list_items: 'ul li', full_items: '> div', delay: 1000 , autoplay : false };
})(jQuery);



/* Character count
----------------------------------------------------------------------------------------------------*/
(function($) {
  $.fn.characterCounter = function(options) {
    var opts = $.extend({}, $.fn.characterCounter.defaults, options);
    $.fn.characterCounter.options = opts;
    // iterate and reformat each matched element
    return this.each(function() { 
      var $target = $(this), wrap = $target.closest(opts.wrap), counter = $(opts.counter , wrap), c_actual = $(opts.c_actual, counter), c_total = $(opts.c_total, counter);
      c_total.text(opts.limit);
      
      $target
        .bind('countChars', function(e){
          var $target_val = $target.val();
            if ($target_val.length >= opts.limit){
              $target.val($target_val.substring(0, opts.limit));
              c_actual.html(0);
            }else{
              c_actual.html(opts.limit-$target_val.length);
            }
          })
        .bind('keyup', function(){
          $target.trigger('countChars');
        })
        .trigger('countChars');
    });
  };
  
  $.fn.characterCounter.defaults = { limit:140, wrap: 'p', counter: '.counter', c_actual: '.actual', c_total: '.total'};
})(jQuery);


/* Configurations for tooltips 
----------------------------------------------------------------------------------------------------*/
if($.fn.qtip !== undefined) {
  $.fn.qtip.styles.login = { 
  	width: 260,
  	color: '#333333',
  	background: 'transparent',
  	textAlign: 'left',
  	border: {
  		width: 0
  	},
  	classes: {
  	  tooltip: 'tooltip_acceso_zona_3'
  	}
  }
  
  var tooltip_click_config = {
  	style: 'login',
  	show: { when: { event: 'click' } },
  	hide: { when: { event: 'click' } },
  	position: { 
  		corner: { target: 'bottomLeft', tooltip: 'topLeft' },
  		adjust: { y: 6, x: -191 }
  	}
  }
}


if($.validator !== undefined){

    $.validator.addMethod(
         "dateExists",
         function(value, element) {
             date = value.split('/');
             date[1]--
             var oDate = new Date(date[2],date[1],date[0]);
             if (date[2] != oDate.getFullYear() ||  date[1] != oDate.getMonth() || date[0] != oDate.getDate()){
                return false;
             }
             return true;
         },
         "&nbsp;"
     );
    
    $.validator.addMethod(
            "dateCoherent",
            function(value, element) {
                date = value.split('/');
                //date[1]--
                var currentDate = new Date();                
                if (date[2] < (currentDate.getFullYear()-100) || date[2] > (currentDate.getFullYear()-1)){
                	logBug("date mal");
                   return false;
                }                
                return true;
            },
            "&nbsp;"
        );
    
    $.validator.addMethod(
            "noTeens" ,
            function (value, element) {
                date = value.split( '/' );
                //date[1]--
                var  currentDate = new  Date();             
                if  (date[2] > (currentDate.getFullYear()-14)){
                logBug( "No se permite el registro de menores." );
                    return   false ;
                } else   if (date[2] == (currentDate.getFullYear()-14 )) {
                if (date[1] > currentDate.getMonth()+1) {                	
                logBug( "No se permite el registro de menores." );
                        return   false ;
                } else   if (date[1] == currentDate.getMonth()+1) {
                if (date[0] > currentDate.getDate()) {
                logBug( "No se permite el registro de menores." );
                            return   false ;
                }
                }
                }
                return   true ;
            },
            "&nbsp;"
        );
    $.validator.addMethod(
            "dni" ,
            function(value, element) {
            if(/^([0-9]{8})*[a-zA-Z]+$/.test(value)){
            var numero = value.substr(0,value.length-1);
            var let = value.substr(value.length-1,1).toUpperCase();
            numero = numero % 23;
            var letra='TRWAGMYFPDXBNJZSQVHLCKET';
            letra=letra.substring(numero,numero+1);
            if (letra==let)
            return true;
            return false;
            }
            return this.optional(element);
            },
            "&nbsp;"
        );
       
}
