var clearQueue = true;
var gotoEnd = true;
var sections = new Array('home','about','advocate','enjoy','getinvolved');
var sectionContent = new Object;

// Use Ajax to get content for hompage images and populate the section content

for(var i in sections){
	var section = sections[i];
	$.ajax({
	  	url: "/" + section + "/",
		async: false,
	  	cache: false,
	  	success: function(xml){
			var content = new Object;
			content['sectiontitle'] = $(xml).find('sectiontitle').text();
	     	content['blurb'] = $(xml).find('blurb').text();
			content['sectioncaption'] = $(xml).find('sectioncaption').text();
		 	content['bgimage'] = $(xml).find('bgimage').text();
			sectionContent[section] = content;
			$("<img>").attr("src", content['bgimage']);
	  	}
	});
}

function changeSection(section){
	
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
	 var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
	}
	if (ieversion>=8){
		
	}else{
		$("#feature.home > #blurb > h2").html(sectionContent[section]['sectiontitle']);
		$("#feature.home > #caption > p").html(sectionContent[section]['sectioncaption']);
		$("#feature.home > #blurb > p").html(sectionContent[section]['blurb']);
		$('#feature.home').css('backgroundImage','url(' + sectionContent[section]['bgimage'] +')');	
	}
}

var modalWindow={
	close:function(link){
		$('#modal').fadeOut('fast', function(){
			$('#modal').remove();
		});
		$(link).fadeTo('slow', 1.0, function(){
			$('object').css({'position':'relative', 'top':'0px'});
		});
	},
	open:function(link){
		var modalimagetitle = $(link).attr('title');									
		var modalContent = '<div id="modal">';
			modalContent += '<div id="overlay-container" class="loading">';
			modalContent += '</div>';
		$('object').css({'position':'relative', 'top':'-10000px'});
		$(link).fadeTo('fast',0.001);
		$('body').append(modalContent);
		loadTopMargin = ($(window).height() - 40) / 2;
		$('.loading').css({marginTop:loadTopMargin});
		$('#modal').fadeIn('fast', function(){
			$('.loading').fadeIn('slow');
		});
		var maxX = Math.ceil($(window).width() * 0.85);
        var maxY = Math.ceil($(window).height() * 0.85);
		var modalimagesrc = '/wp-content/themes/fotfp/lib/phpThumb/phpThumb.php?src=';
		  	  modalimagesrc += $(link).attr('href');
			  modalimagesrc += '&w=' + maxX + '&h=' + maxY + '&q=95';
		  var modalimage = new Image();

		  $(modalimage)
		    .load(function () {   
		      $(this).hide();
		      $('#overlay-container')
		        .removeClass('loading')
		        .append(this);
				var elementX = $(this).width();
            	var elementY = $(this).height();
				modalWindow.elementResize(this,elementX,elementY,maxX,maxY);
		      	$(this).fadeIn();
				$('#modal').append('<div id="title"><p>' + modalimagetitle + '</p></div>');
		    })
		    .error(function () {
		      // notify the user that the image could not be loaded
		    })
			.addClass('overlay-image')
		    .attr('src', modalimagesrc);
		$('#modal').click(function(){
			modalWindow.close(link);
		});
	},
    elementResize:function(element,elementX,elementY,maxX,maxY){
        var ratioXY = elementX/elementY;
        var newX = ((maxX * 0.85) < elementX) ? (maxX * 0.85) : elementX;
        var newY = ((maxY * 0.85) < elementY) ? (maxY * 0.85) : elementY;
		var newXa = newY * ratioXY;
		var newYa = newX / ratioXY;
		if(newXa > newX){
			newY = newYa;
		}else{
			newX = newXa;
		}
        topMargin = Math.ceil(((maxY - newY) / 2));
		//leftMargin = ((maxX - newX) / 2) - 20;
      //  $(element).height(newY);
      //  $(element).width(newX);
		$("#overlay-container").css({marginTop:"0"});
		$(element).css({marginLeft:'auto',marginRight:'auto', marginTop:topMargin});
    }
};

$(document).ready(function(){
	
	$('#navigation > ul > li.top > ul').hide();
//	$('#navigation > ul > li.top > a').click(function(){
//		return false;
//	});
	$('#navigation > ul > li.top').hover(
							function(){
								$('ul', this).stop(clearQueue, gotoEnd);
								changeSection($(this).attr('id').toLowerCase());
								$('#navigation > ul > li.top > ul').hide();
								$('ul', this).show();
							},
							function(){
								$('ul', this).stop(clearQueue, gotoEnd);
								changeSection('home');
								$('ul', this).hide();
							});
	$(".modalimage").click(function(e) {
		modalWindow.open(this);
		return false;
	});

});