/**
 * jQuery.labelify - Display in-textbox hints
 * Stuart Langridge, http://www.kryogenix.org/
 * Released into the public domain
 * Date: 25th June 2008
 * @author Stuart Langridge
 * @version 1.3
 */
jQuery.fn.labelify = function(settings) {
  settings = jQuery.extend({
    text: "title",
    labelledClass: ""
  }, settings);
  var lookups = {
    title: function(input) {
      return jQuery(input).attr("title");
    },
    label: function(input) {
      return jQuery("label[for=" + input.id +"]").text();
    }
  };
  var lookup;
  var jQuery_labellified_elements = jQuery(this);
  return jQuery(this).each(function() {
    if (typeof settings.text === "string") {
      lookup = lookups[settings.text]; // what if not there?
    } else {
      lookup = settings.text; // what if not a fn?
    };
    // bail if lookup isn't a function or if it returns undefined
    if (typeof lookup !== "function") { return; }
    var lookupval = lookup(this);
    if (!lookupval) { return; }

    // need to strip newlines because the browser strips them
    // if you set textbox.value to a string containing them    
    jQuery(this).data("label",lookup(this).replace(/\n/g,''));
    jQuery(this).focus(function() {
      if (this.value === jQuery(this).data("label")) {
        this.value = this.defaultValue;
        jQuery(this).removeClass(settings.labelledClass);
      }
    }).blur(function(){
      if (this.value === this.defaultValue) {
        this.value = jQuery(this).data("label");
        jQuery(this).addClass(settings.labelledClass);
      }
    });
    
    var removeValuesOnExit = function() {
      jQuery_labellified_elements.each(function(){
        if (this.value === jQuery(this).data("label")) {
          this.value = this.defaultValue;
          jQuery(this).removeClass(settings.labelledClass);
        }
      })
    };
    
    jQuery(this).parents("form").submit(removeValuesOnExit);
    jQuery(window).unload(removeValuesOnExit);
    
    if (this.value !== this.defaultValue) {
      // user already started typing; don't overwrite their work!
      return;
    }
    // actually set the value
    this.value = jQuery(this).data("label");
    jQuery(this).addClass(settings.labelledClass);

  });
};

$(document).ready(function () {
    //LISTAS CUSTOM WEBPART ZONE
    $('.SimpleContentList li.pacote:last').css('display', 'none');

    //LIMPA CAMPOS
    $("input.clean").labelify({ text: "label" });

    //CAIXA LOGIN	
    $('.loginBox a').click(function () {
        var $this = $(this);
        if ($this.hasClass('fechado')) {
            //Fecha caixa Pesquisa
            if ($('.searchBox a').hasClass('aberto')) {
                $('.searchBox a').next().slideUp('fast');
                $('.searchBox a').removeClass('aberto');
                $('.searchBox a').addClass('fechado');
                $('#searchStatus').attr('src', $('#searchStatus').attr("src").replace('_on', '_off'))
            }

            $this.next().slideDown('fast');
            $this.removeClass('fechado');
            $this.addClass('aberto');
            $('#loginStatus').attr('src', $('#loginStatus').attr("src").replace('arrow_down', 'close'))

            return false;
        } else if ($this.hasClass('aberto')) {
            $this.next().slideUp('fast');
            $this.removeClass('aberto');
            $this.addClass('fechado');
            $('#loginStatus').attr('src', $('#loginStatus').attr("src").replace('close', 'arrow_down'))
            return false;
        }
    });

    //CAIXA PESQUISA
    $('.searchBox a').click(function () {
        var $this = $(this);
        if ($this.hasClass('fechado')) {
            //Fecha caixa Login
            if ($('.loginBox a').hasClass('aberto')) {
                $('.loginBox a').next().slideUp('fast');
                $('.loginBox a').removeClass('aberto');
                $('.loginBox a').addClass('fechado');
                $('#loginStatus').attr('src', $('#loginStatus').attr("src").replace('close', 'arrow_down'))																//PREVINE O CLICK DO <A HREF>
            }

            $this.next().slideDown('fast');
            $this.removeClass('fechado');
            $this.addClass('aberto');
            $('#searchStatus').attr('src', $('#searchStatus').attr("src").replace('_off', '_on'))

            return false;
        } else if ($this.hasClass('aberto')) {
            $this.next().slideUp('fast');
            $this.removeClass('aberto');
            $this.addClass('fechado');
            $('#searchStatus').attr('src', $('#searchStatus').attr("src").replace('_on', '_off'))
            return false;
        }
    });

    //Abre  caixas de conteudos (HOMEPAGE)
    if ($('.titleBox').length) {
        $('.titleBox').each(function () {
            var $this = $(this);
            $this.click(function () {
                if ($this.hasClass('fechado')) {
                    //Fecha caixa que estiver aberta
                    $('.titleBox').each(function () {
                        if ($this.hasClass('aberto')) {
                            $this.next().slideUp('fast');
                            $this.removeClass('aberto');
                            $this.addClass('fechado');
                        }
                    });

                    $this.next().slideDown('fast');
                    $this.removeClass('fechado');
                    $this.addClass('aberto');
                    return false;
                } else if ($(this).hasClass('aberto')) {
                    $this.next().slideUp('fast');
                    $this.removeClass('aberto');
                    $this.addClass('fechado');
                    return false;
                }
            });
        }).css("cursor", "pointer");
    }

    //Abre  caixas Pacotes 
    if ($('.packageBox .packageTitleBox').length) {
        $('.packageBox .packageTitleBox').each(function () {
            $(this).click(function () {
                if ($(this).hasClass('fechado')) {
                    $(this).next().slideDown('fast');
                    $(this).removeClass('fechado');
                    $(this).addClass('aberto');
                    return false;
                } else if ($(this).hasClass('aberto')) {
                    $(this).next().slideUp('fast');
                    $(this).removeClass('aberto');
                    $(this).addClass('fechado');
                    return false;
                }
            });
        });
    }
    
    


    //Abre  caixas Pacotes 
    if ($('a.box7').length) {
        $('a.box7').each(function () {
            $(this).click(function () {


                if ($(this).hasClass('fechado')) {
                    $('.box7HiddenBox').slideUp('normal');
                    $('a.box7.aberto').addClass('fechado');
                    $('a.box7.fechado').removeClass('aberto');

                    $(this).next('.box7HiddenBox').slideDown('fast');
                    $(this).removeClass('fechado');
                    $(this).addClass('aberto');
                    return false;
                } else if ($(this).hasClass('aberto')) {
                    $('.box7HiddenBox').slideUp('normal');
                    $('a.box7.aberto').addClass('fechado');
                    $('a.box7.fechado').removeClass('aberto');

                    $(this).next('.box7HiddenBox').slideUp('fast');
                    $(this).removeClass('aberto');
                    $(this).addClass('fechado');
                    return false;
                }
            });
        });
    }


    $("#searchSubmit").hover(
	  function () {
	      $(this).attr('src', $('#searchSubmit').attr("src").replace('_ok', '_ok2'))
	  },
	  function () {
	      $(this).attr('src', $('#searchSubmit').attr("src").replace('_ok2', '_ok'))
	  }
	);

    if ($('.tabs_content').length) {


        var tab = getParameterByName('categ');
        if (tab == null)
            tab = 'cat1';

        openTab(tab);
        hideTabs(1);

        var faqID = getParameterByName('faq');
        if (faqID != null)
            openFaqByID(faqID);
    }

});

