jQuery(document).ready(function(){
	jQuery('#contact, a.contactlink').click(function(){
		if(jQuery('#contactform').attr('class') == 'open'){
			jQuery('#contactform').removeClass('open');
		}else{
			jQuery('#contactform').addClass('open');
		}
		jQuery('#contactformcontent').toggle('fast',function(){
			jQuery('html, body').animate({scrollTop: jQuery('#page').height()}, 500);
		});
		return false;
	});
	
	jQuery('#navigation > ul > li').hover(
		function(){
			jQuery('#navigation > ul > li').children('ul').stop(false, true);
			jQuery(this).addClass('active');
			jQuery(this).children('ul').slideDown('fast');
		},
		function(){
			jQuery('#navigation > ul > li').children('ul').stop(false, true);
			jQuery(this).removeClass('active');
			jQuery('#navigation > ul > li').children('ul').hide();
		});
		
	jQuery(".enlarge").click(function(e) {
			modalWindow.open(this);
			return false;
		});
		
	for(var i = 0; i < rotatorimages.length; i++){
	    jQuery("<img>").attr("src", rotatorimages[i]);
	}	
	
	rotator(0);
});

var modalWindow={
	close:function(img){
		jQuery('#modal').fadeOut('fast', function(){
			jQuery('#modal').remove();
		});
		jQuery(img).parent().fadeTo('slow', 1.0, function(){
			jQuery('object').css({'position':'relative', 'top':'0px'});
		});
	},
	open:function(img){									
		var modalContent = '<div id="modal">';
			modalContent += '<div id="overlay-container" class="loading"><div id="overlay"><div class="close">Close</div></div>';
			modalContent += '</div>';
		jQuery('object').css({'position':'relative', 'top':'-10000px'});
		jQuery(img).parent().fadeTo('fast',0.001);
		jQuery('body').append(modalContent);
		loadTopMargin = (jQuery(window).height() - 40) / 2;
		jQuery('.loading').css({marginTop:loadTopMargin});
		jQuery('#modal').fadeIn('fast', function(){
			jQuery('.loading').fadeIn('slow');
		});
		var maxX = Math.ceil(jQuery(window).width() * 0.85);
        var maxY = Math.ceil(jQuery(window).height() * 0.85);
        var modalimagesrc = jQuery(img).attr('href');
		var modalimagecaption = jQuery(img).parent().children('.caption').html();

		//var modalimagesrc = '/wp-content/plugins/random-flickr-photo/lib/phpThumb/phpThumb.php?src=';
		  	  //modalimagesrc += jQuery(img).parent().attr('href');
			  //modalimagesrc += '&w=' + maxX + '&h=' + maxY + '&q=95';
			  //modalimagesrc += '&fltr[]=wmi|/wp-content/plugins/random-flickr-photo/logo.png|BL|100';
		  var modalimage = new Image();

		  jQuery(modalimage)
		    .load(function () {   
		      jQuery(this).hide();
		jQuery("#overlay-container").removeClass('loading');
		      jQuery('#overlay')
		        .append(this)
				.append("<div class=\"caption\">" + modalimagecaption + "</div>");
				var elementX = jQuery(this).width();
            	var elementY = jQuery(this).height();
				modalWindow.elementResize("#overlay",elementX,elementY,maxX,maxY);
		      	jQuery(this).fadeIn();
		    })
		    .error(function () {
		      // notify the user that the image could not be loaded
		    })
			.addClass('overlay-image')
		    .attr('src', modalimagesrc);
		jQuery('#modal .close').click(function(){
			modalWindow.close(img);
		});
	},
    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));
		//sideMargin = Math.ceil(((maxX - newX) / 2));
		jQuery("#overlay-container").css({marginTop:"0"});
		jQuery("#overlay").css({width:elementX + 40});
		jQuery(element).css({marginTop:topMargin});
    }
};

function rotator(id) {
	var max = rotatorimages.length - 1;
	
	if(id > max) id = 0;
	if(id < 0) id = max;
	jQuery('#homepagepromo').animate({opacity:1.0}, 5000, '', function(){
		jQuery('#homepagepromo').fadeOut('fast', function(){
			jQuery('#homepagepromo').html("<a href=\"" + rotatorurls[id] + "\" alt=\"\"><img src=\"" + rotatorimages[id] + "\" /></a>").fadeIn('slow', function(){
				id++;
				rotator(id);
			});
		});	
	});
}
