$('document').ready(function(){
	/* HomeHighLight *************************************** */
    $('#destaquesHome li:last').addClass('last');
    $('.setFinal li:last').addClass('last');
    /* *************************************** */

    /* Menu *************************************** */
    $('#nav li.sub').hover(function () {
        var w = $('#nav li.sub').width();
        $('#nav li.sub div').width(w);
        $(this).addClass('subOn');
    },
	function () {
	    $(this).removeClass('subOn');
	});
    $("#nav > li:not('.sub','.active')").hover(function () {
        $(this).addClass('active');
    },
	function () {
	    $(this).removeClass('active');
	});
    /* *************************************** */

    /* Abas (********************************* */
    $('.boxSobre .abas li').click(function () {
        $('.boxSobre .abas li').removeClass('active');
        $('.boxSobre .contAbas ul li').removeClass('active');
        $(this).addClass('active');
        $($(this).find('a').attr('href')).addClass('active');
        return false;
    });
    $('.especificacoes .abas li').click(function () {
        $('.especificacoes .abas li').removeClass('active');
        $('.boxespecificacoes .cont .Caba').removeClass('active');
        $(this).addClass('active');
        $($(this).find('a').attr('href')).addClass('active');
        return false;
    });
    /* *************************************** */

    /* Expansive ********************************** */
    $('.expansive dt').click(function () {
        var i = $('.expansive dt').index($(this));
        $(this).parent().find('dt:not(:eq(' + i + ')),dd:not(:eq(' + i + '))').removeClass('active');
        $(this).toggleClass('active');
        $(this).next('dd').toggleClass('active');
    });
    $('.expansive dt:last').addClass('last');
    /* ******************************************** */

    /* Forms */
    $('.frmTxt').each(function () {
        var w = $(this).width();
        $(this).find('input').width(w);
        $(this).find('img').width(w);
    });

    if ((!("pointerEvents" in document.body.style) && /ie(7|8)/.test(document.documentElement.className)) || (!!("attachEvent" in document.body) && !!("pointerEvents" in document.body.style))) {
        document.documentElement.className += " no-pointer-event";
        $(".form-custom-select").each(function () {
            var $button = $("<span class='button' />"),
				$text = $("<span class='text' />"),
				$select = $(this).children("select");
            $(this).prepend(
				$button,
				$text
			);
            $text.text(($select.children(":selected")[0] ? $select.children(":selected") : $select.children(":first")).text());
            $select.change(function () {
                $text.text($(this).children("option:selected").text());
            });
        });
    };

    /* Geral ********************************* */
    //Links Externos
    $('a[rel="external"]').click(function () {
        window.open($(this).attr('href'));
        return false;
    });
    /* *************************************** */

	//Modais vídeo e imagem
	if($.fn.lightBox){
		$(".gallery a:not('.video')").lightBox();
	}
	$(".gallery .video").click(function(event){
		var idModal = $(this).attr('href');
		$(".modalVid" + idModal).jqmShow();
		event.preventDefault(); //cancela a função default
	});

	$(".modalAssist").click(function(event){
		$("#modalAssist").jqmShow();
		event.preventDefault();
	});
	
	//para abrir o modal automaticamente via url = ?open=n_BGdTXYfTY
	var nomeVid = gup("open");
	if(nomeVid != null){
		$(".modalVid" + nomeVid).jqmShow();
	}
	/* *************************************** */

	if ($.fn.carousel){
		$(".carousel-overflow").carousel({
            circular: false,
            visible: 4,
            start: 0,
            speed: 1000,
            scroll: 4
        });
    }
});

// captura parametro na url
function gup(name) {
	name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
	var regexS = "[\?&]" + name + "=([^&]*)";
	var regex = new RegExp(regexS);
	var results = regex.exec(window.location.href);
	if (results == null)
		return "";
	else
		return results[1];
}

$(".cont_regr_caracteres").keyup(function() {
	var $self = $(this),
		isTextarea = $self[0].tagName.toLowerCase() === "textarea",
		max = 1000;
	$("span.cont_textarea").text((max - $self.val().length));
	if (isTextarea && (max < $self.val().length)) {
		$self.val($self.val().substring(0, max));
		$("span.cont_textarea").text("0");
	}
}).trigger("keyup");

