/**
 * @author ncalabrese
 */


var varActiveTab = 'web';
var varLink=""; var varLinkTitle=""; 
var varSwapingSection = 'inactive';
var varLightWIndow = 'off';

onstart();
document.getElementById('menuweb').onclick = swaptoweb;
document.getElementById('menuemail').onclick = swaptomail;
document.getElementById('menumedia').onclick = swaptomedia;

	// funciones iniciales

	function onstart(){
		eval("$('thumbnails').morph({height:'435px'})");
		new Effect.Opacity('thumbnails', { duration: 0.1, from: 1.0, to: 0.0 });
		setTimeout("new Effect.Opacity('thumbnails',  { duration: 0.9, from: 0.0, to: 1.0 });",1000);
		setTimeout("xml_load()",1800);
		previewInfo = 'no';
		/* new Effect.Opacity('authorinfo', { duration: 0.1, from: 1.0, to: 0.7 });
		new Effect.Opacity('moreinfo', { duration: 0.1, from: 1.0, to: 0.7 }); */
		varActiveTab = 'web';
		}	
		
		function xml_load() {display.render(menu, "thumbnails");}

		
		
	// funciones generales
		
	function getThumbnailsHeight(id){
		tumbnailsHeight = Element.getHeight(id);	
		return tumbnailsHeight;
	}	
	
	function addClass (id,clase){e=document.getElementById(id).className=clase}	
	function setDisplay (id,estado){e=document.getElementById(id).style.display=estado;}
	function setVisibility(id,estado){e=document.getElementById(id).style.visibility=estado;}
	
	

	// funciones para cambiar de seccion
	
	function swaptoweb(){
	if(varSwapingSection=='inactive' && varLightWIndow=='off'){
		if(varActiveTab != 'web'){
		varSwapingSection = 'active';
		addClass('menuweb','active'); 
		addClass('menuemail','none'); 
		addClass('menumedia','none'); 
		new Effect.Opacity('thumbnails',  { duration: 0.1, from: 1.0, to: 0.0 });
		setTimeout("setDisplay('mailworks','none');setDisplay('mediaworks','none');setDisplay('webworks','block');",250);
		setTimeout("$('thumbnails').morph({height:'"+getThumbnailsHeight('webworks')+"px'})",300);
		setTimeout("new Effect.Opacity('thumbnails', { duration: 0.7, from: 0.0, to: 1.0 });",1200);
		setTimeout("varSwapingSection = 'inactive';",1900);
		}
		varActiveTab='web';  
		}
	}
		
	function swaptomail(){
	if(varSwapingSection=='inactive' && varLightWIndow=='off'){
		if(varActiveTab != 'email'){
		varSwapingSection = 'active';
		addClass('menuweb','none'); addClass('menuemail','active'); addClass('menumedia','none');  new Effect.Opacity('thumbnails',  { duration: 0.1, from: 1.0, to: 0.0 });
		setTimeout("setDisplay('webworks','none');setDisplay('mediaworks','none');setDisplay('mailworks','block');",250);
		setTimeout("$('thumbnails').morph({height:'"+getThumbnailsHeight('mailworks')+"px'})",300);
		setTimeout("new Effect.Opacity('thumbnails', { duration: 0.7, from: 0.0, to: 1.0 });",1200);
		setTimeout("varSwapingSection = 'inactive';",1900);
		} 
		varActiveTab='email';
		}
	}
		
	function swaptomedia(){
	if(varSwapingSection=='inactive' && varLightWIndow=='off'){
		if(varActiveTab != 'media'){
		varSwapingSection = 'active';
		addClass('menuweb','none');	addClass('menuemail','none'); addClass('menumedia','active');  new Effect.Opacity('thumbnails',  { duration: 0.1, from: 1.0, to: 0.0 });
		setTimeout("setDisplay('webworks','none'); setDisplay('mailworks','none');setDisplay('mediaworks','block'); ",250);
		setTimeout("$('thumbnails').morph({height:'"+getThumbnailsHeight('mediaworks')+"px'})",300);
		setTimeout("new Effect.Opacity('thumbnails', { duration: 0.7, from: 0.0, to: 1.0 });",1200);
		setTimeout("varSwapingSection = 'inactive';",1900);
		} 
		varActiveTab='media';
		}
	}
		
	


	// funciones para cargar los trabajos
	
	function loadImage(var_href,var_title,var_author,var_caption){setVisibility('wrapper','hidden'); setTimeout("hideInfo()",1500);varLightWIndow = 'on';
		myLightWindow.activateWindow({href: var_href,title: var_title,author: var_author,caption: var_caption});}
	
	function loadFlash(var_href,var_title,var_author,var_caption,var_w,var_h){setVisibility('wrapper','hidden'); setTimeout("hideInfo()",1500);varLightWIndow = 'on';
		myLightWindow.activateWindow({href: var_href,title: var_title,author: var_author,caption: var_caption, width: var_w,height: var_h});
		/* document.getElementById('workLink').onclick = launch; */
		setHref(varLink);
		}
	

	// activa el link para ver en detalle los trabajos

	function setHref(varLink){
		document.getElementById('workLink').setAttribute('href',varLink);
		document.getElementById('workLink').setAttribute('target','blank');
		document.getElementById('workLink').setAttribute('title',varLink);
	}

	
	// funciones para mostrar/ocultar la info de los trabajos
	
	var infoState = 'active';
	
	function hideInfo(){
	
		var browserName=navigator.appName; 
		var browserVer=parseInt(navigator.appVersion); 
		
		if (browserVer <= 6 && browserName=="Microsoft Internet Explorer" ) {
		/* dont hide the Works Info*/
		} else  {
				document.getElementById('lightwindow_container').onmouseout = okShowTheInfo;
				document.getElementById('lightwindow_overlay').onmouseover = okHideTheInfo;
				}
		}
		
		
	function okHideTheInfo() {
		if	(infoState == 'active') {
				infoState='inactive';
				new Effect.Opacity('lightwindow_data_slide_inner', { duration: 0.5, to: 0.0 });
				new Effect.Opacity('lightwindow_title_bar_title', { duration: 0.5, to: 0.0 });
		}
	}

	function okShowTheInfo() {
		if	(infoState != 'active') {
				infoState='active';
				new Effect.Opacity('lightwindow_data_slide_inner', { duration: 0.5, to: 1.0});
				new Effect.Opacity('lightwindow_title_bar_title', { duration: 0.5, to: 1.0});
		}
	}



	// Freja Tutorial 3 - http://www.csscripting.com/freja
	Freja.AssetManager.HTTP_METHOD_TUNNEL = null;
	Freja.AssetManager.XSLT_SERVICE_URL = "/xml/srvc-xslt.php";	// For safari only

	// Load Model and Views
	var menu = getModel('xml/works_data.xml');
