// ======================= http://meddle.dzygn.com =======================
// Scroshed v.0.30: giving fished dhtml version its scroller capabilities
// GBooshed v.0.10: giving fished guestbook dynamic interactivity features 
// Scroshed/G-booble can be used provided copyright notice remains intact
// ======================= Copyright (c) 2001-2003 =======================                   

// =======================================================================
// ======================== fished BASIC FUNCTIONS =======================
// =======================================================================


// checking for 'feedback' string in the address
// This allows us to add an extra functionality while reusing all the code.
str=location.href;
if (str.search("feedback") != -1) {var useGB = 1;var filename = "feedback"}

if (str.search("weblog") != -1) var filename = "weblog";
if (str.search("about") != -1) var filename = "about";
if (str.search("portfolio") != -1) var filename = "portfolio";
if (str.search("images") != -1) var filename = "images";
if (str.search("locuras") != -1) var filename = "locuras";
if (str.search("links") != -1) var filename = "links";

// checking for konqueror and/or Mac to force former to static 
// and solve the CSS negative % positioning bug/trick for mac:
function checker(){
	this.agent=navigator.userAgent.toLowerCase();
	this.mac=this.agent.indexOf("mac") > -1;
	this.ie = this.agent.indexOf("msie") != -1;
	this.iemac = (this.mac && this.ie)?1:0;
    	this.konq = this.agent.indexOf("konqueror") != -1;
}
var brw = new checker();

// checking for sliding at that moment to avoid multiple clicks:
slidin=0; 

// scrolling height. Bit smaller than clipped area to make sure all lines get readable.
// I know some of them get 'cropped' but it'd be a mess trying to fix that cause we use 
// different line heights and font sizes.
scrH = 170; 


// hack for mozilla little balls positioning (it's all css but we need this hack):
bolaY=0;
if (parseInt(navigator.appVersion) >= 5) {
	bolaY=4;
	}
// end hack

// Following Arrays and vars. will be used to automatically create/reference objects on the fly.
var tabs = new Array ();
var text = new Array ();
var bolasCont = new Array ();
var bolita = new Array ();
var cont = new Array ();
var depth = new Array ();
var buffer = new Array ();
var slider = new Array ();
var gbolas = new Array();
var gbentry = new Array();
var numEntries;

// The current active tab panel
var active = 0; 
// Here we'll use as a buffer to add layers:
var addToWrapper;
var addToForm;

