


function background_menu(div,src)   
{   
document.getElementById(div).style.backgroundImage="url(\""+src+"\")";   
}  


// This function allows you to read form variables from static HTML
function getVar(name)
{
get_string = document.location.search;         
return_value = '';
do { //This loop is made to catch all instances of any get variable.
name_index = get_string.indexOf(name + '=');

if(name_index != -1)
  {
  get_string = get_string.substr(name_index + name.length + 1, get_string.length - name_index);
  
  end_of_value = get_string.indexOf('&');
  if(end_of_value != -1)                
	value = get_string.substr(0, end_of_value);                
  else                
	value = get_string;                
	
  if(return_value == '' || value == '')
	 return_value += value;
  else
	 return_value += ', ' + value;
  }
} while(name_index != -1)

//Restores all the blank spaces.
space = return_value.indexOf('+');
while(space != -1)
  { 
  return_value = return_value.substr(0, space) + ' ' + 
  return_value.substr(space + 1, return_value.length);
				 
  space = return_value.indexOf('+');
  }

return(return_value);        
}

/* Simple Accordion Script 
 * Requires Prototype and Script.aculo.us Libraries
 * By: Brian Crescimanno <brian.crescimanno@gmail.com>
 * http://briancrescimanno.com
 * This work is licensed under the Creative Commons Attribution-Share Alike 3.0
 * http://creativecommons.org/licenses/by-sa/3.0/us/
 */

if (typeof Effect == 'undefined')
  throw("You must have the script.aculo.us library to use this accordion");

var Accordion = Class.create({

    initialize: function(id, defaultExpandedCount) {
        if(!$(id)) throw("Attempted to initalize accordion with id: "+ id + " which was not found.");
        this.accordion = $(id);
        this.options = {
            toggleClass: "accordion-toggle",
            toggleActive: "accordion-toggle-active",
            contentClass: "accordion-content"
        }
        this.contents = this.accordion.select('div.'+this.options.contentClass);
        this.isAnimating = false;
        this.maxHeight = 0;
        this.current = defaultExpandedCount ? this.contents[defaultExpandedCount-1] : this.contents[0];
        this.toExpand = null;

        this.checkMaxHeight();
        this.initialHide();
        this.attachInitialMaxHeight();

        var clickHandler =  this.clickHandler.bindAsEventListener(this);
        this.accordion.observe('click', clickHandler);
    },

    expand: function(el) {
        this.toExpand = el.next('div.'+this.options.contentClass);
        if(this.current != this.toExpand){
			this.toExpand.show();
            this.animate();
        }
    },

    checkMaxHeight: function() {
        for(var i=0; i<this.contents.length; i++) {
            if(this.contents[i].getHeight() > this.maxHeight) {
                this.maxHeight = this.contents[i].getHeight();
            }
        }
    },

    attachInitialMaxHeight: function()
	{
		this.current.previous('div.'+this.options.toggleClass).addClassName(this.options.toggleActive);
    			if(this.current.getHeight() != this.maxHeight)
				{
				this.current.setStyle({height: this.maxHeight+"px"});
				}
				
    },

    clickHandler: function(e) {
        var el = e.element();
        if(el.hasClassName(this.options.toggleClass) && !this.isAnimating) {
            this.expand(el);
        }
    },

    initialHide: function(){
        for(var i=0; i<this.contents.length; i++){
            if(this.contents[i] != this.current) {
                this.contents[i].hide();
                this.contents[i].setStyle({height: 0});
            }
        }
    },

    animate: function() {
		
        var effects = new Array();
        var options = {
            sync: true,
            scaleFrom: 0,
            scaleContent: false,
            transition: Effect.Transitions.sinoidal,
            scaleMode: {
                originalHeight: this.maxHeight,
                originalWidth: this.accordion.getWidth()
            },
            scaleX: false,
            scaleY: true
        };

        effects.push(new Effect.Scale(this.toExpand, 100, options));

        options = {
            sync: true,
            scaleContent: false,
            transition: Effect.Transitions.sinoidal,
            scaleX: false,
            scaleY: true
        };

        effects.push(new Effect.Scale(this.current, 0, options));

        var myDuration = 0.75;

        new Effect.Parallel(effects, {
            duration: myDuration,
            fps: 35,
            queue: {
                position: 'end',
                scope: 'accordion'
            },
            beforeStart: function() {
                this.isAnimating = true;
                this.current.previous('div.'+this.options.toggleClass).removeClassName(this.options.toggleActive);
                this.toExpand.previous('div.'+this.options.toggleClass).addClassName(this.options.toggleActive);
				
				/* JOEL tinc que posar overflow dels div scroll a hidden pq sinó en mozilla fa uns salts penosos l'animació */
				document.getElementById("content_scroll_resize_1").style.overflow="hidden";
				document.getElementById("content_scroll_resize_2").style.overflow="hidden";
				document.getElementById("content_scroll_resize_3").style.overflow="hidden";
				document.getElementById("content_scroll_resize_4").style.overflow="hidden";
				document.getElementById("content_scroll_resize_5").style.overflow="hidden";
            }.bind(this),
            afterFinish: function() {
                this.current.hide();
                this.toExpand.setStyle({ height: this.maxHeight+"px" });
                this.current = this.toExpand;
                this.isAnimating = false;
				
				/* JOEL puc restaurar els overflows perque surti el scroll si cal */
				document.getElementById("content_scroll_resize_1").style.overflow="auto";
				document.getElementById("content_scroll_resize_2").style.overflow="auto";
				document.getElementById("content_scroll_resize_3").style.overflow="auto";
				document.getElementById("content_scroll_resize_4").style.overflow="auto";
				document.getElementById("content_scroll_resize_5").style.overflow="auto";
            }.bind(this)
        });
    }

});
	
	
// function do_acordion()
document.observe("dom:loaded", function()
{
/*
var se=getVar("se");
if (se==null) se=1;

var seInt=parseInt(se);
*/

var seInt=-1;
var get_string = document.location.href;

// inici?
var pos_inicio=get_string.indexOf('index');
if (pos_inicio>0)
{
	seInt=1;
	pos_inicio=get_string.indexOf('ss=4');
	if (pos_inicio>0) seInt=5;
	pos_inicio=get_string.indexOf('ss=5');
	if (pos_inicio>0) seInt=5;
}
pos_inicio=get_string.indexOf('alquiler');
if (pos_inicio>0) seInt=2;
pos_inicio=get_string.indexOf('venta');
if (pos_inicio>0) seInt=3;
pos_inicio=get_string.indexOf('curso');
if (pos_inicio>0) seInt=4;

// alert(get_string);







// l'acordeó menú
accordion = new Accordion("test-accordion", seInt);

// alert("accordion");

document.getElementById('test-accordion').visibility="hidden";

//document.getElementById('div2').style.visibility = "visible"; 

/* obsolet
if ((se!="1")&&(se!="5")) // si no estic a inici ni a contacte
{
accordion = new Accordion("test-accordion",1); // l'acrodeó de la dreta
}
*/

   
}

)
