var currentBalloon = null;
var disable = false; //for the balloons
var min=10; //for text resize
var max=14; //for text resize

var successBalloon = function(o){
	if (o.responseText !== undefined) {
		allparts = o.responseText;
		partarray1 = allparts.split("<!--beginContentPull-->");
		partarray2 = partarray1[1].split("<!--endContentPull-->");
		contentpart = partarray2[0];
        document.getElementById('balloon_contents').innerHTML = contentpart;
	}
}
var failureBalloon = function(o){
	if(o.responseText !== undefined){
		alert('Failure in Balloon Ajax');
	}
}
var callbackBalloon =
{
  success:successBalloon,
  failure:failureBalloon,
  argument: { foo:"foo", bar:"bar" }
};


function increaseSize() {
   var p = document.getElementsByTagName('*');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=max) {
         s += 1;
      }
      p[i].style.fontSize = s+"px"
   }
}
function decreaseSize() {
   var p = document.getElementsByTagName('*');
   for(i=0;i<p.length;i++) {
      if(p[i].style.fontSize) {
         var s = parseInt(p[i].style.fontSize.replace("px",""));
      } else {
         var s = 12;
      }
      if(s!=min) {
         s -= 1;
      }
      p[i].style.fontSize = s+"px"
   }   
}
function changeTop(a) { //this is for the switcharoo section
		var temp = a.parentNode.getAttribute('id');
		//url="../inc/ajax/data_"+temp+".html";
		var topsection = document.getElementById('topsection'); //get the top div
		var featureArray = YAHOO.util.Dom.getElementsByClassName('topthumb') //get all "thumbnails" inside topsection
		for (i=0;i<featureArray.length;i++) { //loop through all DLs
			featureArray[i].style.display="none"; //hide every top thumbnail - we will go back and make the new one visible
		}
		var newFeature = document.getElementById(temp + "top"); //get the top thumbnail at the top of the switcharoo that needs to be made visible
		var newId = newFeature.getAttribute('id');
		document.getElementById('loadingtop').style.display = "block";
		window.setTimeout("showTop('" + newId + "')", 1000);
		//newFeature.style.display="block";
		//document.getElementById('active_feature').removeAttribute('id');
		theActive = YAHOO.util.Dom.getElementsByClassName('active_feature')[0];
		YAHOO.util.Dom.replaceClass(theActive, 'active_feature', 'bottomThumb')
		thisone = a.parentNode;
		thisone.className = "active_feature";
	}
function showTop(a) {
	document.getElementById('loadingtop').style.display="none";
	document.getElementById(a).style.display="block";
}
function findcustomlinks() { //on page load, look for all links that can be added to the custom tile. Check to see if they are already in the custom tile.
	var theHTML = readCookie("custom");
	if (theHTML.length > 5) { //check if there is data in the custom tile
		var acc = document.getElementById('acc_content');
		if (acc) {
			var linkArray = acc.getElementsByTagName('a');
			for (i=0;i<linkArray.length;i++) {
				if (linkArray[i].getAttribute('rel') == 'customlink') { //all links that can be added to the custom tile have a 'rel' value of 'customlink'
					var searchString = linkArray[i].getAttribute('href') + '"'; //add the quote to the end so we make sure we are searching for full href 
					if (theHTML.split(searchString).length > 1) { //check to see if the href is in the custom tile
						linkArray[i].parentNode.getElementsByTagName('a')[1].getElementsByTagName('img')[0].setAttribute('src', '../img/icons/star_blue.gif');
						linkArray[i].parentNode.getElementsByTagName('a')[1].getElementsByTagName('img')[0].setAttribute('title', 'This link is in your custom tile');
					}
				}
			}
		}
	}
}
			
function addtocustom(a) { // "a" is the link that sent the request, which will directly follow the link we want to save. To find the link to save, we need to go to the parent LI and grab the first anchor tag.
							// This assumes that the "star" anchor and the target anchor are always the first two links in an element, for example in a list. If this is not the case, change 
							// the markup by wrapping the target and the star in an element, like a span or div.
	var theHTML = readCookie("custom");
	var theLink = a.parentNode.getElementsByTagName('a')[0].getAttribute('href');
	var theTitle = a.parentNode.getElementsByTagName('a')[0].firstChild.nodeValue;
	newHTML = '<li style="padding:0 0 4px 0;"><span style="font-size:85%;"><a href="#" onclick="customDelete(this);">[x]</a> &nbsp;</span><a href="' + theLink + '">' + theTitle + '</a></li>';
	if (theHTML.length > 5) {
		theHTML += newHTML;
	} else {
		theHTML = newHTML;
	}
	var theImage = a.getElementsByTagName('img')[0]; //get the star image
	theImage.setAttribute('src', '../img/icons/star_blue.gif'); //show blue star so user knows an action was performed
	theImage.setAttribute('title', 'This link has been added to your custom tile');
	setCookie("custom", theHTML, 10000);
}
function alphaBackgrounds() {
	//this function is for explorer - all code is in the explorer only javascript
	//must keep this placeholder function for firefox
}