//=================================================================
// Starter function: basic objects and positionings 
//=================================================================
init = function(){
	// como no puedo hacer $HTTP_ACCEPT_LANGUAGE; entonces, 
	// si no hay cookie el texto de los tips sera en ingles:
	if (!(tipsLang=getCookie("meddleLanguage"))) tipsLang = "getEng";
		if (tipsLang == "getEng") {
			menuTipTxt = "Main menu";
			panelsTipTxt = "Tabs navigation";
			submenuTipTxt = "Fast scroller";
			prefsTipTxt = "Show or Hide Preferences";
			prefsLinkTxt = "Preferences";
			prefsTipsTxt = "Tips onload";
		}
		else {
			menuTipTxt = "Menu Principal";
			panelsTipTxt = "Paneles de Navigacion";
			submenuTipTxt = "Scroller rapido";
			prefsTipTxt = "Mostrar o Esconder Preferencias";
			prefsLinkTxt = "Preferencias";
			prefsTipsTxt = "Etiquetas Ayuda";
		}

	// reemplazamos el menu soso por el menu con imagenes y rollover:
	if (!(menuLang=getCookie("meddleLanguage"))) menuLang = "en";
		if(menuLang=="getEng" || menuLang=="en") menuLang="en"; else menuLang="sp";
	
	// adding layer to body via DOM:
	

	// Creating some basic objects from 'div's, via API.
	wrapperObj = new make("wrapper");
	

	// These are created dynamically, via innerHTML:
	addToWrapper='<div id="meroDiv"></div><div id="rayaDiv"></div>';
	addToWrapper+='<div id="sectionDiv"></div>';
	addToWrapper+='<div id="menuTipDiv"><span>'+menuTipTxt+'</span></div>';
	addToWrapper+='<div id="panelsTipDiv"><span>'+panelsTipTxt+'</span></div>';
	addToWrapper+='<div id="submenuTipDiv"><span>'+submenuTipTxt+'</span></div>';
	addToWrapper+='<div id="prefsTipDiv"><span>'+prefsTipTxt+'</span></div>';
	addToWrapper+='<div id="screenDiv"></div><div id="upDiv"></div><div id="dnDiv"></div>';
	addToWrapper+='<div id="bolasCont0Div" class="bolasCont"></div>';
	addToWrapper+='<div id="bolasCont1Div" class="bolasCont"></div>';
	addToWrapper+='<div id="bolasCont2Div" class="bolasCont"></div>';
	addToWrapper+='<div id="tabsDiv"></div>';
	addToWrapper+='<div id="timer"></div>';
	
	// then added to the wrapper:
	wrapperObj.elm.innerHTML += addToWrapper;
	



	// We create some more objects now:
	loadingObj=new make("loading");
	reefObj=new make("headerDiv");
	meroObj=new make("meroDiv"); 
	rayaObj=new make("rayaDiv");
	screenObj=new make("screenDiv");

	// tips object creation and timers to hide them:
	menuTipObj=new make("menuTipDiv"); 
	panelsTipObj=new make("panelsTipDiv");
	submenuTipObj=new make("submenuTipDiv");
	prefsTipObj=new make("prefsTipDiv");

	if (!(tipsPrefsVal=getCookie("meddleTips"))) tipsPrefsVal = "tipsOn";
		if (tipsPrefsVal == "tipsOn") {
			menuTipObj.show();
			panelsTipObj.show();
			submenuTipObj.show();
			prefsTipObj.show();
			setTimeout("hideTips()",5000);
		}

	
	

	menubarObj=new make("menubarDiv");
	sectionObj=new make("sectionDiv"); 
	meroObj.moveTo(-150,100);meroObj.show();
	rayaObj.moveTo(800,145);rayaObj.show();
	screenObj.setOpacity(20);screenObj.show();
	
	// Section name should be extracted from location and change image accordingly:
	// sectionObj.elm.innerHTML="<img src='/gpx/"+filename+".gif' id="sectionImg" />";
	sectionObj.bgi("/v2/"+filename+".gif");


	// start calculations and DOM stuff to create scrollers etc.:
	computeTabs();
		
	// Centering wrapper in page:
	wrapElement();
	window.onresize = wrapElement;
}

//=================================================================
// Fade out + Hide the tips in 5 seconds:
//=================================================================
hideTips = function(){
	menuTipObj.fadeTo(0,10,10,'menuTipObj.hide()'); 
	panelsTipObj.fadeTo(0,10,10,'panelsTipObj.hide()'); 
	submenuTipObj.fadeTo(0,10,10,'submenuTipObj.hide()'); 
	prefsTipObj.fadeTo(0,10,10,'prefsTipObj.hide()'); 
}

//=================================================================
// Toggles Preferences form:
//=================================================================
var prefsOn = 0; 
togglePrefs = function(){
	if (prefsOn == 0) showPrefs();
	else hidePrefs();
}

showPrefs = function(){
	prefsOn=1;
	//prefsBGObj.show();
	//prefsObj.show();
}

hidePrefs = function(){
	prefsOn=0;
	//prefsBGObj.hide();
	//prefsObj.hide();
}

// ==========================================================================
// ======================== Scroshed v.0.30 FUNCTIONS =======================
// ==========================================================================

//=================================================================
// Basically creates the Tab Panels and calculates depth
//=================================================================
computeTabs = function(){
	// getting tabs links from all 'h1' in textDiv, via DOM.
	tabsFromHtml=document.getElementById('textDiv').getElementsByTagName('h1');
	for (i=0;i<tabsFromHtml.length;i++){ 
		tabs[i]=tabsFromHtml[i].lastChild.nodeValue; 
	}

	// Creating tabs objects from textCont_Div, via API.
	textObj=new make("textDiv");textObj.show();
	for (i=0;i<tabs.length;i++){
	text[i]=new make("textCont"+i+"Div");
	if (i>0){text[i].hide();}
	text[i].timeSlide(0,0,2000,-1);
	}

	// Creating tabs menu object from tabsDiv, via API.
	tabsObj = new make("tabsDiv");tabsObj.show();
	var tabmenu = "";
	// Inserting links in tabs menu object, via innerHTML:
	for (i=0;i<tabs.length;i++){
	tabmenu += '<a href="#" onclick="chooseTab('+i+')">'+tabs[i]+'</a>';
	if (i<tabs.length-1) tabmenu += " :: ";
	}
	tabsObj.elm.innerHTML=tabmenu;

	for (i=0;i<tabs.length;i++){
	// Setting content, depth, and slider position for each tab, via API.
	cont[i]=text[i].getH();
	depth[i]=parseInt(cont[i]/scrH)+1;
	slider[i]=1;
	}

	if (useGB==1) {insertGB();}
	createMenus(); 
}

