$(document).ready(function(){
									
	// Removes SEO text in navItems
	$(".clearText").html("");
	$(".clearText").removeClass("clearText");
	
	// Clears input fields on Focus
	$(".clearField").bind("blur", function() {
		if ($(this).val()=="") {
			$(this).val($(this).attr("title"));
		};
	});
	$(".clearField").bind("focus", function() {
		if ($(this).val()==$(this).attr("title")) {
			$(this).val("");
		};
	});
	
	if ($("a[rel^='photoGallery']").length>0) {
		// HOMEPAGE BANNER
		photoGalleryInit();
		
		$("a[rel^='photoGallery'] img").hover(function(){
			$(this).animate({
				opacity:1
			},100,function(){});
		},function(){
			var opac = $(this).attr('rel');
			if (opac=="" || opac==null) opac = 0.6;
			if (!$(this).parent().hasClass("active")) {
				$(this).animate({
					opacity:opac
				},100,function(){});
			}
		});
		
		$("a[rel^='photoGallery']").click(function(){
			galleryPhotoSwitch($(this));
		});
	}
	
});

// Photos  ---------------------------------------
var photoGalleryIMAGES = new Array;
var photoGalleryTimer;
var photoGalleryTimerInterval = 5000;

function photoGalleryInit() {
	photoGalleryClear();
	photoGalleryPreloadImages();
	$("a[rel^='photoGallery']:last").addClass("last");
	//$("a[rel^='photoGallery']:first").addClass("active").find("img").css({opacity:1});
	galleryPhotoSwitch($("a[rel^=\'photoGallery\']:first"));
}

function photoGalleryPreloadImages() {
	var rel, relSplitIx;
	var ImageStr, ImageArr, ImageURL, ImageNum;
	
	$("a[rel^='photoGallery']").each(function() {
		rel 			= $(this).attr("rel");
		relSplitIx	= rel.indexOf("[");
	
		if (relSplitIx > 0) {
			ImageStr	= rel.substr(relSplitIx+1,rel.length-relSplitIx-2);
			ImageArr	= ImageStr.split(',');
			ImageURL = ImageArr[1];
			ImageNum = $(this).attr("name");
		}
		
		photoGalleryIMAGES[ImageNum] = $('<img />').attr('src', ImageURL);
		//$.get(ImageURL, function(data) {
			//photoGalleryIMAGES[ImageNum] = data;
		  //$('.result').html(data);
		  //alert('Load was performed.');
		//});
	});
	
}

function photoGalleryClear() {
	var opac, thisImg;
	$("a[rel^='photoGallery']").each(function() {
		$(this).removeAttr("href").removeClass("active");
		thisImg = $(this).find("img");
		opac = thisImg.attr('rel');
		if (opac=="" || opac==null) opac = 0.5;
		thisImg.css({opacity:opac});
	});
}

function galleryPhotoSwitch(thisObj) {
	clearTimeout(photoGalleryTimer);
	
	var rel, relSplitIx;
	rel 			= thisObj.attr("rel");
	relSplitIx	= rel.indexOf("[");

	var ImageStr, ImageArr, ImageID, ImageURL, ImageCap, ImageLink, ImageIMG;
	if (relSplitIx > 0) {
		ImageStr	= rel.substr(relSplitIx+1,rel.length-relSplitIx-2);
		ImageArr	= ImageStr.split(',');
		ImageID 	= ImageArr[0];
		ImageURL = ImageArr[1];
		ImageLink = ImageArr[2];
		ImageCap = thisObj.attr("title");
		ImageNum = thisObj.attr("name");
		//ImageURL = photoGalleryIMAGES[ImageNum];
		ImageIMG = photoGalleryIMAGES[ImageNum];
	}
	var animSpeed = 500;
	var ImageOBJ  = $("#"+ImageID);
	
	var currImage, nextImage;
	
	if (ImageOBJ.find("a:animated").length == 0) {
		photoGalleryClear();
		thisObj.addClass("active").css({opacity:1});

		currImage = ImageOBJ.find(".current");
		//ImageOBJ.append("<a href='"+ImageLink+"' class='next' style='position:absolute;left:0px;z-index:1;opacity:0'><img src='"+ImageURL+"' alt='"+ImageCap+"' /></a>");
		ImageOBJ.append("<a href='"+ImageLink+"' class='next' style='position:absolute;left:0px;z-index:1;opacity:0'></a>").find("a").html(ImageIMG);
		nextImage = ImageOBJ.find(".next");
		
		currImage.addClass("current").css({'position':'absolute','left':'0px','zIndex':'2'}).animate({
				opacity: 0
			}, animSpeed, function(){
			currImage.replaceWith("");
		});
		nextImage.animate({
				opacity: 1
			}, animSpeed, function(){
			nextImage.addClass("current").removeClass("next");
		});
		$(".bannerCaption").text(ImageCap);
	}
	
	photoGalleryTimer = setTimeout('galleryPhotoSwitchNext()',photoGalleryTimerInterval);

}

function galleryPhotoSwitchNext() {
	var nextButton;
	if ($("a[rel^=\'photoGallery\'].active").hasClass("last")) {
		nextButton = $("a[rel^=\'photoGallery\']:first");
	} else {
		nextButton = $("a[rel^=\'photoGallery\'].active").parent().next().find("a");
	}
	galleryPhotoSwitch(nextButton);
}

