/**
 * @author iddar
 */

var itemList = "";
var count = 0, files = 'data/', lastTime = 0;

function success(response, status){
    if (status == 'success') {
        lastTime = response.time;
        //$('#daddy-shoutbox-response').html('Su peticion a sido enviada: <img src="../../img/accept.png" />');
		
		$.gritter.add({
			title: 'Peticion Enviada!',
			text: 'Listo, tu peticion ha sido enviada corectamente <b>Gracias<b>.',
			//image: 'http://a0.twimg.com/profile_images/59268975/jquery_avatar_bigger.png',
			sticky: false, 
			time: '4000'
		});
		
        //$('#daddy-shoutbox-list').append(prepare(response));
        $('input[@name=message]').attr('value', '').focus();
        $('#list-' + count).fadeIn('slow');
        timeoutID = 0;
    }
}
    
function validate(formData, jqForm, options){
    for (var i = 0; i < formData.length; i++) {
        if (!formData[i].value) {
           // alert('Rellene los Campos de Favor');
					$.gritter.add({
						title: 'Rellene los Campos de Favor',
						text: 'Para que su peticion sea prosesada correctamente rellene todos los campos.',
						image: 'conf/img/icon_close.png',
						sticky: false, 
						time: '3500'
					});
            $('input[@name=' + formData[i].name + ']').attr('value', 'Su texto');
            return false;
        }
    }
    //$('#daddy-shoutbox-response').html('Espere un momento: <img src="../../img/loader.gif" />');	
	$.gritter.add({
			title: 'Enviando su peticion!',
			text: 'Espere un momento en lo que es sistema procesa su peticion.',
			//image: 'http://a0.twimg.com/profile_images/59268975/jquery_avatar_bigger.png',
			sticky: false, 
			time: '2500'
		});
    clearTimeout(timeoutID);
}
    
	
var name_text_tmp='';
function cancion(){
$("#rolas_b").slideToggle();	
	$.ajax({
	 type: 'GET',
	 url: 'conf/JS/shoutcast.php',
	 dataType: 'xml',
	 cache: false,
	 success: function(xml) {
		 $(xml).find('cancion').each(function(){
		   var name_text = $(this).find('canta').text();
		   var name = $(this).find('rola').text();
		   //$("#canta").html(name_text);
		   //$("#rola").html(name);
		  	if (name_text != name_text_tmp){
				name_text_tmp = name_text;
				camcion_text= '<strong>' + name_text + '<br /> con su cancion ' + name + '</strong>';
					$.gritter.add({
						title: 'Ahora escuchas a:',
						text: camcion_text,
						//image: 'http://a0.twimg.com/profile_images/59268975/jquery_avatar_bigger.png',
						sticky: false, 
						time: '5000'
					});
			}
		 }); //close each(
	 }
 }); //close $.ajax(
setTimeout(cancion,5000);
$("#rolas_b").slideToggle();
}


$(document).ready(function() {
	$('A[rel="external"]').click(function(){window.open($(this).attr('href'));return false;});
	
	$("a.tab").click(function () {
			$(".activo").removeClass("activo");
			$(this).addClass("activo");
			$(".content").slideUp();
			var content_show = $(this).attr("title");
			$("#"+content_show).slideDown();
	});
	
	$("a.tab-news").click(function () {
			$(".activo").removeClass("activo");
			$(this).addClass("activo");
			$(".encabazado").slideUp();
			var content_show = $(this).attr("title");
			$("#"+content_show).slideDown();
	});
		
	$("#domingo").click(function (){ $('#horario').load('conf/hr/domingo.html'); });
	$("#lunes").click(function (){ $('#horario').load('conf/hr/lunes.html'); });
	$("#martes").click(function (){ $('#horario').load('conf/hr/martes.html'); });
	$("#miercoles").click(function (){ $('#horario').load('conf/hr/miercoles.html'); });
	$("#jueves").click(function (){ $('#horario').load('conf/hr/jueves.html'); });
	$("#viernes").click(function (){ $('#horario').load('conf/hr/viernes.html'); });
	$("#sabado").click(function (){ $('#horario').load('conf/hr/sabado.html'); });
	
	cancion();
	
	$('input[type=text]').focus(function() {
	$(this).val('');
	});

    var options = {
        dataType: 'json',
        beforeSubmit: validate,
        success: success
    };
    $('#daddy-shoutbox-form').ajaxForm(options);
    timeoutID = 0;	
	
//---------------Json News---------------------------------//	
function limpiar(){
		 $('#news br').remove();
		 $('br[clear="all"]').remove();
		 $('.blogger-post-footer').remove();
		 $('img#news').remove();
}

var content = "";

	$.getJSON("queik.php",
		function(json){
		  itemList = '';
		 if(json.count > 0) {
		   content = output_feed_items(json);
		 } else {
		   content = "The request did not return results.";
		 }
		 $(".queik").html(content);
		 limpiar();
		}
	);
	  
	$.getJSON("noticias.php",
	   function(json){
		  itemList = '';
	     if(json.count > 0) {
	       content = output_feed_items(json);
	     } else {
	       content = "The request did not return results.";
	     }
	     $(".noticias").html(content);
		 limpiar();
	   }
	  );  
	  
	$.getJSON("deportes.php",
	   function(json){
		  itemList = '';
	     if(json.count > 0) {
	       content = output_feed_items(json);
	     } else {
	       content = "The request did not return results.";
	     }
	     $(".deportes").html(content);
		 limpiar();
	   }
	  );
	  
	});

//-----------------------------------------------//
function output_feed_items(json) {
	//document.title = json.value.title;
	//var heading = '<h3>' + json.value.title + '</h3>';
	//for (i=0;i<json.count;i++) {
	for (i=0;i<5;i++) {
	itemList += make_feed_item(json.value.items[i]);
	/*descList.push(make_feed_desc(json.value.items[i], i));*/
	}
	//return heading + itemList;
	return itemList;
}
	
function make_feed_item(item) {
	var corto = '<div class="desc">' + '<h3>' +
	  '<a target="_blank" href="'+ item.link + '">' +
	  item.title + '</a></h3>';
	  
	var desc_info = '<span>Publicado: ' +
	item.pubDate + '</span>';
	//desc_info += ' - <a target="_blank" class="link" href="' + item.link + '">Link to Article</a>';
	//var desc_info = '<div class="item-info">' + desc_info + '</div>';
	var completo = desc_info + '<p>' +  item.description + '</p>' + '<a target="_blank" class="link" href="' + item.link + '">Leer Nota completa</a>' + '</div>';	  

	var txt_final = corto + completo;
	  
	return txt_final;  
}
