/* 
	Author: underscreen.com
*/

/* ---------------------------------- */
/* Initialize */

jQuery(
  
  function ($) {
		
		$.dbg = false; // habilita debug de acciones

    $.Body = $('body');
    
    $.Window = $(window);
    
    $.Scroll = ($.browser.mozilla || $.browser.msie) ? $('html') : $.Body;
    
    $.Mobile = ($.Body.hasClass('webkit-mobile') || (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i))),
    
    $.Unsupported = $.Body.hasClass('unsupported-browser');
    
    $.Body.Keyboard();
    
    $('[data-controller]').Instantiate();
		
		// init image viewer
		$("a[rel='image_explorer']").colorbox();
		// init vimeo viewer
		$("a[rel='vimeo_explorer']").colorbox({iframe:true, innerWidth:640, innerHeight:460});
		// init vimeo radio viewer
		$("a[rel='radio_explorer']").colorbox({iframe:true, innerWidth:400, innerHeight:225});
    
  } 
  
);

/* ---------------------------------- */
/* Auto Instantiate */

(function($) {

  $.fn.Instantiate = function(settings) {
     
    var config = {};
 
    if (settings) $.extend(config, settings);
  
      this.each(function() { 

					var $self = $(this),
							$controller = $self.attr('data-controller');
									
					if ($self[$controller]) 
					{
						if ($.dbg) console.log('Instantiate data-controller: '+$controller);
						$self[$controller]();
					}
              
      });
      
  }
    
})(jQuery);

/* ---------------------------------- */
/* Items */

(function($) {
  
   $.fn.Items = function(settings) {
		 
		var config = {};
 
    if (settings) $.extend(config, settings);
   
     this.each(function() { 		
      
        var $items = $(this),
						$controles = $('#controles'),						
						$btnMenos = $('<a id="btnMenos">&lt;</a>').appendTo($controles),
						$btnMas = $('<a id="btnMas">&gt;</a>').appendTo($controles),
						currentLeft = 0,
						animating = false;
				
				$btnMas.click(function(){move(-850)});
				$btnMenos.click(function(){move(850)});
				
				function move(cuanto)
				{
					if (!animating)
					{
						animating = true;
						
						currentLeft += cuanto;
						if (currentLeft>0) currentLeft=0;
											
						$items.animate({
							marginLeft: currentLeft,
						}, 1000, 'easeInOutCubic', function(){
							//console.log('complete');
							animating = false;
						});
					}
				}
                      
     });
		      
		return this;
     
  } // Items	
    
})(jQuery);

/* ---------------------------------- */
/* Contacto */

(function($) {
  
   $.fn.Contacto = function(settings) {
		 
		var config = {};
 
    if (settings) $.extend(config, settings);
   
     this.each(function() { 		
      
        var $self = $(this),
						$mapa = $('#mapa');
				
				$mapa.gMap({ 
					markers: [{ 
						latitude: -34.581537,
						longitude: -58.408195				
					}],
					zoom: 15,
					controls:[],
					maptype: G_NORMAL_MAP
				});  
				
     });
		      
		return this;
     
  } // end of Contacto	
    
})(jQuery);

/* ---------------------------------- */
/* Clientes */

(function($) {
  
   $.fn.Clientes = function(settings) {
		 
		var config = {};
 
    if (settings) $.extend(config, settings);
   
     this.each(function() { 		
      
        var $self = $(this);
				
				
                      
     });
		      
		return this;
     
  } // end of Clientes	
    
})(jQuery);

/* ---------------------------------- */
/* Acordeon */

