// JavaScript Document

function ltrim( cpStr ){
    while ( cpStr.charAt(0) == ' ' )
    cpStr = cpStr.replace(' ','');
    return cpStr;
}

function invertStr( cpStr ){
    var cpAux = '';
    for ( i=cpStr.length;i>=0;i--)
        cpAux=cpAux+cpStr.charAt(i);
    return cpAux;
}

function rtrim( cpStr ){
    return invertStr(ltrim( invertStr( cpStr ) ) );
}

function trim( cpStr ){
    return rtrim(ltrim( cpStr ) );
}

function Rigth(cpStr, nLength){
    return cpStr.substr(cpStr.length - nLength, cpStr.length);
}

function IsValidMail(pMail){
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
    return filter.test(trim(pMail));
}

$(document).ready( function() {
    //Roll over a todo lo q tenga el class ".ro"
    PEPS.rollover.init();

    //Solapas de la barra derecha "lo + leido, escuchado, comentado"
    $(".lo_mas h3 a").click(function () {

        $(".lo_mas h3 a").css({'color' : '#323232'});
        $(this).css({'color' : '#649623'});

        $(".solapa").slideUp('fast');

        var link_num = $(".lo_mas h3 a").index(this);

        mytime = setTimeout( function() {
            if (link_num == 0){
                $("#lo_mas_leido").slideDown('fast');
				Set_Cookie('sideopc1','1',7);
            }
            if (link_num == 1){
                $("#lo_mas_descargado").slideDown('fast');
				Set_Cookie('sideopc1','2',7);
            }
            if (link_num == 2){
                $("#lo_mas_comentado").slideDown('fast');
				Set_Cookie('sideopc1','3',7);
            }
        }, 400)

        return false;
    });
	
	
	$("a[rel='popUp']").click(function () {
		window.open(this.href, this.target, 'width=450,height=300');
		return false;
	});
	
	$("a[rel='no_logeado']").click(function () {
		var largeAlt = $(this).attr("title");
		jAlert(  largeAlt, ''  );
		return false;
	});
});

PEPS = {};

PEPS.rollover = {
   init: function() {
      this.preload();
      $(".ro").hover(
         function () { $(this).attr( 'src', PEPS.rollover.newimage($(this).attr('src')) ); },
         function () { $(this).attr( 'src', PEPS.rollover.oldimage($(this).attr('src')) ); }
      );
   },
   preload: function() {
      $(window).bind('load', function() {
         $('.ro').each( function( key, elm ) { $('<img>').attr( 'src', PEPS.rollover.newimage( $(this).attr('src') ) ); });
      });
   },
   newimage: function( src ) {
      return src.substring( 0, src.search(/(\.[a-z]+)$/) ) + '_o' + src.match(/(\.[a-z]+)$/)[0];
   },
   oldimage: function( src ) {
      return src.replace(/_o\./, '.');
   }
};

// Función para cambiar de idioma
function ChangeLang(pLang){
    var sUrl = new String(document.location.href);

    if (sUrl.indexOf('#') > -1)
        sUrl = sUrl.substr(0, sUrl.indexOf('#'));

    if (sUrl.indexOf('nota.php') > -1)
        sUrl = 'portada_contenidos.php';

    sUrl = sUrl.replace('#', '&');
    sUrl = sUrl.replace('/&', '?');

    sUrl = sUrl.replace('&lang=english', '');
    sUrl = sUrl.replace('&lang=spanish', '');
    sUrl = sUrl.replace('?lang=english', '');
    sUrl = sUrl.replace('?lang=spanish', '');

    if (sUrl.indexOf('?') > -1){
        sUrl = sUrl + "&lang=" + pLang;
    } else {
        sUrl = sUrl + "?lang=" + pLang;
    }

    document.location.href = sUrl;
}



$.fn._blank = function() {

    function clickHandler(e) {
        // si el usuario ha utilizado una tecla de control
        // no hacemos nada
        if (e.ctrlKey || e.shiftKey || e.metaKey)
            return;

        // abrimos la ventana
        var w = window.open(this.href, '_blank');
        if (w && !w.closed) {
            // si efectivamente hemos logrado abrirla
            // la ponemos en foco
            w.focus();
            // y cancelamos el comportamiento por defecto
            // del enlace
            e.preventDefault();
        }
    }

    this
        .filter('a[@href]') // que no se nos cuele algo que no sea un enlace
        .bind('click', clickHandler);

    return this; // permitimos concatenabilidad

}

var nCurrentId = 0;
function jsTrackIniciado(pId) {
	if(isNaN(pId)){
		//alert('NOOO cuenta: ' + pId);
	}else{
		$.ajax({
			type: "get",
			cache: false,
			async: false,
			url: 'visitar.php?tabla=usuario_mp3&id=' + pId
		});
		//alert('cuenta una visita: ' + pId);
	}
	nCurrentId = pId;
	
}
function jsEnviaIdFlash() {
	var n_id = nCurrentId;
	return n_id;
}

function Editar(pTabla, pId){
    document.location.href = 'perfil_edicion.php?tabla=' + pTabla + '&id=' + pId + '&tab=' + pTabla;
}

function Borrar(pTabla, pId, pArchivo){
	jConfirm('Quiere borrar el registro?', '', function(r) {
		
		if(r == true){
			$.ajax({
				type: "get",
				url: 'borrar.php?tabla=' + pTabla + '&id=' + pId + '&archivo=' + pArchivo,
				success: function(msg){
					document.location.href = document.location.href;
				}
			});
		}
	 
	});
}


function Set_Cookie( name, value, expires )
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) ;
}
