/*
**
**
** File:         	main.js
** Version:			1.0
** Description:     Generic functions
** Author:          Pedro Castro -- pedro.castro@kaldeera.com
** Created: 		16/Oct/2009
** Last modified:   16/Oct/2009
**
**
** (c) Copyright 2009 Kaldeera, S.L. - All rights reserved.
*/

//$(document).ready(function($) {
//$(function() {
jQuery(function($) {

	// Elimina la barra izquierda de separación del primer elemento de menu
	$("#menu table.topNavItem td:first").css("background", "none");
	$("#submenu table.topNavItem td:first").css("background", "none");
	
	// Actualiza los links de las variaciones
	var str = new String(location.href);
	var language = (str.indexOf("es-ES") != -1 ? "es-ES" : "en-US");
	
	var es_ES_location = str.replace(/en-US/i, "es-ES").replace(/Pages/gi, "Paginas").replace(/Thankyou/gi, "Gracias").replace(/Buy/gi, "Comprar").replace(/Download/gi, "Descargar");
	$("#link_es-ES").attr("href", es_ES_location);
	
	var en_US_location = str.replace(/es-ES/i, "en-US").replace(/Paginas/gi, "Pages").replace(/Gracias/gi, "Thankyou").replace(/Comprar/gi, "Buy").replace(/Descargar/gi, "Download");
	$("#link_en-US").attr("href", en_US_location);
	
	
	// Actualiza los links del footer para que apunten correctamente a la biblioteca de Páginas.
	$(".footerLink").each(function() {
		var attr = $(this).attr("href");
		
		if (attr.indexOf("es-ES") != -1) {
			$(this).attr("href", attr.replace(/Pages/gi, "Paginas"));
		}
		else {
			$(this).attr("href", attr.replace(/Paginas/gi, "Pages"));
		}
	});
	
	
	// IE7 workaround for empty pageContent
	if ($.browser.msie && parseInt($.browser.version) == 7 && $("#pageContent").text() == "" && !$("#pageContent:has(img)").size()) {
		$("#pageContent").css("height", "0px");
	}
	
	
	// Adapta el layout de las web parts de lista y sus formularios (NewForm, EditForm, DisplayForm).
	try
	{
		if ($(".ms-menutoolbar").size()) {
			$(".ms-menutoolbar").parent().css({width: "980px", margin: "10px auto 150px auto"});
		}
		
		if ($("#onetIDListForm").size()) {
			$("#onetIDListForm").css({width: "980px", margin: "30px auto 150px auto"});
		}
		
		// Insert list title if exists
		var summary = $("*[summary]:first");
		if (summary.size()) {
			var ListTitle = summary.attr("summary");
			$(".ms-menutoolbar").parents("td:first").prepend("<div class='listTitle'>" + ListTitle + "</div>");
		}
	}
	catch(ex)
	{ };

	// Monta el link a noticias en el detalle de noticia
	// NOTA: Debe existir un div que envuelva la noticia con la clase "articleLayout".
	var textoBack = "volver a Noticias";
	var title = "Noticias";
	if (language == "en-US") {
		textoBack = "back to News";
		title = "News";
	}
	$('<div style="margin-bottom:20px;"><a href="/' + language + '/News" title="' + title + '">&lt; ' + textoBack + '</a></div>').appendTo(".articleLayout");

	// Ajusta el layout de las funcionalidades de producto.
	$('#features p:empty').hide();

	// Controla la carga de las imagenes en las funcionalidades de producto
	$('#features .featureImageContainer img')
		// Si no existe la imagen, muestra un icono de cortesía
		.error(function() {
			$(this).parent().hide();
			$(this).parents().eq(1).append('<span class="image-error"></span>');
		})
		// Si existe, hace una transición con el canal alpha
		.load(function() {
			//$(this).parent().show();
			//$(this).stop().animate({opacity: 1}, 400);
			$(this).fadeIn(400);

			// NOTA: Al parecer no funciona correctamente debido en parte a algún tipo de bug de 
			//		 jQuery y en parte por culpa de la cache del navegador.
		});

	if ($.browser.msie && $.browser.version == 7) {
		$(".step-wrapper .link-back").css("margin-left", "0px");
	}

});


function my_lightbox($elements, autolink)
{   
    var theme_selected = 'light_rounded';
    
    if(autolink)
    {
        jQuery('a[href$=jpg], a[href$=png], a[href$=gif], a[href$=jpeg], a[href$=.mov] , a[href$=.swf] , a[href*=vimeo.com] , a[href*=youtube.com]').contents("img").parent().each(function()
        {
            if(!jQuery(this).attr('rel') != undefined && !jQuery(this).attr('rel') != '' && !jQuery(this).hasClass('noLightbox'))
            {
                jQuery(this).attr('rel','lightbox[auto_group]')
            }
        });
    }
    
    jQuery($elements).prettyPhoto({
            "theme": theme_selected /* light_rounded / dark_rounded / light_square / dark_square */                                                                 });
    
    jQuery($elements).each(function()
    {   
        var $image = jQuery(this).contents("img");
        $newclass = 'lightbox_video';
        
        if(jQuery(this).attr('href').match(/(jpg|gif|jpeg|png|tif)/)) $newclass = 'lightbox_image';
            
        if ($image.length > 0)
        {   
            if(jQuery.browser.msie &&  jQuery.browser.version < 7) jQuery(this).addClass('ie6_lightbox');
            
            var $bg = jQuery("<span class='"+$newclass+" '></span>").appendTo(jQuery(this));
            
            jQuery(this).bind('mouseenter', function()
            {
                $height = $image.height();
                $width = $image.width();
                $pos =  $image.position();      
                $bg.css({height:$height, width:$width, top:$pos.top, left:$pos.left});
            });
        }
    }); 
    
    jQuery($elements).contents("img").hover(function()
    {
        jQuery(this).stop().animate({opacity:0.4}, 400);
    },
    function()
    {
        jQuery(this).stop().animate({opacity:1}, 400);
    });
}

