$(document).ready( function() {
	// make sure nothing punches through sub menus
	//alert( "just entered document.ready()" );
	$("#mainNavFrame .ddown").bgiframe();

	// make sub nav visible on hover
	$("#mainNavFrame li.menuItem").hover(
		function() {
			$(".ddown", this).show().css({
				width: $(".ddown", this).parent().width() + "px"
				//height: $(".ddown ul", this).height() + "px"
			});
		},function() {
			$(".ddown", this).hide();
		}
	);

	// allow for the div.navUpper to trigger it's containing a, if it has one
	$("#mainNavFrame .navUpper a")
		.click(function(event){
			event.stopPropagation();
			window.location = $(this).attr("href");
		});

	$("#mainNavFrame .navUpper")
		.mouseup(function(){
			$("a", this).trigger("click");
		});

	// allow for the sub menu li to trigger it's containing a, if it has one
	$("#mainNavFrame .ddown li a")
		.click(function(event){
			event.stopPropagation();
			window.location = $(this).attr("href");
		});

	$("#mainNavFrame .ddown li")
		.mouseup(function(){
			$("a", this).trigger("click");
		})
		.hover(function(){
			$(this).css("background-color", "#f60");
		},function(){
			$(this).css("background-color", "#565656");
		});

	// make sure items that cannot be clicked don't have the pointer
	$("#mainNavFrame .navUpper")
		.each(function(){
			if ($("a", this).size() === 0) {
				$(this).css("cursor", "text");
			}
		});

	$(".innerOff .navUpper")
		.mouseover(function() {
			$(this).css({
				"background-position":"50% -200px"
			});
		})
		.mouseout(function() {
			$(this).css({
				"background-position":"50% -250px"
			});
		} );

	$(".leftEndOff .navUpper")
		.mouseover(function() {
			$(this).css({
				"background-position":"-267px -50px"
			});
		})
		.mouseout(function() {
			$(this).css({
				"background-position":"-267px -100px"
			});
		});

	// don't fire body mousemove so the menus can handle their own mouseover/out
	$(".outerOffRt").add(".outerOnRt")
		.mousemove(function(event){
			event.stopPropagation();
		});

	// reset the right outer nav background image, we aren't on the menu or submenu anymore
	$(document.body)
		.mousemove(function(event){
			$(".outerOffRt .navUpper").css({
				"background-position":"-278px -500px"
			});

			$(".outerOnRt .navUpper").css({
				"background-position":"-278px -400px"
			});
		});

	$(".outerOffRt .navUpper")
		.mouseover(function() {
			$(this).css({
				"background-position":"-278px -450px"
			});
		})
		.mouseout(function() {
			$(this).css({
				"background-position":"-278px -550px"
			});
		});

	$(".outerOnRt .navUpper")
		.mouseover(function() {
			$(this).css({
				"background-position":"-278px -450px"
			});
		})
		.mouseout(function() {
			$(this).css({
				"background-position":"-278px -550px"
			});
		});

	var curLoc = window.location.href;

	/* lgBtn states
		- Glow is used for buttons on the white background
		- NoGlow is used for buttons in a container
	*/
	$(".orangeGlow").mouseover( function() {
		$(this).css( {"background-position":"0px -91px"} );
	} ).mousedown( function() {
		$(this).css( {"background-position":"0px -181px"} );
	} ).mouseout( function() {
		$(this).css( {"background-position":"0px -2px"} );
	} );

	$(".blueGlow").mouseover( function() {
		$(this).css( {"background-position":"0px -361px"} );
	} ).mousedown( function() {
		$(this).css( {"background-position":"-0px -451px"} );
	} ).mouseout( function() {
		$(this).css( {"background-position":"-0px -271px"} );
	} );

    $(".grayGlow").mouseover( function() {
		$(this).css( {"background-position":"0px -1179px"} );
	} ).mousedown( function() {
		$(this).css( {"background-position":"-0px -1269px"} );
	} ).mouseout( function() {
		$(this).css( {"background-position":"-0px -1090px"} );
	} );

	$(".orangeNoGlow").mouseover( function() {
		$(this).css( {"background-position":"0px -639px"} );
	} ).mousedown( function() {
		$(this).css( {"background-position":"0px -730px"} );
	} ).mouseout( function() {
		$(this).css( {"background-position":"0px -549px"} );
	} );

	$(".blueNoGlow").mouseover( function() {
		$(this).css( {"background-position":"0px -908px"} );
	} ).mousedown( function() {
		$(this).css( {"background-position":"0px -998px"} );
	} ).mouseout( function() {
		$(this).css( {"background-position":"0px -818px"} );
	} );

	/* smBtn states */
	$(".smBtn").mouseover( function() {
		$(this).css( {"background-position":"-421px -32px"} );
	} ).mousedown( function() {
		$(this).css( {"background-position":"-421px -64px"} );
	} ).mouseout( function() {
		$(this).css( {"background-position":"-421px 0px"} );
	} );

	/* right nav states */
	$(".rtNav div.item").mouseover( function() {
		$(this).css( {"background-color":"#e6e6e6","cursor":"pointer"} );
	} ).mouseout( function() {
		$(this).css( {"background-color":"#fff","cursor":"default"} );
	} ).click( function() {
		var t = $("a", this);
		if ( t.length > 1 ) {
			alert( "There should only be one link inside this element/" );
		} else {
			document.location = $(t[0]).attr( "href" );
			return false;
		}
	} );

	$(".rtNav div.hot").mouseover( function() {
		$(this).css( {"background-color":"#e6e6e6","cursor":"pointer"} );
	} ).mouseout( function() {
		$(this).css( {"background-color":"#ffe8d9","cursor":"default"} );
	} ).click( function() {
		var t = $("a", this);
		if ( t.length > 1 ) {
			alert( "There should only be one link inside this element/" );
		} else {
			document.location = $(t[0]).attr( "href" );
			return false;
		}
	} );

	/*	 If we're on the Resources page set up listeners on the document thumbnails	*/
	if ( document.location.href.match( /productAndService/ ) || document.location.href.match( /company/ ) || document.location.href.match( /images/ ) ) {
		$('.thumb').mouseover( function() {
			// get the name of the associated image
			var thumb = $(this).children().eq(0);
			//var fullImgPath = $(this).children().eq(0).attr( "src" );
            //var ext = fullImgPath.split( "." )[1];
            //var imageName = fullImgPath.split( "." )[0].split( "/" );
            //imageName = imageName[imageName.length - 1];
			//if ( imageName.match(/overviewVideo/) ||
			//	 imageName.match(/cloudExpoPresentation-06_2011/) ||
			//	 imageName.match(/storage-supercast-06_2011/) ||
              //   imageName.match(/zettaMirror/) ||
                // imageName.match(/2011-06_tmcnet/) ||
                 //imageName.match(/strgDsstrRcvryCldVid/) ||
                 //imageName.match(/recoverVid/) ) {
            //imageName = imageName + "1." + ext;
			//} else if ( imageName.match(/2011-0627_sys-con/) ) {
            //    imageName = imageName.substr( 0, imageName.lastIndexOf( "." ) ) + "1.png";
            //}
			var target = $(this).siblings( '.zoom' );
			var pos = $(thumb).position();
			if ( target.hasClass( "last" ) ) {
				target.css( {top:pos.top + $(thumb).outerHeight() - target.innerHeight() + 4 } );
			} else {
				target.css( {top:pos.top} );
			}
			target.show();
		} );
		$('.zoom').mouseout( function() {
			$(this).hide();
		} );
	}

	/* Technology Diagram Rollover controls */
	$(".techDiagramSMP").mouseover( function() {
		$(this).css( {"background-position":"-300px 0px","cursor":"pointer"} );
		$(".sysArch").hide();
		$(".underDiagram h4").css( {"color":"#0085c8"} );
		$(".smpContainer").show();
	} ).mouseout( function() {
		$(this).css( {"background-position":"0px 0px","cursor":"pointer"} );
		$(".sysArch").show();
		$(".underDiagram h4").css( {"color":"#f60"} );
		$(".smpContainer").hide();
	} );

	$(".access").mouseover( function() {
		$(".techDiagramAccess").css( {"background-position":"-330px 0px"} );
		$(".sysArch").hide();
		$(".underDiagram h4").css( {"color":"#0085c8"} );
		$(".accessContainer").show();
	} ).mouseout( function() {
		$(".techDiagramAccess").css( {"background-position":"-30px 0px"} );
		$(".sysArch").show();
		$(".underDiagram h4").css( {"color":"#f60"} );
		$(".accessContainer").hide();
	} );

	$(".controller").mouseover( function() {
		$(".techDiagramController").css( {"background-position":"-330px -156px"} );
		$(".sysArch").hide();
		$(".underDiagram h4").css( {"color":"#0085c8"} );
		$(".controllerContainer").show();
	} ).mouseout( function() {
		$(".techDiagramController").css( {"background-position":"-30px -156px"} );
		$(".sysArch").show();
		$(".underDiagram h4").css( {"color":"#f60"} );
		$(".controllerContainer").hide();
	} );

	$(".media").mouseover( function() {
		$(".techDiagramMedia").css( {"background-position":"-330px -312px"} );
		$(".sysArch").hide();
		$(".underDiagram h4").css( {"color":"#0085c8"} );
		$(".mediaContainer").show();
	} ).mouseout( function() {
		$(".techDiagramMedia").css( {"background-position":"-30px -312px"} );
		$(".sysArch").show();
		$(".underDiagram h4").css( {"color":"#f60"} );
		$(".mediaContainer").hide();
	} );

	if ( $('#bodyFrameHome').length !== 0 ) {
		$('#slideshow').after('<div id="homePgSlideshowNav">').cycle( {
            pager:"#homePgSlideshowNav",
            timeout:8000
        } );
        var slideId = 1;
        $('#homePgSlideshowNav a').each( function() {
            $(this).attr( "id", "s"+slideId ).text( "" );
            slideId++;
        } );
	}

    /* Listen for clicks on document download links, and tell Goolge Analytics */
    $(".docDwnld").click( function() {
        // reset the isVisited cookie to 0
        $.cookie( "isVisited", null );

        // explicitly track a view of the doc
        var docName = $(this).attr( "href" );
        pageTracker._trackPageview( docName );
    } );

	$('#dlTest').click( function() {
		var startTime = new Date();
		$('#dlSpin').show();
		$.ajax( {
			url:'dlTest.php',
			success:function( resp ) {
				$('#howLong p').eq(0).text( 'The download completed in ' + (new Date()-startTime)/1000 + ' seconds.' );
			},
			error:function() {
				$('#howLong p').eq(0).text( "The test download timed out at 12 seconds, suggesting network congestion. Try running the Trace Route button if you haven't already. It may uncover a bottleneck." ); },
			dataType:'text',
			cache:false,
			timeout:12000 }
		);
	} );

	$('#trTest').click( function() {
		$('#trSpin').show();
		$.ajax( {
			url:'traceRoute.php',
			success:function( resp ) {
				var txt = "";
				for ( var i = 0; i < resp.length; i++ ) {
					txt += resp[i]+"<br />";
				}
				$('#trTarget').eq(0).html( txt );
			},
			error:function( XMLHttpRequest, textStatus, errorThrown ) {
				$('#trTarget').eq(0).html( "Sorry, the traceroute command timed out, which suggests that your network is configured to block those packets." ); },
			dataType:'json',
			timeout:30000 }
		);
	} );

    /* Check thank-you.php for a "already seen this page" cookie.
     * If true, then do not load the referenced doc again
     * If false, do load the referenced doc.
     */
    var isVisited = $.cookie( "isVisited" );
    if ( curLoc.match( /thank-you\.php/ ) ) {
        if ( isVisited == 1 ) {
            ;
        } else {
            $.cookie( "isVisited", 1 );
            // Programatically click link on document download thank you pages.
            if ( $('#autoClickThis') ) {
                location.href = $('#autoClickThis').attr('href');
            }
        }
    }

	// LivePerson code
	// Attach a listener to the image element so that we can see when it change and then do stuff.
    $("#lpChatBtn").hover( function( evt ) {
		var im = $(this).find( "img" );
		if ( $(im).attr( "src" ).match( /reponline/ ) ) {
			$(im[0]).attr( "src", "http://www.zetta.net/images/liveperson/reponlineOver.png" );
		}
	}, function() {
		var im = $(this).find( "img" );
		if ( $(im).attr( "src" ).match( /reponline/ ) ) {
			$(im[0]).attr( "src", "http://www.zetta.net/images/liveperson/reponline.png" );
		}
	} );
} );

