/*-- Suckerfish Dropdowns --*/

sfHover = function() {
    if( !document.getElementById( "navigation" ) ) return false;
    
	var sfEls = document.getElementById( "navigation" ).getElementsByTagName( "li" );
	
	for( var i = 0; i < sfEls.length; i++ ) {
		sfEls[ i ].onmouseover = function() {
			this.className += " sfhover";
		}
		sfEls[ i ].onmouseout = function() {
			this.className = this.className.replace( new RegExp( " sfhover\\b" ), "" );
		}
	}
}

if( window.attachEvent ) window.attachEvent( "onload", sfHover );
    

$( document ).ready( function() {
    var fancyboxOptions = { 'overlayShow': true };
                        
    if ($.browser.msie && $.browser.version == 6.0) {
        fancyboxOptions = { 'overlayShow': true, 'zoomSpeedIn': 0, 'zoomSpeedOut': 0 }
    }
    
    $( "p.image-block" ).each( function() {
        var currLink = $( "a", this );
        
        if( currLink.attr( "href" ) && currLink.attr( "href" ) != "#" ) {
            $( "a", this ).fancybox( fancyboxOptions );
        }
    } );
    
    /*-- Text Size --*/
	
	var currSize = getCookie( "textSize" );
//	alert( currSize );
	
	if( currSize == "large" ) {
		$( "body" ).css( "font-size", "1.0em" );
/* 		$( "body" ).addClass( "large" ); */
		$( "#container" ).addClass( "large" );
	}
	
	/*-- Content Color --*/
	
	var colorList = Array( "white", "gray", "black" );
	var defaultColor = colorList[ 0 ];
	var currColor = getCookie( "contentColor" );
	
	//alert( document.cookie + "\n" + getCookie( "contentColor" ) );
	
	if( !currColor ) {
		currColor = defaultColor;
    }
	
	for( var i = 0; i < colorList.length; i++ ) {
		if( currColor != colorList[ i ] && $( "body" ).hasClass( colorList[ i ] ) ) {
			//alert( "removed" );
			$( "body" ).removeClass( colorList[ i ] );
		}
	}
	
	if( !$( "body" ).hasClass( currColor ) ) {
		//alert( "dumb" );
		$( "body" ).addClass( currColor );
    }
	
	if( $( "body" ).attr( "id" ) == "home" ) {
		var numFigures = 3;
		var randomFigure = Math.floor( Math.random() * numFigures + 1 );			
		
		$( "#figure" ).removeClass( "figure1" ).addClass( "figure" + randomFigure );
	}
	
	
	/*-- Text Only --*/
	
	var isTextOnly = getCookie( "textOnly" );
	
	if( isTextOnly == "true" ) {
		$( "body" ).addClass( "text-only" );
	}
	
	
	/*-- Utility Bar --*/
	
	$( "#util-size a" ).click( function() {
	    if( currSize != "large" ) {
	        currSize = "large";
	        $( "body" ).css( "font-size", "1.0em" );
/* 	        $( "body" ).addClass( "large" ); */
		    $( "#container" ).addClass( "large" );
	    }
	    else {
	        currSize = "small";
	        $( "body" ).css( "font-size", "0.8em" );
/* 	        $( "body" ).removeClass( "large" ); */
		    $( "#container" ).removeClass( "large" );
	    }
		
		setCookie( "textSize", currSize );
		
		$( this ).blur();
		
		return false;
	} );
	
	$( "#util-contrast a" ).click( function() {
		var newColor;
		
		if( currColor ) {
			for( var i = 0; i < colorList.length; i++ ) {
				if( currColor == colorList[ i ] ) {
					if( i + 1 >= colorList.length ) {
						newColor = colorList[ 0 ];
					}
					else {
						newColor = colorList[ i + 1 ];
					}
				}
			}
		}
		else {
			currColor = defaultColor;
		}
				
		$( "body" ).removeClass( currColor ).addClass( newColor );
		
		currColor = newColor;
		
		setCookie( "contentColor", currColor );
		
		$( this ).blur();
		
		return false;
	} );
	
	$( "#util-text a" ).click( function() {
		$( "body" ).toggleClass( "text-only" );
		
		setCookie( "textOnly", ( $( "body" ).hasClass( "text-only" ) ? "true" : null ) );
		
		$( this ).blur();
		
		return false;
	} );
} );

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      "; path=/" +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");

  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    "; path=/" +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}