// JavaScript Document

var currentPageName;

window.addEvent('domready',function(){
	try {
	MooInitialize();
	} catch(e) {alert(e);}
	
});


function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

function openPage(pageName, id) {
	
	currentPageName = pageName;
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}

	var url=pageName+".php";
	
	if (pageName == 'clientes_content') {
		var url=url+"?id="+id;
	}


	elementId = "mainContainer";
	xmlhttp.onreadystatechange=stateChanged;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
		
}

function openFlash(pageName) {

	switch (pageName) {
		case 'serv_entr_comu': 
		case 'serv_prod_edit': 
		case 'serv_prog_comu': 
			flashName = 'servicios';
			break;
		case 'clientes_content': 
			flashName = 'clientes';
			break;
		default:
			flashName = pageName;
			break;
	}
		
str1 = "<embed style='width:745px;height:174px;' src='images/"+ flashName +".swf' menu='false' "
+"quality='high' width='745' height='174' wmode='transparent' type='application/x-shockwave-flash' "
+"pluginspage='http://www.macromedia.com/go/getflashplayer'/>";

	
	document.getElementById("banner").innerHTML = str1;

	
}

function stateChanged() {

	if (xmlhttp.readyState==4)	{
		document.getElementById(elementId).innerHTML=xmlhttp.responseText;
		//alert(document.getElementById(elementId).innerHTML);
		$('mainContainer').fade(0,1);
		try {
		MooInitialize();
		openFlash(currentPageName);
		} catch(e) {alert(e);}

	}
	else {
		$('mainContainer').fade(1,0);
		$('mainContainer').fade(0,1);
		document.getElementById(elementId).innerHTML="<img src=\"images/WhiteLoading.gif\" style=\"height:30px;  width:30px;\" /> ";
	}

}


function MooInitialize() {
	
	Mediabox.scanPage();
	
	if ($('smallContainerLabelNosotros')) {
		$('smallContainerLabelNosotros').addEvent('mouseover', function(e) {
				this.fade(1, 0.5);
		});
	
		$('smallContainerLabelNosotros').addEvent('mouseout', function(e) {
				this.fade(0.5, 1);
		});	
	}
	
	if ($('smallContainerLabelServicios')) {
		$('smallContainerLabelServicios').addEvent('mouseover', function(e) {
				this.fade(1, 0.5);
		});
	
		$('smallContainerLabelServicios').addEvent('mouseout', function(e) {
				this.fade(0.5, 1);
		});
	}
	
	if ($('smallContainerLabelProduccionesEditoriales')) {
		$('smallContainerLabelProduccionesEditoriales').addEvent('mouseover', function(e) {
				this.fade(1, 0.5);
		});
	
		$('smallContainerLabelProduccionesEditoriales').addEvent('mouseout', function(e) {
				this.fade(0.5, 1);
		});
	}
	
	if ($('smallContainerLabelEntrenamiento')) {
		$('smallContainerLabelEntrenamiento').addEvent('mouseover', function(e) {
				this.fade(1, 0.5);
		});
	
		$('smallContainerLabelEntrenamiento').addEvent('mouseout', function(e) {
				this.fade(0.5, 1);
		});
	}
	
	if ($('smallContainerLabelServicioPrensaDifusion')) {
		$('smallContainerLabelServicioPrensaDifusion').addEvent('mouseover', function(e) {
				this.fade(1, 0.5);
		});
	
		$('smallContainerLabelServicioPrensaDifusion').addEvent('mouseout', function(e) {
				this.fade(0.5, 1);
		});
	}
	
	$$('.menuItem').addEvent('mouseover', function(e) {
			this.fade(1, 0.5);
	});

	$$('.menuItem').addEvent('mouseout', function(e) {
			this.fade(0.5, 1);
	});
	
	$$('.moreInfo').addEvent('mouseover', function(e) {
			this.fade(1, 0.8);
	});

	$$('.moreInfo').addEvent('mouseout', function(e) {
			this.fade(0.8, 1);
	});

	$clear(interval);
	
	if (currentPageName == 'clientes_content') {
		setScrollbar('clientesWrap', 'slide');
		setImageSlider('imageWrap', 'list-container', 'slide-container');
		
	}
	else {
		setScrollbar('fullWrap', 'slide');
		setScrollbar('fullWrap2', 'slide2');
	}

}

