//Scrips
window.addEvent('domready', function(){

});//winload

//pag empresas
function clearSelected(){
	var el = $$('#empresas_nav td');
    el.each(function(){			
		el.removeClass('empresasNavSelected');
		});	
}

function closeAllActividade(){		
		var actividade = $$('.actividade');	
		actividade.each(function(el){
				if (el.sld){				 
				el.sld.slideOut().chain(function(){$('indice').sld.slideIn();});				 
				}
			 });
}
							

function pagEmpresas(seccao){		
    if ($('page_empresas') || $('page_particulares')) {
		$('indice').sld = new Fx.Slide($('indice'),{duration:250});
		var actividade = $$('.actividade');	
		var toOpen = false;
		if ($('actividade-wrapper').className != ''){
			toOpen = $('actividade-wrapper').className;
			$('actividade-wrapper').removeClass(toOpen);
		};
		actividade.each(function(el){
			//criar os slides					 
            el.sld = new Fx.Slide(el, {
                duration: 1000,
				mode:'horizontal'
            }).hide();
			if (el.id == toOpen) {$('indice').sld.slideOut().chain(function(){el.sld.slideIn()})};
			//criar os backbuttons
			var butBack = new Element('a',{'class' :'act-back'}).addEvent('click',function(){
																						   el.sld.slideOut().chain(function(){$('indice').sld.slideIn();});

																						   }).setText(' Fechar').injectTop(el);
	
			//criar os items do indice
			var sDiv = new Element('div',
								   {'class':'indiceItem',
									'events':{'click':function(){										
										$('indice').sld.slideOut().chain(function(){el.sld.toggle()});
										}
									}
								   }); 			
			var i = el.getElements('div.act-img')[0];
			var h = el.getElements('h3')[0];
			
			h.clone().addClass('indiceToggler').inject(sDiv);
			i.clone().addClass('indiceImg').inject(sDiv);
			sDiv.inject($('indice'));			
							 });
		
		var fb = new Element('div',{'class':'float-breaker'});
		fb.setText(" ");
		fb.injectInside($('indice'));
			
        var secc = $$('.dynamic_div');
        secc.each(function(el){
            el.sld = new Fx.Slide(el, {
                duration: 1000
            }).hide();
        });
        var seccTogg = $$('#empresas_nav td');
        var c = 1;
        seccTogg.each(function(elTog, c){
			if (c == seccao-1) elTog.addClass('empresasNavSelected');			
            elTog.addEvent('click', function(){
				closeAllActividade();							 
				clearSelected();
                var secc = $$('.dynamic_div');
                secc.each(function(el){
                    var divId = el.id.substr(16).toInt();
                    if (c != divId) {
                        el.sld.slideOut();												
                    }
                    else {						
                        el.sld.slideIn.delay(900,el.sld);
						elTog.addClass('empresasNavSelected');						
                    }
                });
            });
            c++;
        });	
	if ($('empresas_seccao_'+seccao)) $('empresas_seccao_'+seccao).sld.slideIn();			
    } //página empresas	
	
}

//console para ie7
if (!console) {
    var cons = new Class({
        log: function(v){
            //dummy func
        }
    })
    
    var console = new cons();
}


//Classe para fading de imagens	
var FadeSlideshow = new Class({

    options: {
        imgNames: new Array(), //array com os nomes das imagens
        duracao: 5000,
        espera: 15000,
        auto: true
    },
    
    initialize: function(contentor, options){
        this.setOptions(options);
        this.vDiv = 0;
        this.prox = 0;
        this.imgs = new Array();
        largMax = 0;
        altMax = 0;
        this.yin = new Element('div', {
            'id': 'yin',
            'class': 'divSld'
        });
        this.yang = new Element('div', {
            'id': 'yang',
            'class': 'divSld'
        });
        this.yang.injectBefore(contentor);
        this.yin.injectBefore(contentor);
        
        
        for (i = 0; i < this.options.imgNames.length; i++) {
            this.imgs[i] = new Element('img', {
                'src': this.options.imgNames[i],
                'class': 'sldItem'
            });
            if (this.imgs[i].getSize().size.x > largMax) 
                largMax = this.imgs[i].getSize().size.x;
            if (this.imgs[i].getSize().size.y > altMax) 
                altMax = this.imgs[i].getSize().size.y;
        };
        this.actual = Math.floor(Math.random() * i);
        this.setImg();
        if (this.options.auto) 
            this.start();
        
    },
    
    setImg: function(){
        if (this.actual > this.imgs.length - 1) 
            (this.actual = 0);
        this.prox = this.actual + 1;
        if (this.prox > this.imgs.length - 1) 
            (this.prox = 0);
        if (this.vDiv == 1) {
            this.imgs[this.actual].inject(this.yin);
            this.imgs[this.prox].inject(this.yang);
        }
        else {
            this.imgs[this.prox].inject(this.yin);
            this.imgs[this.actual].inject(this.yang);
        }
        
        this.toggle();
        this.actual++;
    },
    start: function(){
        this.setImg.periodical(this.options.espera, this);
    },
    toggle: function(){
        if (this.yin.fx) 
            this.yin.fx.stop();
        if (this.yang.fx) 
            this.yang.fx.stop();
        if (this.vDiv == 1) {
            this.yin.fx = this.yin.effect('opacity', {
                duration: this.options.duracao
            }).start(0);
            this.yang.fx = this.yang.effect('opacity', {
                duration: this.options.duracao
            }).start(1);
            this.vDiv = 0;
        }
        else {
            this.yin.fx = this.yin.effect('opacity', {
                duration: this.options.duracao
            }).start(1);
            this.yang.fx = this.yang.effect('opacity', {
                duration: this.options.duracao
            }).start(0);
            this.vDiv = 1;
        }
        
    }
    
});
FadeSlideshow.implement(new Options);


