
// Variables
var fancyboxArgs = {
	'titleShow'        : false,
	'centerOnScroll'   : true,
	'overlayOpacity'   : 0.6,
	'overlayColor'     : "#000"
};

// Funcion Precargar imagenes
(function($) {
	var cache = [];
	// Arguments are image paths relative to the current page.
	$.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);
		}
	}
})(jQuery)


// Page Load
$( function() {

	// JS class in html
	$("html").removeClass("no-js").addClass("js");

	// Infinite Scroll
	$(".autopagerize_page_element").infinitescroll({
		navSelector : "#pagination",
		nextSelector : "#pagination a.next",
		itemSelector : ".hentry",
		loadingImg : "http://moskis.net/tumblr/l4dfans/img/ajax-loader.gif",
		loadingText : "",
		bufferPx : 400,
		donetext : "FIN"
	}, function() { // Callbacks afters loadings new posts
		$(".fancybox").fancybox(fancyboxArgs);
		$(".photo-src").each(function() {
			if ( ! $(this).attr("href") ) {
				$(this).hide();
			}
		});
		h(e(r), true);
	});
	
	// From Effector theme
	function h(j, k) {
		var l = j ? j : e(".hentry:not(.loaded)");
		l.each(function () {
			var z = e(this);
			var x = z.attr("id").split("-")[1];
			if (z.hasClass("type-audio")) {
				if (k) {
					var n = z.find(".player-btn").css("visibility", "hidden");
					var r = z.find(".js-string").html();
					var A = /\\x3cembed.+\\x3c\/embed\\x3e/;
					var m = r.match(A)[0].replace(/\\x3cembed/, "\x3cembed wmode=\x22opaque\x22");
					n.append('<script type="text/javascript">replaceIfFlash(9,"audio_player_' + x + "\",'\x3cdiv class=\x22audio_player\x22\x3e" + m + "\x3c/div\x3e')<\/script>").css("visibility", "visible");
					z.find(".js-string").remove()
				} else {
					var t = z.find(".audio_player");
					var v = t.html();
					var C = v.replace(/<embed\s/i, "<embed wmode='opaque' ");
					t.html(C);
					z.find(".js-string").remove()
				}
			}
			if (z.hasClass("type-video")) {
				if (k) {
					var q = z.find(".media");
					if (/span id="?video_player/i.test(q.html())) {
						var r = z.find(".js-string").html();
						var u = r.split("x22video_player_" + x + "\\x22,")[1].replace(/\\'/g, "'").replace(/\\\\x26/g, "\x26").replace(/'\)\\x3c\/script\\x3e'/, "");
						q.append('<script type="text/javascript">renderVideo("video_player_' + x + '", ' + u + "')<\/script>")
					}
				}
				g(z)
			}
			if (z.hasClass("type-quote")) {
				var D = z.find(".quote-text");
				var o = D.find(":first-child");
				var B = D.find(":last-child");
				var w = '<span class="ldquo">&ldquo;</span>';
				var p = '<span class="rdquo">&rdquo;</span>';
				if ((o.length > 0) && o.is("p")) {
					o.prepend(w)
				} else {
					D.prepend(w)
				}
				if ((B.length > 0) && (/<\/[a-zA-Z]+>$/.test(D.html()))) {
					B.append(p)
				} else {
					D.append(p)
				}
			}
			z.addClass("loaded")
		});
	}
	
		
	// Hide source link in photos that dont have a click-through
	$(".photo-src").each(function() {
		if ( ! $(this).attr("href") ) {
			$(this).hide();
		}
	});

	
	// Fancybox
	$(".fancybox").fancybox(fancyboxArgs);

	$("a[rel=box_group]").fancybox({
		'titleShow'          : false,
		'centerOnScroll'     : true,
		'overlayOpacity'     : 0.6,
		'overlayColor'       : "#000",
		'transitionIn'       : 'none',
		'transitionOut'      : 'none',
		'titlePosition'      : 'over'
	});
	
	
	// Auto scroll tags widget
	var topHeight = $('#header').height() + $('#acerca-de').height();
	$(window).scroll( function() {
		if ( $(document).scrollTop() > topHeight ) {
			$('#autoscroll').css({ position: 'fixed', marginTop: -topHeight });
		} else {
			$('#autoscroll').css({ position: 'static', marginTop: 0 });
		}
	});
	
	/* -------  SCRIPTS DEL FORO  ---------- */ 
	
	/* -------------------- *\
			COMUN
	\* -------------------- */
	
	// Header popup (click on whole block)
	$("#header-popup").click( function() {
		location.href = $(this).find("a").attr('href');
	});
	
	
	// Ajustar tamaņo de embeds youtube a 520x325 (16:10) ---- alternativo 645x363 (16:9)
	$('embed[src*="youtube"]').width('520px').height('325px');
	
	
	// Precargar imagenes
	$.preLoadImages(
		"http://losmiserables.net/foro/Themes/miserables/images/miserables/bg-logoheader.png",
		"http://losmiserables.net/foro/Themes/default/images/theme/menu_hover.png");
	

	// Enlace portada web / indice foro del header
	$("#logoheader").bind("mouseenter", function(){
		$("#logoheader-bg").fadeIn("fast");
	});

	$("#logoheader").bind("mouseleave", function(){
		$("#logoheader-bg").fadeOut("fast");
	});
	
	
	
	/*-----------------------------------------------------------------------------------*\
		Notify bar
	\*-----------------------------------------------------------------------------------*/
	/*
	$('#notification-bar .close').click( function () {
		$('#wrapper').animate({top: 0}, 200, function() {
			$('#notification-bar-open').animate({top: -5}, 200).animate({top: -10}, 90).animate({top: -5}, 90);
			$.cookie("notificationBar", "closed", { path: '/', expires: 30 });
		});
		return false;
	});
	
	$('#notification-bar-open').click( function () {
		$(this).animate({top: 0}, 90).animate({top: -100}, 300, function () {
			$('#wrapper').animate({top: 30}, 200);
			$.cookie("notificationBar", "open", { path: '/', expires: 30 });
		});
		return false;
	});
	
	if ( $.cookie("notificationBar") === "closed" ) {
		$("#wrapper").css("top", 0);
		$("#notification-bar-open").css("top", -5);
	}
	
	$('#notification-bar-list').cycle({
		fx:				'fade',
		speed:		1000,
		timeout:	6000,
		next:			'#notification-controls .next',
		prev:			'#notification-controls .prev'
	});
	
	$("#notification-bar").bind("mouseenter", function() {
		$('#notification-bar-list').cycle('pause');
	});

	$("#notification-bar").bind("mouseleave", function() {
		$('#notification-bar-list').cycle('resume');
	});
	*/
	
	
	/*-----------------------------------------------------------------------------------*\
		Auto expand wrapper to fill window height if necessary
	\*-----------------------------------------------------------------------------------*/
	
	function rez() {
		if ( $('#wrapper').css('top') == '30px' ) {
			$('#container').css('min-height', $(window).height() - 276 );
		} else {
			$('#container').css('min-height', $(window).height() - 246 );
		}
	};

	$(window).resize(function() {
		rez();
	});

	$(window).load(function () {
		rez();
	});

});


