$(document).ready(function () {
	/*$.ajax({
		url: 'app/noticias-home',
		type: "GET",
		dataType: "xml",
		contentType: "application/x-www-form-urlencoded; charset=UTF-8",
		success: function (xml) {
		var c = 0;
		$(xml).find("noticia").each(function () {
			if (c < 3) {
				$('div#nn_' + (c + 1)).html($(this).attr('html'));
			}
			c++;
		});
	}
	});*/
    $.get('app/noticias-home', function (resp) {
    	$('#novas_noticias').html(resp);
    });

    $.ajax({
        url: 'app/twitter',
        type: "GET",
        dataType: "xml",
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        success: function (xml) {
            twit = '';
            var c = 0;
            var l = 0;
            $(xml).find("item title").each(function () {
                if (c < 3) {
                    twit += "<div class='tweet_" + l + "'>" + $(this).text() + "</div>";
                }
                c++;
            });
            $('#conteudo_twitter').html(twit).hide().show("slow");
            $('#conteudo_twitter div.tweet_1, #conteudo_twitter div.tweet_0').tweetify();
        }
    });
    $.ajax({
        url: 'app/delicious',
        type: "GET",
        dataType: "xml",
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        success: function (xml) {
            deli = '';
            var c = 0;
            var l = 0;
            $(xml).find("item").each(function () {
                if (c < 5) {
                    deli += '<a href="' + $(this).find("guid").text() + '">' +  Utf8.decode($(this).find("title").text()) + '</a><br />';
                }
                c++;
            });
            $('#conteudo_delicious').html(deli).hide().show("slow");
        }
    });
    $.ajax({
        url: 'app/flickr',
        type: "GET",
        dataType: "xml",
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        success: function (xml) {
            flic = '';
            var c = 0;
            var l = 0;
            $(xml).find("item").each(function () {
                if (c < 4) {
                    flic += "<a href='" + $(this).find("link").text() + "'><img alt='" + $(this).find("title").text() + "' title='" + $(this).find("title").text() + "' src='" + $(this).find("[nodeName=media:thumbnail]").attr("url") + "' width='79' height='79' /></a>";
                }
                c++;
            });
            $('#conteudo_flickr').html(flic).hide().show("slow");
        }
    });
    $.ajax({
        url: 'app/slideshare',
        type: "GET",
        dataType: "xml",
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        success: function (xml) {
            slid = '';
            var c = 0;
            var l = 0;
            $(xml).find("item").each(function () {
                if (c < 3) {
                    slid += "<a href='" + $(this).find("link").text() + "'><img alt='" + $(this).find("title").text() + "' title='" + $(this).find("title").text() + "' src='" + $(this).find("[nodeName=media:thumbnail]").attr("url") + "' width='118' height='" + $(this).find("media\\:thumbnail").attr("height") + "' /></a>";
                }
                c++;
            });
            $('#conteudo_slideshare').html('<div id="centralizado">' + slid + '</div>').hide().show("slow");
        }
    });
    $.ajax({
        url: 'app/noticias-home-flash',
        type: "GET",
        dataType: "xml",
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        success: function (xml) {
            fNot = '';
            var c = 0;
            var l = 0;
            $("noticia",xml).each(function () {
				
				fNot += '<li><div><h3>' + $.trim($(this).find('titulo').text()) + '</h3><p>' + $.trim($(this).find('texto').text())  + '</p></div><a href="' + $.trim($(this).find('url').text()) + '"><span>Leia mais</span></a></li>';
            });
            $('#flash-noticias').html('<ul>' + fNot + '</ul>');
			slideFnot($('#flash-noticias li').length)
			function slideFnot(eq){
				$('#flash-noticias li').eq((eq + 2) % $('#flash-noticias li').length).fadeIn('normal').css({'width': '172px', 'left': '797px'});
				$('#flash-noticias li').eq((eq + 1) % $('#flash-noticias li').length).fadeIn('normal').css({'width': '', 'left': '388px'});
				$('#flash-noticias li').eq((eq + 0) % $('#flash-noticias li').length).css({'width': '', 'left': '0px'}).fadeIn('normal', function(){
					setTimeout(function(){
						$('#flash-noticias li').eq((eq + 2) % $('#flash-noticias li').length).fadeOut('normal');
						$('#flash-noticias li').eq((eq + 1) % $('#flash-noticias li').length).fadeOut('normal');
						$('#flash-noticias li').eq((eq + 0) % $('#flash-noticias li').length).fadeOut('normal', function(){
							slideFnot((eq + 3) % $('#flash-noticias li').length);});}, 10000);
				});
			}
        }
    });
    $.ajax({
        url: 'app/youtube',
        type: "GET",
        dataType: "xml",
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        success: function (xml) {
            yout = '';
            var c = 0;
            var l = 0;
            $(xml).find("item").each(function () {
                if (c < 1) {
                    vid = $(this).find("link").text();
                    vid = vid.substring(vid.indexOf("?v=") + 3, vid.indexOf("&feature"));
                    yout = '<div id="video_produto_home" title="' + Utf8.decode($(this).find("title").text()) + ' no Youtube- http://www.youtube.com/v/' + vid + '&amp;hl=pt_BR&amp;fs=1"> &nbsp; </div>';
                }
                c++;
            });
            $('#conteudo_youtube').html(yout).hide().show("slow");
            $("div#video_produto_home").each(function () {
                $(this).html('<a href="http://www.adobe.com/go/getflashplayer"><img src="imagens/icones/get_flash_player.png" alt="Get Adobe Flash player" /></a>');
                flvYoutube = $(this).attr("title").substring($(this).attr("title").indexOf("http://www.youtube.com"));
                var flashvars = {};
                var params = {};
                params.wmode = "transparent";
                params.bgcolor = "#000000";
                params.allowfullscreen = "true";
                params.allowscriptaccess = "always";
                var attributes = {};
                attributes.id = "video_produto_home";
                swfobject.embedSWF(flvYoutube, "video_produto_home", "381", "182", "9.0.0", "js/expressInstall.swf", flashvars, params, attributes);
            });
        }
    });
    $("#botao_rede_twitter").click(function () {
        if ($(this).attr("title") == "Fechar") {
            $("#twitter").stop(true).animate({
                height: "29px"
            }, {
                duration: 500
            });
            $(this).attr("title", "Abrir");
            $(this).children().css("background-position", "0px 0px");
        } else {
            $(this).children().css("background-position", "-16px 0px");
            $("#twitter").stop(true).animate({
                height: "213px"
            }, {
                duration: 500
            });
            $(this).attr("title", "Fechar");
        }
    });
    $("#botao_rede_youtube").click(function () {
        if ($(this).attr("title") == "Fechar") {
            $("#youtube").stop(true).animate({
                height: "30px"
            }, {
                duration: 500
            });
            $(this).attr("title", "Abrir");
            $(this).children().css("background-position", "0px 0px");
        } else {
            $(this).children().css("background-position", "-16px 0px");
            $("#youtube").stop(true).animate({
                height: "214px"
            }, {
                duration: 500
            });
            $(this).attr("title", "Fechar");
        }
    });
    $("#botao_rede_flickr").click(function () {
        if ($(this).attr("title") == "Fechar") {
            $("#flickr").stop(true).animate({
                height: "30px"
            }, {
                duration: 500
            });
            $(this).attr("title", "Abrir");
            $(this).children().css("background-position", "0px 0px");
        } else {
            $(this).children().css("background-position", "-16px 0px");
            $("#flickr").stop(true).animate({
                height: "214px"
            }, {
                duration: 500
            });
            $(this).attr("title", "Fechar");
        }
    });
    $("#botao_rede_facebook").click(function () {
        if ($(this).attr("title") == "Fechar") {
            $("#facebook").stop(true).animate({
                height: "29px"
            }, {
                duration: 500
            });
            $(this).attr("title", "Abrir");
            $(this).children().css("background-position", "0px 0px");
        } else {
            $(this).children().css("background-position", "-16px 0px");
            $("#facebook").stop(true).animate({
                height: "137px"
            }, {
                duration: 500
            });
            $(this).attr("title", "Fechar");
        }
    });
    $("#botao_rede_slideshare").click(function () {
        if ($(this).attr("title") == "Fechar") {
            $("#slideshare").stop(true).animate({
                height: "30px"
            }, {
                duration: 500
            });
            $(this).attr("title", "Abrir");
            $(this).children().css("background-position", "0px 0px");
        } else {
            $(this).children().css("background-position", "-16px 0px");
            $("#slideshare").stop(true).animate({
                height: "138px"
            }, {
                duration: 500
            });
            $(this).attr("title", "Fechar");
        }
    });
    $("#botao_rede_delicious").click(function () {
        if ($(this).attr("title") == "Fechar") {
            $("#delicious").stop(true).animate({
                height: "30px"
            }, {
                duration: 500
            });
            $(this).attr("title", "Abrir");
            $(this).children().css("background-position", "0px 0px");
        } else {
            $(this).children().css("background-position", "-16px 0px");
            $("#delicious").stop(true).animate({
                height: "138px"
            }, {
                duration: 500
            });
            $(this).attr("title", "Fechar");
        }
    });
    $(".botao_rede_social div").ifixpng();
});

