/*******************************************************************************
 Copyright (c) 2012 -  Jeff Margenau, Wisconsin Department of Natural Resources
 ------------------------------------------------------------------------------
For questions on use 
Contact: Jeffrey.Margenau@dnr.wi.gov (608)264-6298 for questions
 ------------------------------------------------------------------------------
*******************************************************************************/
var slideshowWidth="375"
var slCaptionx="slCaption"+slideshowWidth
var iC;
var onHomepage=0;
var pauseOn=0;
var playOn=0;
var timerID;
var imageNum = 1;
var interval=16000
var showCaption=1
var showControls=1
var imageArray = new Array()
var dAir=0
var dImp=0
var maxmatch=6,tL=1,ttL=1,matched=false,suggestion='',soFar='',soFarLeft='',matches=0,level=1


function mwi(u,x,y,sc,opt,name){
wn=name==null?Math.round(Math.random()*100):name
if(sc==null)sc=1
if(opt==null)opt=1
window.open(u,wn,'width='+x+',height='+y+',toolbar='+opt+',directories='+opt+',menubar='+opt+',status='+opt+',resizable=1,location='+opt+',scrollbars='+sc+',copyhistory='+opt)
}


document.onkeydown = function(e){check(e)}

//document.onclick=function(e){if(onHomepage==1){var evt=window.event || e ;resetSearch()}}

function sT( level ){
if (isNaN(level))level = 1;var f = document.getElementById('form1');var listbox = f.sTL;var textbox = f.q;soFar = textbox.value.substring(0,tL+1).toString();
if (soFar.length>0){document.getElementById('sTL').style.display='block';document.getElementById('bigSearchDiv').style.height='420px'}else{document.getElementById('sTL').style.display='none';document.getElementById('bigSearchDiv').style.height='315px'}
soFarLeft = soFar.substring(0,level).toLowerCase();matched = false;suggestion = '';matches=0;
for ( var m = 0; m < theList.length; m++ ) {suggestion = theList[m].toString();suggestion = suggestion.substring(0,level).toLowerCase();
if ( soFarLeft == suggestion && maxmatch>matches) {matches++;if(matches==1){listbox.options.length=0;listbox.options[listbox.options.length]=new Option(theList[m],theList[m],true,true);textbox.value=theList[m];
matched = true;ttL=theList[m].length+1;}else{listbox.options[listbox.options.length]=new Option(theList[m],theList[m],false,false)}listbox.size=matches>1?matches:matches+1;}}
if (!matched){listbox.options.length=0;document.getElementById('sTL').style.display='none'}else{sPI(textbox,level,ttL)}
}

function sPI(el, startPos, endPos) {if (typeof el.selectionStart != "undefined") {el.selectionStart = startPos;el.selectionEnd = endPos;} else if (document.selection && document.selection.createRange) {el.focus();el.select();var r = document.selection.createRange();r.moveEnd("character", endPos);r.moveStart("character", startPos);r.select();}tL=startPos+1;
}

function resetSearch(){ttL=1;tL=1;level=1;matched = false;matches=0;suggestion = '';soFar='';document.getElementById('sTL').style.display='none';document.getElementById('form1').q.value='';}

function check(e){
var oldkey
var x = (e) ? e : window.event;//IE 
if (tL>1 && x.keyCode==13) document.getElementById('form1').submit()
if (tL>1 && (x.keyCode==38 && document.getElementById('sTL').options[0].selected==true))document.getElementById('q').focus() 
if (tL>1 && (x.keyCode==8 || x.keyCode==37))tL=tL-2 
if (x.keyCode==9 || x.keyCode==40) {document.getElementById('sTL').focus();} // FF WORKS
//if (x.keyCode==9 || x.keyCode==40) {document.getElementById('sTL').focus();document.getElementById('sTL').options[0].selected=true;alert(document.getElementById('sTL').options[0].value)} // FF WORKS
//if (x.keyCode==9 || x.keyCode==40) {document.getElementById('sTL').focus();;document.getElementById('sTL').options[0].selected=true;} 
//if ((x.keyCode==9 || x.keyCode==40) && oldkey!=40) {document.getElementById('sTL').focus();document.getElementById('sTL').options[0].selected=true;}
if (tL>1 && (x.keyCode==27 || x.keyCode==46 )) resetSearch()
if (tL>1 && (x.keyCode < 16 || (x.keyCode > 16 && x.keyCode < 32) || (x.keyCode > 32 && x.keyCode < 41))) {return false;}else{if (x.keyCode > 31 && x.keyCode < 256) return true;}
oldkey=x.keyCode
}

