function PAhighlightWidget(index, node, allWidgets){
	this.index = index;
	this.node = node;
	this.allWidgets = allWidgets;

	var widget = this;

	this.originalPadding = parseInt(DY.Dom.getStyle(this.node, 'paddingTop'), 10);

	var divs = this.node.getElementsByTagName('div');

	for (var i = 0; i < divs.length; i++){
		if(divs[i].className == 'highlightImages'){
			this.image = divs[i];
		}
	}

	var images = this.image.getElementsByTagName('img');

	for(var i = 0; i < images.length; i++){
		if(images[i].className == 'over'){
			this.imgOver = images[i];
		}

		if(images[i].className == 'out'){
			this.imgOut = images[i];
		}
	}

//	this.imgOver.style.overflow = 'hidden';

	this.imageContent = this.image.getElementsByTagName('a')[0];

//	this.imageContent.style.overflow = 'hidden';


	//Get contents CSS to read the height values
	var sheet = document.getElementById('contentStyle');
	this.closedHeight = parseInt(getSelectorValue(sheet, '.highlightContent img.out', 'height'), 10);
	this.openedHeight = parseInt(getSelectorValue(sheet, '.highlightContent img.over', 'height'), 10);

	this.imgOver.style.clip = 'rect(0, auto, '+this.closedHeight+'px, 0)';

	this.speed = .5;

	this.over = function(){
		widget.setOver();
	}

	this.out = function(){
		widget.setOut();
	}

	//We need to create a reference to "this" that can be used within the timeout (which runs outside this scope)
	this.obj = 'highlightWidget'+PAhighlightWidget.counter++;
	eval(this.obj+'=this');

	$(this.node).hover(this.over, this.out);

}

PAhighlightWidget.counter = 0;

PAhighlightWidget.prototype.setOver = function(){
/*
	for (var i = 0; i < this.index; i++){
		this.allWidgets[i].disable();
	}

	for (var i = this.index + 1; i < this.allWidgets.length; i++){
		this.allWidgets[i].disable();
	}
*/

	this.open();

/*
	if(this.index == 0){
		this.node.parentNode.style.marginBottom = '65px';
	}
	else{
		this.node.parentNode.style.marginTop = '65px';
	}
*/
}

PAhighlightWidget.prototype.setOut = function(){
/*
	for (var i = 0; i < this.index; i++){
		this.allWidgets[i].enable();
	}

	for (var i = this.index + 1; i < this.allWidgets.length; i++){
		this.allWidgets[i].enable();
	}
*/
	this.close();
/*
	if(this.index == 0){
		this.node.parentNode.style.marginBottom = '0px';
	}
	else{
		this.node.parentNode.style.marginTop = '20px';
	}
*/
}

PAhighlightWidget.prototype.disable = function(){
	clearTimeout(this.timer);
	this.timer = setTimeout(this.obj+'.doDisable()', 100);
//	$(this.node).addClass('highlightContentDisabled');
}

PAhighlightWidget.prototype.doDisable = function(){
	clearTimeout(this.timer);

	this.imageContent.style.height = this.closedHeight+'px';
	this.imgOver.style.clip = 'rect(0, auto, '+this.closedHeight+'px, 0)';

	if(PA_IE6){
		this.node.style.paddingTop = '0px';
	}
	else{
		this.node.style.paddingTop = this.closedHeight+'px';
	}

	DY.Dom.setStyle(this.imgOver, 'display', 'none');
	DY.Dom.setStyle(this.imgOut, 'display', 'block');

	DY.Dom.setStyle(this.image, 'display', 'none');
	this.pad = this.node.style.paddingTop;
	this.node.style.paddingTop = '0px';
}

PAhighlightWidget.prototype.enable = function(){
	DY.Dom.setStyle(this.image, 'display', 'block');
	this.node.style.paddingTop = this.pad;
//	$(this.node).removeClass('highlightContentDisabled');
}

PAhighlightWidget.prototype.open = function(){
	clearTimeout(this.timer);

	this.direction = 'open';

	this.endH = this.openedHeight;

	DY.Dom.setStyle(this.imgOver, 'display', 'block');
	DY.Dom.setStyle(this.imgOut, 'display', 'none');

	this.current = DY.Dom.getClip(this.imgOver)[2];


	this.loop();
}

PAhighlightWidget.prototype.close = function(){
	clearTimeout(this.timer);

	this.timer = setTimeout(this.obj+'.doClose()', 125);
}

PAhighlightWidget.prototype.doClose = function(){
	clearTimeout(this.timer);

	this.direction = 'close';

	this.endH = this.closedHeight;
	this.loop();
}

PAhighlightWidget.prototype.loop = function(){
	clearTimeout(this.timer);

	var h = DY.Dom.getClip(this.imgOver)[2];

	var newH = parseInt((this.endH + this.speed * h) / (this.speed + 1));


	var pad = newH - this.originalPadding;


	if(Math.abs(newH - this.endH) <= this.speed){
		newH = this.endH;

		if(this.direction == 'close'){
			DY.Dom.setStyle(this.imgOver, 'display', 'none');
			DY.Dom.setStyle(this.imgOut, 'display', 'block');
		}
	}
	else{
		this.timer = setTimeout(this.obj+'.loop()', 50);
	}

	this.imageContent.style.height = newH+'px';
	this.imgOver.style.clip = 'rect(0, auto, '+newH+'px, 0)';

	if(PA_IE6){
		this.node.style.paddingTop = '0px';
	}
	else{
		this.node.style.paddingTop = newH+'px';
	}

}