function shadowText(){
    var elems = $$('.shadow');
    elems.each(function(el){
        var clone = el.clone().injectAfter(el);
        var coord = el.getCoordinates();
        var clr = el.color;
        el.setStyle('color', '#999');
        clone.setStyles({
            position: 'relative',
            color: clr,
            top: -1 * coord.height - 2 + 'px',
            left: '-2px'
        });
    });
}


function popInfo(){
	//não usada
    var empTips = new Tips($$('.empTips'), {
        initialize: function(){
            this.fx = new Fx.Style(this.toolTip, 'opacity', {
                duration: 500,
                wait: false
            }).set(0);
        },
        onShow: function(toolTip){
            this.fx.start(1);
        },
        onHide: function(toolTip){
            this.fx.start(0);
        }
    });
    
}

function startTestemunhos(pagina){
	var d = new Date();
    var ajx = new Ajax(thisPath + '/testemunhos-ajx.php?tipo='+pagina, {
		data :'nada',			   
		method: 'post',
        update: $('testemunhos-cont')
    }).request();    
    testemunhosAjx.periodical(10000,this,pagina);
}

function testemunhosAjx(pagina){
	var d = new Date();
    var ajx = new Ajax(thisPath + '/testemunhos-ajx.php?tipo='+pagina, {
        method: 'POST',
        onComplete: function(){			
            updateTestemunhos(ajx.response.text);
        }
    }).request($time() + $random(0, 100));
}

function updateTestemunhos(testemunho){
    var flag = true;
    var fx = new Fx.Style($('testemunhos-cont'), 'opacity', {
        duration: 2000,
        transition: Fx.Transitions.Quad.easeOut,
        onComplete: function(){
            if (flag) {
                $('testemunhos-cont').setHTML(testemunho);
                this.start(0, 1);
                flag = false;
            }
            else 
                return true;
        }
    }).start(1, 0);
}

function startActividades(){
    var items = $$('.act-item');
    var sl = new actividadesAnim(items);
}


var actividadesAnim = new Class({
    elems: new Array(), //array com os items
    actualId: null,
    nextId: null,
    contentorPai: null,
    contentor: null,
    efx: null,
    actualCnt: 0,
    nextCnt: 0,
    altura: 0,
    topo: 0,
    options: {
        duracao: 2000,
        espera: 5000,
        auto: true
    },
    
    initialize: function(elementos, options){
        this.setOptions(options);
        this.elems = elementos;
        if (this.elems.length >= 2) {
            //s� funciona com mais de dois				
            this.contentorPai = this.elems[0].getParent();
            this.altura = this.elems[0].getCoordinates().height;
            this.topo = this.elems[0].getCoordinates().top;
            var bkg = this.contentorPai.getStyle('background-image');
            var bkgp = this.contentorPai.getStyle('background-position');
			if (!window.ie) this.contentorPai.empty();
            if (!bkgp) 
                bkgp = 0;
            this.contentor = new Element('div', {
                'styles': {
                    'position': 'relative',
                    'background-image': bkg,
                    'background-position': bkgp
                }
            });
            this.contentorPai.adopt(this.contentor);
            this.actualCnt = $random(0, this.elems.length - 1);
            this.nextCnt = this.getNextCnt();
            this.actualId = 'act01';
            this.nextId = 'act02';
            this.elems.each(function(el){
                var c = $(el).getProperty('href');
                if (c != null) {
                    el.addEvent('click', function(){
                        document.location.href = c;
                    });
                    el.setStyle('cursor', 'pointer');
                }
            });
            $(this.elems[this.actualCnt]).id = this.actualId;
            $(this.elems[this.nextCnt]).id = this.nextId;
            $(this.elems[this.actualCnt]).injectInside(this.contentor);
            $(this.elems[this.nextCnt]).injectInside(this.contentor);
            this.efx = new Fx.Style(this.contentor, 'top', {
                'duration': this.options.duracao,
                onComplete: function(){
                    this.getNext();
                }
.bind(this)
            });
            
            this.comeca.periodical(this.options.espera, this);
        }
        else {
            //ops não deu para slide....	
        }
    },//initialize
    getNextCnt: function(){
        if (this.actualCnt >= this.elems.length - 1) {
            this.actualCnt = 0;
        }
        else {
            this.actualCnt++;
        }
        if (this.actualCnt < this.elems.length - 1) {
            return this.actualCnt + 1;
        }
        else {
            return 0;
        }
    },
    comeca: function(){
        this.efx.start(0, 0 - this.altura);
    },
    getNext: function(){
        $(this.actualId).remove();
        this.contentor.setStyle('top', 0);
        this.nextCnt = this.getNextCnt();
        $(this.nextId).id = this.actualId;
        $(this.elems[this.nextCnt]).id = this.nextId;
        $(this.elems[this.nextCnt]).injectInside(this.contentor);
    }
});
actividadesAnim.implement(new Options);