function buttonize(div,typ, flip){
flipbutcls=flip==1?'tasknavButtonOn':'tasknavButtonOff'
fliptxtcls=flip==1?'tasknavtextOn':'tasknavtextOff'
flipbutcls=flipbutcls=='tasknavButtonOn'?'tasknavButtonOn'+typ:'tasknavButtonOff'+typ
butele='nb'+div
txtele='nt'+div
document.getElementById(butele).className=flipbutcls
document.getElementById(txtele).className=fliptxtcls
}

function grayOut(vis,options,extra,pic,caption) {
pict='';captiont=''  
var options = options || {};
var zindex = options.zindex || 150;
var opacity = options.opacity || 95;
var opaque = (opacity / 100);
var bgcolor = options.bgcolor || '#fff';
var closecolor=bgcolor=='#fff'?'#333':'#fff'
var dark=document.getElementById('dSO');
if (!pic)pic=''
if (!caption)caption=''
if (pic!=''){
picp=pic.lastIndexOf('x')
ph=parseInt(pic.substr(picp+1,3))
pw=parseInt(pic.substr(picp-3,3))}
else{ph=0;pw=0;}
if (!dark) {
var tbody = document.getElementsByTagName("body")[0];
var Shade = document.createElement('div');
Shade.style.position='absolute';
Shade.style.top='0px';
Shade.style.left='0px';
Shade.style.overflow='hidden';
Shade.style.display='none';
Shade.id='dSO';
var Promo = document.createElement('div');
Promo.style.position='fixed';
Promo.style.display='none';
Promo.id='box';
Promo.style.width = pw+2+'px';
Promo.style.height = ph+2+'px';
Promo.style.marginLeft= Math.abs((pw+2)/2)*-1+'px';      
Promo.style.marginTop= Math.abs((ph+2)/2)*-1+'px'; 
Promo.style.textAlign = 'center';
Promo.style.top= "50%";
Promo.style.left="50%";
tbody.appendChild(Promo);
tbody.appendChild(Shade);
dark=document.getElementById('dSO');}
if (vis) {
// Calculate the page width and height
if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
var pageWidth = document.body.scrollWidth+'px';
var pageHeight = document.body.scrollHeight+'px';
} else if( document.body.offsetWidth ) {
var pageWidth = document.body.offsetWidth+'px';
var pageHeight = document.body.offsetHeight+'px';
} else {
var pageWidth='100%';
var pageHeight='100%';
}
dark.style.opacity=opaque;
dark.style.MozOpacity=opaque;
dark.style.filter='alpha(opacity='+opacity+')';
dark.style.zIndex=zindex;
dark.style.backgroundColor=bgcolor;
dark.style.width= pageWidth;
dark.style.height= pageHeight;
dark.style.display='block';

if(extra == 'Y'){
document.body.style.overflow =  'hidden';
document.getElementById('box').style.zIndex = zindex+10;
document.getElementById('box').style.border = 'transparent solid 1px';
document.getElementById('box').style.display = 'block';
document.getElementById('box').style.width = pw+2+'px';
document.getElementById('box').style.height = ph+2+'px';
document.getElementById('box').style.background = '#dedede';

document.body.onclick = function() //attach a event handler to hide both div
{
dark.style.display="none";
document.getElementById("box").style.display = "none";
document.body.style.overflow = 'auto';
}

document.getElementById('box').style.backgroundColor='transparent';
if(pic.lastIndexOf('.jpg')<0){captiont='<div style="text-align:left;padding:30px;border-top:1px solid #ccc;border-right:1px solid #222;border-bottom:1px solid #222; border-left:1px solid #ccc;background:#fff;">'+caption+'</div>';
}else{pict='<img src="'+pic+'" alt="Click to Close" width="'+pw+'" height="'+ph+'" style="border-top:1px solid #ccc;border-right:1px solid #222;border-bottom:1px solid #222; border-left:1px solid #ccc;" /><br /><p style="text-align:left;margin-top:0;padding:5px;background:#666;color:#fff;font-family:verdana, arial;font-size:12px;">'+caption+'</p>'}

document.getElementById('box').innerHTML = '<div style="margin0;background:#ccc;color:'+closecolor+';font-family:verdana, arial;text-align:right;font-size:11px;"><a href="javascript:grayOut(false)" title="Close" style="color:#333;text-decoration:none;">Close <img src="/images/icons/close.gif" width="20" height="20" border="0" alt="Close" /></a></div>'+pict+captiont}} else {dark.style.display='none';}
}

