var cssClass;
var json;
var imagecount = 0;
var currentimage;
var nextimage;
var rotate;
var setbg;
var lastPortfolio;

$(document).ready( function() {
	
	$("#nav ul li.annual-report").hover( 		
		function() {
			//hoverOver();
			//$(this).find("ul").css("display","inline");
			$(this).find("a:first").addClass("annual");
		},
		function() {
			//if( $(this).find("a:first").attr("id") != "active2" )
			if( $(this).find("a:first").attr("id") != "active" )
			{
				//$(this).find("ul").css("display","none");
				$(this).find("a:first").removeClass("annual");
			}
		
		//hoverOut();
	});
							
	$("#nav ul li.interactive-media").hover( 
		function() {
			//hoverOver();
			//$(this).find("ul").css("display","inline");
			$(this).find("a:first").addClass("interactive");
		},
		function() {
			//if( $(this).find("a:first").attr("id") != "active2" )
			if( $(this).find("a:first").attr("id") != "active" )
			{
				//$(this).find("ul").css("display","none");
				$(this).find("a:first").removeClass("interactive");
			}
			//hoverOut();
	});
							
	$("#nav ul li.advertising-marketing").hover( 
		function() {
			//hoverOver();
			//$(this).find("ul").css("display","inline");
			$(this).find("a:first").addClass("advertising");
		},
		function() {
			//if( $(this).find("a:first").attr("id") != "active2" )
			if( $(this).find("a:first").attr("id") != "active" )
			{
				//$(this).find("ul").css("display","none");
				$(this).find("a:first").removeClass("advertising");
			}
			//hoverOut();
	});
	
	$("#nav ul li.about-us").hover( 
		function() {
			hoverOver();
			$(this).find("ul").css("display","inline");
			$(this).find("a:first").addClass("about");
		},
		function() {
			if( $(this).find("a:first").attr("id") != "active2" )
			{
				$(this).find("ul").css("display","none");
				$(this).find("a:first").removeClass("about");
			}
			hoverOut();
	});
							
	$("ul.portfolio li a").click( 
		function() {
			if( lastPortfolio != this )
			{
				clearTimeout(rotate);
				$("ul.portfolio li div").slideUp(500);
				$(this).parent().find("div").slideToggle(500);
				getImages($(this).parent().attr("id"));
				externalLink = $(this).parent().find('.extLink');
				if(externalLink.length != 0){
					linkDest = externalLink.html();
					$('#launch').html("<a href=\""+linkDest+"\" target=\"_blank\">Click to launch</a>");
				}
				else{
					$('#launch').html("");	
				}
				$(lastPortfolio).removeClass('current');
				$(this).addClass("current");
				lastPortfolio = this;
			}
			else
			{
				$(this).next().slideUp(500);
				$(this).removeClass("current");
				lastPortfolio = "";
			}
		}
	);
	
	$("#auxmenu li").hover( function() {
		$(this).find("div").css("display","block");
	},
	function() {
		$(this).find("div").css("display","none");
	});
	
});

function fadeHome()
{
	
	clearTimeout(rotate);
	rotate = setTimeout("fadeHome()",5000);

	var numberofimages = 4;
	
	if( imagecount >= numberofimages ) { currentimage = imagecount % numberofimages; nextimage = currentimage + 1; } 
		else { currentimage = imagecount; nextimage = imagecount+1; }
	if( nextimage >= numberofimages ) { nextimage = 0; } 
		else {  }
	
	$("#splashimage img:first").css("opacity",0).attr('src', "images/home"+currentimage+".jpg" );
	$("#splashimage img:first").animate({opacity: 1},1000).animate({opacity: 1},2000);
	setTimeout('$("#splashimage").css( { backgroundImage: "url(images/home"+currentimage+".jpg)"})',2000);
	
	imagecount++;

}

function getImages( id )
{
	$.ajax({
		url: "include/fadeimages.php",
		type: "POST",
		data: "a=fadeImages&id="+id,
		cache: false,
		success: function(html){
			$("#rightimage").fadeOut(500, function() {				
				json = eval("("+html+")");
				fadeImages( json );
			});
			
	  }
	});
}


function fadeImages( json )
{	
	imagecount = 0;
	rotateImage();
	
	// Set initial image
	if(json.numimages == 0)
	{
		return false;
	}
	$("#rightimage").css("background-image","url("+json["files"][0]+")");
	$("#rightimage img").attr("src",json["files"][0]);
	
	// Image Preloading
	jQuery.preloadImages = function()
	{
	  for(var i = 0; i<arguments.length; i++)
	  {
		jQuery("<img>").attr("src", arguments[i]);
	  }
	}
	for( x=0; x<= json.numimages; x++ )
	{
		setTimeout('$.preloadImages("'+json["files"][x]+'");',2000);
	}
	
	// Show image box
	$("#rightimage").fadeIn( 1000 );
}

function rotateImage() {
	clearTimeout(rotate);
	clearTimeout(setbg);
	rotate = setTimeout("rotateImage()",5000);

	var numberofimages = json["numimages"];
	
	if(numberofimages == 0)
	{
		clearTimeout(rotate);
		return false;	
	}
	
	if( imagecount >= numberofimages ) { currentimage = imagecount % numberofimages; nextimage = currentimage + 1; } 
		else { currentimage = imagecount; nextimage = imagecount+1; }
	if( nextimage >= numberofimages ) { nextimage = 0; } 
		else {  }

	$("#rightimage img").css("opacity",0).attr('src', json["files"][currentimage] );
	$("#rightimage img").animate({opacity: 1},1000).animate({opacity: 1},2000);
	setbg = setTimeout('$("#rightimage").css( { backgroundImage: "url("+json["files"][currentimage]+")"})',2000);
	
	//$("#rightimage").animate({opacity: 0},2000);
	imagecount++;
}

function hoverOver()
{
			$("#active").attr("id","active2");
			cssClass = $("#active2").attr("class");
			$("#active2").removeClass(cssClass);
			$("#active2").parent().find("ul:first").css("display","none");
}

function hoverOut()
{
			$("#active2").attr("id","active");
			$("#active").addClass(cssClass);
			$("#active").parent().find("ul:first").css("display","inline");
}

function validateForm()
{
	var err = "Please fill in the following fields: ";
	var errLength = err.length;
	
	if( $("#Name").val().length <= 1 )
	{
		err += "Name, ";
	}
		
	if( $("#Company").val().length <= 1 )
	{
		err += "Company, ";
	}
		
	if( $("#Address").val().length <= 1 )
	{
		err += "Address, ";
	}
		
	if( $("#City").val().length <= 1 )
	{
		err += "City, ";
	}
		
	if( $("#Phone").val().length <= 1 )
	{
		err += "Phone, ";
	}
		
	if( $("#Email").val().length <= 1 )
	{
		err += "Email, ";
	}
		
	if( $("#Zip").val().length <= 1 )
	{
		err += "Zip, ";
	}
		
	if( err.length > errLength )
	{
		alert( err.substring(0,(err.length-2))+"." );	
	}
	else
	{
		$("#contactform").submit();	
	}
	
}