// *********************************
// Francisco Javier Martínez
// francisco_javier_martinez@hotmail.com
// www.javiscript.com
// *********************************

/* Setting the default values for FORM */
jQuery(document).ready(function() {
	//Assign default value to form field #1
/*
	jQuery("#Asunto").DefaultValue("Asunto");
	jQuery("#Nombre").DefaultValue("Nombre");
	jQuery("#Correo_electronico").DefaultValue("Correo electrónico");
	jQuery("#Telefono").DefaultValue("Teléfono");
	jQuery("#Comentario").DefaultValue("Comentario");
*/
});

/* ***************************************************************************************************************
********** START TWITTER Ajax Loader ************************************************************************** */

var actual_anchor_id = "";

jQuery(document).ready(function () {
	jQuery("#submenu").find("a").each(function () {
		jQuery(this).click(function (theEvent) {
			theEvent.preventDefault();
			
			var actual_id = jQuery(this).parent().parent().attr("id");

			actual_anchor_id = actual_id.split("twitter_")[1];
	
			// Eliminamos la clase SELECTED del submenú
			jQuery("#submenu ul li.selected").removeClass("selected");
			
			// Ponemos la clase SELECTED al elemento que hemos pulsado
			jQuery("#" + actual_id).addClass("selected");
			
			
			// Desplazamos el conenedor general hasta el elemento en cuestión
			var section_div = jQuery("[name=anchor_" + actual_id + "]").parent();
			var content_position = section_div.position();
			content_position_left = content_position.left * -1;
			
			// eliminamos el contenido de lo que hubiera anteriormente
			var tweet_feed = jQuery("#twitter_feed_" +  actual_anchor_id).find(".tweet_list").get(0);
			jQuery(tweet_feed).remove();
			
			// Insertamos el cargador (loading)
			jQuery("#twitter_feed_" +  actual_anchor_id).html('<div class="twitter_loader"><img class="twitter_logotype" src="http://www.pignoise.com/img/twitter_newbird_blue.png" alt="Twitter" /><div class="clear sp fullWidth"></div><img class="loader" src="http://www.pignoise.com/img/ajax-loader.gif" alt="Cargando..." /></div>');
			
			jQuery("#submenu_content_crop").stop().animate({
				left: content_position_left
			}, 1500, function() {
				// Ponemos la capa al a altura correspondiente
				if (actual_id.indexOf("twitter") != -1) {
					// Lanzamos en Ajax con Twitter
					var url = "";
					switch(actual_anchor_id.toLowerCase()) {
						case "pignoise":
							twitter_user = "pignoise";
						break;
						
						case "alvaro":
							twitter_user = "alvarobenitopig";
						break;
						
						case "pablo":
							twitter_user = "pabloalonsopig";
						break;
						
						case "polo":
							twitter_user = "polopignoise";
						break;
					}
					
					jQuery("#twitter_feed_" +  actual_anchor_id).tweet({
						username: twitter_user,
			            join_text: "auto",
			            avatar_size: 80,
			            count: 20,
/*
			            auto_join_text_default: "nos dijo,",
			            auto_join_text_ed: "nosotros",
			            auto_join_text_ing: "nos",
			            auto_join_text_reply: "respondió a ",
			            auto_join_text_url: "nos marchábamos",
*/
//			            loading_text: "Cargando tweets...", 
						template: "{avatar}{text}{time}{retweet_action}{reply_action}"
					}).bind("loaded", function(){
						jQuery(this).find("a.tweet_action").click(function(ev) {
							window.open(this.href, "Retweet",
							'menubar=0,resizable=0,width=550,height=420,top=200,left=400');
							
							ev.preventDefault();
						});
					});
				}
			});
		});
	});
});


/* ******* END TWITTER Ajax Loader *******************************************************************************
*************************************************************************************************************** */

/* ***************************************************************************************************************
********** START Carcar Facebook COmments dentro de LigthBox *****************************************************
***************** loadFacebookComments() está llamada desde Ligthbox.js *************************************** */

