// check browser ///////////////////////
agent = navigator.userAgent.toLowerCase(); 
mac = (agent.indexOf("mac")!=-1);
win = (!this.mac) ?true:false;
w3c = (document.getElementById) ?true:false;
iex = (document.all) ?true:false;
ns4 = (document.layers) ?true:false;

//// define if cross-browser code shall be used ////
var crossCompatible = false
var moveMenu = false

var resize_thumbs_container = false;

var plus = '<img src="images/plus.gif" alt="" />'; //'[+]';
var minus = '<img src="images/minus.gif" alt="" />'; //'[-]';

//// CONTENT LOADING SCRIPTS ////

function showDescription(id) {
	var text = document.getElementById('more_text_'+id)
	text.style.display = (text.style.display=='block') ? 'none' : 'block'
	
	var link = document.getElementById('more_link_'+id)
	link.innerHTML = (link.innerHTML=='more...') ? 'less...' : 'more...'
}

function changeDivContent(id,content) {
	document.getElementById(id).innerHTML = content
}

function showHideDiv(id,display) {
	document.getElementById(id).style.display = display;
}

function showImage(img,w,h,imgid,id) {	
	original_width = w
	original_height = h
	
	pageSize = getPageSize()
	
	total_width = (pageSize[2] - 240)   // SCREEN WIDTH !!!
	total_height = (pageSize[3] - 130) // SCREEN HEIGHT !!!
	
	if (original_height > total_height) {
		h = total_height
		w = (original_width * total_height)/original_height
	}
	
	if ((w > total_width) && (((h * total_width)/w) < total_height)) {
		original_height = h
		original_width = w
		w = total_width
		h = (original_height * total_width) / original_width
	}
	
	imgexp = img.split("/")
	name = imgexp[imgexp.length-1].replace(/\_/g," ").replace(".jpg","")
	
	imgname = '<p><strong>'
		+'<a class="back" href="javascript:closeImage();">&#171; back to images</a>'
		+'</strong></p>'
		
		+'<p><strong>['+imgid+'] '+name+'</strong> '
		+'<a class="back" href="'+img+'" target="_blank">'
		//+plus
		+'[actual size]'
		+'</a>'
		+'</p>'
	
	//// navigation ////
	pid = (imgid-1)
	nid = (imgid+1)
	pimg = (imageList[pid]) ? imageList[pid] : 0
	nimg = (imageList[nid]) ? imageList[nid] : 0
	parrow = '';
	narrow = '';
	
	if (pimg) {
		parrow = "<a class=\"arrow left\" href=\"javascript:showImage('"+pimg[0]+"','"+pimg[1]+"','"+pimg[2]+"',"+pid+")\">"
		//parrow += "<img src=\"\" alt=\"\">"
		parrow += '&laquo; prev ['+pid+']'
		parrow += "</a>"
	}
	
	if (nimg) {
		narrow = "<a class=\"arrow right\" href=\"javascript:showImage('"+nimg[0]+"','"+nimg[1]+"','"+nimg[2]+"',"+nid+")\">"
		//narrow += "<img src=\"\" alt=\"\">"
		narrow += '['+nid+'] next &raquo;'
		narrow += "</a>"
	}
	
	//alert(img)
	imgcontent = ''
		+ parrow
		+ '<a href="javascript:closeImage()">'
		+ '<img id="bigimage" class="image" src="'
		//+ 'wm.php?src='
		+ img +'" width="'+w+'" height="'+h+'" />'
		+ '</a>'
		+ narrow
	
	changeDivContent("imagesname",imgname)
	changeDivContent("imagesdiv",imgcontent)
	
	showHideDiv("images","block");
}

function closeImage()
{
	showHideDiv("images","none")
	
	changeDivContent("imagesname",'')
	changeDivContent("imagesdiv",'')
}

function __moveDiv(targetDiv,horPos,verPos) {
	d = document.getElementById(targetDiv).style
	hor = d.left
	ver = d.top
	d.left = horPos
	d.top = verPos
}

