/* functions.js */

/* Libreria dinamica del portal */
/* Copyright (C) 2010 Felix Rios @infelix               */


/* ----------------------------------------------------*/
/* Histórial de modificaciones 						  	*/
/* 26/11/10 Primera versión			  					*/
/*														*/
/* ----------------------------------------------------*/

// Oculta el cuadro del reproductor y borra su contenido
function hideSideCanvas()
{
	  $('#sidecanvas').slideUp('slow', function() {
		    // Animation complete.
		  document.getElementById("innercanvas").innerHTML="";
	      document.getElementById("sidecanvas").hidden = true;
	  });	  
	  return;	
}

function hideSearchItems()
{
	  $('#searchitems').slideUp('slow', function() {
		    // Animation complete.
		  document.getElementById("searchitems").innerHTML="";
	      //document.getElementById("searchitems").hidden = true;
	  });	  
	  return;	
}


// Busqueda

function showSearch(keywords, page)
{ 
	//hideSideCanvas();
	document.getElementById('waitsearch').innerHTML='<img src="images/layout/white-ajax.gif" width="12" height="12">';
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
	    {
		document.getElementById('waitsearch').innerHTML='';
	    document.getElementById("searchitems").innerHTML=xmlhttp.responseText;
	    $('#searchitems').slideDown('slow', function() { });
	  }
	  }
	xmlhttp.open("GET","ajax/search.php?keywords="+keywords+"&page="+page,true);
	xmlhttp.send();	

}

// Para codificar
function urlencode( str ) {  
	  
    var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];  
    var ret = str.toString();  
  
    var replacer = function(search, replace, str) {  
        var tmp_arr = [];  
        tmp_arr = str.split(search);  
        return tmp_arr.join(replace);  
    };  
  
    // The histogram is identical to the one in urldecode.  
    histogram['!']   = '!';  
    histogram['%20'] = ' ';  
  
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions  
    ret = encodeURIComponent(ret);  
  
    for (search in histogram) {  
        replace = histogram[search];  
        ret = replacer(search, replace, ret) // Custom replace. No regexing  
    }  
  
    // Uppercase for full PHP compatibility  
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {  
        return "%"+m2.toUpperCase();  
    });  
  
    return ret;  
} 

// Actualizar cuadro del reproductor (y lo muestra)
function updateSideCanvas(uid,pid,pname,type)
{  
	var strtype;
	var url;

	// ocultar busqueda
	//hideSearchItems();
	document.getElementById("innercanvas").innerHTML='<img src="images/layout/white-ajax.gif" width="12" height="12">';
	
	url = 'http:%2F%2Foyesto.com%2F' + pid;
	img = '';
	
    if (type == 0) {
    	strtype = 'oyendo el ultimo episodio';
    }
    else {
    	strtype = 'viendo el ultimo video';
    }
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
	    {
		    document.getElementById("innercanvas").innerHTML=xmlhttp.responseText;
		    
		    // Twitter hack
		    //document.getElementById("twitbtn").innerHTML="<a onclick='MyWindow=window.open(\"http://twitter.com/share?url="+url+"&via=oyesto&text="+strtype+" de "+pname+"&related=infelix:Creador de oyesto.com&lang=es\",\"MyWindow\",\"toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=550,height=450\"); return false;' href='#'>Tweet</a>";
		    document.getElementById("twitbtn").src="http://platform.twitter.com/widgets/tweet_button.html?url="+url+"&via=oyesto&text="+strtype+" de "+urlencode(pname)+"&related=infelix:el creador de oyesto.com&lang=es";
		    // Facebook hack
		    document.getElementById("fbbtn").src = "http://www.facebook.com/plugins/like.php?href="+url+"&layout=button_count&show_faces=false&action=like&colorscheme=light&height=21";
		    // Buzz hack
		    document.getElementById("buzzbtn").src = 'ajax/buzz.php?url='+url+'&img=http://www.oyesto.com/images/layout/small_logo.png';

		    $('#sidecanvas').slideDown('slow', function() {
		    });
	    }
	  }
	xmlhttp.open("GET","ajax/episode.php?uid="+uid+"&pid="+pid,true);
	xmlhttp.send();

	
}

// Favoritea o desfavoritea un elemento
function toggleFav(uid,pid,name)
{   
	if (uid == 0)
	{
		return;
	}
	document.getElementById("favoriteicon").innerHTML='<img align="absmiddle" src="images/layout/ajax-small.gif">';
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
	    {
	    document.getElementById("favoriteicon").innerHTML=xmlhttp.responseText;
	    }
	  }
	xmlhttp.open("GET","ajax/toggleFav.php?uid="+uid+"&pid="+pid+"&name="+name,true);
	xmlhttp.send();	

}

//Enviar nueva clave a correo
function sendPwd(email)
{   
	if (email == '')
	{
		return;
	}
	document.getElementById("mailstatus").innerHTML='<br><center><img align="absmiddle" src="images/layout/ajax-small.gif"></center>';
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
	    {
	    document.getElementById("mailstatus").innerHTML=xmlhttp.responseText;
	    }
	  }
	xmlhttp.open("GET","ajax/sendpassword.php?email="+email,true);
	xmlhttp.send();	

}


// Muestra el listado indicado
function showListing(code,country,page,type,cat)
{ 
	document.getElementById('waiting').innerHTML='<br><center><img src="images/layout/ajax-loader.gif"></center><br>';
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
	    {
		document.getElementById('waiting').innerHTML='';
	    document.getElementById("listing").innerHTML=xmlhttp.responseText;
	    }
	  }
	xmlhttp.open("GET","ajax/listPodcasts.php?code="+code+"&country="+country+"&page="+page+'&type='+type+'&cat='+cat,true);
	xmlhttp.send();	

}

// Muestra más info de un podcast actual
function showPodcast(pid)
{ 

	document.getElementById('waiting').innerHTML='<br><br><br><img src="images/layout/ajax-loader.gif">';
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
	    {
		document.getElementById('waiting').innerHTML='';
	    document.getElementById("listing").innerHTML=xmlhttp.responseText;
	    }
	  }
	xmlhttp.open("GET","ajax/showPodcast.php?pid="+pid,true);
	xmlhttp.send();	

}

function checkFeed(url)
{ 
	document.getElementById("feedstatus").innerHTML='<img src="images/layout/ajax-small.gif">';
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
	    {
	    document.getElementById("feedstatus").innerHTML=xmlhttp.responseText;
	    }
	  }
	xmlhttp.open("GET","ajax/validateFeed.php?url="+url,true);
	xmlhttp.send();		

}

function previewvideo(url)
{
	document.getElementById("videopreview").innerHTML='<img src="images/layout/ajax-loader.gif">';
	if (window.XMLHttpRequest)
	  {// code for IE7+, Firefox, Chrome, Opera, Safari
	  xmlhttp=new XMLHttpRequest();
	  }
	else
	  {// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	  }
	xmlhttp.onreadystatechange=function()
	  {
	  if (xmlhttp.readyState==4 && xmlhttp.status==200)
	    {
	    document.getElementById("videopreview").innerHTML=xmlhttp.responseText;
	    $('#videopreview').slideDown('slow', function() {
	    });

	    }
	  }
	xmlhttp.open("GET","ajax/previewVideo.php?url="+url,true);
	xmlhttp.send();		

}

function showFBstream()
{
    document.getElementById("twstream").style.display = 'none';
    document.getElementById("fbstream").style.display = 'block';
    return true;
}

function showTWstream()
{
    document.getElementById("fbstream").style.display = 'none';
    document.getElementById("twstream").style.display = 'block';
    return true;	
}