function splashit(ab){
if(ab==1){grayOut(true);document.getElementById("splasher").style.display='block';}
else {grayOut(false);document.getElementById("splasher").style.display='none';}
}


function imgurl(){
if(imgU[imageNum]!='')top.location=imgU[imageNum]
}

function imageItem(i_loc) {
this.image_item = new Image();
this.image_item.src = i_loc;
}

function iLoc(imageObj) {
return(imageObj.image_item.src)
}

//this can replace the other two functions
function getImage(dir) {
imageNum=dir<1?imageNum-1:imageNum+1;
var new_image = iLoc(imageArray[imageNum]);
if(showCaption==1){
document.getElementById(slCaptionx).innerHTML =iC+cA[imageNum]
iC=ttx==1?imageNum+'. ':''
}
document.images.switchImage.alt=tA[imageNum]
return(new_image);
}

function playit(){
if(imageNum==imageArray.length-1){imageNum=0}
timerID=setTimeout('playit()',interval);
document.images.switchImage.src = getImage(1);
}

function slideButtons(dir){
img=dir==1?getImage(1):getImage(-1)
document.images.switchImage.src = img;
}

var pOs=0
var pOs2=0
var xe=0
function moveit(x){
pOs++
if((iPos[xe][0]+pOs) < 800)da('Info_Layer').style.posLeft=iPos[xe][0]+pOs
setTimeout("moveit(x)",10)
}

var steps = 27
var showz = 200
var sleep = 0
var loop = false
var textfadetimer;
var sp1="<span style='color:{COLOR};'>"
var colors = new Array(steps);
var color = 1;
var text = 1;
var step = 1;
var a0=""
var a1=""
var hex=255

function fade(c,stepx,t) {
showz=500
iC=ttx==1?imageNum+'. ':''
if(color<(steps-2))var text_out = sp1.replace("{COLOR}", colors[color])+iC+cA[imageNum]+"</span>"
else var text_out=sp1.replace("{COLOR}", fcolor)+iC+cA[imageNum]+"</span>"
document.getElementById(slCaptionx).innerHTML = text_out;
color += step; 
if (color >= colors.length-1) { color=1;return
step = -1
}
if (color == 0) {step = 1
}
textfadetimer=setTimeout("fade()", (color == colors.length-2 && step == -1) ? showz : ((color == 1 && step == 1) ? sleep : 50))
}

function getFadeColors(ColorA, ColorB, Colors) {
len = Colors.length; 
if (ColorA.charAt(0)=='#') ColorA = ColorA.substring(1);
if (ColorB.charAt(0)=='#') ColorB = ColorB.substring(1);
var r = HexToInt(ColorA.substring(0,2));
var g = HexToInt(ColorA.substring(2,4));
var b = HexToInt(ColorA.substring(4,6));
var r2 = HexToInt(ColorB.substring(0,2));
var g2 = HexToInt(ColorB.substring(2,4));
var b2 = HexToInt(ColorB.substring(4,6));
var rStep = Math.round((r2 - r) / len);
var gStep = Math.round((g2 - g) / len);
var bStep = Math.round((b2 - b) / len);

for (i = 0; i < len-1; i++) {
Colors[i] = "#" + IntToHex(r) + IntToHex(g) + IntToHex(b);
r += rStep;
g += gStep;
b += bStep;
}
Colors[len-1] = ColorB;Colors[23] = ColorB // make sure we finish exactly at ColorB
}

function IntToHex(n) {
var result = n.toString(16);
if (result.length==1) result = "0"+result;
return result;
}

function HexToInt(hex) {
return parseInt(hex, 16);
}

function resizeImgOO(el){ 
function imgRatio(){ return (el.height / el.width);} 
function holderRatio(){ return (el.offsetParent.offsetHeight / el.offsetParent.offsetWidth);} 
function fitToContainer(){
if(imgRatio>holderRatio){el.height = el.offsetParent.offsetHeight;} 
else {el.width = el.offsetParent.offsetWidth;} 
}
this.imgRatio = imgRatio; 
this.holderRatio = holderRatio; 
this.resize = fitToContainer; 
}
var img = new resizeImgOO(document.getElementById('imgId'));
//img.resize();