function __openImage(link) {
	//alert(link);
	vars = 'width=400,height=400,resize=0,scrolling=auto,status=1'
	w = open(link,'popup',vars)
	w.focus()
}

function __resizeImage(w,h) {
	ww = document.getElementById('bigimage').width
	hh = document.getElementById('bigimage').height
	p = 100
	if (ww > w) {
		text = plus
		p = Math.floor((w * 100) / ww)
	} else {
		text = minus
		p = 100 //Math.floor((w * 100) / ww)
	}
	newlink = 'javascript:resizeImage('+ww+','+hh+')'
	
	document.getElementById('bigimage').width = w
	document.getElementById('bigimage').height = h
	document.getElementById('bigimg').href = newlink
	document.getElementById('bigimg').innerHTML = text
	document.getElementById('percent').innerHTML = p
}

function __resizeThumbsContainer()
{
	var pageSize = getPageSize();
	
	var hh = document.getElementById('header').offsetHeight
		+ document.getElementById('data').offsetHeight
		+ document.getElementById('caption').offsetHeight
		//+ document.getElementById('thumbs').offsetHeight
		//+ document.getElementById('images').offsetHeight
	
	//alert('page:'+pageSize[3]+'\nhh: '+hh)
	var h = pageSize[3] - hh - 60
	if (resize_thumbs_container) document.getElementById('thumbs').style.height = h+'px'
}

//// LOADING LAYER SCRIPTS //////
function hidelayer() {
	if (iex || ns4){
		if (ns4) document.loading.visibility = "hidden"
		else loading.style.visibility = "hidden"
	} else document.getElementById("loading").style.visibility = "hidden"
	loaded = true
}

function modifyLoadLayer(inner) {
	if (!iex) {
		if (iex || ns4){
			if (ns4) document.loader_text.innerHTML = inner
			else loader_text.innerHTML = inner
		} else {
			document.getElementById("loader_text").innerHTML = inner
		}
    }
}

function showLoadLayer(inner) {
	//alert(document.getElementById("loading").style.visibility)
	document.getElementById("loading").style.visibility = "visible"
	document.getElementById("loader_text").innerHTML = inner
}

function writeLoadingLayer() {
	var layer = ''
	layer += '<div id="loading">'
	layer += '<div id="loader" onclick="javascript:hidelayer();">'
	layer += '<b>FRWRD.NET</b><br /><b>process: <span id="loader_text">loading HTML...</span></b>'
	layer += '</div>'
	layer += '</div>'
    
	document.write(layer)
}

function showFooterData(id) {
	document.getElementById("footer").style.display = "block"
	document.getElementById("footer_project").innerHTML = "<strong>"+id+"</strong>";//"<a href=\"javascript:get_project('"+id+"')\">"+id+"</a>"
}


//// DIV ROLLOVER SCRIPT ////
function changebg(obj) {
	pref = obj.parentNode.className.substr(0,obj.parentNode.className.indexOf("_"))
	if (obj.parentNode.className != pref+'_sel' && obj.parentNode.className != pref+'_seen') {
		obj.parentNode.className = ((obj.parentNode.className == pref+"_off") ? pref+"_on" : pref+"_off")
	}
}

function visited(obj) {
	//items = document.getElementById('itemcontainer').childNodes
	items = getElementsByClassName("item_sel");
	for (i=0; i<items.length; i++) {
		items[i].className = 'item_seen'
		/*
		pref = item.className.substr(0, item.className.indexOf("_"))
		alert(pref)
		if (item.className != pref+'_sel') {
			item.className = pref+"_off"
		}
		*/
	}
	
	pref = obj.parentNode.className.substr(0,obj.parentNode.className.indexOf("_"))
	obj.parentNode.className = pref+"_sel"
}

