/* Author: 

*/

var do_subscribe = function(f)
{
	var $el = $(f.subscriber);
	var e = $el.val();
	
	$.post('/ajax/subscribe',{email:e},function(result)
	{
		if(result == 'success')
		{
			$el.blur().val('Thank you');
			setTimeout(function()
			{
				$el.val('Subscribe');
				$el.animate({'background-color':'#fff'},500,'easeOutQuad');
			},2000);
		} else
		{
			$el.blur().val('Invalid email');
			setTimeout(function()
			{
				$el.val('Subscribe');
				$el.animate({'background-color':'#fff'},500,'easeOutQuad');
			},2000);
		}
	});

	
	
	return false;
}


$(function()
{
	if($('#map').length)
	{
		
		init_map();
	}
	
	equal_heights();
	
	if($('#gallery').length)
	{
		$fsImage = $('#gallery img').first();
		setFS();
		
		$(window).resize(function()
		{
			updateFS();
			var h = $(window).height();
			var o = h - (340);
			$('#home-caption').css('margin-top',o+'px');
		});
		$(window).trigger('resize');
		$('#home-caption,#gallery').fadeIn();
		
	}
	
	$('img.over').hover(function()
	{
		var id = $(this).attr('id').replace('image','caption');
		$('#'+id).addClass('over');
		$(this).parent().find('.no').stop(true,true).fadeIn();
	},function()
	{
		var id = $(this).attr('id').replace('image','caption');
		$('#'+id).removeClass('over');
		$(this).parent().find('.no').stop(true,true).fadeOut();
	});
	
	$('.caption.active').hover(function()
	{
		var id = $(this).attr('id').replace('caption','image');
		$(this).addClass('over');
		$('#'+id).parent().find('.no').stop(true,true).fadeIn();
	},function()
	{
		var id = $(this).attr('id').replace('caption','image');
		$(this).removeClass('over');
		$('#'+id).parent().find('.no').stop(true,true).fadeOut();
	});
	
	$('.press-article,.diary .post').click(function()
	{
		var a = $(this).find('a').first().attr('href');
		location = a;
	});
	/*
	$('#subscriber').focus(function()
	{
		var pos = 4;
		$(this).addClass('focus');
		if($(this).val() == 'Subscribe') {
			///$(this).val('Enter your email');
			$(this).select();
			//return false;
		}
	});
	*/
	$('.subscriber').live('focus mouseup', function(e) {
		if (e.type == 'focusin') {
			$(this).addClass('focus');
			if($(this).val() == 'Subscribe') {
				$(this).val('Enter your email');
				$(this).animate({'background-color':'#e7e3e0'},500,'easeOutQuad');
				this.select();
			}
		}

		if (e.type == 'mouseup') {
			return false;
		}
	});
	
	$('.subscriber').blur(function()
	{
		var pos = 4;
		$(this).removeClass('focus');
		if($(this).val() == 'Enter your email') {
			$(this).val('Subscribe');
			$(this).animate({'background-color':'#fff'},500,'easeOutQuad');
		}
	});

	
});

var equal_heights = function()
{
	var max = 0;
	$('.equal').each(function()
	{
		var h = $(this).height();
		max = Math.max(h,max);
	});
	
	$('.equal').height(max);
}

var setFS = function()
{
	origWidth = $fsImage.attr('width');
	origHeight = $fsImage.attr('height');
	updateFS();
}

var updateFS = function()
{
	//if($fsImage == null) return;
	var w = $(window).width();
	var h = $(window).height();
	var widthRatio = (origHeight/origWidth) * 100;
	var heightRatio = (origWidth/origHeight) * 100;
	var followWidth = new Array();
	var followHeight = new Array();
	
	// Calculate dimensions based on width
	
	followWidth.push(w); // width
	followWidth.push(Math.ceil((w / 100) * widthRatio)); // height
	followHeight.push(Math.ceil((h / 100) * heightRatio)); // width
	followHeight.push(h); // height
	
	if(followWidth[1] > h)
	{
		$fsImage.css('width',followWidth[0]+'px');
		$fsImage.css('height',followWidth[1]+'px');
		var offsetTop = Math.round((followWidth[1] - h) / 2);
		$fsImage.css('top','-'+offsetTop+'px');
		$fsImage.css('left','0px');
	}
	else if(followHeight[0] > w)
	{
		$fsImage.css('width',followHeight[0]+'px');
		$fsImage.css('height',followHeight[1]+'px');
		var offsetLeft = Math.round((followHeight[0] - w) / 2);
		$fsImage.css('left','-'+offsetLeft+'px');
		$fsImage.css('top','0px');
	}
}
