/**
 * Arquivo responsavel pela chamada de todas as funcionalidades da Campanha MCPC 2010
 *
 * @author: 
 * @date: 10/05/2010
 *
 * Copyright(c) Todos os direitos reservados a TechNova
 */
 
if (window.console == null) window.console = { log: function(p) { } };

var self;

Cufon.replace( ".cufonReplace", {
	hover: true
});

var Actions = function()
{
	self = this;

	self.init();
}

Actions.fn = Actions.prototype;
Actions.fn.extend = jQuery.extend;
Actions.fn.extend(
{
    init: function () {
        // URL
        self.url = BASE_URL;

        // Função para realizar HOVER das imagens
        self.houverImage();

        // Função para exibir videos nas modais
        self.openModal();

        // Função pora ativar hover do player
        self.hoverManifest();

        self.hovers();

        // Flash do Adesometro
        setTimeout(function () {
            if ($("#adesometro").length)
                self.adesometro();
        }, 3000);
        if ($(".contentLogin").length) {
            self.hoverOk();
        }

        if ($("div.details").length)
            self.navegationGalery();

//        if ($("#popupflash").length) {
//            self.openPopUpHome();
//            //   console.log("abrir pop up!");
//        }

    },

    navegationGalery: function () {
        $("div.image a.btPrev, div.image a.btNext").live("click", function () {
            var id = $(this).attr("href"),
				param = {
				    "Id": id
				};

            self.getJson("/mcpc/Galeria/DetalheJson?Id=" + id);
            return false;
        });
    },

    getJson: function (url) {
        $.ajax(
		{
		    url: url,
		    dataType: "json",
		    type: "POST",
		    error: function (jqXHR, textStatus, errorThrown) {
		        alert("Ocorreu um  erro, tente novamente");
		    },
		    success: function (response) {
		        self.buildDetalImagem(response);
		    }
		});
    },

    buildDetalImagem: function (json) {
        var data = json,
			botaoPrev = '<a href="' + data.IdProxima + '" class="btPrev" title="Anterior"><</a>',
			botaoNext = '<a href="' + data.IdAnterior + '" class="btNext" title="Pr&oacute;ximo">></a>';

        $("div.image img").fadeOut("500", function () {
            $("div.image img").attr("src", "/mcpc/Service/Thumb.aspx?imagem=" + data.Arquivo + "&w=557&h=424").load(function () {
                $("div.image img").fadeIn();
                $("div.details p.title").text(data.Titulo);
                $("div.details p.description").text(data.Descricao);
                $("div.details h6 span.position").text(data.Posicao);
                if ($("div.details a.btPrev").length) {
                    if (!data.IdAnterior)
                        $("div.details a.btPrev").remove();
                    else
                        $("div.details a.btPrev").attr("href", data.IdAnterior);
                } else {
                    $("div.details div.image").append(botaoPrev);
                }
                if ($("div.details a.btNext").length) {
                    if (!data.IdProxima)
                        $("div.details a.btNext").remove();
                    else
                        $("div.details a.btNext").attr("href", data.IdProxima);
                } else {
                    $("div.details div.image").append(botaoNext);
                }
            });
        });

    },

    houverImage: function () {
        var zIndex = "";

        $("div.userImage").hover(function () {
            var $this = $(this);

            zIndex = $this.css("z-index");

            $this.addClass("active");

            if ($this.css("position") != "absolute")
                $this.css({ "position": "relative", "z-index": "1000" });
            else
                $this.css({ "z-index": "1000" });

        }, function () {
            var $this = $(this);

            $this.removeClass("active");

            $this.css({ "z-index": zIndex });

        });
    },

    hoverManifest: function () {
        $("li.manifest a").hover(function () {
            $("a.bt_playMovie").addClass("active");
        }, function () {
            $("a.bt_playMovie").removeClass("active");
        });
    },

    hoverOk: function () {
        $("input.bt_ok").hover(function () {
            $(this).addClass("active");
        }, function () {
            $(this).removeClass("active");
        });
    },

    hovers: function () {
        $("input.btNext").hover(function () {
            $(this).addClass("active");
        }, function () {
            $(this).removeClass("active");
        });
    },

    /* início da função Adesometro*/
    adesometro: function () {
        var flashvars = {
            servico: window.BASE_URL + "WebForms/xmlContador.aspx",
            adesao: window.BASE_URL + "Adesao/Regulamento",
            jaDoou: window.JA_DOOU.toLowerCase(),
            abrir: "actions.adesometroControl( 'abrir' )",
            fechar: "actions.adesometroControl( 'fechar' )"
        },
		params = {
		    menu: "false",
		    scale: "noscale",
		    salign: "c",
		    align: "middle",
		    wmode: "transparent",
		    allowScriptAccess: "always"
		},
		attributes = {};

        swfobject.embedSWF(window.BASE_URL + "_global/swf/contador_2010.swf", "contadorFlash", "435", "270", "7.0.0", null, flashvars, params, attributes);
    },

    adesometroControl: function (action) {
        var divAdesometro = $("#barMC div.mundoCaixa div#adesometro");

        if (action == "abrir")
            divAdesometro.addClass("active");
        else if (action == "fechar")
            setTimeout(function () { divAdesometro.removeClass("active"); console.log("fechou") }, 1000);
        else
            return false;
    },

    openPopUpHome: function () {

        var flashvars = {
            clickurl: "http://mcpc.mundocaixa.com.br/Adesao/Regulamento"
        };
        var params = {
            menu: "false",

            scale: "noscale",
            salign: 't',
            align: "t",
            wmode: 'transparent'
        };
        var attributes = {
        };
        $("#popupflash").show();

        swfobject.embedSWF(window.BASE_URL + "_global/swf/dhtml_novembro.swf", "fs", "100%", "100%", "7.0.0", null, flashvars, params, attributes);
    },
    closePopUpHome: function () {

        //alert("teste");
        $("#popupflash").hide();
    },
    /* final da função Adesometro*/
    openModalRegulamento: function () {

        if ($("#popupflash").length) {
            $("#popupflash").fadeIn(function () {
                $("#popupflash").remove();
            }
            );
        }
        $("#overlayPopup").fadeIn();
        var modal = $('<div id="modalRegulamento"></div>');

        modal.appendTo("body");

        modal.load("home/ModalRegulamento/",
            function () {
                self.prepareModalRegulamento();
            }
        );

    },

    prepareModalRegulamento: function () {

        if ($("div#diaDRegulamento a.fechar").length) {
            $("div#diaDRegulamento a.fechar").click(

                function (event) {
                    event.preventDefault();
                    self.closeModalRegulamento();
                    return false;
                }
            );
        }

//        if ($("div#diaDRegulamento div.bg button#btParticipar").length) {
//            $("div#diaDRegulamento div.bg button#btParticipar").click(

//                function (event) {
//                    event.preventDefault();
//                    console.log("checado: " + $("input#regulationAccept").is(":checked"));
//                    if ($("input#regulationAccept").is(":checked")) {
//                        $("div#modalRegulamento form").submit();
//                    } else {
//                        $("div#diaDRegulamento div.control p.messageError").show();
//                    }
//                    return false;
//                }
//            );
//        }
    },
    closeModalRegulamento: function () {
        $("#overlayPopup").fadeOut(function () { $("#overlayPopup").remove() });
        $("#modalRegulamento").fadeOut(function () { $("#modalRegulamento").remove() });
        $("#popupflash").remove();
    },
    openModal: function () {
        $("a.bt_playMovie").click(function () {
            $.ajax({
                url: this.href,
                success: function (response) {
                    $(response).appendTo("body").modal({
                        buttonClose: ".btnFechar"
                    }, function () {
                        Cufon.replace("div#contModal .cufonReplace");
                        var flashvars = {
                            urlVideo: "nossoManifesto.flv",
                            trasparente: "false"
                        };
                        var params = {
                            menu: "false"
                        };
                        var attributes = {
                            visibility: "visible"
                        };
                        var url = $(".menu").is("li.manifest a")
                                    ? window.BASE_URL + "_global/swf/player_mens.swf" + "?time=" + new Date().getTime()
                                    : window.BASE_URL + "_global/swf/player_mens.swf" + "?time=" + new Date().getTime();

                        swfobject.embedSWF(url, "contVideo", "640", "385", "7.0.0", window.BASE_URL + "_global/js/swfobject/expressInstall.swf", flashvars, params, attributes);
                        //alert(url);
                    });

                }
            });
            return false;
        });

        $("li.manifest a").click(function () {
            //alert('dasdsa');
            $.ajax({
                url: this.href,
                success: function (response) {
                    $(response).appendTo("body").modal({
                        buttonClose: ".btnFechar"
                    }, function () {
                        Cufon.replace("div#contModal .cufonReplace");
                        var flashvars = {
                            urlVideo: "nossoManifesto.flv",
                            trasparente: "false"
                        };
                        var params = {
                            menu: "false"
                        };
                        var attributes = {
                            visibility: "visible"
                        };
                        var url = $(".menu").is("li.manifest a")
                                    ? window.BASE_URL + "_global/swf/player_mens.swf" + "?time=" + new Date().getTime()
                                    : window.BASE_URL + "_global/swf/player_mens.swf" + "?time=" + new Date().getTime();

                        swfobject.embedSWF(url, "contVideo", "640", "385", "7.0.0", window.BASE_URL + "_global/js/swfobject/expressInstall.swf", flashvars, params, attributes);
                        //alert(url);
                    });

                }
            });
            return false;
        });
        $("div.colModel a.imgTax").click(function () {
            var _src = $(this).attr("class").split(" ")[1];
            $.ajax({
                url: this.href,
                success: function (response) {
                    var resposta = $(response);

                    resposta.appendTo("body").modal({
                        buttonClose: ".btnFechar"
                    }, function () {
                        _src == "irpf"
							? _src = window.BASE_URL + "_global/img/graph/graph_impostoRenda2011.png"
							: _src = window.BASE_URL + "_global/img/graph/IR_2007_papel.jpg";

                        $("img", resposta).attr("src", _src);
                        console.log($("img", resposta))
                    });

                }
            });
            return false;
        });
        $(".highlightSide a.player").click(function () {
            $.ajax({
                url: this.href,
                success: function (response) {
                    $(response).appendTo("body").modal({
                        buttonClose: ".btnFechar"
                    }, function () {
                        Cufon.replace("div#contModal .cufonReplace");
                        var flashvars = {
                            urlVideo: "entenda-o-mcpc.flv",
                            trasparente: "false"
                        };
                        var params = {
                            menu: "false"
                        };
                        var attributes = {
                            visibility: "visible"
                        };
                        var url = $(".menu").is("li.manifest a")
                                    ? window.BASE_URL + "_global/swf/player_mens.swf" + "?time=" + new Date().getTime()
                                    : window.BASE_URL + "_global/swf/player_mens.swf" + "?time=" + new Date().getTime();

                        swfobject.embedSWF(url, "contVideo", "640", "385", "7.0.0", window.BASE_URL + "_global/js/swfobject/expressInstall.swf", flashvars, params, attributes);
                        //alert(url);
                    });

                }
            });
            return false;
        });
        $("div.listNews .playVideo, div.listNews .btPlayer").click(function () {

            var endereco = $(this).attr("rel");

            //alert(endereco);

            $.ajax({
                url: this.href,
                success: function (response) {
                    $(response).appendTo("body").modal({
                        buttonClose: ".btnFechar"
                    }, function () {
                        Cufon.replace("div#contModal .cufonReplace");
                        var flashvars = {
                            urlVideo: endereco,
                            trasparente: "false"
                        };
                        var params = {
                            menu: "false"
                        };
                        var attributes = {
                            visibility: "visible"
                        };
                        /*var url = $(".menu").is("li.manifest a")
                        ? "_global/swf/player_mens.swf" + "?time=" + new Date().getTime()
                        : "../_global/swf/player_mens.swf" + "?time=" + new Date().getTime();*/

                        swfobject.embedSWF(window.BASE_URL + "_global/swf/player_mens.swf", "contVideo", "640", "385", "7.0.0", window.BASE_URL + "_global/js/swfobject/expressInstall.swf", flashvars, params, attributes);
                        //alert(url);
                    });

                }
            });
            return false;
        });
    }
});
$(function() {
    actions = new Actions();
//    if ($.browser.msie && $.browser.version == 6)
//        DD_belatedPNG.fix(".png24");
});

