/*************************************************************************
  This code is from Dynamic Web Coding at www.dyn-web.com
  Copyright 2001-4 by Sharon Paine 
  See Terms of Use at www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/

var strHTML = '';

function initScrollLayer() {
  // arguments: id of layer containing scrolling layers (clipped layer), id of layer to scroll, 
  // if horizontal scrolling, id of element containing scrolling content (table?)
  var wndo = new dw_scrollObj('wn', 'lyr1', 't1');
  
  // pass id's of any wndo's that scroll inside tables
  // i.e., if you have 3 (with id's wn1, wn2, wn3): dw_scrollObj.GeckoTableBugFix('wn1', 'wn2', 'wn3');
  dw_scrollObj.GeckoTableBugFix('wn');
  
  // this allows us to position the div with the current asset centred, on image and video pages only  
  var strOffset=0;
  var lyr1obj = document.getElementById('lyr1');
  if(document.location.pathname.indexOf('/image-')!=-1 || document.location.pathname.indexOf('/video-')!=-1){ 		  	
  	var tdid = document.getElementById('curAsset');
  	if(tdid) strOffset = tdid.offsetLeft + (tdid.clientWidth / 2) - 267;
  	if(strOffset > lyr1obj.clientWidth-534) strOffset = lyr1obj.clientWidth-534;
  	if(strOffset < 0) strOffset = 0;
  }
	strOffset = '-' + strOffset.toString() + 'px';	
	lyr1obj.style.left = strOffset;
	configureScrollButtons(wndo.maxX,strOffset.replace("px",""));	 
}		
		
function ajaxLoader(url,id){
	// reset the record of media chooser loading progress for each new loading
	assetsLoaded=false;
	mediaChooserIsLoaded=false;
	//reset image counter
	imgLoadingCounter = 0;
				
	if (document.getElementById)
		var x = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();

	if (x){
		x.onreadystatechange = function(){
			if (x.readyState == 4 && x.status == 200){
				el = document.getElementById(id);
				el.innerHTML = x.responseText;
				mediaChooserLoaded();
			}
		}
		x.open("GET", url, true);
		x.send(null);
	}
}
	    
function PageQuery(q){
	if(q.length > 1) 
		this.q = q.substring(1, q.length);
	else 
		this.q = null;
	
	this.keyValuePairs = new Array();
	if(q){
		for(var i=0; i < this.q.split("&").length; i++){
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function(){	return this.keyValuePairs; }
	this.getValue = function(s){
		for(var j=0; j < this.keyValuePairs.length; j++){
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}
	this.getParameters = function(){
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++){
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function(){ return this.keyValuePairs.length; } 
}
	
function queryString(key){
	var page = new PageQuery(window.location.search); 
	return unescape(page.getValue(key)); 
}
	
function displayItem(key){
	if(queryString(key)=='false')
		document.write("you didn't enter a ?name=value querystring item.");
	else
		document.write(queryString(key));
}


/* ------------------------------
// MEDIA CHOOSER
-------------------------------*/

var mediaChooserState ='';
var sPath = window.location.pathname;
var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
if (sPage.substr(0, 6) == 'video-')
    mediaChooserState = 'video';
else if (sPage.substr(0, 6) == 'image-')
    mediaChooserState = 'image';
else if (sPage.substr(0, 4) == 'more')
    mediaChooserState = '';
else if (sPath.substr(0, 13) == '/eco-regions/')
    mediaChooserState = 'image';
else
    mediaChooserState = 'image';
imgLoadingCounter = 0;
assetsLoaded=false;
mediaChooserIsLoaded=false;
currentAssetQSstart = window.location.href.indexOf("?");
if (currentAssetQSstart >= 0)
	currentAsset = window.location.href.substring(0,currentAssetQSstart);
else
	currentAsset = window.location.href;
		
if (document.images){
	preload_image = new Image(16,17); 
	preload_image.src="/images/mediaChooser/link_go_btn.gif";
}

function changeMediaChooserState(state){	
	mediaChooserState = state;

	if (state == 'image'){
		if (document.getElementById('videosLinkBt')){
			document.getElementById('videosLinkBt').src='/images/mediaChooser/video.gif';
			document.getElementById('videosLink').style.color='#ffffff';
		}
	}
	if (state == 'video'){
		document.getElementById('imagesLinkBt').src='/images/mediaChooser/image.gif';
		document.getElementById('imagesLink').style.color='#ffffff';
	}
}

function MCImageLinkMouseOut() {
	if (mediaChooserState!='image'){
		document.getElementById('imagesLink').style.color='#ffffff';
		document.getElementById('imagesLinkBt').src='/images/mediaChooser/image.gif';
	}
}

function MCVideoLinkMouseOut() {
	if (mediaChooserState!='video'){
		document.getElementById('videosLinkBt').src='/images/mediaChooser/video.gif';
		document.getElementById('videosLink').style.color='#ffffff';
	}
	else
		document.getElementById('videosLink').style.color='#F78D1E';						
}

function mediaChooserAssetLoaded(assetcount,containingId){
	imgLoadingCounter++;
	if (imgLoadingCounter==assetcount){
		assetsLoaded=true;
		if(mediaChooserIsLoaded){
			initScrollLayer();
			hel = document.getElementById('innerLoadingMsg');
			hel.style.visibility = 'hidden';

			jQuery('div#lyr1 a').click(function() {
			    var href = org.arkive.speciesArea.mediaChooser.adjustHref();
			    if (href)
			        this.href = this.href + '#' + href;
			});
		}
	}
}



function mediaChooserLoaded(){		
	mediaChooserIsLoaded=true;
	if(assetsLoaded){
		initScrollLayer();
		hel = document.getElementById('innerLoadingMsg');
		hel.style.visibility='hidden';
	}		
}

function changeMainAsset(newAssetLocation,currentType){
	offset = 0;
	if (mediaChooserState == currentType)
	offset = document.getElementById('lyr1').style.left;			
	document.location=newAssetLocation;return false;
}
				
function configureScrollButtons(maxX,offset)
{
	if(maxX > 0){
		if(offset < 0){
			document.getElementById('dw_scrollLeftEnabled').style.display = "block";
		}else{
			document.getElementById('dw_scrollLeftEnabled').style.display = "none";
		}		
		if(offset <= -maxX){
			document.getElementById('dw_scrollRightEnabled').style.display = "none";
		}else{
			document.getElementById('dw_scrollRightEnabled').style.display = "block";
		}						
	}else{
		// reset to "nothing showing"
		document.getElementById('dw_scrollLeftEnabled').style.display = "none";
		document.getElementById('dw_scrollRightEnabled').style.display = "none";
	}
}