function __showname(obj,flag) {
	if (flag) {
		a_nombre = obj.firstChild.src.toString().split("/");
		nombre = a_nombre[a_nombre.length-1].replace("th_","").replace(".jpg","").replace(/\_/g," ").toUpperCase();
		
		//// tooltip (not used)
		//x = obj.firstChild.offsetLeft + obj.firstChild.width + 8;
		//document.getElementById('tooltip').style.left = x+4;
		//document.getElementById('tooltip').innerHTML = nombre;
		//document.getElementById('tooltip').style.display = "block";
		
		//// caption ////
		document.getElementById('caption').innerHTML = "<p>IMAGE: <strong>"+nombre+"</strong></p>";
		
	} else {
		//// tooltip (not used)
		//document.getElementById('tooltip').style.display = "none";
		//document.getElementById('tooltip').innerHTML = "";
		
		//// caption ////
		document.getElementById('caption').innerHTML = "";
	}
}

function __resizeme() {
	w = screen.availWidth-80
	h = screen.availHeight-80
    window.moveTo(40,40)
    window.resizeTo(w,h)
}

//// move and resize window /////			
function moveAndResize(w,h) {
	if (w == null) {
    	w = 600
    	h = 450
	}
    l = ((screen.availWidth - w) / 2)
    t = ((screen.availHeight - h) / 2 - 40)
    window.moveTo(l,60)
    window.resizeTo(w,h)
}


// Hides the layer onload
function loader(){
	if(ie || nn){
		if(nn) document.loading.visibility = 'hidden'
		else loading.style.visibility = 'hidden'
	}
}

//// safemail anti-spam script ////
function safemail(name, domain, display) {
    displayed=(typeof(display)=="undefined") ? name+"@"+domain : display
    document.write('<a href=mailto:' + name + '@' + domain + '>' + displayed + '</a>')
}


//// opens a window ////////////////
function openWin(url, name, width, height, kind) {
	var my_win
	var resize = 1
	var scroll = "auto"
	var full = 0
	var l = 0
   	var t = 0
   	var status = 1
	if (kind == "ws") {
	 	w = ((screen.width - 40) - 10)
	 	h = ((screen.height - 40) - 75)
	 	l = 20
	 	t = 20
	 	
	 } else if (kind == "fs") {
	 	if (navigator.userAgent && navigator.userAgent.indexOf("MSIE")>=0 
				&& (navigator.appVersion.indexOf("Win") != -1)) {
   			w = screen.width
   			h = screen.height
	 		full = 1
	 		
	 	} else {
	 		h = screen.height - 40
	 		w = screen.width
	 		
	 	}
	 } else if (kind == "fix") {
		h = height + 32
	 	w = width + 20
	 	l = ((screen.width - w) / 2)
	 	t = ((screen.height - h) / 2 - 40)
		resize = 0
		scroll = 0
		status = 1
		
	 } else if (kind == "scroll") {
		h = height
	 	w = width
	 	l = ((screen.width - w) / 2)
	 	t = ((screen.height - h) / 2 - 40)
		scroll = 1
		
	 } else {
	 	h = height;
	 	w = width;
	 	l = ((screen.width - w) / 2)
	 	t = ((screen.height - h) / 2 - 40)
	 	
	 }
	my_win = open(url, name, "width="+ w +",height="+ h 
		+",left=" + l + ",top=" + t 
		+ ",directories=0,location=0,menubar=0,scrollbars=" + scroll
		+ ",status=" + status + ",toolbar=0,fullscreen=" + full 
		+ ",resizable=" + resize + "");
	my_win.focus();
}