function testClear() { //stop the bubble from disappearing
		if (window.theTimeout) { //test to see if the bubble timer is set to make the bubble dissappear
			clearTimeout(window.theTimeout);
		}
}
	function setTimeSlow() { //start the bubble timer to make the bubble dissappear
		window.theTimeout = setTimeout('hideBalloon()',500);
	}
	function setTimeFast() { //start the bubble timer to make the bubble dissappear
		window.theTimeout = setTimeout('hideBalloon()',150);
	}
	
function addLoadEvent(func)
{
  var oldonload = window.onload;
  if (typeof window.onload != 'function')
  {
    window.onload = func;
  }
  else
  {
    window.onload = function()
    {
      oldonload();
      func();
    }
  }
}

function findPosX(obj)
{
    var curleft = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curleft += obj.offsetLeft;
            obj = obj.offsetParent;
        }
        if ( obj != null )
            curleft += obj.offsetLeft;
    }
    else if (obj.x)
        curleft += obj.x;
    return curleft;
}

function findPosY(obj)
{
    var curtop = 0;
    if (obj.offsetParent)
    {
        while (obj.offsetParent)
        {
            curtop += obj.offsetTop;
            obj = obj.offsetParent;
        }
        if ( obj != null )
            curtop += obj.offsetTop;
    }
    else if (obj.y)
        curtop += obj.y;
    return curtop;
}

