﻿var propDetails = {
    getMapWidget: function() {
        $.getScript('/static/js/sections/imoveis-mapa.js', function() {
            propDetails.getGallery();
        });
    },

    getGallery: function() {
        $.getScript('/static/js/sections/imoveis-galeria.js', function() {
            propDetails.startExternalItems();
        });
    },

    startExternalItems: function() {
        setTimeout(function() {
            mapWidget.init();
            propDetails.insert3dVideo();
            gallery.mount('#gallery1');
            $('.nav-print-map a').css('visibility', mapWidget.hasMap ? 'visible' : 'hidden');
        }, 500);
    },

    init: function() {
        propDetails.getMapWidget();

        structure.trackingReady(function() {
            $.each($('a[rel~=sp-tracking]'), function(i, item) {
                var disabled = $(item).hasClass('disabled') || $(item).parent().hasClass('disabled');
                if (structure.currentState == 'sp' && !disabled) {
                    var action = $(item).attr('rel').split(' #')[1];
                    var defaultValue = $(item).attr('onclick') == undefined ? "" : $(item).attr('onclick');
                    $(item).attr('onclick', action + defaultValue);
                }
            });

            $('.nav-hotsite a').bind('click', function(e) {
                e.preventDefault();

                if (!$(e.currentTarget).hasClass('disable-nav')) window.open($(e.currentTarget).attr('href'));

                if (tracking.specialMode) {
                    //Atendimento MCMV
                    tracking.funnel.steps01.call('65756');
                    tracking.google.call('gfL_CJur2gEQ0ZmG6wM');

                } else {
                    //Atendimento normal
                    tracking.funnel.steps01.call('65749');
                    tracking.google.call('XG53CLGotQEQ0ZmG6wM', this);
                }

            });
        });

        $('.property-contact-form .nav-send').bind('click', propDetails.sendForm);

        //Ajuste que pega a cor da div e seta em um eventual link no meio do texto
        $('.banner > div a').css('color', $('.banner > div').css('color'))

        $('.property-phase .gallery ul li a').lightBox(structure.modalGalleries.defaults);
        $('.property-phase .gallery > a').bind('click', function() {

            if (structure.currentState == 'sp') {
                tracking.funnel.steps01.call('65754');
                tracking.google.call('MAIWCIOu2gEQ0ZmG6wM');
            }

            $('.property-phase .gallery ul li a:first').trigger('click');
            return false;
        });

        if ($('.property-neighborhood .box-see-photos').length == 0) $('.property-neighborhood .box-description').addClass('full-mode');

        $('.property-neighborhood .box-see-photos ul li a').lightBox(structure.modalGalleries.defaults);
        $('.property-neighborhood .box-see-photos > a').bind('click', function() {
            $('.property-neighborhood .box-see-photos ul li a:first').trigger('click');
            return false;
        });

        $('.list-thumbs li a.nav-thumb').lightBox(structure.modalGalleries.defaults);
        $.each($('.list-thumbs li'), function(i, e) {
            $(this).find('.nav-metrics').bind('click', function() {
                $(this).find('.nav-thumb').trigger('click');
                return false;
            });
        });

        $('.nav-options .disabled').click(function() {
            return false;
        }).animate({
            opacity: .5
        }, 0);

        $.each($('.nav-options li'), function(i, e) {
            if (!$(this).hasClass('disabled')) {
                $(this).find('a').neoHover();
                $(this).find('a').addClass('neo-hover');
            }
            else {
                $(this).find('a').addClass('disable-nav');
            }
        });

        if ($('div.property-channels li.talk-now').length == 1) {
            var data = $.trim($('div.property-channels li.talk-now').text());
            swfobject.embedSWF("/static/swf/nav-phone-property.swf", "flash-phone-property", "100%", "35", "10", "expressInstall.swf", { 'txtPhone': data }, { 'wmode': 'transparent' }, {});
        }

        $('.property-factsheet dd:last, .property-phase .detailed-status dd:last').addClass('last');

        $.each($('.property-planes ul.list-thumbs'), function(i, i_item) {
            var li_list = $(i_item).find('li');
            var count = 0;

            $.each(li_list, function(j, j_item) {
                count++;
                var li = $(j_item);
                if (count == 5) {
                    li.addClass('last')
                    count = 0;
                }
            });
        });
    },

    insert3dVideo: function(){
        if ($('.property-plane3d #unity-video-holder').length == 1) {
            var guid = $.trim($('.property-plane3d #unity-video-holder').text());
            var content = SWFEngine.create(structure.baseUrl + 'static/swf/unity-video-holder.swf', '760', '475', 10);
            
            content.flashVars = {
                videoURL: structure.baseUrl + 'static/swf/unity-video.flv',
                thumbURL: structure.baseUrl + 'static/swf/unity-video-print.jpg',
                projectURL: structure.baseUrl + 'ajax/planta.aspx?guid=' + guid
            };

            content.params = {
                allowFullscreen: "true",
                allowScriptAccess: "always",
                wmode: "opaque"
            };

            content.minWidth = 760;
            content.minHeight = 475;
            content.write('unity-video-holder');
        }
    },

    sendForm: function(e) {
        e.preventDefault();

        $('.msg-status').removeClass('success').removeClass('error');

        var notValid = $('.node-name input').val() == '' || $('.node-email input').val() == '' || $('.node-message textarea').val() == '';

        if (notValid) {
            $('.msg-status').addClass('error').text('Preencha todos os campos corretamente.');
            return;
        }

        $.ajax({
            type: 'post',
            url: '/ajax/maisInformacoes.aspx',
            data: {
                to: $('#hdn-email').val(),
                nome: $('.node-name input').val(),
                email: $('.node-email input').val(),
                mensagem: $('.node-message textarea').val(),
                urlImovel: document.location.href,
                nomeImovel: $('#hdn-name').val(),
                estado: structure.currentState
            },
            success: function(e) {
                $('.msg-status').removeClass('error').addClass('success').text('Mensagem enviada com sucesso.');
                $('.node-name input').val('');
                $('.node-email input').val('');
                $('.node-message textarea').val('');
                
                var url = document.location.pathname;
				url = url.replace(/SP/g, '');
				url = url.replace(/imoveis/g, '');
				if(url == "///marquesbusinesscenter"){
					$('body').append($('<img/>').attr('src', 'http://tracking.hotwords.com.br/tracking.jsp?&networkId=BR&id=9214').addClass('img-tracking'));
				}
            }
        });
    },

    slideToMap: function() {
        $('html, body').animate({ scrollTop: $(".property-map").offset().top }, 1000);

        if (structure.currentState == 'sp' && tracking.specialMode == true) {
            //Tagueamento para Minha Casa Minha Vida
            tracking.funnel.steps01.call('65759');

        } else if (structure.currentState == 'sp') {
            //Tagueamento normal
            tracking.funnel.steps01.call('65755');
            tracking.google.call('HCbiCPuu2gEQ0ZmG6wM');
        }
    }
}

$(document).ready(propDetails.init);

var buttonViewMap = propDetails.slideToMap;