//// opens a window ////////////////
function openVideo(url, vname, vwidth, vheight, vkind) {
	var output = url.split("&")
	var video = new Array();
	
	for (i=0; i<output.length; i++) {
		pair = output[i].split("=");
		video[pair[0]] = pair[1];
	}
	
	/*
	name	= fullpipe
	file	= fullpipe_web.mov
	w		= 208
	h		= 112
	d		= true
	*/
	
	name = video.name
	file = video.file
	filepath = '../media/'+name+'/'+file
	width = video.width
	height = video.height
	//d = video_url[4]
	
	videoname = '<p><small>'
	videoname += '<a href="javascript:closeImage();">&#171; back to images</a>'
	videoname += '</small></p>'
	videoname += '<p><strong>'+name+'</strong>'
	//videoname += '<a href="'+img+'" target="_blank">'+plus+'</a>'
	videoname += '</p>'
	
	videocontent = '<div id="video" align="center" valign="middle">'
	videocontent += '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" codebase="http://www.apple.com/qtactivex/qtplugin.cab" height="'+(height+32)+'" width="'+width+'" name="fphi">'
	videocontent += '<param name="controller" value="true">'
	videocontent += '<param name="autoplay" value="true">'
	videocontent += '<param name="src" value="'+filepath+'">'
	videocontent += '<param name="bgcolor" value="#999999">'
	videocontent += '<param name="cache" value="false">'
	videocontent += '<embed pluginspage="http://www.apple.com/quicktime/download/" type="video/quicktime" height="'+(height+32)+'" width="'+width+'" name="fphi" cache="false" bgcolor="#999999" src="'+filepath+'" autoplay="true" controller="true">'
	videocontent += '</object></div>'
	
	changeDivContent("imagesname",videoname)
	changeDivContent("imagesdiv",videocontent)
	
	showHideDiv("images","block");
}

//// resize window to given width/height ////
function shrinkMe(my_w,my_h) {
	if (my_w == null) {
		my_w = 300
		my_h = 200
	}
	my_l = ((screen.width - my_w) / 2)
	my_t = ((screen.width - my_h) / 2 - 40)
	window.resizeTo(my_w,my_h)
	window.moveTo(my_l,my_t)
}

//// resize and move (centered pop-up) window ////
function resizeAndMove(w,h) {
	if (w == null) {
    	w = 600
    	h = 450
	}
    l = ((screen.width - w) / 2)
    t = ((screen.height - h) / 2 - 40)
    window.resizeTo(w,h)
    window.moveTo(l,t)
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize()
{
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
		
	// all but Explorer Mac
	} else if (document.body.scrollHeight > document.body.offsetHeight) {
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
		
	// Explorer Mac...would also work in Explorer 6 Strict,
	// Mozilla and Safari
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	// all except Explorer
	if (self.innerHeight) {
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
		
	// Explorer 6 Strict Mode
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	
	// other Explorers
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height
	// less than height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width
	// less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

/*
	Developed by Robert Nyman, http://www.robertnyman.com
	Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/
var getElementsByClassName = function (className, tag, elm){
	if (document.getElementsByClassName) {
		getElementsByClassName = function (className, tag, elm) {
			elm = elm || document;
			var elements = elm.getElementsByClassName(className),
				nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
				returnElements = [],
				current;
			for(var i=0, il=elements.length; i<il; i+=1){
				current = elements[i];
				if(!nodeName || nodeName.test(current.nodeName)) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	else if (document.evaluate) {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = "",
				xhtmlNamespace = "http://www.w3.org/1999/xhtml",
				namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
				returnElements = [],
				elements,
				node;
			for(var j=0, jl=classes.length; j<jl; j+=1){
				classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
			}
			try	{
				elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
			}
			catch (e) {
				elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
			}
			while ((node = elements.iterateNext())) {
				returnElements.push(node);
			}
			return returnElements;
		};
	}
	else {
		getElementsByClassName = function (className, tag, elm) {
			tag = tag || "*";
			elm = elm || document;
			var classes = className.split(" "),
				classesToCheck = [],
				elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
				current,
				returnElements = [],
				match;
			for(var k=0, kl=classes.length; k<kl; k+=1){
				classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
			}
			for(var l=0, ll=elements.length; l<ll; l+=1){
				current = elements[l];
				match = false;
				for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
					match = classesToCheck[m].test(current.className);
					if (!match) {
						break;
					}
				}
				if (match) {
					returnElements.push(current);
				}
			}
			return returnElements;
		};
	}
	return getElementsByClassName(className, tag, elm);
};
