/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
Cufon.replace('.text');

$(document).ready(function(){
    calibraHeightContent();
    configuraAutosuggest();
    elaboraSlides();
    
    $('input[type=submit]').click(function(){
        if($('.obl[value=""]').size()==0){
            if( (($('input[name=recive]:checked').val()=='on') && ($('input.inp[value=""]').size()==0)) || ($('input[name=recive]:checked').val()!='on') ){
                if($('input[name=agree]:checked').val()=='on'){
                    alert('Gràcies per col·laborar amb fundació small.\nGracias por colaborar con fundación small.\nThanks to colaborate with small fundation.');
                    return true;
                }else{
                    alert(leerPrivacidad);
                    return false;
                }
            }else{
                alert(todosllenos);
                return false;
            }
        }else{
            alert(camposObligatorios);
            return false;
        }
    });

    $('.flag').click(function(){
        var val=$(this).attr('name');
        canviaIdioma(val);

    });
    /*
    $('ul.quefem.punts ul:nth-child(1) a').click(function(){
        window.location=proyectos_ass;
        return false;
    });
    $('ul.quefem.punts ul:nth-child(2) a').click(function(){
        window.location=proyectos_invest;
        return false;
    });
    $('ul.quefem.punts ul:nth-child(3) a').click(function(){
        window.location=proyectos_familia;
        return false;
    });*/
});

function canviaIdioma(nou_val)
{
    url = document.URL;
    newurl=changeOrAddGetParam(url,'chlang',nou_val);
    window.location = newurl;
}

function changeOrAddGetParam(url,name,val)
{
    //Retorna url+name[val]
    split_arr = url.split('?');
    if(split_arr[1]==undefined){
        //No hi ha cap parametre GET
        return url+'?'+name+'='+val;
    }else if(split_arr[1]==''){
        //Valor de GET en blanc ( porta "?" )
        return url+name+'='+val;
    }else{
        params = split_arr[1];
        params_arr = params.split('&');
        for( it in params_arr){
            exp=new RegExp(name+"=.*");
            if(exp.test(params_arr[it])){
                params_arr[it]=name+'='+val;
                return split_arr[0]+'?'+params_arr.join('&');
            }
        }
        var new_params = split_arr[0]+'?'+params_arr.join('&');
        return new_params+'&'+name+'='+val;
    }
}

function calibraHeightContent()
{
    height1 = $('#content').height();
    height2 = $('#llista_dreta').height();
    if(height1>height2){
        maxheight=height1+20;
        $('#wrapper').css('height',maxheight+'px');
    }else{
        maxheight = height2;
    }
    $('#wrapper').css('height',maxheight+'px');
}


function configuraAutosuggest(){
    $('.entbancsugg').suggest("inc/suggestEntitatsBancaries");
}

function elaboraSlides(){
	var navegador = navigator.appName;
	if (navegador == "Microsoft Internet Explorer"){
		$('#slideshows .text').css('left','auto').css('top','auto').css('fontSize','30');
		var left=0;
		$('#slideshows div').not(".text").each(function(){
			left=Math.ceil(Math.random()*150);
			$(this).css('display','block').css('position','relative').css('height','40px').css('marginLeft',left);
		});
	}else{
		totalSlides = $('#slideshows').children().css('opacity',0).size();
		actSlides = Math.round(Math.random()*totalSlides)+1;
		$('#slideshows div:nth-child('+actSlides+')').not(".text").fadeTo(500,1);
		setInterval('transitionsSlides()',2000);
	}
}
function transitionsSlides(){
    $('#slideshows div:nth-child('+actSlides+')').not(".text").fadeTo(500,0);
    if(actSlides==totalSlides){
        actSlides=1;
    }else{
        actSlides++;
    }
    $('#slideshows div:nth-child('+actSlides+')').not(".text").fadeTo(500,1);
}

function addBookmark(title,url) {
if (window.sidebar) {
window.sidebar.addPanel(title, url,"");
} else if( document.all ) {
window.external.AddFavorite( url, title);
} else if( window.opera && window.print ) {
return true;
}
} 

