/**
	CCR - Global Library (Projeto-global-lib) v: 0.1
	Developer(s):
		+ Diogo Silva (diogo.ferreira@hellointeractive.com.br)
*/

// Return GET params
function returnParam(url,paramName){
	for(var qs = url.split("&"), i=0; i < qs.length; i++)
		if(qs[i].indexOf(paramName)>=0)
			return qs[i].split("=")[1];
	return false;
}

// Flash Calls
function openLink(url) {
	if(!url.match('http')) {
		switch(url) {
			case "example": window.location = 'http://www.example.com.br/'; break;
			case "exampl2": doSomething(); return true; break;
			default: return false;
		}
	}else
		window.location = url;
	return true;
}

// Popup handler
function openPopup(u,n,w,h,o,c) {
	var l = t = 18;
	if (c) {
		l = (screen.availWidth-w) / 2;
		t = (screen.availHeight-h) / 2;
	}
	//p = window.open(u,'pop_'+n,'left='+l+',top='+t+',width='+w+',height='+h+',scrollbars='+((o)?o:'1') + 'status=yes');
	p=window.open(u,'pop_'+n,'left='+l+',top='+t+',width='+w+',height='+h+''+((o)?','+o:''));
}

function abrePopupNoticia(url) {
	openPopup(url,'noticia',670,500,'scrollbars=1',true);
}

var margin = 0;

function next(i) {
    var id = "fotos"+i;
    var thumbs=document.getElementById(id).getElementsByTagName('img').length;
    var deslocamento = parseInt(thumbs-4) * 111;
    if (margin >= -(deslocamento)) {
        margin -= 112;
        document.getElementById(id).style.left = margin + "px";
    }
}
function prev(i) {
    var id = "fotos"+i;
    if (margin != 0) {
        margin += 112;
        document.getElementById(id).style.left = margin + "px";
    }
}

/* FUNÇÃO EXCLUSIVA PARA A LINHA DE TEMPO */
function lt_prev() {
    var id = "lt_fotos";
    var thumbs=document.getElementById(id).getElementsByTagName('img').length;
    var deslocamento = parseInt(thumbs-5) * 102;
    if (margin >= -(deslocamento)) {
        margin -= 222;
        document.getElementById(id).style.left = margin + "px";
    }
}

function lt_next() {
    var id = "lt_fotos";
    if (margin != 0) {
        margin += 222;
        document.getElementById(id).style.left = margin + "px";
    }
}

/* GIROS HOME */

function proxGiro() {
    
    if(document.getElementById('Giro1').style.display == 'block') {
        document.getElementById('Giro1').style.display = 'none';
        document.getElementById('Giro2').style.display = 'block';
    }
   else if(document.getElementById('Giro2').style.display == 'block') {
        document.getElementById('Giro2').style.display = 'none';
        document.getElementById('Giro3').style.display = 'block';
    }
   else if(document.getElementById('Giro3').style.display == 'block') {
        document.getElementById('Giro3').style.display = 'none';
        document.getElementById('Giro4').style.display = 'block';
    }
   else if(document.getElementById('Giro4').style.display == 'block') {
        document.getElementById('Giro4').style.display = 'none';
        document.getElementById('Giro1').style.display = 'block';
    }
   /* else if(document.getElementById('Giro5').style.display == 'block') {
        document.getElementById('Giro5').style.display = 'none';
        document.getElementById('Giro1').style.display = 'block';
    } */   
}

function antGiro() {
    
    /*if(document.getElementById('Giro5').style.display == 'block') {
        document.getElementById('Giro5').style.display = 'none';
        document.getElementById('Giro4').style.display = 'block';
    }*/
   if(document.getElementById('Giro4').style.display == 'block') {
        document.getElementById('Giro4').style.display = 'none';
        document.getElementById('Giro3').style.display = 'block';
    }
   else if(document.getElementById('Giro3').style.display == 'block') {
        document.getElementById('Giro3').style.display = 'none';
        document.getElementById('Giro2').style.display = 'block';
    }
    else if(document.getElementById('Giro2').style.display == 'block') {
        document.getElementById('Giro2').style.display = 'none';
        document.getElementById('Giro1').style.display = 'block';
    }
   else if(document.getElementById('Giro1').style.display == 'block') {
        document.getElementById('Giro1').style.display = 'none';
        document.getElementById('Giro4').style.display = 'block';
    }
}






/* script Galeria  */