function PAmultimediaWidget(node){

	if(node){
		this.node = node;

		this.timer;
		//Check if it's a flash or a image gallery
		var imgs = this.node.getElementsByTagName('img');

		if(imgs.length > 0){
			this.initImageGallery(imgs);
		}
		else{
			this.initFlash();
		}
	}
}

PAmultimediaWidget.prototype.initImageGallery = function(imgs){
	//No need for a gallery if there's just one image
	if(imgs.length < 2){
		return false;
	}
	//Time between automatic transitions
	this.time = 5000;

	//this.imgs = imgs;
	this.imgs = new Array();
	for (var i = 0; i < imgs.length; i++){
		this.imgs[i] = imgs[i];
	}
	this.imageslength = imgs.length;

	//Get the height of the images
	var imageHeight = DY.Dom.getHeight(this.imgs[0]);
/*
	//Set padding-top of the node according to image height.
	DY.Dom.setStyle(this.node, 'paddingTop', imageHeight);
	//Now we can set image[0] in absolute position
	DY.Dom.setStyle(this.imgs[0], 'position', 'absolute');
	DY.Dom.setX(this.imgs[0], 0);
	DY.Dom.setY(this.imgs[0], 0);
*/
	this.buttons = new Array();

	//Create a button for every img
	var button;
	var index;

	//Get footer
	this.footers = this.node.getElementsByTagName('p');
	var footer = this.footers[0];

	for (var i = 0; i < this.imageslength; i++){
		//The first button is the rightmost one, so it really belongs to the last image.
		//Here we transform index making the first button point to the last image.
		var index = this.imageslength - (i + 1);
		button = document.createElement('img');
		button.setAttribute('src', '/media/img/common/utils/audiovisual_on.gif');
		button.className = 'button';

		this.node.insertBefore(button, footer);
		this.buttons[this.buttons.length] = new PAmultimediaWidgetButton(index, button, this);
	}

	/*
	//Add a clearer
	var clearer = document.createElement('span');
	clearer.innerHTML = '.';
	clearer.className = 'clearer';
	this.node.appendChild(clearer);
	*/
	//Get position of the leftmost button
	var width = DY.Dom.getWidth(button);
	var marginLeft = parseInt(DY.Dom.getStyle(button, 'marginLeft'), 10);
	var marginRight = parseInt(DY.Dom.getStyle(button, 'marginRight'), 10);

	var pos = (width + marginLeft + marginRight) * this.buttons.length;

	//Set max width for the paragraph
	width = DY.Dom.getWidth(this.node) - (pos + 15);
	for (var i = 0; i < this.footers.length; i++){
		this.footers[i].style.width = width+'px';
	}

	//We need to create a reference to "this" that can be used within the timeout (which runs outside this scope)
	this.obj = 'multimediaWidget';
	eval(this.obj+'=this');

	this.setImage(0);

}

PAmultimediaWidget.prototype.setImage = function(index){
	//Clear timeout for automatic animation
	clearTimeout(this.timer);

	this.resetImages();

	this.imgs[index].style.display = 'block';
	this.footers[index].style.display = 'block';

	this.resetButtons();

	//Transform image index to button index. See "initImageGallery" for details.
	var index = this.imageslength - (index + 1);

	this.buttons[index].setSelected(true);

	//Set timeout for automatic animation
	this.timer = setTimeout(this.obj+'.run()', this.time);
}

PAmultimediaWidget.prototype.resetImages = function(){
	for (var i = 0; i < this.imageslength; i++){
		this.imgs[i].style.display = 'none';
		this.footers[i].style.display = 'none';
	}
}

PAmultimediaWidget.prototype.resetButtons = function(){
	for (var i = 0; i < this.buttons.length; i++){
		this.buttons[i].setSelected(false);
	}
}

PAmultimediaWidget.prototype.run = function(){
	var current;
	//Get currently displaying image
	for (var i = 0; i< this.imageslength; i++){
		if(this.imgs[i].style.display == 'block'){
			current = i;
			break;
		}
	}

	//Add one to current image index
	current++;

	//If we are on the last image, get back to the beginning
	if(current == this.imageslength){
		current = 0;
	}

	this.setImage(current);
}

PAmultimediaWidget.prototype.stop = function(){
	clearTimeout(this.timer);
}

PAmultimediaWidget.prototype.initFlash = function(){

}


function PAmultimediaWidgetButton(index, node, parent){

	this.node = node;

	var parent = parent;
	var index = index;
	//"click" function will be invoked on the node scope,
	//so we create an inner function to get access to previous variables.
	this.click = function(){
		parent.setImage(index);
	}

	this.setSelected(false);
}

PAmultimediaWidgetButton.prototype.setSelected = function(selected){
	if(selected){
		//Unbind event
		$(this.node).unbind('click');
		this.node.setAttribute('src', '/media/img/common/utils/audiovisual_on.gif');
	}
	else{
		//Bind event
		$(this.node).bind('click', this.click);
		this.node.setAttribute('src', '/media/img/common/utils/audiovisual_off.gif');
	}
}

