/*************************************************************************************************************************
slideMenu class
Mootools v.1.11 is required!

By Herbert Cuba Garcia


***************************************************************************************************************************/

var slideMenu=new Class({

	//BtnSlide is the element that you click on in order to toggle the menues.
	//allowMultipleOpen regulates if we can have more than slider open at a time
	//id is the id of the class, referenced by a integer
	options: {
		btnSlideElem: 'slide',
		allowMultipleOpen: true,
		id: 0,
		slideClasses: '',
		cookieName: 'myCookie'
		
	},
						

						
	initialize: function(field, options){

		this.setOptions(options);
		this.btnSlide=$(this.options.btnSlideElem);
		this.mult=this.options.allowMultipleOpen;
		this.id=this.options.id;
		this.cookieName=this.options.cookieName;
		this.allSlides=this.options.slideClasses;
		this.mySlider=new Fx.Slide(field, {duration: 500, transition: Fx.Transitions.expoOut});				
		this.mySlider.hide();
		$(field).setStyles({display:'block',visibility:'visible'});
		
		
		//sets all menu-classes to "closed"
		if((this.btnSlide).hasClass('closed')){
			(this.btnSlide).removeClass('closed');
		}else if((this.btnSlide).hasClass('open')){
			(this.btnSlide).removeClass('open');
		}

		(this.btnSlide).addClass('closed');

		//sets the specified sections to "open"		
		this.openMenu=getCookie(this.cookieName);
		//alert(this.cookieName);
		
		if(this.openMenu){						
			for(j=0; j<this.openMenu.length; j++){
				
				if(this.openMenu[j]==this.id){  
					(this.btnSlide).removeClass('closed');
					(this.btnSlide).addClass('open');
					this.mySlider.show();
				}
			}
		}


	

		//defines the function when opening and closing a section		
		if(this.mult){//if "allowMultipleOpen" is true
			(this.btnSlide).addEvent('click', function() {
				this.mySlider.toggle();	
		
				if((this.btnSlide).hasClass('closed')){
					(this.btnSlide).removeClass('closed');
					(this.btnSlide).addClass('open');
					if(this.openMenu){
						for(i=0; i<this.openMenu.length; i++){
								if(this.openMenu[i]!=this.id){
									//add the menu section to the cookie
									this.updateCookie('add', this.id);
									
								}					
						}
					}else{//if no cookie has been set
						this.updateCookie('add', this.id);
					}
				}else if((this.btnSlide).hasClass('open')){
					(this.btnSlide).removeClass('open');
					(this.btnSlide).addClass('closed');
					if(this.openMenu){
						for(i=0; i<this.openMenu.length; i++){
								if(this.openMenu[i]==this.id){
									
									//remove the opened menu section in the cookie
									this.updateCookie('remove', this.id);
								}					
						}
					}else{//if no cookie has been set
						this.updateCookie('remove', this.id);
					}
				}
	  
			}.bind(this));
			
		}else{//if we do not allow multiple sections open at the same time
			(this.btnSlide).addEvent('click', function() {
				
				if((this.btnSlide).hasClass('closed')){
					hideAllSlides(this.allSlides);
					(this.btnSlide).removeClass('closed');
					(this.btnSlide).addClass('open');
					this.mySlider.toggle();
					setCookie(this.cookieName,this.id,1,'/','','');	
					//alert('opening section #'+this.id);
				}else if((this.btnSlide).hasClass('open')){				
					(this.btnSlide).removeClass('open');
					(this.btnSlide).addClass('closed');
					this.mySlider.toggle();	
					setCookie(this.cookieName,'',1,'/','','');	
					//alert('closing section #'+this.id);
				}
			}.bind(this));
			
			
		}
		
	},
	//saves the id of the sections open in a cookie as an integer - tex "2,3,5,1"
	updateCookie: function(action, id){

		//add a opened menu section to the cookie
		if(action=='add'){
			counter=0;
			added=getCookie(this.cookieName);
			if(added){
				for(i=0; i<added.length; i++){
					if(added[i]==id){
						counter++;
					}
				}
				if(counter==0){
					if(added.length>1){
						newValue=added.join(',');
					}else{
						newValue=added[0];
					}
					//alert(newValue+','+id);
					setCookie(this.cookieName,newValue+','+id,1,'/','','');						
				}
			}else{//if no cookie has been set
				setCookie(this.cookieName,id,1,'/','','');
			}
		
		
		//remove a section (int) from the cookie
		}else if(action=='remove'){
			counter=0;
			var added=getCookie(this.cookieName);
			if(added){
				for(i=0; i<added.length; i++){
					if(added[i]==id){
						added=added.remove(id+'');
						counter++;
					}
				}
				if(counter>0){
					if(added.length>1){
						newValue=added.join(',');
					}else if(added.length==1){
						newValue=added[0];
					}else{
						newValue="";
					}
					setCookie(this.cookieName,newValue,1,'/','','');		
				}
			}else{//if no cookie has been set
				//do nothin
			}
		}
		
	},
	toggle: function(){
		this.mySlider.toggle();
	},
	hide: function(){
		//alert('section #'+this.id+' - '+(this.btnSlide).className);
		if((this.btnSlide).hasClass('open')){
			(this.btnSlide).removeClass('open');
			(this.btnSlide).addClass('closed');
			this.mySlider.toggle();
			//alert('closing section #'+this.id);
		}
		
	},
	
	echo: function(){
		return this.id;
		
	}
	
	
	
});
slideMenu.implement(new Options);                 

//manages all slideMenues
function hideAllSlides(sm){
	for(i=1; i<sm.length; i++){
		sm[i].hide();
	}
}

/*COOKIES*/
//cookie functions - outside the class!!
function setCookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires ){
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}


function getCookie( check_name ) {
	
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split(';');
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	
	
	for ( i = 0; i < a_all_cookies.length; i++ ){
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			if(a_temp_cookie[1]){
				b_cookie_found = true;
				cookie_value = ( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );								
				return cookie_value.split(',');
				break;
			}
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found ){
		
		return null;
	}
}		

// this deletes the cookie when called
function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}



