// ======================= http://meddle.dzygn.com =======================
// custom scroller developed for manuelblancafort.org by Sergi Meseguer
// Free to use for personal projects. Contact meddle for commercial use
// Requires Dan and Michael's api.js, and animation.js files
// see 13thparallel.org/?title=about for more specific Copyright
// ======================= Copyright (c) 2002-2003 =======================                   


// Scoping Navigation methods:
UI  = {
	// properties for da navigation:
	isSlidin		: 0,
	wrapperX 		: 0,
	contentsX 		: 0,
	contentsWidth		: 0,
	scrollBarY 		: 0,
	scrollBarW 		: 0, 
	scrollerHandlerX	: 0,
	scrollerHandlerY	: 0,
	scrollerHandlerW	: 0, 
	// initial duration of scroll animation:
	Vi			: 90000,

		
	init 			: function(){

		// we create layer "inici", contents depend on language:
		// defining text depending on language:
		var iniciTXT = 
		(lang=="cat")?
		"<h1>inici</h1><p>Dirigeixi el ratolí cap a la part de la pantalla que vulgui visualitzar i els elements aniran apareixent de manera automàtica. També pot desplaçar-se per la pàgina usant la barra situada a la part inferior. El menú principal es troba al final de la pàgina. Pot baixar la <a rel='ext' title='versió PDF' href='http://www.manuelblancafort.org/docs/bio_cat.pdf'>versió en PDF</a> [145 KB].</p>"
		:(lang=="esp")?
		"<h1>inicio</h1><p>Dirija el ratón hacia la parte de la pantalla que quiera visualizar y los elementos irán apareciendo de manera automática. También puede desplazarse por la página usando la barra de desplazamiento situada en la parte inferior. El menú principal se encuentra al final de la página.  Puede bajar la <a rel='ext' title='versión PDF' href='http://www.manuelblancafort.org/docs/bio_spa.pdf'>versión en PDF</a> [145 KB].</p>"
		:(lang=="eng")?
		"<h1>start</h1><p>Point your mouse to the part of the screen you want to read and the contents will show up automatically. You can also use the scrollbar at the bottom of this page. Menu is at the right end.  You can download the <a rel='ext' title='PDF version' href='http://www.manuelblancafort.org/docs/bio_eng.pdf'>PDF version</a> [145 KB].</p>"
		:"<h1>start</h1><p>details pour la navigation</p>";

		document.getElementById( 'contingut' ).innerHTML += "<div id=\"inici\">"+iniciTXT+"</div>"; 


		var external = document.getElementsByTagName("a");
		for (var k=0; k<external.length; k++){
			if (external[k].href) {
			var url = external[k].href;
				if (external[k].rel == "ext" || (url.indexOf(document.domain) == -1 && url.indexOf("javascript:") == -1 && url.indexOf("mailto:") == -1))
				{
					var ExternalTxt = "";
					external[k].target = "_blank";
					external[k].title += " " + ExternalTxt;
				}
			}
		}


	
		// initializing objects through 13th api:
		var contingutObj = new make("contingut");
		var wrapperObj = new make("wrapper");
	
		// we create layer "fi", beware with cookies(language) for contents:
		// defining text depending on language:
		var fiTXT = (lang=="cat")?"fi":(lang=="esp")?"fin":(lang=="eng")?"the end":"fi";
		var fiTit = (lang=="cat")?"<h1 class=\"titolweb\">FUNDACIÓ <strong>MANUEL BLANCAFORT</strong></h1>":(lang=="esp")?"<h1 class=\"titolweb\">FUNDACIÓN <strong>MANUEL BLANCAFORT</strong></h1>":(lang=="eng")?"<h1 class=\"titolweb\"><strong>MANUEL BLANCAFORT</strong> FOUNDATION</h1>":"fi";
		document.getElementById( 'menu' ).innerHTML += "<div id=\"fi\">"+fiTit+"<h2>"+fiTXT+"</h2></div>"; 
		var menuObj = new make("menu");
		// hack ie:
		menuObj.bg("rgb(237,28,36)");
	
		// we create layer "guerra", image name depends on language:
		document.getElementById( 'guerra' ).innerHTML = "<img src=\"/imgsvida/guerra"+lang+".gif\"  />"; 
	
		// Adding buttons for da navigation:
		document.getElementById( 'sD' ).innerHTML = "<a href=\"#\" onmouseover=\"UI.goRight()\" onmouseout=\"UI.stopDaThing()\" onclick=\"this.blur\"><img src=\"/imgsvida/transp.gif\" width=\"70\" height=\"420\"/></a>"; 
		document.getElementById( 'sI' ).innerHTML = "<a href=\"#\" onmouseover=\"UI.goLeft()\" onmouseout=\"UI.stopDaThing()\" onclick=\"this.blur\"><img src=\"/imgsvida/transp.gif\" width=\"70\" height=\"420\"/></a>"; 
		
		// init scrollbar and scrollHandler objects through 13th api:
		scrollBarObj = new make("barraDiv"); 
		scrollerHandlerObj = new make("screenDiv"); 
		
	
		// scrollHandler size (scrollerHandlerW size in scrollbar is relative scale of contents/wrapper ratio):
		UI.scrollerHandlerW = Math.floor((wrapperObj.getW()/(contingutObj.getW()/wrapperObj.getW())));
		//document.getElementById( 'screenDiv' ).innerHTML = "<img src=\"/imgsvida/screen.gif\" width=\""+UI.scrollerHandlerW+"\" height=\"5\" />"; 
		scrollerHandlerObj.setW(UI.scrollerHandlerW);
		scrollerHandlerObj.setH(parseInt(scrollBarObj.getH()-2)); //borders 
		
	
		// content size (taken from the CSS through the api):
		UI.contentsWidth = contingutObj.getW();
	
		// scrollbar size (taken from the CSS through the api):
		UI.scrollBarW = wrapperObj.getW();

		// Adding onmousedown event to scrollerHandler (not draggable yet):
		//addEvent(document.getElementById( 'screenDiv' ), "mousedown", UI.startDragObj, false);
		document.getElementById( 'screenDiv' ).onmousedown = UI.startDragObj;

		// center wrappers:
		setTimeout("UI.wrapAll()", 50);
	
		// show all:
		wrapperObj.show();	
		scrollBarObj.show();
		scrollerHandlerObj.show();
		window.onresize = UI.wrapAll;


		// Printing hack for dhtml version:
		//addPrinter();
	},

	// Function to position wrapper and scrollBar onresize: 
	wrapAll 		: function(){
		wrapElement();

		// moz pos. hack:
		if (parseInt(navigator.appVersion) >= 5) wrapperObj.moveBy(0,4);
		else wrapperObj.moveBy(0,-2);
		
		// scrollBar Y dist to wrapper:
		UI.wrapperX = parseInt(document.getElementById( 'wrapper' ).style.left); 
		UI.scrollBarY = parseInt(getPageTop(document.getElementById( 'wrapper' )) + wrapperObj.getH() + 3); 
		scrollBarObj.moveTo(UI.wrapperX,UI.scrollBarY);
	},
	
	// Firing events for scrollerHandler:
	startDragObj 		: function(e){
		document.onmousemove	= UI.drag;
	},

	// Calculates mouse and scrollerHandler positions and calls dragDaThing:
	drag		: function(e){
		if (!e) e = window.event;
		//UI.scrollerHandlerX = (e.layerX)?scrollerHandlerObj.x-UI.scrollerHandlerW:-(getPageLeft(document.getElementById( 'barraDiv' ))-(event.x)); //moz or ie
		UI.scrollerHandlerX = e.clientX - scrollBarObj.x;
		//window.status = UI.dragIniX + " " + UI.scrollerHandlerX + " " + scrollBarObj.x;
		if (UI.scrollerHandlerX >= parseInt(UI.scrollBarW-UI.scrollerHandlerW)) {UI.scrollerHandlerX = parseInt(UI.scrollBarW-UI.scrollerHandlerW-2); var fi = 1} else {var fi = 0};
		UI.contentsX = (((parseInt(contingutObj.getW()))*UI.scrollerHandlerX)/UI.scrollBarW); 
		UI.scrollerHandlerX = (fi==0)?UI.scrollerHandlerX:parseInt(UI.wrapperX+(UI.scrollBarW-UI.scrollerHandlerW));

		document.onmouseup	= UI.endDragObj;
		UI.dragDaThing();
	},

	// Moves scrollerHandler and contents:
	dragDaThing 		: function(){
		this.isSlidin = 1;
		if (this.scrollerHandlerX > 0 && (this.scrollerHandlerX < this.scrollBarW - this.scrollerHandlerW)) {
		scrollerHandlerObj.moveTo(this.scrollerHandlerX,this.scrollerHandlerY);
		contingutObj.moveTo(-this.contentsX,0);
		document.getElementById( 'barraDiv' ).style.borderColor = "black";
		}
		if(document.selection) document.selection.empty();
	},

	// Killing events for scrollerHandler (but onmousedown to allow start again):
	endDragObj 		: function(e){
		document.onmousemove = null;
		document.onmouseup   = null;
		UI.resetDaThing();
	},

	goLeft 			: function(){
		if (this.isSlidin==0 && (this.scrollerHandlerX > 0)) {
			this.isSlidin=1;
			scrollerHandlerObj.timeSlide(0,this.scrollerHandlerY,UI.getNewDuration("l"),0);
			contingutObj.timeSlide(0,0,UI.getNewDuration("l"),0,null,'UI.resetDaThing()');
			document.getElementById( 'barraDiv' ).style.borderColor = "black";
		}
		else UI.stopDaThing();
	},

	goRight 		: function(){
		if (this.isSlidin==0 && (this.scrollerHandlerX < this.scrollBarW - this.scrollerHandlerW) )	{
			this.isSlidin=1; 
			scrollerHandlerObj.timeSlide(parseInt(this.scrollBarW-this.scrollerHandlerW),this.scrollerHandlerY,UI.getNewDuration("r"),0);
			contingutObj.timeSlide(-parseInt(contingutObj.getW()-wrapperObj.getW()),0,UI.getNewDuration("r"),0,null,'UI.resetDaThing()');
			document.getElementById( 'barraDiv' ).style.borderColor = "black";
		}
		else UI.stopDaThing();
	},

	getNewDuration 		: function(dir) {
		// set new scrolling duration depending on contents position
		// dir determines scrolling direction
		if (dir=="r") dist = scrollBarObj.w - this.scrollerHandlerW - scrollerHandlerObj.x;
		else dist = scrollerHandlerObj.x;
		s = (dist * this.Vi)/(this.scrollBarW - this.scrollerHandlerW);
		return s;
	},

	refreshCoords 		: function() {
		this.contentsX = -contingutObj.x; 
		this.scrollerHandlerX = parseInt(this.wrapperX+(this.contentsX*this.scrollBarW)/contingutObj.getW()); 
	},

	resetDaThing 		: function(){
		document.getElementById( 'barraDiv' ).style.borderColor = "rgb(237,28,36)";
		this.isSlidin=0;
		if(document.selection) document.selection.empty();
	},

	stopDaThing 		: function(){
		if (this.isSlidin==1) {
			scrollerHandlerObj.doCancelMoves(true);
			contingutObj.doCancelMoves(true);
		}
		UI.refreshCoords();
		this.isSlidin=0;
		UI.resetDaThing();
	}

}






//===============================================================================================
// If browser supports enough DOM/CSS we add CSS on the fly and start javascript functions.
//===============================================================================================
if (document.getElementById && document.getElementsByTagName && document.createElement 
&& !brw.konq && typeof document.getElementsByTagName("head")[0].innerHTML != "undefined" ) {

		addStyleSheet("/css/vida_dragdrop.css", "screen");
		onload=UI.init;
}
