if($.browser.msie) {
	try {
		document.execCommand('BackgroundImageCache', false, true);
    } catch(err) {}
}

$(document).ready( function() {
  addIds(document.getElementById('subnavList'));
  addIds(document.getElementById('navList'));
  
  var nav = document.getElementById('nav');
  var lis = nav.getElementsByTagName('li');
  for (var j = 0; j < lis.length; j++) {
    lis[j].onmouseover = function() {
      var divs = this.getElementsByTagName('div');
      var a = this.getElementsByTagName('a')[0];
      if (a) {
        a.className = 'active';
      }
      for (var i = 0; i < divs.length; i++) {
        if (divs[i].className == 'dropdown') {
            divs[i].className = 'dropdown visible';

			/* Calculate position of dropdown div, and hide the search if needed */
			var link_pos = parseInt( $(a).offset().left ) - parseInt( $('#container').offset().left ); 
			if(link_pos > 400){
				$("#search").css('visibility','hidden');
			}

        }
      }
    }
    lis[j].onmouseout = function() {
      var divs = this.getElementsByTagName('div');
      var a = this.getElementsByTagName('a')[0];
      if (a) {
        a.className = '';
      }
      for (var i = 0; i < divs.length; i++) {
        if (divs[i].className == 'dropdown visible') {
            divs[i].className = 'dropdown';
			$("#search").css('visibility','visible');
        }
      }
    }
  }
}
);

function trim(stringToTrim) {
  return stringToTrim.replace(/^\s+|\s+$/g,"");
}

//Add ids to list items
function addIds(ul) {
  var list_items = ul.childNodes;
  for(var item in list_items) {
    if(list_items[item].childNodes) {
      var list_children = list_items[item].childNodes;
      for(var child in list_children) {
        if(list_children[child].nodeName && list_children[child].nodeName.toLowerCase() == 'a') {
          var id = trim(list_children[child].innerHTML);
          id = id.toLowerCase();
          id = id.replace(/\s/g,'_');
          list_items[item].id = id;
        }
      }
    }
  }
}

$(document).ready(function(){
    $.each($.browser, function(i) {
        if($.browser.msie){
            $('#nav #navList').find('a#first').each(function(){
                var text = $(this).text().toLowerCase().replace(/\s/g,'_');
                var new_width = $('#nav li#'+text+' a').eq(0).css('width');
                $(this).css('width',new_width);
            });
        }
    });

	$('#read_more_link').click(function(){
		$('#container').css('position','relative');
		$('#content-wide').css('background-color','#FFFFFF');
		$('#full_text').html($('#full_text_placeholder').html());
		$('#full_text').show();
		var content_wide_height = $('#content-wide').outerHeight();
		if ($.browser.mozilla) { 
			$('#full_text').css({'top':'200px','width':'730px'});
		}
		$('#full_text').css('height',content_wide_height+'px')
		$(this).css('visibility', 'hidden');
	});



	/* Add hover to next button */
	$("#next_button").hover(
		function () {
			$(this).attr('src','/atf/cf/{97c6d565-bb43-406d-a6d5-eca3bbf35af0}/btn-next-on.gif');
		}, 
		function () {
			$(this).attr('src','/atf/cf/{97c6d565-bb43-406d-a6d5-eca3bbf35af0}/btn-next.gif');
		}
	);

	/* Add hover to go buttons */
	$(".go_button").hover(
		function () {
			$(this).attr('src','/atf/cf/{97c6d565-bb43-406d-a6d5-eca3bbf35af0}/btn-go-on.gif');
		}, 
		function () {
			$(this).attr('src','/atf/cf/{97c6d565-bb43-406d-a6d5-eca3bbf35af0}/btn-go.gif');
		}
	);

	/* Add textsize button for FF */
	if( $("#ql_email").length>0 && $("#ql_print").length>0 && $.browser.mozilla ){
		$("#ql_print").parent().after(' <a href="Javascript: setFaceSize(1)"><img src="/atf/cf/{97c6d565-bb43-406d-a6d5-eca3bbf35af0}/textsize_icon_1.gif" border="0"></a><img src="/atf/cf/{97c6d565-bb43-406d-a6d5-eca3bbf35af0}/textsize_icon_2.gif" border="0"><a href="Javascript: setFaceSize(0)"><img src="/atf/cf/{97c6d565-bb43-406d-a6d5-eca3bbf35af0}/textsize_icon_3.gif" border="0"></a>');
	}
});

function close_link(){
	$('#container').css('position','');
	$('#content-wide').css('background-color','transparent');
	$('#full_text').hide();
	$('#read_more_link').css('visibility', 'visible');
}

function change_toolkit_image(size){

	if(size=='small'){
		/* Change big 'world' image to small one */
		$('#bigworld').replaceWith('<div id="world"><img src="/atf/cf/{97c6d565-bb43-406d-a6d5-eca3bbf35af0}/world.gif" alt="" /></div>');
		$('#title h2').before('<div class="link"><a class="orange" href="http://www.kintera.org/site/lookup.asp?c=lkIXLbMNJrE&b=5136575">&lt;&nbsp;TOOLKIT HOME</a></div>');
	}else{
		/* Change small 'world' image to big one */
		$('#world').replaceWith('<div id="bigworld"><img src="/atf/cf/{97c6d565-bb43-406d-a6d5-eca3bbf35af0}/bigworld.gif" alt="" /></div>');
		$('#title div.link').remove();
	}

}
