(function($) {
	$.fn.equalHeights = function() {
		var maxheight = 0;
		$(this).each(function(){
			maxheight = ($(this).height() > maxheight) ? $(this).height() : maxheight;
		});
		$(this).css('height', maxheight);
	}
})(jQuery);

$(document).ready(function() {

	$('body').addClass('js');

	if ($('#home #image').length > 0) {
	
		var imgs = [];
		$('#home #image img').each(function (index) {
			var i = new Image();
			i.onload = function () {
				var h = i.height;
				var w = i.width;
				imgs[index] = [w, h];
				$(window).trigger('resize');
			}
			i.src = $(this).attr('src');
		});

		
		$(window).resize(function () {
			
			//var height = $(window).height() - ($('#hdr').outerHeight() + $('#menu').outerHeight());
			var height = $(window).height() - 108;

			
			
			var width = $('#home').width();
			$('#home #image').height(height);
			
			$('#home #image').css({
				'width': width,
				'height': height
			});
			
			$('#home #image img').each(function (index) {
				var self = this;
				
				if (imgs[index] && imgs[index].length === 2) {
					var w = imgs[index][0];
					var h = imgs[index][1];
					
					if ((height/width) > (h/w)) {
						$(self).css({
							'width': (w/h) * height,
							'height': height
						});
						
						//alert("A:height/width£º" + height/width + "h/w£º" + h/w);
						
					} else {
						$(self).css({
							'width': width,
							'height': (h/w) * width
						});
						//alert("B:height/width£º" + height/width + "h/w£º" + h/w);
					}
				}
				
				
			});
			
		}).trigger('resize');
	}

	$('#home ul#panels').each(function() {
		margin = $('#ftr').outerHeight();
		$(this).css('margin-bottom',margin);
	});

	$('#home ul#panels li, #int #main #content .right ul li').each(function() {
		$(this).hover(function() {
			$(this).addClass('hover');
		}, function() {
			$(this).removeClass('hover');
		}).click(function() {
			theLink = $(this).find('a').attr('href');
			window.location = theLink;
		});
	});

    $('#banner').append('<a href="#" id="bannerprev"></a><a href="#" id="bannernext"></a>').find('.items').cycle({
		fx: 'fade',
		pause: 1,
		next: '#bannernext',
		prev: '#bannerprev'
	});
	$('#home #image').cycle({
		fx: 'fade',
		pause: 0
	});

	$("#content .form form").validate();

});

$(window).bind('load', function() {

	$('#home ul#panels .text').equalHeights();

});

function blankInputField(a,b,c) {
    if (document.getElementById(a).value == b) {
        document.getElementById(a).value = "";
        document.getElementById(a).className = c;
    }
}

function populateInputField(a,b,c) {
    if (document.getElementById(a).value == "") {
	    document.getElementById(a).value = b;
	    document.getElementById(a).className = c;
    }
}
