$(document).ready(function()
{
	initTopBox();
	initPopups();
	initTopMenu();
	initLocationMenu();
	initMenu();
	$('a.present-3level-lnk').click(function(event){
		//event.preventDefault();
	});
	/*
	var myInterval = 0;
	myInterval = setInterval( 'scrollSolutions()', 1500);
	$('#treefrogSolutions').mouseover(
	function() {
		clearInterval(myInterval);
	});
	$('#treefrogSolutions').mouseout(
	
	function() {
		myInterval = 0;
		myInterval = setInterval( 'scrollSolutions()', 1500);
	});
	*/
	randomFrog();
	var tabTimer = 0;
	showTabs();
	$.preloadCssImages();
});
function showQuote() {
	if($('#sideQuoteButton').hasClass("out")) {
		$('#sideQuoteButton a').animate({marginLeft:"-35px"},1000);
		$('#sideQuoteButton').removeClass('out');
		$('#sideQuoteButton').addClass('in');
		$('#quote').fadeIn(1000);
	} else {
		$('#quote').fadeOut(1000);
		$('#sideQuoteButton a').animate({marginLeft:"0"},1000);
		$('#sideQuoteButton').removeClass('in');
		$('#sideQuoteButton').addClass('out');
	}
}
function initMenu()
{
	$("#nav > li").each(function(i, el)
	{
		var drop;
		$(el).hover(
			function ()
			{
				_this = this;
				drop = $(this).find(".drop-second");
				if(drop.length > 0)
				{
					$(this).addClass("hover").css({"position": "relative"});
					$(drop).animate({"height": $(drop).find(".drop-second-h").height()}, function(){$(drop).css({"overflow": "visible"});});
				}
			}, 
			function ()
			{
				// FF 3.5 Mac: this function fires when it shouldn't (fixed in 3.6)
				_this = this;
				$(".drop-second").css({"height": 0, "overflow": "hidden"});
				drop = $(this).find(".drop-second");
				if(drop.length > 0)
				{
					$(_this).removeClass("hover").css({"position": "static"});
				}
			}
		);
	});
	$(".drop-second li.present-3level").each(function(i, el)
	{
		var drop;
		$(el).hover(
			function ()
			{
				_this = this;
				drop = $(this).find(".drop-level3");
				if(drop.length > 0)
				{
					$(this).addClass("hover");
					$(drop).animate({"width": 182}, function(){$(drop).css({"overflow": "visible"});});
				}
			}, 
			function ()
			{
				_this = this;
				$(".drop-level3").css({"width": 0, "overflow": "hidden"});
				drop = $(this).find(".drop-level3");
				if(drop.length > 0)
				{
					$(_this).removeClass("hover");
				}
			}
		);
	});
}
function initTopBox()
{
	$("a#open").click(function()
	{
		$(".open-box").slideToggle("slow").parent().toggleClass("opened");
		load();
	});
}
function initPopups()
{
	var popups = $(".popupMe");
	$(popups).css({"display": "none"}).css({"opacity": 0});

	$("a.popupMe-opener").click(function()
	{
		$(popups).css({"display": "none"}).animate({"opacity": 0});
		var myID = "#" + this.rel;
		var popup = $("#" + this.rel);
		//load it up
		var fID = $(this).attr("title");
		$.post('/_xml/popup.lasso', {folioID: fID, closeID : myID } , function(data) {
			//alert(data);
			$(popup).html(data);
		});

		var _dur = 400;
		var _top = 0;
		if(document.documentElement.scrollTop > document.body.scrollTop)
			_top = document.documentElement.scrollTop;
		else
			_top = document.body.scrollTop;

		if ($.browser.msie) _dur = 0;
		$(popup).css({"display": "block", "top": 100 + _top}).animate({"opacity": 1}, _dur, function() { $(popup).css({"opacity": "auto"});});
		return false;
	});
	$("a.popupMe-close").click(function()
	{
		$(popups).css({"display": "none"}).animate({"opacity": 0});
		return false;
	});
}
function initTopMenu() {
	//remove Headlines from the second level links 

	$("ul.second-level > li").each( function(a,l) {
		var t = $(l).children("a");
		if($(t).html().indexOf('<h3>') != -1) {
			var tt = $(t).children("span.lnk-text").html();
			if(a == 0) { //first headline
				$(l).parent(".second-level").before(tt);
				$(l).remove();
			} else { // not the first close the list and the div above this, start the new div add headline and start the list again.
				var q = '</ul></div><div class="drop-box">'+tt+'<ul class="second-level">';
				$(l).before(q);
				$(l).remove();
			}
		}
	});
	$("ul.second-level > li").each( function(a,l) {
		if($(l).hasClass('li2')){
			var c = $(l).children('div.drop-level3').length;
			if (c > 0) {
				$(l).addClass('present-3level');
				$(l).children("a").addClass('present-3level-lnk');
			}
		}
	});	
	//if in the 3rd level make the second level link have the checkmark
	$("ul.list-level3 > li").each(function(a,l) {
		if(!$(l).hasClass('present-4level') ) {
			var p = $(l).parents("li");
			$(p).each( function() {			
				if($(this).hasClass('present-3level') ) {
					$(this).addClass('active');
				}
			});
		}
	});
	//add class to last second level drop box container
	$("div.drop-info").children(".drop-box:last").addClass("drop-box1");
}
function initLocationMenu() {
	$('#locationsMenu .li1 a').each( function() {
		var s = $(this).html();
		var t = $(s).text();
		var l = t.split(" ");
		var y = '';
		for(i=0; i<l.length; i++){
			var k = l[i].split("");
			var n = '<span>'+k[0]+'</span>';
			for(j = 1; j<k.length; j++) {
				n = n+k[j];
			}
			l[i] = n
		}
		y = l.join(" ");
		$(this).html(y);
	});

}
function popupClose(d) {
	$(d).css("display", "none").animate("opacity", 0);

}