var slider;

function setScrollbar(container, slideElem)
{
	//alert(document.getElementById('textContainer').scrollHeight);
	// First Example
	var el = $(slideElem),
		textOverflow = $(container);
		

	if (textOverflow) {
		// Create the new slider instance
		slider = new Slider(el, el.getElement('.knob'), {
			steps: 80,	// There are 35 steps
			range: [0],	// Minimum value is 8
			mode: 'vertical',
			wheel: true,
			onChange: function(step){
				try {
				var x = 0;
				if (step == 0) {
					var y = 0;
				} else {
					//var y = document.getElementById('textContainer').scrollHeight * ( step / document.getElementById('textContainer').offsetHeight );
					var y = (textOverflow.getScrollSize().y - textOverflow.getSize().y) * ( step / 80 );
				}
				textOverflow.scrollTo(x,y);
				} catch(e) { alert(e);}
			}
		}).set(0);
		
		$(container).addEvent('mousewheel', function(e) {
			e.stop();
			var step = slider.step - e.wheel * 4;
			slider.set(step);
		});

		var idInterval = 0;

		var scrollDown = function() {
			var step = slider.step + 4;
			slider.set(step);
		}		
		
		var scrollUp = function() {
			var step = slider.step - 4;
			slider.set(step);
		}	
		
		$('scrollDown').addEvent('mousedown', function(e) {
			e.stop();
			idInterval = scrollDown.periodical(100);
		});
		
		$('scrollDown').addEvent('mouseup', function(e) {
			$clear(idInterval);
		});
		
		$('scrollUp').addEvent('mousedown', function(e) {
			e.stop();
			idInterval = scrollUp.periodical(100);
		});
		
		$('scrollUp').addEvent('mouseup', function(e) {
			$clear(idInterval);
		});
		
	}
												  
};

var interval = 0; 

function setImageSlider(wrapperId, listContainerId, imgContainerId){
	
	$clear(interval);
	
	if (wrapperId) {
		var showDuration = 5000; 
		var container = $(wrapperId); 
		var images = $(imgContainerId).getElements('div'); 
		var clientList = $(listContainerId).getElements('div.clientListPicker');
		var currentIndex = 0; 

		/* opacity and fade */ 
		images.each(function(img,i){ 
			if(i > 0) { 
			  img.set('opacity',0); 
			} 
			
			img.addEvents({
				'mouseover': function(){
					$clear(interval);
				},
				'mouseout': function(){
					interval = show.periodical(showDuration); 
				}

			});
			
		}); 

		/* opacity and fade */ 
		clientList.each(function(cli,i){ 
			if(i > 0) { 
   		    	cli.set('opacity',0.3);
			}
			
			cli.addEvents({
				'mouseover': function(){
					if (currentIndex != i) {
						this.fade(0.8);
					}
				},
				'mouseout': function(){
					if (currentIndex != i) {
						this.fade(0.3);
					}
				},
				'click': function(){
					this.fade(1);
					clientList[currentIndex].fade(0.3);
					images[currentIndex].fade('out'); 
					images[currentIndex = i].fade('in');
					$clear(interval);
					/* start the loop again */ 
					interval = show.periodical(showDuration); 
				}
			});
		}); 

		/* start once the page is finished loading */ 

		 var show = function() { 
			images[currentIndex].fade('out'); 
			clientList[currentIndex].fade(0.3);;
			images[currentIndex = currentIndex < images.length - 1 ? currentIndex+1 : 0].fade('in');
			clientList[currentIndex].fade('in');

			porcAvanzado = (currentIndex * 30) * 100 / (clientList.length * 30);
			step = (porcAvanzado * 100) / 80;
			slider.set(step);
			
			
		}; 
	
		interval = show.periodical(showDuration); 
	}
}
