function graRand( min, max ) {
	if( min > max ) {
		return -1;
	}
	if( min == max ) {
		return min;
	}
 
    return min + parseInt( Math.random() * ( max - min + 1 ) );
}

function getValueById( id ) {
	return document.getElementById( id ).value;
}

if( currentPage == 'references' ) {
	
	$( '#pageHeaderImage' ).hide();
	
}


$( 'a[ref]' ).live( 'click', 
	function() {
		//window.alert( 'Invoked' );
		target = $( this ).attr( 'ref' );
		//window.alert( target );
		act = target.split( ':' );
		//window.alert( target + ' - ' + act[ 0 ] + ' - ' + act[ 1 ] );
		if( act.length != 2 ) {
			return;
		}
		
		if( act[ 1 ] == currentPage && act[ 1 ] != 'references' ) {
			return;
		}
		switch( act[ 0 ] ) {
			case 'page':
			case 'nhpage':
				$.ajax( {
				   url: 'load.php',
				   success: function( data ) {
					   //window.alert( 'Response income' );
					   //$( '#pageContent' ).fadeOut( 'normal', function() {
								$( '#pageContent' ).html( data );
								//$( this ).fadeIn( 'normal' );
								$( 'a[ref^="reference"] img' ).css( { opacity : "0.6", filter : "alpha(opacity=60)" } ).hover(
									function() {
										$( this ).css( { opacity : "1", filter : "alpha(opacity=100)" } );
									}, function() {
										$( this ).css( { opacity : "0.6", filter : "alpha(opacity=60)" } )
									}
								);
						//} );
					   
				   },
				   type: 'GET',
				   data: 'p=' + act[ 1 ],
				   error: function( req, msg, type ) {
					   window.alert( "An Error has occured.\nError Message:\n" + msg + "\nError Type:\n" + type );
				   }
				} );
				
				if( act[ 1 ] == 'references' ) {
					$( '#pageHeaderImage' ).hide();
				} else {
					$( '#pageHeaderImage:hidden' ).show();
				}


				
				$( 'a[ref]' ).css( { color : "#999999" } );
				$( 'a[ref="page:' + act[ 1 ] + '"]' ).css( { color : "#003399" } );
				
				currentPage = act[ 1 ];
				//window.alert( 'Request sent' );
				break;
			case 'news':
				//window.alert( 'Invoked' );
				$.ajax( {
				   url: 'load.php',
				   success: function( data ) {
					   //window.alert( 'Response income' );
					   //$( '#pageContent' ).fadeOut( 'normal', function() {
								$( '#pageContent' ).html( data );
								//$( this ).fadeIn( 'normal' );
								$( 'a[ref^="reference"] img' ).css( { opacity : "0.6", filter : "alpha(opacity=60)" } ).hover(
										function() {
											$( this ).css( { opacity : "1", filter : "alpha(opacity=100)" } );
										}, function() {
											$( this ).css( { opacity : "0.6", filter : "alpha(opacity=60)" } )
										}
								);
						//} );
				   },
				   type: 'GET',
				   data: 'newsId=' + act[ 1 ],
				   error: function( req, msg, type ) {
					   window.alert( "An Error has occured.\nError Message:\n" + msg + "\nError Type:\n" + type );
				   }
				} );
				
				$( 'a[ref]' ).css( { color : "#999999" } );
				$( 'a[ref="page:news"]' ).css( { color : "#003399" } );
				
				currentPage = act[ 1 ];
				
				//window.alert( 'Request sent' );
				break;
			case 'contact':
				
				var errcss = { border : "1px solid red" };
				var defcss = { border : "1px solid #CBCBCB" }
				
				switch( act[ 1 ] ) {
					case 'clear':
						document.getElementById( 'contactForm' ).reset();
						break;
					case 'send':
						
						var err = false;
						
						$( '#contactForm input' ).css( defcss );
						$( '#contactForm textarea' ).css( defcss );
						
						/* Pre-validate vorm inputs */
						if( getValueById( 'contactName' ).length < 4 ) {
							$( '#contactName' ).css( errcss );
							document.getElementById( 'contactName' ).value = '';
							err = true;
						}
						
						if( !getValueById( 'contactEmail' ).match( /^[a-zA-Z0-9._-]+@[a-z0-9A-Z._-]+$/ ) || getValueById( 'contactEmail' ).length < 5 ) {
							$( '#contactEmail' ).css( errcss );
							document.getElementById( 'contactEmail' ).value = '';
							err = true;
						}
												
						if( getValueById( 'contactText' ).length < 10 ) {
							$( '#contactText' ).css( errcss );
							document.getElementById( 'contactText' ).value = '';
							err = true;
						}
						
						if( err == true ) {
							window.alert( contactErrMessage );
						} else {
							$.ajax( {
							   url: 'contact.php',
							   success: function( data ) {
								   //window.alert( 'Response income' );
								   window.alert( contactSuccMessage );
								   $( '#contactForm' ).hide();
							   },
							   type: 'POST',
							   data: 'name=' + getValueById( 'contactName' ) + '&email=' + getValueById( 'contactEmail' ) + '&phone=' + getValueById( 'contactPhone' ) + '&company=' + getValueById( 'contactCompany' ) + '&address=' + getValueById( 'contactAddress' ) + '&website=' + getValueById( 'contactWebsite' ) + '&text=' + getValueById( 'contactText' ),
							   error: function( req, msg, type ) {
								   window.alert( "An Error has occured.\nError Message:\n" + msg + "\nError Type:\n" + type );
							   }
							} );
						}
						
						
						break;
				
				}
				break;
			case 'reference':
				
				$( '#pageHeaderImage' ).hide();
				
				$.ajax( {
				   url: 'load.php',
				   success: function( data ) {
					   //window.alert( 'Response income' );
					   //$( '#pageContent' ).fadeOut( 'normal', function() {
								$( '#pageContent' ).html( data );
								//$( this ).fadeIn( 'normal' );
								$( 'a[ref^="reference"] img' ).css( { opacity : "0.6", filter : "alpha(opacity=60)" } ).hover(
										function() {
											$( this ).css( { opacity : "1", filter : "alpha(opacity=100)" } );
										}, function() {
											$( this ).css( { opacity : "0.6", filter : "alpha(opacity=60)" } )
										}
								);
						//} );
				   },
				   type: 'GET',
				   data: 'p=references&refId=' + act[ 1 ],
				   error: function( req, msg, type ) {
					   window.alert( "An Error has occured.\nError Message:\n" + msg + "\nError Type:\n" + type );
				   }
				} );
			
			
				break;	
		case 'referencethumb':

		//window.alert( act[ 1 ] );
			$( '#fullsizeReferenceImage' ).fadeTo( 'slow', 0, function() {
				$( this ).attr( 'src', act[ 1 ] );
				$( this ).fadeTo( 'slow', 100 );
			} );
			
			break;
		}
} );

$( 'a[ref]' ).hover(
	function() {
		
		target = $( this ).attr( 'ref' );
		//window.alert( target );
		act = target.split( ':' );
		
		if( act[ 0 ] == 'page' && act[ 1 ] != currentPage ) {
			$( this ).css( { color : "#003399" } );
		}
	}, function() {
		
		target = $( this ).attr( 'ref' );
		//window.alert( target );
		act = target.split( ':' );
		
		if( act[ 0 ] == 'page' && act[ 1 ] != currentPage ) {
			$( this ).css( { color : "#999999" } );
		}
	}
);

$( '#pageHeaderImage' ).cycle( { fx: 'fade', speed : 1000, timeout : 10000 } );

$( 'a[ref^="reference:"] img' ).css( { opacity : "0.6", filter : "alpha(opacity=60)" } ).hover(
		function() {
			$( this ).css( { opacity : "1", filter : "alpha(opacity=100)" } );
		}, function() {
			$( this ).css( { opacity : "0.6", filter : "alpha(opacity=60)" } )
		}
);