function scrollSolutions() {
	//get content of first li
	var l = $('#treefrogSolutions li:first').html();
	//add new li to bottom
	$('#treefrogSolutions').append('<li>'+l+'</li>');
	//slide the ul up
	var t = $('#treefrogSolutions').css('line-height');
	t.replace(/px/,'');
	t = '-'+t;
	$('#treefrogSolutions').animate({top: t},"slow", function() {
		$('#treefrogSolutions li:first').remove();
		$('#treefrogSolutions').css('top', '0px');
	});
	//remove the first li element;
		
}

function showTabs(){
	var a = $('.tabs-box .tabs-holder-bg1 .tab:visible');
	if(a.length == 1) {
		$('.tabs-box').css('visibility','visible');
	} else {
		//check again in half a second;
		tabTimer = setTimeout("showTabs()",100);
	}
}

function randomFrog() {
	var r = Math.floor(Math.random()*10)%4;
	//r should be 0,1,2,or 3
	$('.frogInfo:eq('+r+')').show();
}
function hasThisLinkBeenVisited(url) {
        var link = document.createElement('a');
        link.href = url;
        link.className = "testMe";
        document.body.appendChild(link);
        if (link.currentStyle) {
                var color = link.currentStyle.color;
                if (color == '#ff0000')
                        return true;
                return false;
        } else {
                link.setAttribute("href",url);
                var computed_style = document.defaultView.getComputedStyle( link, null );
                if (computed_style) {
                        if (computed_style.color == 'rgb(255, 0, 0)')
                                return true;
                }
                return false;
        }
}


function checkBoxesLoad() {
	$('a.moreNewsLink').each( function() {
		var u = $(this).attr('href');
		var i = '#'+$(this).attr('rel');
		if(hasThisLinkBeenVisited(u)) {
			$(i).attr('checked', true);
		}
	});
}


function splitDomain(){
	var d = $('#domainName').val();
	var dp = d.split('.');
	if(dp.length != 2 ) {
		alert('Invalid Domain Name');
		return false;
	} else {
		$('#domainQuery').attr('action', 'Register-My-Domain');
		$('#domainPrefix').val(dp[0]);
		$('#domainSuffix').val(dp[1]);
		$('#domainQuery').submit();
	}

}