/**
 * Arquivo responsavel pelo JS do portal Índice de Transparência
 *
 * @author: Daniel Lopes - danielmlopes@gmail.com
 * @date: 01/06/2010
 *
 * Copyright(c) Todos os direitos reservados
 */

if (window.console == null) window.console = { log: function(p) { } };

var self;

var Functions = function()
{
	self = this;
	
	self.init();
}

Functions.fn = Functions.prototype;
Functions.fn.extend = jQuery.extend;
Functions.fn.extend(
{
    init: function() {
        this.header = $("#header");
        this.content = $("#content");
        $("input[type=radio], input[type=checkbox]").addClass("noBorder");

        self.cufonReplace();
        self.actions();

        if ($(".defaultValue").length)
            $(".defaultValue").resetDefaultValue();

        if ($("div.twitter", self.content).length)
            self.twitter();

        if ($("div.highlights div.blog div.important", self.content).length)
            self.paginationHighlights();

        if ($("div.videos", self.content).length)
            self.videos();

        if ($("div.ranking", self.content).length)
            self.rankingZero();

        $.browser.msie && $.browser.version == "6.0"
        	&& self.ieNavigation();

    },

    twitter: function() {
        if ($("ul.menu li.first.current_page_item", self.header).length)
            var numberT = 2;
        else
            var numberT = 3;

        $("div.twitter", self.content).getTwitter({
            userName: "transparenciaIT",
            numTweets: numberT,
            loaderText: "Lendo tweets...",
            slideIn: true,
            slideDuration: 750,
            showHeading: true,
            headingText: "Twitter",
            showProfileLink: true,
            showTimestamp: true
        });
    },

    paginationHighlights: function() {
        var button = $("div.highlights div.blog div.important button", self.content),
			lis = $("div.highlights div.blog div.important ul li", self.content),
			liLast = $("div.highlights div.blog div.important ul li:last", self.content),
			liFirst = $("div.highlights div.blog div.important ul li:first", self.content),
			ulAbstract = $("div.abstract ul", self.content);

        $("li:first", button.parent()).addClass("active");
        $("li:first", ulAbstract).addClass("active");
        verification();

        if (lis.length > 1) {
            button.not(".bt_prev").removeClass("inactive");
        }

        button
			.unbind()
			.click(function() {
			    var $this = $(this),
					liActive = lis.siblings(".active"),
					theActive = $("li.active:not( ul li.map.last )", ulAbstract);

			    if ($this.is(".bt_next")) {
			        liActive.removeClass("active").next().addClass("active");
			        theActive.removeClass("active").next().addClass("active");
			        verification();
			    } else {
			        liActive.removeClass("active").prev().addClass("active");
			        theActive.removeClass("active").prev().addClass("active");
			        verification();
			    }

			    return false;
			});

        function verification() {
            if (lis.length == 0)
                button.parent().hide();

            if (liLast.is(":visible"))
                button.not(".bt_prev").hide();
            else
                button.not(".bt_prev").show();

            if (liFirst.is(":visible"))
                button.not(".bt_next").hide();
            else
                button.not(".bt_next").show();
        }
    },

    cufonReplace: function() {
        Cufon.replace(".cufonReplace, #header ul.menu li a", {
            hover: true
        });
    },

    videos: function() {
        var li = $("div.videos ul li:not(div.pagination ul li)", self.content),
			location = $("html head link:first").attr("href").split("style.css"),
			videoSwf = location[0] + "_global/swf/player.swf";

        if (li.length) {
            li.each(function(i) {
                var $this = $(this);

                $("a:first", $this).addClass("bt_play");

                if ($this.parents("div.main.videos").length) {
                    if ((i + 1) % 4 == 0)
                        $this.addClass("last");
                } else {
                    if ((i + 1) % 2 == 0)
                        $this.addClass("last");
                }

                if ($("div.videoOwner", $this).length) {
                    var object = "",
						urlVideo = $("a.bt_play", $this).attr("href");
                    if ($.browser.msie) {
                        object = "<object id='" + urlVideo + "'  type='application/x-shockwave-flash' width='510' height='400'> ";
                        object += "<param name='movie' value='" + videoSwf + "?file=" + urlVideo + "&autoStart=true'/>";
                        object += "<param name='wmode' value='black'/>";
                        object += "<param name='allowscriptaccess' value='always'/>";
                        object += "<param name='allowfullscreen' value='false'/>";
                        object += "</object>";
                    }
                    else {
                        object = "<object id='" + urlVideo + "'  type='application/x-shockwave-flash' width='510' height='400'";
                        object += "wmode='black' data='" + videoSwf + "?file=" + urlVideo + "&autoStart=true'>";
                        object += "<param name='allowscriptaccess' value='always'/>";
                        object += "<param name='allowfullscreen' value='false'/>";
                        object += "</object>";
                    }
                    /*
                    object += "<object id='player' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' name='player' width='510' height='400'>";
                    object += "<param name='movie' value='" + videoSwf + "' />";
                    object += "<param name='allowscriptaccess' value='true' />";
                    object += "<param name='allowfullscreen' value='true' />";
                    object += "<param name='searchbar' value='false' />";
                    object += "<param name='usefullscreen' value='false' />";
                    object += "<param name='flashvars' value='file=" + urlVideo + "' />";
                    object += "<embed type='application/x-shockwave-flash' id='player2' name='player2' src='" + videoSwf + "' width='510' height='400' allowscriptaccess='always' allowfullscreen='false' flashvars='file=" + urlVideo + "' />";
                    object += "</object>";
                    */

                    $(object).appendTo("div.videoOwner div.modal.video div.theVideo");
                } else {
                    var object = $("div.theVideo object", $this),
						embed = $("div.theVideo embed", $this);

                    object.add(embed).css({ "width": "510px", "height": "400px" })
                    object.add(embed).attr("width", "510")
                    object.add(embed).attr("height", "400")

                    $("embed", object).attr("width", "510");
                    $("embed", object).attr("height", "400");
                }
            });

            $("a.bt_play", li)
				.unbind()
				.click(function() {
				    $(this).display = "";
				    var $this = $(this),
						next = $this.next("div"),
						overlay = $("#modal");

				    overlay
						.fadeIn("fast", function() {
						    $("html").css("overflow", "hidden");
						    //$this.next("object").css("display", "");
						    next.clone().appendTo("body").fadeIn("fast").css("zIndex", "11000");

						    $("a.bt_close", "div.modal:visible")
								.unbind()
								.click(function() {
								    var _this = $(this);

								    overlay.fadeOut("fast", function() {
								        _this
											.parent()
											.fadeOut("fast")
											.remove();

								        if ($.browser.msie)
								            $("html").css("overflowY", "scroll");
								        else
								            $("html").css("overflow", "visible");

								    });

								    return false;
								});
						})

				    return false;
				});

        }
    },

    rankingZero: function() {
        var prev = $("table tr td.mun").prev(),
			change = [];

        prev.each(function(i) {
            if ($(this).text() == prev.eq(i + 1).text()) {
                change.push(i + 1);
                $(this).css("borderBottomColor", "#ffffff");
            }
        });

        for (r = 0; r <= change.length; r++) {
            prev.eq(change[r]).html('&nbsp;');
        }
    },

    ieNavigation: function() {
        var boxIE = "<div class='modal ieFail'><h2>Alerta!</h2><h3>Este portal não dá suporte à versão do seu navegador. Por favor, atualize para a versão recente ou escolha um de sua preferência abaixo.</h3><ul><li class='safari'><a href='http://www.apple.com/br/safari/download/' title='Safari Browser'>Safari Browser</a></li><li class='firefox'><a href='http://br.mozdev.org/download/' title='Firefox Browser'>Firefox Browser</a></li><li class='chrome'><a href='http://www.google.com/chrome' title='Google Chrome Browser'>Google Chrome Browser</a></li><li class='opera'><a href='http://www.opera.com/download/' title='Opera Browser'>Opera Browser</a></li><li class='ie'><a href='http://www.microsoft.com/brasil/windows/internet-explorer/' title='Internet Explorer Browser'>Internet Explorer Browser</a></li></ul><p>Clique no ícone do navegador para ir ao site.</p></div>";

        $(boxIE).appendTo("body");

        $("#modal").fadeIn("fast", function() {
            $("div.modal.ieFail").fadeIn("fast");
            $("html").css("overflow", "hidden");
        });
    },

    mask: function() {
        $(".number").onlyNumber();
        $(".cep").mask("99999-999");
        $(".cnpj").mask("99.999.999/9999-99");
        $(".cpf").mask("999.999.999-99");
        $(".phone").mask("(99)9999-9999");
    },

    actions: function() {
        var pPublished = $("div.main.blog ul li p.published, div.main.single p.published, div.main.archive p.published", self.content),
			menu = $("<span></span>").appendTo("ul.menu li.current_page_item", self.header),
			ulCommittee = $("div.main ul.committee", self.content),
			divInterviews = $("div.main.interviews", self.content);

        if ($("div.main div.abstract ul li", self.content).length)
            $("div.main div.abstract ul li:last", self.content).addClass("last");

        if (pPublished.length) {
            pPublished.each(function() {
                var $this = $(this);

                if ($("a:first", $this).text() == "Blog") {
                    $("a:first", $this).remove();
                    $("span:first", $this).remove();
                }
            });
        }

        if (ulCommittee.length || divInterviews.length) {
            var liImg = $("li img", ulCommittee);

            liImg.add("div.character img", divInterviews).each(function() {
                $(this).parent().addClass("thisImg");
            });
        }

        if ($("div.paginationText").length) {
            var pagination = $("div.paginationText");

            $("a", pagination).each(function() {
                var $this = $(this);

                if ($this.text() == "Ver anteriores")
                    $this.addClass("bt_prev bt_bulletDefault");
                else
                    $this.addClass("bt_next bt_bulletDefault");
            });
        }
    }
});

$(function()
{
    $.fn.resetDefaultValue = function()
    {
        function _clearDefaultValue()
        {
            var _$ = $(this);
            if (_$.val() == this.defaultValue) { _$.val(''); }
        };
        function _resetDefaultValue()
        {
            var _$ = $(this);
            if (_$.val() == '') { _$.val(this.defaultValue); }
        };
        return this.click(_clearDefaultValue).focus(_clearDefaultValue).blur(_resetDefaultValue);
    }
});

$( function()
{
	functions = new Functions();
});


