function ltrim(s){
	return s.replace(/\s*((\S+\s*)*)/, "$1");
}
function rtrim(s){
	return s.replace(/((\s*\S+)*)\s*/, "$1");
}
function trim(s){
	return ltrim(rtrim(s));	
}

//	Category class.
function Category(name, description, initialImage, initialHref){ 
	Category.prototype.setName =  function(name){
		if (!name)
			return;  
		this.name = name;
	};
	
	Category.prototype.setDescription =  function(description){ 
		this.description = description;
	};
	
	Category.prototype.setInitialImage = function(initialImage){
		if (!initialImage)
			return;  
		this.initialImage = initialImage;  
	};
	
	Category.prototype.setInitialHref = function(initialHref){
		this.initialHref = (!initialHref) ? 'javascript:undefined' : initialHref;
	};
	
	this.setName(name);
	this.setDescription(description);
	this.sponsors = new Array();
	this.setInitialImage(initialImage);
	this.setInitialHref(initialHref);
	
	this.imageDivId = null;
	this.anchorId = null;
	this.imageShuffler = null;
	
	Category.prototype.addSponsor = function(s){
		if (!s)
			return false;
	
		for (var i = 0; i < this.sponsors.length; i++)
			if (this.sponsors[i] == s)
				return false;
	
		this.sponsors.push(s);
		return true;   
	};
	
	Category.prototype.getSponsorAt = function(index){
		if ((index == undefined) || (index == null) || (index < 0) || (index >= this.sponsors.length))
			return null;
		return this.sponsors[index];   
	};
	 
	Category.prototype.getVisualRepresentation = function(){
		var containingDiv = document.createElement('div');
		var nameSpan = document.createElement('span');
	
		if(!this.description){
			nameSpan.appendChild(document.createTextNode(''));
		}else{
			nameSpan.appendChild(document.createTextNode(this.description + ':'));
		}
		
		nameSpan.className = 'description';
	
		var anchor = document.createElement('a');
		anchor.href = this.initialHref;
		anchor.target = '_blank';
		anchor.id = this.name + 'AnchorId';
		this.anchorId = anchor.id;
		
		var imageDiv = document.createElement('div');
		imageDiv.appendChild(this.initialImage);
		imageDiv.id = this.name + 'ImageDiv';
		imageDiv.className = 'photodiv';
		this.imageDivId = imageDiv.id;
		anchor.appendChild(imageDiv);
	
		containingDiv.appendChild(nameSpan);
		containingDiv.appendChild(anchor);
		containingDiv.className = 'sponsor';
		containingDiv.id = this.name + 'Id';
	
		return containingDiv;  
	};
	  
	Category.prototype.initializeImageShuffler = function(index, gblPauseSeconds, gblFadeSeconds){
		this.imageShuffler = new PhotoShuffler(index, this, this.initialImage.id, this.anchorId, gblPauseSeconds, gblFadeSeconds);
	};
	 
	Category.prototype.startShuffling = function(){
		this.imageShuffler.photoShufflerLaunch();
	};

}



//	Sponsor Class.
function Sponsor(image, website){
	Sponsor.prototype.setImage = function(image){
		if ((!image) || (!image.src))
			return;

		if (!image.alt)
			image.alt = '';

		this.image = image;  
	};
	
	Sponsor.prototype.setWebsite = function(website){
		if (!website)
			return;  
		this.website = website;  
	};

	this.setImage(image);
	this.setWebsite(website);
}

//	PhotoShuffler Class.
function PhotoShuffler(index, category, gblPhotoShufflerImgId, gblPhotoShufflerAnchorId, gblPauseSeconds, gblFadeSeconds){
	this.index = index;
	this.category = category;
	this.gblPhotoShufflerImgId = gblPhotoShufflerImgId;
	this.gblPhotoShufflerAnchorId = gblPhotoShufflerAnchorId;
	this.gblPauseSeconds = gblPauseSeconds;
	this.gblFadeSeconds = gblFadeSeconds;
	this.gblOpacity = 100;
	this.gblOnDeck = 0;

	PhotoShuffler.prototype.photoShufflerLaunch = function(){
		var theimg = document.getElementById(this.gblPhotoShufflerImgId);
		var theanchor = document.getElementById(this.gblPhotoShufflerAnchorId);

		document.getElementById(this.category.imageDivId).style.backgroundImage='url(' + this.category.getSponsorAt(this.gblOnDeck).image.src + ')';
		setTimeout("categories[" + this.index + "].imageShuffler.photoShufflerFade()", this.gblPauseSeconds*1000);
	};

	PhotoShuffler.prototype.photoShufflerFade = function(){
		var theimg = document.getElementById(this.gblPhotoShufflerImgId);
		//	determine delta based on number of fade seconds
		//	the slower the fade the more increments needed
		var fadeDelta = 100 / (30 * this.gblFadeSeconds);
		//	fade top out to reveal bottom image
		if (this.gblOpacity < 2*fadeDelta ){
			this.gblOpacity = 100;
			//	stop the rotation if we're done
			this.photoShufflerShuffle();
			//	pause before the next fade;
			setTimeout("categories[" + this.index + "].imageShuffler.photoShufflerFade()", this.gblPauseSeconds*1000);
		}else{
			this.gblOpacity -= fadeDelta;
			setOpacity(theimg, this.gblOpacity);
			setTimeout("categories[" + this.index + "].imageShuffler.photoShufflerFade()",30); // 1/30th of a second
		}
	};

	PhotoShuffler.prototype.photoShufflerShuffle = function(){
		var thediv = document.getElementById(this.category.imageDivId);
		var theimg = document.getElementById(this.gblPhotoShufflerImgId);
		var theanchor = document.getElementById(this.gblPhotoShufflerAnchorId);

		//	copy div background-image to src
		theimg.src = this.category.getSponsorAt(this.gblOnDeck).image.src;
		theanchor.href = this.category.getSponsorAt(this.gblOnDeck).website;
		window.status = this.category.getSponsorAt(this.gblOnDeck).image.alt; // updates status bar (optional)
		//	set img opacity to 100
		setOpacity(theimg, 100);
		//	shuffle the deck
		this.gblOnDeck = ++this.gblOnDeck % this.category.sponsors.length;
		//	slide next image underneath
		thediv.style.backgroundImage='url(' + this.category.getSponsorAt(this.gblOnDeck).image.src + ')';
	};
}

//	Funciones globales
function createNewCategory(initialImageSource, initialImageAlt, initialImageHref, categoryName, categoryDescription){
	var initialImage = new Image();
	initialImage.src = initialImageSource;
	initialImage.alt = initialImageAlt;
	initialImage.id = categoryName + 'Image';
	var category = new Category(categoryName, categoryDescription, initialImage, initialImageHref);
	return category;
}

function createNewSponsor(sponsorWebsite, imageSource, imageAlt){
	var image = new Image();
	image.src = imageSource;
	image.alt = imageAlt;
	var m = new Sponsor(image, sponsorWebsite);
	return m;
}

function setOpacity(obj, opacity){
	opacity = (opacity == 100) ? 99.999 : opacity;
	//	IE/Win
	obj.style.filter = "alpha(opacity:" + opacity + ")";
	//	Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	//	Oldder Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	//	Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}
