function Vacanza(selector)
{
	var switches = document.getElementsByTagName( "A" );
	
	this.watched = new Array();
	this.state = new Array();
	
	for( var j = 0; j < switches.length; j ++ ) {
		var needed = false;

		if( switches[j].className !== undefined) {
			if( switches[j].className == selector )
				needed = true;
		} else if( switches[j].getAttribute ) {
			if( switches[j].getAttribute( "class" ) == selector )
				needed = true;
		}
		
		if( needed ) {
			this.watched[ this.watched.length ] = switches[j];
			this.setup( switches[j] );
		}
	}
}

Vacanza.prototype.setup = function(element)
{
	element.style.background = "url(" + element.getAttribute( "von" ) + ")";	
	this.state[ "cat_" + element.getAttribute( "vc" ) ] = "on";
}

Vacanza.prototype.showCategory = function(catid, show)
{
	var divs = document.getElementsByTagName( "DIV" );
	
	for( var j = 0; j < divs.length; j ++ ) {
		var dd = divs[ j ];
		
		if( dd.className !== undefined) {
			if( dd.className != "pack" )
				continue;
		} else if( switches[j].getAttribute ) {
			if( dd.getAttribute( "class" ) != "pack" )
				continue;
		} else continue;
		
		var cats = dd.getAttribute( "vcats" ).split( "," );
		var hit = false;
		var isvisible = false;
		
		for( var r = 0; r < cats.length; r ++ ) {
			if( cats[ r ] == catid ) {
				hit = true;
			} else if( this.state[ "cat_" + cats[ r ] ] == "on" ) {
				isvisible = true;
			}
		}
		
		if( hit ) {
			if( show ) {
				if( !isvisible ) {
					dd.style.display = "";
				}
			} else {
				if( !isvisible ) {
					dd.style.display = "none";
				}
			}
		}
	}
}

Vacanza.prototype.isolateCategory = function(catid)
{
	for( var k in this.state ) {
		if( k != "cat_" + catid ) {
			this.state[ k ] = "off";
		}
	}
	
	for( var j = 0; j < this.watched.length; j++ ) {
		if( this.watched[ j ].getAttribute( "vc" ) != catid ) {
			this.watched[ j ].style.background = "url(" + this.watched[ j ].getAttribute( "voff" ) + ")";
		}
	}
	
	var divs = document.getElementsByTagName( "DIV" );
	
	for( var j = 0; j < divs.length; j ++ ) {
		var dd = divs[ j ];
		
		if( dd.className !== undefined) {
			if( dd.className != "pack" )
				continue;
		} else if( switches[j].getAttribute ) {
			if( dd.getAttribute( "class" ) != "pack" )
				continue;
		} else continue;
		
		var cats = dd.getAttribute( "vcats" ).split( "," );
		var hit = false;
		
		for( var r = 0; r < cats.length; r ++ ) {
			if( cats[ r ] == catid ) {
				hit = true;
			}
		}
		
		if( hit ) continue;
		dd.style.display = "none";		
	}
}

Vacanza.prototype.enableAll = function()
{
	for( var k in this.state ) {
		this.state[ k ] = "on";
	}

	for( var j = 0; j < this.watched.length; j++ ) {
		this.watched[ j ].style.background = "url(" + this.watched[ j ].getAttribute( "von" ) + ")";
	}

	var divs = document.getElementsByTagName( "DIV" );
	
	for( var j = 0; j < divs.length; j ++ ) {
		var dd = divs[ j ];
		
		if( dd.className !== undefined) {
			if( dd.className != "pack" )
				continue;
		} else if( switches[j].getAttribute ) {
			if( dd.getAttribute( "class" ) != "pack" )
				continue;
		} else continue;
		
		dd.style.display = "";
	}
}

Vacanza.prototype.isAllOn = function()
{
	for( var k in this.state ) {
		if( this.state[ k ] != "on" )
			return false;
	}
	
	return true;
}

Vacanza.prototype.isAllOff = function()
{
	for( var k in this.state ) {
		if( this.state[ k ] != "off" )
			return false;
	}
	
	return true;
}

Vacanza.prototype.handleClick = function(srcElement)
{
	if( this.state[ "cat_" + srcElement.getAttribute( "vc" ) ] == "on" ) {		
		if( this.isAllOn() ) {
			this.isolateCategory( srcElement.getAttribute( "vc" ) );
		} else {
			srcElement.style.background = "url(" + srcElement.getAttribute( "voff" ) + ")";
			this.state[ "cat_" + srcElement.getAttribute( "vc" ) ] = "off";
			this.showCategory( srcElement.getAttribute( "vc" ), false );

			if( this.isAllOff() ) {
				this.enableAll();
			}
		}
	} else if( this.state[ "cat_" + srcElement.getAttribute( "vc" ) ] == "off" ) {
		srcElement.style.background = "url(" + srcElement.getAttribute( "von" ) + ")";
		this.state[ "cat_" + srcElement.getAttribute( "vc" ) ] = "on";
		this.showCategory( srcElement.getAttribute( "vc" ), true );
	}
}

Vacanza.prototype.onclick = function(category)
{
	for( var j = 0; j < this.watched.length; j ++ ) {
		if( this.watched[j].getAttribute ) {
			if( this.watched[j].getAttribute( "vc" ) == category ) {
				this.handleClick( this.watched[j] );
			}
		}
	}
}