//=================================================================
// Basically creates the Scrollers for each Tab Panel
//=================================================================
createMenus = function(){
	// Adding up/down fast menu navigation, via innerHTML.
	upObj=new make("upDiv"); 
	upObj.elm.innerHTML='<a href="#" onclick="goUp(); return false"><img src="/v2/arriba.gif" alt="up - arriba" width="12" height="12"  /></a>';
	dnObj=new make("dnDiv");
	dnObj.elm.innerHTML='<a href="#" onclick="goDn(); return false"><img src="/v2/abajo.gif" alt="down - abajo" width="12" height="12"  /></a>';

	// Adding fast menu for each tab from bolasCont_Div, via API/innerHTML.
	for (i=0;i<tabs.length;i++){
	bolasCont[i]=new make("bolasCont"+i+"Div");
	buffer[i]='<div id="bolita'+i+'Div" class="bolita"></div>';
		for (j=0;j<depth[i];j++){
		m=parseInt(j+1);
		buffer[i] += '<div id="capita0'+j+'" class="bola"><a href="#" onclick="goThere(\'' + j + '\')"><img src="/v2/bola.gif" alt="'+m+' / '+depth[i]+'" width="8" height="8" /></a></div>';	
		}
	bolasCont[i].elm.innerHTML=buffer[i];
	bolita[i]=new make("bolita"+i+"Div");
	bolita[i].moveTo(0,bolaY);
	if (i>0) {bolasCont[i].hide();}
	}

	callAnimations();
}

//=================================================================
// Tab Panels General show/hide function 
//=================================================================
chooseTab = function (a){
	// Firstly we hide all tabs and fast menus.
	for (i=0;i<tabs.length;i++) {
	text[i].hide(); 
	bolasCont[i].hide(); 
	if (useGB==1) {text[i].css.display="none";}
	}
	// We change the active parameter.
	active=a;
	// And show just the active tab and fast menu.
	text[active].show(); if (useGB==1) {text[active].css.display="block";}
	bolasCont[active].show();
}

//=================================================================
// Scroller functions in action
//=================================================================
goThere = function(w){
if (slidin==0){
	slidin=1;  w=parseInt(w);	
	text[active].timeSlide(0,-scrH*w,1750,-1);
	bolita[active].timeSlide(0,bolaY+(13*w),1750,-1,null,'resetSlide()');
	slider[active]=w+1;
	}
}

goUp = function(){
if (slider[active]>1 && slidin==0){
	slidin=1;
	text[active].timeSlideBy(0,scrH,1750,-1);
	slider[active]--;
	bolita[active].timeSlideBy(0,-13,1750,-1,null,'resetSlide()');
	}
}

goDn = function(){
if (slider[active]<depth[active] && slidin==0){
	slidin=1;
	text[active].timeSlideBy(0,-scrH,1750,-1);
	slider[active]++;
	bolita[active].timeSlideBy(0,13,1750,-1,null,'resetSlide()');
	}
}

resetSlide = function(){
slidin=0;
}



// ==========================================================================
// ======================== GBooshed v.0.10 FUNCTIONS =======================
// ==========================================================================

//=================================================================
// Basically inserts balls to guestbook tab for each entry:
//=================================================================
insertGB = function(){
	// Creating and inserting in textCont1Div as many 'div' as 'h2' are in textCont1Div, via DOM.
	h2FromCont=document.getElementById('textCont1Div').getElementsByTagName('h2');
	
	numEntries = h2FromCont.length;
	
	// Creating guestbook balls, via innerHTML/API.
	var gbb = "";
	var gbReversed = numEntries;
	for (i=0;i<numEntries;i++){
	var alt = h2FromCont[i].lastChild.nodeValue;
	n = parseInt(i+1);
	gbb += '<div id="gbola'+i+'" class="gbBola"><a href="#" onclick="stopGBshowMSG('+i+')"><img src="/v2/bola.gif" title="'+alt+'" />'+gbReversed+'</a></div>';
	gbReversed--;
	}
	// Be careful with this object. It could change!
	text[1].elm.innerHTML += gbb;
	
	for (i=0;i<numEntries;i++){
	gbolas[i] = new make("gbola"+i);
	gbolas[i].moveTo(aleat(0,375),aleat(0,100));
	gbolas[i].slideBolas();
	gbentry[i]=new make("entry"+i+"Div");
	}

	// Creating layer with link to close messages, via DOM.
	var closeLyr = document.createElement("div");
		closeLyr.id = "closeMSG";
	var closeLink = document.createElement("a");
		var closeLinkTxt = document.createTextNode("X");
		closeLink.appendChild(closeLinkTxt);
		closeLyr.appendChild(closeLink);
	document.getElementById("wrapper").appendChild( closeLyr );
		// add events here:
		clEv = document.getElementById("closeMSG");
		addEvent(clEv, "click", stopTimer, false);
	closeMSGObj = new make("closeMSG");
	timerObj = new make("timer");
}

