jQuery.noConflict();
jQuery(document).ready(function($){
	
	$("#slider img").removeAttr('height');
	
	$('.juegos div.img')
		.each(function(){ 
		var $t = $(this);
		var img = $t.find('img');
		
		$t.width(img.width()).height(img.height());
	});
	
	$('#last .item').not(':first').css('display','none');
	
	$('#last a.arrow')
		.click(function(){
			var $t = $(this);
			var $now = $('#last div.item:visible');
			var $next;
			if( $t.hasClass('left') ){
				$next = ( $now.prev('div').is('.item:hidden') ) ? $now.prev('div') : $('#last div.item:last') ;
			} else if( $t.hasClass('right') ) {
				$next = ( $now.next('div').is('.item:hidden') ) ? $now.next('div') : $('#last div.item:first') ;
			}
			$now.fadeOut('normal',function(){ $next.fadeIn(); });
		});
	
	$('#menu li')
		.hover(
		function(){ $(this).find('ul.sub-menu:first').fadeIn(0,'linear'); },
		function(){ $(this).find('ul.sub-menu:first').fadeOut(0,'linear');}
		);
	
	$('#menu-item-19 > a, #menu-item-18 > a, #menu-item-147 > a').attr('href','#');
	
	$('#menu .sub-menu .sub-menu').each(function(){ 
		var left = $(this).parents('li').parents('li').width()+'px';
		$(this).css('left',left);
	});
	
	$('.juegos .item img,.juegos .item .gratis_label,.juegos .info, #slider .img img, #slider .img .gratis_label')
		.click(function(){ 
			var $t = $(this).parents('.item');
			var links = $t.find('.seleccion_idioma a');
			var $idlink = $t.attr('id').replace('game','#gamelink');
			
			if (links.length == 1){
				if ( $($idlink).attr('href') != '#suscripcion' ){
					document.location = $($idlink).attr('href');
				} else {
					$($idlink).trigger('click');
				}				
			} else {
				Confirm('Seleccione Edioma', $t ,function(yes) {											  
					var $idlink = $t.attr('id');
					$idlink = ( yes ) ? $idlink.replace('game','#gamelink_es') : $idlink.replace('game','#gamelink_en');
					if ( $($idlink).attr('href') != '#suscripcion' ){
						document.location = $($idlink).attr('href');
					} else {
						$($idlink).trigger('click');
					}
				});				
			}
		});
		
	/*$('.idioma')
		.hover(function(){ $(this).animate({ opacity: .7 }) },function(){ $(this).animate({ opacity: 0 }); });*/
	
	$('a[href=#suscripcion]')
		.fancybox({
			'hideOnContentClick': true,
			'type': 'iframe',
			'href': '/wp/suscribete-a-crucigramas-educactivos/',
			'width': 546,
			'height': 648,
			onStart: function(){ $('.idioma').animate({ opacity: 0 }); } 
		});
		
	$('a.solve').fancybox();
	
	$('a.poll')
		.fancybox({
			'hideOnContentClick': true,
			'type': 'iframe',
			'href': '/wp/encuesta/',
			'width': 556,
			'height': 326
		});
	
	// Dialog idioma
	function dialogue(content, title, target) {
		  /* 
		   * Since the dialogue isn't really a tooltip as such, we'll use a dummy
		   * out-of-DOM element as our target instead of an actual element like document.body
		   */
		  $('<div />').qtip(
		  {
			 content: {
				text: content,
				title: {
					text: title,
					button: true
				 }

			 },
			 position: {
				my: 'center', at: 'center', // Center it...
				target: target // ... in the window
			 },
			 show: {
				ready: true, // Show it straight away
				modal: {
				   on: true, // Make it modal (darken the rest of the page)...
				   blur: false // ... but don't close the tooltip when clicked
				}
			 },
			 hide: false, // We'll hide it maunally so disable hide events
			 style: 'ui-tooltip-light ui-tooltip-rounded ui-tooltip-dialogue', // Add a few styles*/
			 events: {
				// Hide the tooltip when any buttons in the dialogue are clicked
				render: function(event, api) {
				   $('button', api.elements.content).click(api.hide);
				},
				// Destroy the tooltip once it's hidden as we no longer need it!
				hide: function(event, api) { api.destroy(); }
			 }
		  });
   }
   
   // Our Confirm method
   function Confirm(question, target, callback)
   {
      // Content will consist of the question and ok/cancel buttons
      var message =
         ok = $('<button />', { 
            text: 'Español',
            click: function() { callback(true); }
         }),
         cancel = $('<button />', { 
            text: 'Inglés',
            click: function() { callback(false); }
         });
 
      dialogue( message.add(ok).add(cancel), question, target );
   }

	
	$('#game_content .categories a').removeAttr('href');
	
});
