$(document).ready(function(){
	/*$('#mainContent p img').animate({'opacity':0}, 0);
	if($('#mainContent p img').width()>300){
		var scalePercentage = 300/$('#mainContent p img').width();
		$('#mainContent p img').height(Math.round($('#mainContent img').height()*scalePercentage));
		$('#mainContent p img').width(Math.round($('#mainContent img').width()*scalePercentage));
		$('#mainContent p img').animate({'opacity':1}, 500);
	} else {
		$('#mainContent p img').animate({'opacity':1}, 500);
	}*/
	if($('div.videoContainer').length ==1){
		if($('div.videoContainer iframe').width() > $('div#mainContent').width()){
			var scalePercentage = 540/$('div.videoContainer iframe').width();
			$('div.videoContainer iframe').height(Math.round($('div.videoContainer iframe').height()*scalePercentage));
			$('div.videoContainer iframe').width(Math.round($('div.videoContainer iframe').width()*scalePercentage));
		}
		$('div.videoContainer').height($('div.videoContainer iframe').height());
		$('div.videoContainer').width($('div.videoContainer iframe').width());
	} else if ($('div.videoContainer').length > 1){
		$('div.videoContainer').each(function(){
			$(this).height($(this).children().height());
			$(this).width($(this).children().width());
		});
		
	}
	
	$('#imagesContainer').insertAfter('h1');
	
	
	var placeHills	=	function(){
		//console.log($('html, body').scrollTop());
		if($(window).height()>$('body').height()){
			$('#hills').css({
				position 	: 	'fixed',
				bottom		:	'0'
			});
		} else {
			$('#hills').css({
				position 	: 	'relative'
			});
			//console.log('window height '+$(window).height()+' and content bottom '+($('#mainContent').offset().top+$('#mainContent').height()));
			if($('#top').length==0 && $(window).height() < ($('#mainContent').offset().top+$('#mainContent').height())){
				$('#wrapper').before('<a id="top"></a>');
				$('#mainContent').append('<a href="#top" class="returnToTop" style="visibility:hidden;">Return to Top &uarr;</a>');
				$('a.returnToTop').click(function(){
					$('html, body').animate({scrollTop:0}, 'slow');
					return false;
				});
				if($('html, body').scrollTop()+$(window).height() >= $('a.returnToTop').offset().top){
					$('a.returnToTop').css('visibility','visible');
				} else {
					// gracefully fade in the return to top link when necessary	
					$(window).scroll(function(){
						if($('html, body').scrollTop()+$(window).height() > $('a.returnToTop').offset().top && $('a.returnToTop').css('visibility')=='hidden'){
							$('a.returnToTop').animate({'opacity':0}, 0).css('visibility','visible').delay(300).animate({'opacity':1}, 500);
							$(window).unbind('scroll');
						} else if($('a.returnToTop').css('visibility')!='hidden'){
							$(window).unbind('scroll');
						}
					});
				}
				
			}
		}
	}
	
	placeHills();
	
	$(window).resize(function(){
		placeHills();		
	});	

	// search modal
	$('a.search').click(function(){
		$('body').append('<div id="search-modal-cover" style="display:none"></div>');
		$('#search-modal-cover').fadeIn(500);
		// load the search form via ajax
		$.ajax({
			url: 'http://www.boulderwindpower.com/search/index',
			success: function(data) {
				$('#search-modal-cover').append('<div id="search-modal-wrapper" style="display:none"><div id="search-modal"><a href="#" id="close" title="Close">x</a>'+$(data).find('#mainContent').html()+'</div></div>');
				var placeModal = function(){
					$('#search-modal-wrapper').css({
						top : $(window).height()/2 - $('#search-modal-wrapper').height()/2,
						left : $(window).width()/2 - $('#search-modal-wrapper').width()/2							
					});
				}
				
				//add placeholder text functionality for browsers that do not support the placeholder attribute
				$("'[placeholder]'").focus(function() {					
				  var input = $(this);
				  if (input.val() == input.attr('placeholder')) {
				    input.val('');
				    input.removeClass('placeholder');
				  }
				}).blur(function() {
				  var input = $(this);
				  if (input.val() == '' || input.val() == input.attr('placeholder')) {
				    input.addClass('placeholder');
				    input.val(input.attr('placeholder'));
				  }
				}).blur();
			
				$('a#close').click(function(){
					$('#search-modal-wrapper').fadeOut(300, function(){
						$('#search-modal-cover').fadeOut(200, function(){
							$(this).remove();
						});
					});
					return false;
				});
				
				$(document).keyup(function(e) {
				  if (e.keyCode == 27) { // esc
				  	$('a#close').click(); 
				  }   
				});

				
				placeModal();
				$('#search-modal-wrapper').fadeIn(500, function(){
					$('#search-modal-cover').css('background-image','none');
				});
			}
		});
		return false;
	});		
});
