function touchscreen() {
	$("#touch").fadeOut();	
}

$(document).ready(function() {
	
	$.tmp_value = '';
	$('#langs a').hover(function(){
		if(!$(this).hasClass('active'))
		{
			$.tmp_value = $(this).html();
			$(this).html($(this).attr('rel'));
		}
	},
	function(){
		if(!$(this).hasClass('active'))
		{
			$(this).html($.tmp_value);
		}
	});
	
	/* Pluguin videowall */
	$("#videowall").videowall();
	//Funciones al poner el ratón sobre un bloque
	$("#videowall").videowall.setMouseOver(function(selector){
		//console.log($(selector).attr("work"));
	}); 
	$("#videowall").videowall.stop();
	
	$.touch_manual = false;
	
	$("#touch").mousemove(function(e){
		if($.touch_manual == false)
		{
			if(!$.pageX) $.pageX = e.pageX;
			if(!$.pageY) $.pageY = e.pageY;
			if(e.pageX > $.pageX + 50 || e.pageX < $.pageX - 50 || e.pageY > $.pageY + 50 || e.pageY < $.pageY - 50)
			{
				$(this).delay(300).fadeOut(1000);
				$.touch_manual = true;
			}
		}
  });
	
	$("#touch").click(function(){
		$(this).fadeOut();
		$("#videowall").videowall.play();
	});
	
	$(".logo").click(function(){
		$("#touch").fadeIn();
		$("#videowall").videowall.stop();
	});

	// Defaults
	$.current = 'main';
	$.wpage = 0;
	$.social_mode = 'main'; //main o right
	$.status_transition = true;
	$.gstatus_transition = true;
	$.transition_time = 1500;
	
	$.min_height = 660; // 660px
	$.min_width = 1010; // 1010px
	
	if(($.height = $(document).height()) < $.min_height) $.height = $.min_height;
	if(($.width = $(document).width()) < $.min_width) $.width = $.min_width;
	
	// Probar margins auto, falta centrar al redimensionar ventana / OPTIMIZAR DECLARACIONES
	$('#left, #right, #main, #top, #bottom').css('height', $.height).css('width', $.width);
	$('#main').fadeIn();
	
	// Nunca margen negativo
	$.margin_top = ($.height - parseInt($('#main .content').css('height')))/2;
	if($.margin_top < 0) $.margin_top = 0;
	
	// Margen
	$('.content').css('margin-top', $.margin_top);

	/**
	* AJAX LOADERS - pendiente!
	*/
	/*function getHtml(s) {
		return '';
	}*/
	
	/**
	* gSocial - construccion
	*/

	/**
	* gStatus
	*/
	$('#gstatus').css('top', parseInt($(document).height()/2)-30);
	$('#gstatus').hover(function(){ $(this).fadeIn(500); });
	$('#gstatus').mouseout(function(){ $(this).fadeOut(500); });
	
	function setStatus(s) {
		if($.gstatus_transition)
		{
			$.gstatus_transition = false;
			$('#gstatus').removeClass('scroll kleft kright kup kdown').addClass(s).fadeIn(500).delay(500).fadeOut(500, function() { $.gstatus_transition = true; });
			
		}
	}
	
	/**
	* EVENT HANDLERS
	*/
	
	var uagent = navigator.userAgent.toLowerCase();

	if (uagent.search('iphone') > -1)
	{
		$.toucheventson = true;
	}
	else if (uagent.search('ipad') > -1)
	{
		$.toucheventson = true;
	}
	else
	{
		$.toucheventson = false;
	}
	
	/**
	* iPhone, iPad, iPod Touch... (Android?)
	*/ 
	if($.toucheventson)
	{
	$("div").touchwipe({
     wipeRight: function() {
			 	$('#touch').fadeOut();
				if($.status_transition)
				{
					// LEFT
					if($.current == 'main')
					{
						setStatus('tleft');
						$('#'+$.current).transition({target: 'left'});
					}
					else if($.current == 'right')
					{
							if($.wpage > 0)
							{
								//alert('pasa pagina');
							}
							else
							{
								setStatus('tleft');
								$('#'+$.current).transition({target: 'main'});
							}
					}
				}
		 },
     wipeLeft: function() {
			  $('#touch').fadeOut();
				if($.status_transition)
				{
			 		// RIGHT
					if($.current == 'main')
					{
						setStatus('tright');
						$('#'+$.current).transition({target: 'right'});
					}
					else if($.current == 'left')
					{
						setStatus('tright');
						$('#'+$.current).transition({target: 'main'});
					}
					else if($.current == 'right')
					{
						//alert('pasa pagina');
					}
				}
		 },
     wipeUp: function() { 
		 		$('#touch').fadeOut();
				if($.status_transition)
				{
					if($.current == 'main')
					{
						setStatus('tup');
						$('#'+$.current).transition({target: 'top'});
					}
					else if($.current == 'bottom')
					{
						if($.social_mode == 'main')
						{
							setStatus('tup');
							$('#'+$.current).transition({target: 'main'});
						}
						else if ($.social_mode == 'right')
						{
							setStatus('tup');
							$('#'+$.current).transition({target: 'right'});
						}
					}
				}
		 },
     wipeDown: function() {
			  $('#touch').fadeOut();
				if($.status_transition)
				{
			 		// DOWN
					if($.current == 'main')
					{
						setStatus('tdown');
						$('#'+$.current).transition({target: 'bottom'});
					}
					else if($.current == 'top')
					{
						setStatus('tdown');
						$('#'+$.current).transition({target: 'main'});
					}
				}
		 },
     min_move_x: 20,
     min_move_y: 20,
     preventDefaultEvents: true
	});
	
	}
	
	/**
	* Control de links
	*/
	$('.button').click(function(){
		$('#'+$.current).transition({target:$(this).attr('rel')});
	});
	
	/**
	* Control de scroll
	*/
	//Mozilla
  if(window.addEventListener) document.addEventListener('DOMMouseScroll', scrollNav, false);
 
  //IE/OPERA etc
  document.onmousewheel = scrollNav;
	
	function scrollNav(event)
	{
		$('#touch').fadeOut();
		if (!event) event = window.event;
		if (event.wheelDelta) {
        // IE and Opera
        delta = event.wheelDelta / 60;
 
    } else if (event.detail) {
        // W3C
        delta = -event.detail / 2;
    }
		if($.status_transition)
		{
			setStatus('scroll');
			if(delta > 0)
			{
				//scroll arriba
				if($.current == 'main')
				{
					$('#'+$.current).transition({target: 'top'});
				}
				else if ($.current == 'bottom')
				{
					if($.social_mode == 'main')
					{
						$('#'+$.current).transition({target: 'main'});
					}
					else if ($.social_mode == 'right')
					{
						$('#'+$.current).transition({target: 'right'});
					}
				}
			}
			else
			{
				//scroll abajo
				if($.current == 'main')
				{
					$('#'+$.current).transition({target: 'bottom'});
				}
				else if ($.current == 'top')
				{
					$('#'+$.current).transition({target: 'main'});
				}
				/*else if ($.current == 'right')
				{
					$('#'+$.current).transition({target: 'bottom'});
				}*/
			}
		}
	}
	
	/**
	* Control de flechas de teclado
	*/
	$(document).keydown(function(e){
		$('#touch').fadeOut();
		if($.status_transition)
		{
			var kcode = e.keyCode;
			switch (kcode) {
				case 40:
					// DOWN
					if($.current == 'main')
					{
						setStatus('kdown');
						$('#'+$.current).transition({target: 'bottom'});
					}
					else if($.current == 'top')
					{
						setStatus('kdown');
						$('#'+$.current).transition({target: 'main'});
					}
					else if($.current == 'right')
					{
						//setStatus('kdown');
						//$('#'+$.current).transition({target: 'bottom'});
					}
					break;
				case 38:
					// UP
					if($.current == 'main')
					{
						setStatus('kup');
						$('#'+$.current).transition({target: 'top'});
					}
					else if($.current == 'bottom')
					{
						if($.social_mode == 'main')
						{
							setStatus('kup');
							$('#'+$.current).transition({target: 'main'});
						}
						else if ($.social_mode == 'right')
						{
							setStatus('kup');
							$('#'+$.current).transition({target: 'right'});
						}
					}
					break;
				case 37:
					// LEFT
					if($.current == 'main')
					{
						setStatus('kleft');
						$('#'+$.current).transition({target: 'left'});
					}
					else if($.current == 'right')
					{
							if($.wpage > 0)
							{
								//alert('pasa pagina');
							}
							else
							{
								setStatus('kleft');
								$('#'+$.current).transition({target: 'main'});
							}
					}
					break;
				case 39:
					// RIGHT
					if($.current == 'main')
					{
						setStatus('kright');
						$('#'+$.current).transition({target: 'right'});
					}
					else if($.current == 'left')
					{
						setStatus('kright');
						$('#'+$.current).transition({target: 'main'});
					}
					else if($.current == 'right')
					{
						//alert('pasa pagina');
					}
					break;
				case 8:
					if($.history.length > 0)
					{
						$('#'+$.current).transition({target: $.history[$.history.length - 1]});
					}
					return false;
					break;
				case 27:
					if($.history.length > 0)
					{
						$('#'+$.current).transition({target: $.history[$.history.length - 1]});
					}
					return false;
					break;
			}
		}
	});
	
	// Historial de navegación - para controlar el paso hacia atrás.
	$.history = [];
	
	/**
	* TRANSITION CORE
	*/
	
	jQuery.fn.transition = function() {
		
		/**           [top]
		       [left] [main] [right]
					 				[bottom]
		*/

		$.status_transition = false;
		
		var args = arguments[0] || {};
		$("#videowall").videowall.stop();
		
		if(args.target == 'top') {
			
			// Sólo si nos encontramos en [main]
			if($.current == 'main')
			{
				$(this).animate({ bottom: '-'+$.height+'px', top: $.height+'px' }, $.transition_time, function(){
					$(this).hide();
				});
				$('#top').css('top', '-'+$.height+'px');
				$('#top').show().animate({ top: '0px' }, $.transition_time, function(){ $.status_transition = true; });
			} else {
				$.status_transition = true;
			}
			$.history.push($.current);
			$.current = args.target;
			
		} else if(args.target == 'bottom') {
			
			// Sólo si estamos en [main] o [right]
			if($.current == 'main') {
				
				// Evento para [main]
				$(this).animate({ bottom: $.height+'px', top: '-'+$.height+'px' }, $.transition_time, function(){
					$(this).hide();
				});
				$('#bottom').css('bottom', '-'+$.height+'px').css('top', $.height+'px');
				$('#bottom').show().animate({ bottom: '0px', top: '0px' }, $.transition_time, function(){ $.status_transition = true; });
				
				$.social_mode = 'main';
				
			} else if($.current == 'right') {
				
				// Evento para [right]
				/*$(this).animate({ bottom: $.height+'px', top: '-'+$.height+'px' }, $.transition_time, function(){
					$(this).hide();
				});
				$('#bottom').css('bottom', '-'+$.height+'px');
				$('#bottom').show().animate({ bottom: '0px' }, $.transition_time, function(){ $.status_transition = true; });
				
				$.social_mode = 'right';*/
				$.status_transition = true;
				
			} else {
				$.status_transition = true;
			}
			
			$.history.push($.current);
			$.current = args.target;
			
		} else if(args.target == 'left') {
			
			// Sólo si estamos en [main]
			if($.current == 'main')
			{
				$(this).animate({ left: $.width+'px' }, $.transition_time, function(){
					$(this).hide();
				});
				$('#left').css('left', '-'+$.width+'px');
				$('#left').show().animate({ left: '0px' }, $.transition_time, function(){ $.status_transition = true; });
				
			} else if($.current == 'right') {
				
				$(this).animate({ left: $.width+'px' }, $.transition_time, function(){
					$(this).hide();
				});
				$('#main').css('left', '-'+$.width+'px');
				$('#main').show().animate({ left: '0px' }, $.transition_time, function(){ $.status_transition = true; });
			}
			$.history.push($.current);
			$.current = args.target;
		
		} else if(args.target == 'right') {
			
			// Sólo si estamos en [main]
			if($.current == 'main')
			{
				$(this).animate({ right: $.width+'px', left: '-'+$.width+'px' }, $.transition_time, function(){
					$(this).hide();
				});
				$('#right').css('right', '-'+$.width+'px').css('left', $.width+'px');
				$('#right').show().animate({ right: '0px', left: '0px' }, $.transition_time, function(){ $.status_transition = true; });				
				
			} else if($.current == 'bottom') {
				
				if($.social_mode == 'main') {
					$(this).animate({ bottom: '-'+$.height+'px', top: $.height+'px' }, $.transition_time, function(){
						$(this).hide();
					});
					$('#main').css('top', '-'+$.height+'px');
					$('#main').show().animate({ top: '0px' }, $.transition_time, function(){ $.status_transition = true; });
					
				} else if ($.social_mode == 'right') {
					
					$(this).animate({ bottom: '-'+$.height+'px', top: $.height+'px' }, $.transition_time, function(){
						$(this).hide();
					});
					$('#right').css('top', '-'+$.height+'px');
					$('#right').show().animate({ top: '0px' }, $.transition_time, function(){ $.status_transition = true; });
					
				}
			} else {
				$.status_transition = true;
			}
			$.history.push($.current);
			$.current = args.target;
			
		} else if(args.target == 'main') {
			  
				$("#videowall").videowall.play();
				
				// Sólo si estamos en [top] o [left] o [right] o [bottom]
				if($.current == 'top') {
					
					$(this).animate({ bottom: $.height+'px', top: '-'+$.height+'px' }, $.transition_time, function(){
						$(this).hide();
					});
					$('#main').css('top', $.height+'px');
					$('#main').show().animate({ top: '0px' }, $.transition_time, function(){ $.status_transition = true; });
					
				} else if($.current == 'left') {
					
					$(this).animate({ left: '-'+$.width+'px' }, $.transition_time, function(){
						$(this).hide();
					});
					$('#main').css('left', $.width+'px');
					$('#main').show().animate({ left: '0px' }, $.transition_time, function(){ $.status_transition = true; });
					
				} else if($.current == 'right') {
					
					$(this).animate({ left: $.width+'px' }, $.transition_time, function(){
						$(this).hide();
					});
					$('#main').css('left', '-'+$.width+'px');
					$('#main').show().animate({ left: '0px' }, $.transition_time, function(){ $.status_transition = true; });
					
				} else if($.current == 'bottom') {
					
					$(this).animate({ bottom: '-'+$.height+'px', top: $.height+'px' }, $.transition_time, function(){
						$(this).hide();
					});
					$('#main').css('top', '-'+$.height+'px');
					$('#main').show().animate({ top: '0px' }, $.transition_time, function(){ $.status_transition = true; });
					
				} else {
					$.status_transition = true;
				}
				$.history.push($.current);
				$.current = args.target;
		}
	}
	
	$('a.youtube').each(function()
   {
		 	
      // Grab video ID from the url
      var videoID = $(this).attr('rel');

      // Create content using url as base
      $(this).qtip(
      {
         // Create content DIV with unique ID for swfObject replacement
         content: '<div id="youtube-embed-'+videoID+'">You need Flash player 8+ to view this video.</div>',
         position: {
            corner: {
               tooltip: 'bottomMiddle', // ...and position it center of the screen
               target: 'topMiddle' // ...and position it center of the screen
            }
         },
         show: {
            when: 'click', // Show it on click...
            solo: true // ...and hide all others when its shown
         },
         hide: 'unfocus', // Hide it when inactive...
         style: {
            width: 300,
            height: 200,
            padding: 0,
            tip: true,
            name: 'dark'
         },
         api: {
            onRender: function()
            {
               // Setup video paramters
               var params = { allowScriptAccess: 'always', allowfullScreen: 'false' };
               var attrs = { id: 'youtube-video-'+videoID };

               // Embed the youtube video using SWFObject script
               swfobject.embedSWF('http://www.youtube.com/v/'+videoID+'&enablejsapi=1&playerapiid=youtube-api-'+videoID,
                                 'youtube-embed-'+videoID, '300', '200', '8', null, null, params, attrs);
            },

            onHide: function(){
               // Pause the vide when hidden
               var playerAPI = this.elements.content.find('#youtube-video-'+videoID).get(0);
               if(playerAPI && playerAPI.pauseVideo) playerAPI.pauseVideo();
            }
         }
      }
      ).attr('href', '#');
   });
	/*
		/* Pluguin videowall */
	/*$("#videowall").videowall();*/
	//Funciones al poner el ratón sobre un bloque
	/*$("#videowall").videowall.setMouseOver(function(selector){*/
		//console.log($(selector).attr("work"));
	/*});*/
	
});
