$(document).ready(function(){
	slideShow();
    $('#contact').validate();
	$('.box-news img').each(function() {
        var maxWidth = 470; // Max width for the image
        var maxHeight = 700;    // Max height for the image
        var ratio = 0;  // Used for aspect ratio
        var width = $(this).width();    // Current image width
        var height = $(this).height();  // Current image height
 
        // Check if the current width is larger than the max
        if(width > maxWidth){
            ratio = maxWidth / width;   // get ratio for scaling image
            $(this).css("width", maxWidth); // Set new width
            $(this).css("height", height * ratio);  // Scale height based on ratio
            height = height * ratio;    // Reset height to match scaled image
            width = width * ratio;    // Reset width to match scaled image
			$(this).wrap('<a href="'+$(this).attr('src')+'" class="lightbox">');
        }

    });
	$('.lightbox').lightBox();
	
});
function slideShow() {
	gallery();
	setInterval('gallery()',5000);
}

function gallery() {
	
	//if no IMGs have the show class, grab the first image
	var current = ($('#gallery a.show')?  $('#gallery a.show') : $('#gallery a:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('caption'))? $('#gallery a:first') :current.next()) : $('#gallery a:first'));	

	//Get next image caption
	var url = next.attr('href');
	
	var caption = next.find('img').attr('rel');	
	
	var title = next.find('img').attr('alt');	
	
	caption = '<h3><a href="'+url+'">'+title+'</a></h3><br/>'+caption;
	
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');
	
	//Set the opacity to 0 and height to 1px
	$('#gallery .caption').animate({opacity: 0.0}, { queue:false, duration:0 }).animate({height: '1px'}, { queue:true, duration:300 });	
	
	//Animate the caption, opacity to 0.7 and heigth to 100px, a slide up effect
	$('#gallery .caption').animate({opacity: 0.7},100 ).animate({height: '80px'},500 );
	
	//Display the content
	$('#gallery .content').html(caption);
	
	
}
function popitup(url,w,h) {
	if(h!='1'){
		h = ',height='+h;
	}else{
		h = '';
	}
	newwindow=window.open(url,'name','width='+w+h+',scrollbars=yes');
	if (window.focus) {newwindow.focus()}
	return false;
}