function hideTabs(tabInicial) {
    if (tabHeaderSize('cat' + tabInicial) == 0) {
        $('div#tabs > .tabs_btns #cat' + tabInicial).hide();
        $('div#tabs > .tabbedWindow #cat' + tabInicial).hide();
    }
    tabInicial = tabInicial + 1;
    if (tabInicial <= 3)
        hideTabs(tabInicial);
}

function openTab(tab) {
    if (tabHeaderSize(tab) > 0) {
        openTabByID(tab);
    }
    else {
        openRecursiveTab(1);
    }
}

function openRecursiveTab(idx) {
    if (idx <= 3) {
        if (tabHeaderSize('cat' + idx) > 0) {
            openTabByID('cat' + idx);
        } else {
            openRecursiveTab(idx + 1);
        }
    }
}

function tabHeaderSize(tabId) {
    var Containers = $('div#tabs > .tabs_btns #' + tabId + ' span');
    if (Containers.html() != null) {
        var size = Containers.html().replace('&nbsp;', '').trim().length;
        return size;
    }
    return 0;
}

function openTabByID(tab) {
    var tabContainers = $('div#tabs > .tabbedWindow');
    tabContainers.hide().filter('#' + tab).show();
    $('div#tabs ul.tabs_btns a').click(function () {
        tabContainers.hide();
        tabContainers.filter(this.hash).show();
        $('div#tabs ul.tabs_btns a').removeClass('active');
        $(this).addClass('active');
        return false;
    }).filter('#' + tab).click();
}

function openFaqByID(FaqID){
    var destination = $('#' + FaqID).offset().top;
   $("html:not(:animated),body:not(:animated)").animate({scrollTop: destination - $('#' + FaqID).height() - 20}, 700, 'swing', function(){
		$('#' + FaqID).next().slideDown('fast');					
		$('#' + FaqID).removeClass('fechado');						
		$('#' + FaqID).addClass('aberto');			   
   });
}

function getParameterByName(name) {    
	var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);    
	return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

// SIMULADOR

function validateFormula(_x, _y) {
        var _somXY = parseInt(_x) + parseInt(_y);
      
        if (_x > 10)
            return false;
        if (_y > 10)
            return false;
        if ((3 <= _somXY && _somXY <= 10) == false)
            return false;

        return true;
    }

    // Generico

    function equalHeight(group) {
        var tallest = 0;
        group.each(function () {
            var thisHeight = $(this).height();
            if (thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        group.height(tallest);
    }