function initGaleria(){
//    $("#galeriaTotal").html($(".alternaImg a").size());
    
    var list = $("#imgGaleria");    
    
    if (list != null && list.length > 0)
    {
        list[0].imgCurrent = 0;
    }
    else
    {
        return;
    }    
    
    //Carrega Imagens
    listImg = new Array();    
    $(".alternaImg a").each(function(index, value) {
        this.idItem = index;
        
        var imgGal = document.createElement("img");
        $(imgGal).attr({src:$(this).attr("href"), width:$("#imgGaleria").attr("width"), height:$("#imgGaleria").attr("height")});
           
        listImg.push(imgGal);
	});
    
    if($("#controleSlide").size() != 0)
        initSlideShow(listImg);
    
    // ALTERNA IMAGENS
    $(".alternaImg a").click(function(){         
        var urlImg = $(this).attr("href");
        if($(".boxGaleria").size() == 0 && $(".boxGaleriaVer").size() == 0){
        
            var imgClone = $(listImg[this.idItem]).clone();
            
            $($(listImg[this.idItem]).attr({id:"imgGaleriaClone"})).insertBefore("#imgGaleria");
            listImg[this.idItem] = imgClone;
            
            $("#imgGaleria").animate({
                    opacity: 0
                },600, function(){
                $("#imgGaleria").remove();
                $("#imgGaleriaClone").attr({id:"imgGaleria"});   
            });
        }else{
            //animate da galeria
            $("#imgGaleria")[0].imgCurrent = this.idItem;
            $(".alternaImg a img").animate({opacity: 0.5},"fast");
            $(this).find("img").animate({opacity: 1},"fast");
            
            $("#imgGaleria").animate({
                opacity: 0
                },50, function(){
                    $("#imgGaleria").attr("src",urlImg)
                    $("#imgGaleria").animate({
                        opacity: 1
                        },600);        
            });
        }

        var legend = $(this).attr("title");
        var credit = $(this).attr("rel");
        
        if(legend == undefined)
        {
			legend = "";
        }   
        
        if(credit == undefined)
        {
			credit = "";
        }
        
        $(".txtLegendaFoto").text(legend);
        $(".txtCreditoFoto").text(credit);

        return false;
    });
    
    $(".alternaImg a img").css({position:"relative"});
    galeriaVert();
}

function galeriaVert(){
    // GALERIA DE IMAGENS
    if($(".boxGaleriaVer").size() != 0){
        $(".boxGaleriaVer")[0].heightAdd = $(".boxGaleriaVer").find(".boxGaleriaVerMasc")[0].offsetHeight;
        $(".boxGaleriaVer")[0].height = $(".boxGaleriaVer").find(".boxGaleriaVerMascAux .boxGaleriaVerAux")[0].offsetHeight;
        $(".boxGaleriaVer")[0].controle = true;
    }
    
	$(".btProximoVer").click(function() {
		var heightAdd = $(this).parents(".boxGaleriaVer")[0].heightAdd;
		var height = $(this).parents(".boxGaleriaVer")[0].height;
        
		var top = $(this).parents(".boxGaleriaVer").find(".boxGaleriaVerMascAux").css("top");
		top = parseInt(top.substring(0,top.length-2));        
        
		if(height - heightAdd > (top *(-1))){            
            animateProxVer(top - heightAdd);
		}
		return false;
	});
	
	$(".btAnteriorVer").click(function() {
		var heightAdd = $(this).parents(".boxGaleriaVer")[0].heightAdd;
        
		var top = $(this).parents(".boxGaleriaVer").find(".boxGaleriaVerMascAux").css("top");
		top = parseInt(top.substring(0,top.length-2));
        
		if(top != 0){
			$(this).parents(".boxGaleriaVer").find(".boxGaleriaVerMascAux").animate({
				top: top + heightAdd
				},"slow"
			);
		}
		return false;
	});
}

function animateProxVer(range){
    $(".boxGaleriaVer")[0].controle = false;
    $(".boxGaleriaVer").find(".boxGaleriaVerMascAux").animate({
        top: range
        },"slow",function(){                    
            $(this).parents(".boxGaleriaVer")[0].controle = true;   
        }
    );
}

function abrePopupNoticia(url){
    window.open(url,'news','width=680,height=550,scrollbars=yes,resizable=yes');
}

function abrePopupDestaque(pagina,id) {
	openPopup('/admin/'+pagina+'/item.aspx?return=1&id=' + id,'Destaque',800,535,'scrollbars=1',true);
}

function abrePopupFiltro(id) {
	openPopup('/admin/filtro/default.aspx?return=1&idFiltro=' + id,'Destaque',800,535,'scrollbars=1',true);
}

function abrePopupFiltroArquivo(id) {
	openPopup('/admin/filtro/arquivo.aspx?return=1&idFiltro=' + id,'Destaque',800,535,'scrollbars=1',true);
}

function abrePopupFiltroGaleria(id) {
	openPopup('/admin/filtro/galeria.aspx?return=1&idFiltro=' + id,'Destaque',800,535,'scrollbars=1',true);
}

function abrePopupGaleria(id) {
	openPopup('/admin/galeria/item.aspx?return=1&id=' + id,'Destaque',800,535,'scrollbars=1',true);
}

function abrePopupRevista() {
	openPopup('/admin/revista/Default.aspx','Destaque',800,535,'scrollbars=1',true);
}

function FxHeight() {
	var varHeight = $('.boxHeight').eq(0).height();
	for (x = 0; x < $('.boxHeight').length; x++){
		if(varHeight < $('.boxHeight').eq(x).height()){
			varHeight = $('.boxHeight').eq(x).height();
		}
	}
	$('.boxHeight').css('height',varHeight);
}

//liminar
function popLiminar(url) {
	openPopup(url,'Urgente',500,530,'scrollbars=0',true);
}

/* funcao para fechar o banner dhtml */
//#bannerDhtml
function closeBanner(obj){
	$(".boxBannerFlash").remove();
}


