// VALIDATORS
function CheckValidators(groupID,cssClass,cssEscondeError)
{
    Page_ClientValidate(groupID);
    var controlsAlreadyValidated = new Array();
    for (var i = 0; i < Page_Validators.length; i++) {
          var control = Page_Validators[i].controltovalidate;
          if($.inArray(control, controlsAlreadyValidated) == -1) 
          {
                if(!Page_Validators[i].isvalid){
                    ChangeControlStyle(control,Page_Validators[i].isvalid,cssClass,cssEscondeError);
                    controlsAlreadyValidated.push(control);
                }
          }
    }
}

function ElementValidator(element, groupID,cssClass,cssEscondeError)
{
    Page_ClientValidate(groupID);
    var controlsAlreadyValidated = new Array();
    for (var i = 0; i < Page_Validators.length; i++) {
          var control = Page_Validators[i].controltovalidate;
          if($.inArray(control, controlsAlreadyValidated) == -1) 
          {
                if(element == control)
                {
                    ChangeControlStyle(control,Page_Validators[i].isvalid,cssClass,cssEscondeError);
                }
                
                if(!Page_Validators[i].isvalid){
                      controlsAlreadyValidated.push(control);
                }
          }
    }
}

function ChangeControlStyle(id, isvalid, cssClass,cssEscondeError)
{    
    if(!isvalid)
    {
        $('#'+id).parent().removeClass("separador-sucesso");
        $('#'+id).parent().addClass(cssClass);
        //$('#'+id).next().css("display","block");
        if(cssEscondeError != null)
        {
            $('.'+cssEscondeError).css("display","block");
            $('.msg-status-server').css("display","none");
        }
    }
    else
    {
        $('#'+id).parent().removeClass(cssClass);
        $('#'+id).parent().addClass("separador-sucesso");
    }
}


/*****************************************************/

/* AddThis */
var addthis_config = {
   ui_click: true
}


jQuery.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);
}

/* EMBED - used by TinyMCE */

function writeFlash(p) {
	writeEmbed(
		'D27CDB6E-AE6D-11cf-96B8-444553540000',
		'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0',
		'application/x-shockwave-flash',
		p
	);
}

function writeShockWave(p) {
	writeEmbed(
	'166B1BCA-3F9C-11CF-8075-444553540000',
	'http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,1,0',
	'application/x-director',
		p
	);
}

function writeQuickTime(p) {
	writeEmbed(
		'02BF25D5-8C17-4B23-BC80-D3488ABDDC6B',
		'http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0',
		'video/quicktime',
		p
	);
}

function writeRealMedia(p) {
	writeEmbed(
		'CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA',
		'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0',
		'audio/x-pn-realaudio-plugin',
		p
	);
}

function writeWindowsMedia(p) {
	p.url = p.src;
	writeEmbed(
		'6BF52A52-394A-11D3-B153-00C04F79FAA6',
		'http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701',
		'application/x-mplayer2',
		p
	);
}

function writeEmbed(cls, cb, mt, p) {
	var h = '', n;

	h += '<object classid="clsid:' + cls + '" codebase="' + cb + '"';
	h += typeof(p.id) != "undefined" ? 'id="' + p.id + '"' : '';
	h += typeof(p.name) != "undefined" ? 'name="' + p.name + '"' : '';
	h += typeof(p.width) != "undefined" ? 'width="' + p.width + '"' : '';
	h += typeof(p.height) != "undefined" ? 'height="' + p.height + '"' : '';
	h += typeof(p.align) != "undefined" ? 'align="' + p.align + '"' : '';
	h += '>';

	for (n in p)
		h += '<param name="' + n + '" value="' + p[n] + '">';

	h += '<embed type="' + mt + '"';

	for (n in p)
		h += n + '="' + p[n] + '" ';

	h += '></embed></object>';

	document.write(h);
}


/* end EMBED - used by TinyMCE */

function fixHeights()
{
    $('.RightColumn .Wrapper').height('auto');
    $('.MainContent').css('min-height', $('.RightColumn .Wrapper').height());
    if($('.RightColumn .Wrapper').height() < $('.MainContent').innerHeight())
    {
        $('.RightColumn .Wrapper').height(-1 * $('.RightColumn .Wrapper').position().top + $('.MainContent').innerHeight());
    };
}

