$(document).ready(function() {
    
    // Index Navigation
    var host = 'http://juegosnova.com/';
    var loc = window.location.hash.toString();
    if ( loc != '' ) {

		var url = loc.replace("#/", "");
		$("#middle_list").load(url+" #middle_list_container");

	}
	
	$("#middle_list_nav a").live("click", function() {
        
        var cnt_hg = $("#middle_list_content").height();
        
        $('html,body').animate({scrollTop: '133px'}, 500);
        $("#middle_list_content").css("height", cnt_hg+"px").html('<span style="padding-left: 20px;" class="loading2">Cargando</span>');
        
		var url = $(this).attr("href");
		document.location.href = "#"+url;
		$("#middle_list").load(url+" #middle_list_container");

		return false;

	});
	// Ends Index Navigation
	
	$('#marks_fav').click(function() {
		url = location.href;
		title = $(this).attr("rel");
		if(document.all){
			window.external.AddFavorite(url, title);
		} else {
			window.sidebar.addPanel(title, url,"");
		}
		
		return false;
	});
	
	$('#marks_nel').click(function() {
		if ( $('#marks_newsletter:hidden').length > 0 ) {
			$('#marks_newsletter').slideDown('400');
		} else {
			$('#marks_newsletter').slideUp('400');
		}
		return false;
	});
	
	// Email Subscribe
	var options2 = {
		beforeSubmit: beforeReq2,
        success: afterReq2
	};
	
	function beforeReq2(formData, jqForm, options) {
		$('#email_subscribe .form').slideUp('300');
		$('#email_subscribe .loading').show('300');
		$('#email_subscribe .result').hide();
	}
	
	function afterReq2(responseText, statusText) {
		$('#email_subscribe .loading').hide('300');
		$('#email_subscribe .result').html(responseText).show("500");
	}
	
	$('#email_subscribe').submit(function() {
		$(this).ajaxSubmit(options2);
		
		return false;
	});
	
	// AJAX Game Rating
	$("#rate_stars.allowed a").mouseover(function() {
	  var rel = $(this).attr("rel");
	  for ( i=rel; i>0; i-- ) {
	    $("#rate_stars a[rel="+i+"]").addClass("hover");
	  }
	});
	$("#rate_stars.allowed a").mouseout(function() {
	  $("#rate_stars a").removeClass("hover");
	});
	
	$("#rate_stars.allowed a").click(function() {
	  
	  var id = $("#rate_stars").attr("rel");
	  var rel = $(this).attr("rel");
	  
	  // Hide stars and show loading spinner
	  $("#rate_stars a").hide();
	  $("#rate_stars .loading2").show();
	  
	  $.ajax({
      type: "POST",
      url: "rating.php",
      data: "id="+id+"&rate="+rel,
      dataType: "text",
      success: function(msg) {
        $("#rate_stars .loading2").hide();
        $("#rate_stars .stars").html(msg).show("300");
      }
    });
    
    return false;
      
	});
	
	$("#rate_stars a").click(function() {
	    
	    if ( !$("#rate_stars").hasClass("allowed") ) {
	        $("#rate_stars .rate_msg").html("Solo puedes hacer un Voto cada 24 horas.").show();
  	        setTimeout('$("#rate_stars .rate_msg").hide("500")', 2000);
	     }
	  
	});
	
	// Use Google Analytics to track JavaScript //
    function GA_Track(page) {
        pageTracker._trackPageview(page);
    }
	
	
	$("#fb_publish a").live("click", function() {
	    fbPublish(text, attachment);
	    $("#fb_publish").animate({
            top: '-350px'
        }, 600);
		
        return false;
	});
	
	$(window).scroll(function() {
		height = $(document).height();
		top = $(window).height()+$(window).scrollTop();
	    top = top - $("#fb_publish").height() - 30;
		if ( (top > height) || (top == height) ) {
			top = top-30;
		}
	    $("#fb_publish").stop().css({
	        top: top+"px"
	    });
	});
	
});

function askToPublish() {
    html = '<div id="fb_publish">';
    html +=	'   <img src="http://static.juegosnova.com/templates/v2/images/icon-bigfb.png" alt="FaceBook" />';
    html +=	'   <a href="http://www.facebook.com/pages/JuegosNOVA/106668855457" class="header">Comparte este Juego con tus <b>Amigos en Facebook</b>!</a>';
    html += '</div>';
    $('body').append(html);
    window_height = $(window).height()+$(window).scrollTop();
    $("#fb_publish").show();
    top = window_height - $("#fb_publish").height() - 30;
    $("#fb_publish").animate({
        top: top
    }, 600);
}

function fbPublish(text, attachment) {
	if (attachment == "") {
		var attachment = {
			'name': 'JuegosNOVA.com - Cientos de Juegos Flash',
			'href': 'http://juegosnova.com/',
			'description': 'Los mejores Juegos Flash, mas de 3,000 recopilados para que pases horas de diversion.',
			'media':[{
				'type':'image',
				'src':'http://static.juegosnova.com/templates/v2/images/logo.jpg',
				'href':'http://juegosnova.com/'
			}]
		};
	}
	FB.Connect.streamPublish(text, attachment);
}