$(window).load(function(){
	if ($.fn.jScrollPane){
		$('.scroll').jScrollPane({
			verticalDragMinHeight: 115,
			verticalDragMaxHeight: 115,
			horizontalDragMinWidth: 14,
			horizontalDragMaxWidth: 14
		});
	};
});

function abreJanela(URL){
  var width = 448;
  var height = 350;
  var left = 150;
  var top = 100;
  window.open(URL,'janela', 'width='+width+', height='+height+', top='+top+', left='+left+', scrollbars=no, status=no, toolbar=no, location=no, directories=no, menubar=no, resizable=no, fullscreen=no');
}


/* jqModal - Minimalist Modaling with jQuery * (http://dev.iceburg.net/jquery/jqModal/) * $Version: 03/01/2009 +r14 */
(function($){
	$.fn.jqm = function (o) {
        var defaults = {
            overlay			: 60,
            overlayClass	: "jqmOverlay",
            closeClass		: "jqmClose",
            trigger			: ".jqModal",
            ajax			: null,
            ajaxText		: "",
            target			: null,
            modal			: null,
            toTop			: null,
            onShow			: null,
            onHide			: null,
            onLoad			: null
        };
        return this.each(function () {
            if (this._jqm) {
            	return $.jqm.hash[this._jqm].c = $.extend({}, $.jqm.hash[this._jqm].c, o);
            };
            s++;
            this._jqm = s;
            $.jqm.hash[s] = {
                c		: $.extend(defaults, $.jqm.params, o),
                a		: null,
                w		: $(this).addClass("jqmID" + s),
                s		: s
            };
            defaults.trigger && $(this).jqmAddTrigger(defaults.trigger);
        });
    };
    $.fn.jqmAddClose = function (e) {
        return hs(this, e, "jqmHide");
    };
    $.fn.jqmAddTrigger = function (e) {
        return hs(this, e, 'jqmShow');
    };
    $.fn.jqmShow = function (t) {
        return this.each(function () {
            t = t || window.event;
            $.jqm.open(this._jqm, t);
        });
    };
    $.fn.jqmHide = function (t) {
        return this.each(function () {
            t = t || window.event;
            $.jqm.close(this._jqm, t)
        });
    };
    $.jqm = {
        hash: {},
        open: function (s, t) {
        	var h 		= $.jqm.hash[s], 								// Hash
                c 		= h.c,											// Object config.
                cc 		= '.' + c.closeClass,							// Classe que fecha o modal
                z 		= h.w.css("z-index") !== "auto"? z : 3000,		// Caso tenha o z
                o 		= $("<div />").css({
	                	zIndex		: z - 1,
	                    opacity		: (c.overlay/100)
                });
            if (h.a) {
            	return false;
            };
            h.t = t; // Define o elemento que chamou o modal.
            h.a = true;
            h.w.css("z-index", z);
            if (c.modal) {
                if (!A[0]) L('bind');
                A.push(s);
            } else if (c.overlay > 0) {// Se tiver overlay
            	h.w.jqmAddClose(o);
            } else {// Deleta o overlay
            	o = false;
            };
			// Caso exista overlay adiciona no topo do body com sua respectiva classe.
            h.o = (o) && o.addClass(c.overlayClass).prependTo("form");
            // Caso for IE6 define o overlay como absoluto.
            if (ie6) {
                if (o) {
                    o = o.css({
                        position: "absolute"
                    })[0];
                    for (var y in {
                        Top		: 1,
                        Left	: 1
                    }) {
                    	//o.style.setExpression(y.toLowerCase(), "(_=(document.documentElement.scroll" + y + "))+'px'");
                    };
                };
            };
			// Caso tenha ajax
            if (c.ajax) {
            	var r 		= c.target || h.w, // Local onde irá adicionar o html.
                    url 	= c.ajax, // URL do ajax.
                    r 		= (typeof r == 'string') ? $(r, h.w) : $(r), // Retorno um objeto jQuery com o elemento.
                    u 		= (url.substr(0, 1) == '@') ? $(t).attr(url.substring(1)) : url; // Usa o valor do atributo como URL
                r
                	.html(c.ajaxText) // Define o texto que irá aparecer enquanto carrega o conteúdo do ajax
                	.load(url, function() {
	                    c.onLoad && c.onLoad.call(this, h); // Chama o onload.
	                    cc && h.w.jqmAddClose($(cc, h.w)); // Seta a função que fecha o modal.
	                    e(h); // Adiciona o focus no primeiro input visível. Caso for o ie6 define o iframe.
                	});
            } else if (cc) {
            	h.w.jqmAddClose($(cc, h.w)); // Seta a função que fecha o modal.
            };
            if (c.toTop && h.o) h.w.before('<span id="jqmP' + h.w[0]._jqm + '"></span>').insertAfter(h.o);
            (c.onShow) ? c.onShow(h) : h.w.show();
            e(h); // Insere o iframe caso for ie6.
            return false;
        },
        close: function (s) {
            var hash = $.jqm.hash[s];
            if (!hash.a) {
            	return false;
            };
            hash.a = false;
            if (A[0]) {
                A.pop();
                if (!A[0]) L('unbind');
            };
            if (hash.c.toTop && hash.o) {
            	$('#jqmP' + hash.w[0]._jqm).after(hash.w).remove();
            };
            if (hash.c.onHide) {
            	hash.c.onHide(hash);
            } else {
                hash.w.hide();
                hash.o && hash.o.remove();
            };
            return false;
        },
        params: {}
    };
    var s = 0,
        //H = $.jqm.hash,
        A = [],
        ie6 = $.browser.msie && ($.browser.version == "6.0"),
        //F = false,
        i = $('<iframe src="javascript:false;document.write(\'\');" class="jqm"></iframe>').css({
            opacity: 0
        }),
        e = function (h) {
            if (ie6) {
            	if (h.o) {
            		h.o.html('<p style="width:100%;height:100%"/>').prepend(i);
            	} else if (!$('iframe.jqm', h.w)[0]) {
            		h.w.prepend(i);
            	};
            };
            f(h);
        },
        f = function (h) {
            try {
                $(":input:visible", h.w)[0].focus();
            } catch (_) {}
        },
        L = function (t) {
            $()[t]("keypress", m)[t]("keydown", m)[t]("mousedown", m);
        },
        m = function (e) {
            var h = $.jqm.hash[A[A.length - 1]],
                r = (!$(e.target).parents('.jqmID' + h.s)[0]);
            if (r) f(h);
            return !r;
        },
        hs = function (w, t, c) {
            return w.each(function () {
                var s = this._jqm;
                $(t).each(function () {
                    if (!this[c]) {
                        this[c] = [];
                        $(this).click(function () {
                            for (var i in {
                                jqmShow: 1,
                                jqmHide: 1
                            }) for (var s in this[i]) if ($.jqm.hash[this[i][s]]) $.jqm.hash[this[i][s]].w[i](this);
                            return false;
                        });
                    };
                    this[c].push(s);
                });
            });
        };
    /* Extras */
    $(".jqmWindow").jqm({
		onShow: initModal,
		onHide: exitModal
	});
    function initModal(hash){
		var winWidth 		= $(window).width(),
			winHeight 		= $(window).height(),
			modalWidth 		= hash.w.width(),
			modalHeight 	= hash.w.height(),
			winScroll 		= $(window).scrollTop(),
			left 			= (modalWidth > winWidth) ? 0 : ((winWidth - modalWidth) /2),
			top 			= winScroll + ((modalHeight > winHeight) ? 50 : ((winHeight - modalHeight) / 2));
			
		hash.w
			.css({
				top 		: top,
				left 		: left
			})
			.fadeIn();
	};
	// FUNÇÕES P/ FINALIZAR MODAL
	function exitModal(hash){
		hash.o.hide().remove();
		hash.w.hide();
	};
	/* // Extras */
})(jQuery)