function hideBalloon()
{
	if ( !document.getElementById ) return;
	if ( disable ) return; //if balloons are disabled
	if ( currentBalloon == null) return; //if the balloon is already hidden
	theLi = document.getElementById(currentBalloon);
	theUl = theLi.parentNode;
	liArray = theUl.getElementsByTagName('li');
	/*for (i=0;i<liArray.length;i++) {
		var myFx = new Fx.Style(liArray[i], 'opacity').start(1);
	}*/
  
  var balloon = document.getElementById("balloon");
  var balloon_contents = document.getElementById("balloon_contents");
  var balloon_disable = document.getElementById("balloon_disable");
  if ( balloon )
  {
    balloon.style.display = 'none';
    currentBalloon = null;
  }
  if ( balloon_contents )
  {
    balloon_contents.style.display = 'none';
    currentBalloon = null;
  }
  if ( balloon_disable )
  {
    balloon_disable.style.display = 'none';
    currentBalloon = null;
  }
  return false;
}
function showDisable() {
	var theDisable = document.getElementById('balloon_disable');
	theDisable.style.display = 'none';
	var theContent = document.getElementById('balloon_contents');
	var newContent = "<p>Are you sure you want to disable Info Balloons?</p><br /><form><input type='button' value='Disable Balloons' onclick='disablePerm()' />&nbsp;<input type='button' value='Cancel' onclick='hideBalloon();' /></form>";
	theContent.innerHTML = newContent;
}
function disableBalloons() {
	hideBalloon();
	disable = true;
}
function disablePerm() {
	disableBalloons();
	setCookie("balloons", "disabled", 10000);
}
function displayBalloon(anLi)
{
  if ( !document.getElementById ) return;
  if ( disable ) return;
	testClear(); //keeps the balloon from timing out when mousing over a different image
	//fadedown(anLi);
  var liId = anLi.getAttribute('id');
  if ( liId == currentBalloon ) return false;
  
  currentBalloon = liId;
  var objX = findPosX(anLi);
  var objY = findPosY(anLi);
  var balloon = document.getElementById("balloon");
  var balloon_contents = document.getElementById("balloon_contents");
  var balloon_disable = document.getElementById("balloon_disable");
  if ( balloon )
  {
    if ( balloon.childNodes )
    {
      while ( balloon.childNodes.length > 0 )
      {
        balloon.removeChild(balloon.childNodes[0]);
      }
    }
	if (balloon_contents) {
		document.getElementsByTagName("body")[0].removeChild(balloon_contents);
	}
	if (balloon_disable) {
		document.getElementsByTagName("body")[0].removeChild(balloon_disable);
	}
	
    /*var closeElt = document.createElement('img');
    closeElt.setAttribute("id","closebox");
    closeElt.setAttribute("src","http://www.kung-foo.tv/images/closebox.png");
    closeElt.onclick = function(){ return hideBalloon(); }
    balloon.appendChild(closeElt);*/
	var newElt = document.createElement('div'); //create the bubble background image
	newElt.setAttribute("id","balloon_back");
	balloon.appendChild(newElt); //append the image to the balloon div
    var newElt = document.createElement('div'); //create the contents of the balloon
    newElt.setAttribute("id","balloon_contents");
	document.getElementsByTagName("body")[0].appendChild(newElt); //append the contents to the html page - it will be absolutely positioned since the div contains links and will be over an IE hacked transparent png file - if it was appended to the balloon the links would not be clickable
	var contentObj = document.getElementById("balloon_contents");
	contentObj.onmouseover = function() {
		testClear();} //keep the balloon from dissappearing if the mouse scrolls onto it
	contentObj.onmouseout = function() {
		setTimeFast();} //make the balloon dissappear if the mouse scrolls off of it
	var newElt = document.createElement('div'); //create the disable button
	newElt.setAttribute("id","balloon_disable");
	newElt.innerHTML = '<a onclick="showDisable();">Disable</a>';
	document.getElementsByTagName("body")[0].appendChild(newElt); //append the contents to the html page - it will be absolutely positioned
	var contentObj = document.getElementById("balloon_disable");
	contentObj.onmouseover = function() {
		testClear();} //keep the balloon from dissappearing if the mouse scrolls onto it
	contentObj.onmouseout = function() {
		setTimeFast();} //make the balloon dissappear if the mouse scrolls off of it
	var curUrl = location.href;
	
	/*
	var findWWW = curUrl.split('www.');
	var findWWWlength = findWWW.length;
	if (findWWWlength > 1) {
		var url = 'http://www.kansas.gov/inc/ajax/' + currentBalloon + ".html"; //this absolute URL needs to change
	} else {
		var url = 'http://kansas.gov/inc/ajax/' + currentBalloon + ".html"; //this absolute URL needs to change
	}
	*/
	url = "/inc/ajax/" + currentBalloon + ".html";
	//loadXMLDoc2(url, 'balloon_contents');
	var request = YAHOO.util.Connect.asyncRequest('GET', url, callbackBalloon);
	
	if ((liId == "mycarousel-item-4") || (liId == "mycarousel-item-5") || (liId == "mycarousel-item-6") || (liId == "mycarousel-item-10") || (liId == "mycarousel-item-11") || (liId == "mycarousel-item-12") || (liId == "mycarousel-item-16") || (liId == "mycarousel-item-17") || (liId == "mycarousel-item-18")) {
		balloon.style.left = (objX-225) + 'px';
		document.getElementById('balloon_back').setAttribute('id', 'balloon_back2');
		alphaBackgrounds();
		document.getElementById('balloon_contents').style.left = (objX-215) + 'px';
		document.getElementById('balloon_disable').style.left = (objX - 215) + 'px';
	} else {
    	balloon.style.left = (objX+75) + 'px';
		document.getElementById('balloon_back').setAttribute('id', 'balloon_back1');
		alphaBackgrounds();
		document.getElementById('balloon_contents').style.left = (objX+95) + 'px';
		document.getElementById('balloon_disable').style.left = (objX + 345) + 'px';
	}
	balloon.style.display = 'block';
	balloon.style.top = (objY+130) + 'px';
	document.getElementById('balloon_contents').style.top = (objY+155) + 'px';
    document.getElementById('balloon_disable').style.top = (objY+290) + 'px';
  }
  return false;
}

function addBalloons() 
{
  if ( !document.getElementsByTagName &&
       !document.getElementById ) return;
  var nav = document.getElementById("scrollimages");
  if ( !nav ) return;
  //var ul = nav.getElementsByTagName("ul")[0];
  var liElts = nav.getElementsByTagName("li");
  if ( liElts.length > 0 )
  {
    for ( var i = 0; i < liElts.length; i++ )
    {
      var curLi = liElts[i];
      curLi.onmouseover = function()
        { 
		return displayBalloon(this); }
	  curLi.onmouseout = function()
		{ return setTimeSlow(); }
    }
  }
  var divElt = document.createElement('div');
  divElt.setAttribute("id", "balloon");
  divElt.onmouseover = function() 
	{ 
	testClear();} //keeps the bubble from dissappearing if it is moused over
  divElt.onmouseout = function()
	{ setTimeFast(); } //starts the bubble timer to make the bubble dissapear
	
  document.getElementsByTagName("body")[0].appendChild(divElt);
}

var theBalloons = readCookie("balloons"); //check to see if balloons disabled
if (theBalloons == 'disabled') {
	disable = true;
} else {
	disable = false;
}


