function ieHover()
{
	var nav = document.getElementById("navigation");
	if (nav)
	{
		var nodes = nav.getElementsByTagName("li");
		for (var i=0; i<nodes.length; i++)
		{
			nodes[i].onmouseover = function() 
			{
				this.className += " hover";
			}
			nodes[i].onmouseout = function()
			{
				this.className = this.className.replace(" hover", "");
			}
		}
	}
}
function initTabs()
{
	var sets = document.getElementsByTagName("ul");
	for (var i = 0; i < sets.length; i++)
	{
		if (sets[i].className.indexOf("tabset") != -1)
		{
			var tabs = [];
			var links = sets[i].getElementsByTagName("a");
			for (var j = 0; j < links.length; j++)
			{
				if (links[j].className.indexOf("tab") != -1)
				{
					tabs.push(links[j]);
					links[j].tabs = tabs;
					var c = document.getElementById(links[j].href.substr(links[j].href.indexOf("#") + 1));

					//reset all tabs on start
					if (c) if (links[j].className.indexOf("active") != -1) c.style.display = "block";
					else c.style.display = "none";

					links[j].onclick = function ()
					{
						var c = document.getElementById(this.href.substr(this.href.indexOf("#") + 1));
						if (c)
						{
							//reset all tabs before change
							for (var i = 0; i < this.tabs.length; i++)
							{
								document.getElementById(this.tabs[i].href.substr(this.tabs[i].href.indexOf("#") + 1)).style.display = "none";
								this.tabs[i].className = this.tabs[i].className.replace("active", "");
							}
							this.className += " active";
							c.style.display = "block";
							return false;
						}
					}
				}
			}
		}
	}
}
if (window.attachEvent && !window.opera){window.attachEvent("onload", ieHover);}
if (window.addEventListener) window.addEventListener("load", initTabs, false);
else if (window.attachEvent) window.attachEvent("onload", initTabs);

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function ToggleTextOnly()
{
    if(readCookie("textonly")==null||readCookie("textonly")=="false")
    {
        createCookie("textonly","true");
        $('#Stylesheet1').attr('href','null');
        $('#TextOnly').attr('style','color:Red; font-weight:bold; font-size:1.6em;');
        //$('TextOnly').css('color','red');
    }
    else
    {
        createCookie("textonly","false");
        $('#Stylesheet1').attr('href','/css/all.css');
        $('#TextOnly').attr('style','');
    }
}



$(document).ready(function(){
    InitPhotoTab();
    InitWelcomeVids();
    InterruptLinks();
    if(readCookie("textonly")=="true")
    {
        $('#Stylesheet1').attr('href','null');
        $('#TextOnly').attr('style','color:Red; font-weight:bold; font-size:1.6em;');
    }
    ClickInitialTab();
    $('input#SelectAll:checkbox').click(function(){
        var checked_status = this.checked;
        $('input:checkbox').each(function(){
            this.checked = checked_status;
        });
    });

    //$('.photobuttons .next').corner('bevel tr br');
    //$('.visual p').corner();
    //var nothing = 5;
});

function InterruptLinks(){
    $("a").click(function(event){
        var myLink = $(this).attr('href');
        var currentUrl = jQuery.url.attr('source').toLowerCase();
        var nothing3=5;
        if(currentUrl!=null)
        {
            if(currentUrl.search('/webreturn/') != -1)
            {
                top.location=myLink;
                event.preventDefault();
            }
        }
        var host = jQuery.url.attr('host');
        var newhost = jQuery.url.setUrl(myLink).attr('host');
        if (newhost!=null)
        {
            newhost = newhost.toLowerCase();
            if((newhost != host))// || (newhost.toLowerCase().search('/documentquery/') == 0))
            {
                var dqCheck = newhost.search(/documentquery/);
                var dsCheck = newhost.search(/documentsingle/);
                var dpCheck = newhost.search(/documentprint/);
                var twitterCheck = newhost.search(/twitter/);
                var ytCheck = newhost.search(/youtube/);
                var fbCheck = newhost.search(/facebook/);
                var gmCheck = newhost.search(/maps\.google/);
                var jsCheck = newhost.search(/javascript/gi);
                var pbCheck = newhost.search(/postback/gi);
                if(dqCheck == -1 && dsCheck == -1 && dpCheck == -1 && twitterCheck == -1 && ytCheck == -1 && fbCheck == -1 && gmCheck == -1 && jsCheck == -1 && pbCheck == -1)
                {
                    event.preventDefault();
                    var browseTo = '/webreturn/?url='+myLink;
                    var nothing3=5;
                    window.location = browseTo;
                }
            }
        }
         //alert("As you can see, the link no longer took you to jquery.com");
     });
}
function InitWelcomeVids(){
    $('#WelcomeVid #KillVid').click(function(){
        $('#WelcomeVid').remove();
    });
//    $('#WelcomeVid').hover(
//        function(){
//            $(this).css('border', '1px solid #ccc');
//        },
//        function(){
//            $(this).css('border','0');
//        }
//    );
}

function InitPhotoTab()
{
    var photoToShow = 1;
    var numPhotos = $('.photoscrollers li').length;
    $('.photobuttons li[class!=next][class!=prev]:gt('+(numPhotos-1)+')').hide();
    $('.photobuttons a').each(function(){
        $(this).click(function(){
            var slide = $(this).attr('rel');
            if(slide == "Prev")
            {
                if(photoToShow > 1)
                {
                    photoToShow = photoToShow - 1;
                }
                else
                {
                    photoToShow = numPhotos;
                }
                $('.photoscrollers li').hide();
                $('.photoscrollers #flex'+photoToShow).show();
            }
            else if (slide == "Next")
            {
                if(photoToShow < numPhotos)
                {
                    photoToShow = photoToShow + 1;
                }
                else
                {
                    photoToShow = 1;
                }
                $('.photoscrollers li').hide();
                $('.photoscrollers #flex'+photoToShow).show();
            }
            else
            {
                $('.photoscrollers li').hide();
                $('.photoscrollers #flex'+slide).show();
            }
        });
    });
}