$(document).ready(function() {    
	
	$('input.clearInput').resetDefaultValue();
	
	/* SHADOWBOX INIT */
    Shadowbox.init({
        modal: false
    });
	
	if($('#flip').length)
	{
	    //Para mais exemplos e onde se encontra o js actualizado com o fix ja implementado 
        //e tambem a função wrapItemsAround, visitar este link https://github.com/NewSignature/jcoverflip
        // Init
        jQuery('#flip').jcoverflip({
            'current': 1,
            'time': 500,
            beforeCss: function(el,container,offset){
                return [
                    $.jcoverflip.animationElement( el, { left: ( container.width( )/2 - 180 - 228*offset - 228 -5 )+'px', top: '39px' }, { } ),
                    $.jcoverflip.animationElement( el.find( 'img' ), { opacity: 1, width: '212px', height: '163px' }, {} )
                ];
            },
            afterCss: function(el,container,offset){
                return [
                    $.jcoverflip.animationElement( el, { left: ( container.width( )/2 + 228*offset + 180 + 5 )+'px', top: '39px' }, { } ),
                    $.jcoverflip.animationElement( el.find( 'img' ), { opacity: 1, width: '212px', height: '163px' }, {} )
                ];
            },
            currentCss: function(el,container){
                return [
                    $.jcoverflip.animationElement( el, { left: ( container.width( )/2 - 180 )+'px', top: 3 }, { } ),
                    $.jcoverflip.animationElement( el.find( 'img' ), { opacity: 1, width: '344px', height: '223px' }, {} )
                ];
            }
        });
          
        // Next/Previous
        jQuery( '#t-previous' ).click( function( ){
            jQuery( '#flip' ).jcoverflip('previous');
        });

        jQuery('#t-next').click( function( ){
            jQuery('#flip').jcoverflip('next');
        });
	}
	
	$('.Projectos .detail').hide();
    $('.Projectos .Item tbody > tr').click(function(){
        $(this).next('.detail').toggle();
        $(this).find('.Expandir a').toggleClass('abrir').toggleClass('fechar');
        fixHeights();
        return false;
    });
	    
    $('.SubMenuWrapper').each(function(){
        var MaxWidth = 450;
        var FullWidth = 0;
        $(this).children('ul').each(function(){
            var MaxHeight = 0;
            $(this).children('li').each(function(){
                if($(this).innerHeight() > MaxHeight)
                {
                    MaxHeight = $(this).innerHeight();
                }                
            });
            $(this).height(MaxHeight);
            if (FullWidth < $(this).width() && $(this).width() < MaxWidth)
            {
                FullWidth = $(this).width() * $(this).children().length;
            }
            else
            {
                FullWidth = MaxWidth;
            }
        });
        $(this).width(FullWidth);
        
        if($(this).offset().left - $('#menu').offset().left > $('#menu').width() - FullWidth)
        {
            $(this).css('right','-1px');
            $(this).css('border-radius','5px 0 5px 5px');
        }
    });
    
    $('.FormCancelar').hide();
    $('.CancelarNewsletter').click(function(){
        $(this).parent().next().toggle();
        $('.HideOnCancel').toggle();
        $(this).parent().toggle();
        return false;
    });
    
    /* Faqs */
    if ($('.FaqLista').length != 0) {
        $('.resposta').hide();
        
        $('.FaqLista h3.pergunta').click(function() {            
		    $(this).next().toggle();
		    $(this).toggleClass("clicktoclose");
		    fixHeights();
		    return false;
	    });	    
	    $('.FaqLista h3 .ToOpen').click(function() {
	        $(this).parent().toggleClass("clicktoclose");
	        $(this).parent().next().find('h3').addClass("clicktoclose");
	        $(this).parent().next().find('.resposta').show().addClass("clicktoclose");;
	        fixHeights();
	    });
	    $('.FaqLista h3 .ToClose').click(function() {
	        $(this).parent().toggleClass("clicktoclose");
	        $(this).parent().next().find('h3').removeClass("clicktoclose");
	        $(this).parent().next().find('.resposta').hide();
	        fixHeights();
	    });
    };
    if('.HomepageArea'.length)
    {
        $('.HomepageArea .Texto').hide();
        $('.HomepageArea .esconder').hide();
        
        $('.HomepageArea .vermais').click(function(){
            $(this).hide();
            $('.Texto').show();
            $('.HomepageArea .esconder').show();
            fixHeights();
            return false;
        });
        $('.HomepageArea .esconder').click(function(){
            $(this).hide();
            $('.Texto').hide();
            $('.HomepageArea .vermais').show();
            fixHeights();
            return false;
        });
    }
    $(window).resize(function() {
        
	});
	
	fixHeights();
});