function loadFacebookComments(imageDataContainer, activeImage) {
	// Eliminamos el bloque de FAcebook por si anteriormente se ha cargado
	jQuery(imageDataContainer).find(".facebook_lightbox_comments_container").remove();
	
	// Insertamos el contenedor de Facebook Comments
	jQuery(imageDataContainer).append('<div id="facebook_comments_container_' + (activeImage + 1) + '" class="facebook_lightbox_comments_container"></div>');
	jQuery('#facebook_comments_container_' + (activeImage + 1)).append('<fb:comments xid="foto_' + (activeImage + 1) + '_comments" id="foto_' + (activeImage + 1) + '_comments" href="' + jQuery('#lightboxImage').attr("src") + '" num_posts="20" reverse="true" width="530" data-colorscheme="dark"></fb:comments>');
	
	// Inicializamos Facebook Comments
	FB.XFBML.parse(document.getElementById('facebook_comments_container_' + (activeImage + 1)));
}

/* ******* END Carcar Facebook COmments dentro de LigthBox ***********************************************************
******************************************************************************************************************* */

// Rollover sobre imágenes y pasar a gris
jQuery(document).ready(function () {
	jQuery(".entry_photos a").mouseover(function() {
		var actualImage = jQuery(this).find("img").get(0);
		var newURL = jQuery(actualImage).attr("src");
		newURL = newURL.replace(".jpg","_grey.jpg");
		jQuery(actualImage).attr("src", newURL);
	}).mouseout(function() {
		var actualImage = jQuery(this).find("img").get(0);
		var newURL = jQuery(actualImage).attr("src");
		newURL = newURL.replace("_grey.jpg", ".jpg")
		jQuery(actualImage).attr("src", newURL);
	});
});



function go_to_scroll (container, name_element)
{
	if (jQuery("[name=" + name_element + "]").length) {
		jQuery(container).animate({scrollTop: jQuery("[name=" + name_element + "]").offset().top}, "slow", function() {
			// Do somthing
		});
	}
}



// Redimensionamos coverAll porque no coje todo el tamaño del documento en altura
jQuery(document).ready(function () {
	jQuery("#coverAll").height(jQuery(document).height());
});


// Centrar algo
jQuery.fn.center = function () {
    this.css("position","absolute");
    this.css("top", ((jQuery(window).height() - this.outerHeight()) / 2) + jQuery(window).scrollTop() + "px");
    this.css("left", ((jQuery(window).width() - this.outerWidth()) / 2) + jQuery(window).scrollLeft() + "px");
    return this;
}


/* Hack para que los enlaces con la clase EXTERNAL carguen en ventana nueva */
jQuery(document).ready(function () {
	jQuery("a.external").click(function(){
		this.target = "_blank";
	});
	
	jQuery(".iframe_location a").click(function(){
		this.target = "_blank";
	});
});

// Hack para reemplazar la dirección de correo escrita (cuenta [AT] dominio [dot] ext) por la dirección real
jQuery.fn.mailto = function() {
	return this.each(function(){
		
		switch (jQuery(this).get(0).tagName) {
			case "A":
			default:
				if (jQuery(this).attr("href").indexOf("(at)") != -1)
				{
					var email = jQuery(this).attr("href").replace("(at)", "@");
					email = email.replace("(dot)", ".");
					var email = email.replace("mailto:", ""); // Delete mailto: if exists
					var text = jQuery(this).html().replace("(at)", "@"); // Replace (at) on text if exists
					text = text.replace("(dot)", "."); // Replace (at) on text if exists
					var classNames = jQuery(this).attr("class");
					jQuery(this).before('<a href="mailto:' + email + '" rel="nofollow" class="' + classNames + '" title="Email ' + email + '">' + text + '</a>').remove();
				}
			break;
			
			case "SPAN":
				var text = jQuery(this).html().replace("(at)", "@"); // Replace (at) on text if exists
				text = text.replace("(dot)", "."); // Replace (at) on text if exists
				var classNames = jQuery(this).attr("class");
				jQuery(this).before('<span class="' + classNames + '">' + text + '</span>').remove();
			break;
		}
	});
};

jQuery(document).ready(function () {
	jQuery(".email").mailto();
});