//	var display = getView('xsl/works_es.xsl');
//	var display_en = getView('xsl/works_en.xsl');
	
	

/** This is high-level function.
 * It must react to delta being more/less than zero.
 */
function handle(delta) {
        if (delta < 0){
			if (varActiveTab=='web') {swaptomail();
			}else if (varActiveTab=='email') {swaptomedia();}
			
		}else if (delta > 0){
			if (varActiveTab=='media') {swaptomail();
			}else if (varActiveTab=='email') {swaptoweb();}

		}
}

/** Event handler for mouse wheel event.
 */
function wheel(event){
        var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera)
                        delta = delta;
        } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail/3;
        }
        /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
        if (delta)
                handle(delta);
        /** Prevent default actions caused by mouse wheel.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
        if (event.preventDefault)
                event.preventDefault();
	event.returnValue = false;
}

/** Initialization code. 
 * If you use your own event management code, change it as required.
 */

//		if (window.addEventListener)
//		        /** DOMMouseScroll is for mozilla. */
//		        window.addEventListener('DOMMouseScroll', wheel, false);
//		/** IE/Opera. */
//		window.onmousewheel = document.onmousewheel = wheel;

// Event.observe($('thumbnails'), 'DOMMouseScroll', wheel, false);
// Event.observe($('thumbnails'), 'mousewheel', wheel);

Event.observe($('nav'), 'DOMMouseScroll', wheel, false);
Event.observe($('nav'), 'mousewheel', wheel);

Event.observe(document, 'keypress', function(event){ if(event.keyCode == Event.KEY_LEFT) swapToLeft();});
Event.observe(document, 'keypress', function(event){ if(event.keyCode == Event.KEY_RIGHT) swapToRight();});

function swapToLeft() {
			if (varActiveTab=='media') {swaptomail();
			}else if (varActiveTab=='email') {swaptoweb();}}

function swapToRight() {
			if (varActiveTab=='web') {swaptomail();
			}else if (varActiveTab=='email') {swaptomedia();}}