//=================================================================
// Guestbook General show/hide functions 
//=================================================================
var activeMSG;
var countdown = 10000; 
stopGBshowMSG = function(n) {
	// Firstly we hide all the balls.
	for (i=0;i<numEntries;i++){ 
	    gbolas[i].hide(); 
	}
	// Then we show the selected Guestbook message.
	gbentry[n].show(); 
	closeMSGObj.show();
	activeMSG = n;
	showTime(); 
	// And replace it again for the balls in x seconds.
	gbTimer = setTimeout("resumeGBhideMSG(activeMSG)",countdown);
}

resumeGBhideMSG = function(n) {
	// Firstly we hide the selected Guestbook message.
	gbentry[n].hide(); 
	closeMSGObj.hide();
	clearTimeout(cdTimer);
	timerObj.elm.innerHTML = "";
	countdown = 10000;
	// Then we show again all the balls.
	for (i=0;i<numEntries;i++){ 
	    gbolas[i].show(); 
	}
}

callAnimations = function() {
	// hide loading div and position all other elements in page:
	loadingObj.hide();
	menubarObj.timeSlide(10,0,500,0);
	meroObj.timeSlide(435,215,3000,-1);
	rayaObj.timeSlide(70,145,4000,-1);
	sectionObj.timeSlide(11,5,2000,-1);
	

}

stopTimer = function() {
	clearTimeout(gbTimer);
	clearTimeout(cdTimer);
	resumeGBhideMSG(activeMSG);	
}

showTime = function() {
	countdown = countdown - 500;
	timerObj.elm.innerHTML = (countdown/1000)+" s";
	if(countdown>500) cdTimer = setTimeout("showTime()",500);
}

//=================================================================
// header script extracted from http://13thparallel.net
//=================================================================
// Utility functions for importing a stylesheet on the fly.
// The href argument is a string.
// The optional media argument is a string.
// The return value is false if none of the methods are supported.
//=================================================================

addStyleSheet = function(href, media) {
	// Trying createStyleSheet first, because that seems to work best with IE5/Mac.
	if (document.createStyleSheet) {
		document.createStyleSheet(href);
		return true;
	}
	else if (document.getElementsByTagName && document.createElement) {
		var head = document.getElementsByTagName("head")[0];
		if (!head || !head.appendChild) return false;
		
		var link = document.createElement("link");
		link.rel = "stylesheet";
		link.type = "text/css";
		if (media) link.media = media;
		link.href = href;
		link = head.appendChild(link);
		
		return true;
	}
	return false;
}



//===============================================================================================
// If browser supports enough DOM we change CSS on the fly and start javascript functions.
//===============================================================================================

if (document.getElementById && document.getElementsByTagName && document.createElement 
&& !brw.konq && typeof document.getElementsByTagName("head")[0].innerHTML != "undefined" ) {

	// Disabling the basic sylesheet and creating a new one for the dynamic interface.
	// It would be easier to set a new value to the href of the link with the basic stylesheet,
	// but the href property is officially read-only, so we'll treat it as such.
	
	// document.getElementsByTagName("style")[0].disabled = true;
	document.getElementById("doubleplusgoodstyle").disabled = true;
addStyleSheet("/v2/reef.css", "screen, tv");
	
	if (document.all && navigator.userAgent.toLowerCase().indexOf("mac") == -1)	{
		document.getElementsByTagName("html")[0].style.overflow = "hidden";
	}

onload=init;
}


// stupid names note:
// Scroshed stands for Scro(ller) for (fi)shed.
// GBooshed stands for G(uest)Boo(k) for (fi)shed.