(function($) {
  
   $.fn.Acordeon = function(settings) {
		 
		var config = {};
 
    if (settings) $.extend(config, settings);
   
     this.each(function() { 		
      
        var $self = $(this),
						$btns = $('a.acordeonBtn',$self),
						$infos= $('.acordeonInfo',$self);
				
				$btns.each(function(){
					
					var $btn = $(this),
							$info = $btn.next('.acordeonInfo');
							
					function infoClose() {
						$info.css('display','none');
						$btn.removeClass('active');
					};
					
					// preparo el contenido. Cierro si no esta activo.
					if (!$btn.hasClass('active')) infoClose();
					
					// Listeners
					$btn.bind('click', function(e)
					{						
						e.preventDefault();
												
						if($btn.hasClass('active')) 
						{ // cerrar
							$btn.removeClass('active');
							infoClose();						
						}	
						else 
						{ // abrir	
							$.fn.AcordeonClose({infos:$infos}); // cierra los abiertos
							
							$btn.addClass('active');													
							$info.show();
						}
						
					});
					
				});		
				
                      
     });
		      
		return this;
     
  } // Acordeon
	
	$.fn.AcordeonClose = function(settings) {
		 
		var config = {};
 
    if (settings) $.extend(config, settings);
		
		// cierra todos los acordeones
		$acordeones = (config.infos) ? config.infos : $('.acordeonInfo');
		
		$acordeones.each(function(){
			$(this).css('display','none');
			$(this).prev('a').removeClass('active');
		});
	}
    
})(jQuery);

/* ---------------------------------- */
/* Keyboard */

(function($) {

   $.fn.Keyboard = function(settings) {
     
    var config = {};
 
    if (settings) $.extend(config, settings);
  
      this.each(function() { 
      
        var $self = $(this);
      
        $(document)
        .bind('keydown',on_keydown);
        
        function on_keydown(e) {
          
          var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
          
          switch(key) {
             
             case 27: //escape
              $.Body.triggerHandler($.Events.KEY_ESC);                        
             break;
             
             case 32: //space
              $.Body.triggerHandler($.Events.KEY_SPACE);                        
             break;
             
             case 38: //top              
              $.Body.triggerHandler($.Events.KEY_UP);                        
             break;
           
             case 39: //right
              $.Body.triggerHandler($.Events.KEY_RIGHT);
              e.preventDefault();              
             break;
             
             case 40: ///bottom            
              $.Body.triggerHandler($.Events.KEY_DOWN);                        
             break;
              
             case 37: //left             
              $.Body.triggerHandler($.Events.KEY_LEFT);                        
             break;

          }//switch
          
        }//keydown
  
      }); 
      
      return this;    
  } 
})(jQuery);

/* ---------------------------------- */
/* Events */

(function($) {

  $.Events = {
     
     SECTION_ENTER: 'sectionEnter',
     
     SCROLL_TO: 'scrollTo',
     
     SCROLL: 'windowScroll',
     SCROLL_ENTER: 'windowScrollEnter',
     SCROLL_LEAVE: 'windwScrollLeave',
     
     KEY_UP: 'keyUp',
     KEY_DOWN: 'keyDown',
     KEY_LEFT: 'keyLeft',
     KEY_RIGHT: 'keyRight',
     KEY_ESC: 'keyEsc',
     KEY_SPACE: 'keySpace'
    
  } // Events  
  
  $.Views = {
  
  
  } // Views 

})(jQuery);

/* ---------------------------------- */
/*  @CONTCATO JS  */
/*  FUNCTION VALIDATE FORM */

function validate_form()
{
	var $form = $('#contacto'),
			$resp = $('#response');	
	//alert(form.serialize());	
	if(isValidForm($form))
	{	
		$resp.html('Enviando formulario...').show();
		$form.hide();
		ajax_send_form($form);
	}	
}

/* FUNCTION AJAX SEND FORM */

function ajax_send_form(form)
{
	$.ajax({
		url: 'sections/home/enviarForm.php',
		type: "POST",
		data: form.serialize(),
		success: function(data) {
			$('#response').empty();
			$('#response').html(data);
		}
	});
}

/* COMMON FUNCTION */

function isValidEmail(email)
{
	apos = email.indexOf("@");
	dotpos = email.lastIndexOf(".");
	if (apos<1||dotpos-apos<2) 
		return false;
	else 
		return true;
}

function isValidForm()
{
	email = $('#email').val();
	if (isValidEmail(email)) {
		return true
	}
	else {
		alert('Por favor ingrese una direccion de email válida.');
		return false;
	}
}
