// JavaScript Document

var photoWidth=0;
var photoHeight=0;

function photoPlace(){
	var contentWidth=$('#content').width();
	var contentHeight=$('#content').height();
	var newPhotoWidth=photoWidth;
	var newPhotoHeight=photoHeight;	
	var top=0;
	var left=0;
	
	if(photoWidth*photoHeight>contentWidth*contentHeight){																														
		if(photoWidth>=contentWidth){
			newPhotoWidth=contentWidth;
			newPhotoHeight=(photoHeight*newPhotoWidth)/photoWidth;
		}
		if(newPhotoHeight>=contentHeight){
			newPhotoHeight=contentHeight;
			newPhotoWidth=(photoWidth*newPhotoHeight)/photoHeight;
		}
		if(newPhotoWidth<contentWidth){
			newPhotoWidth=contentWidth;
			newPhotoHeight=(photoHeight*newPhotoWidth)/photoWidth;
			left=0;
			top=((newPhotoHeight-contentHeight)/2)*(-1);		
		}
		if(newPhotoHeight<contentHeight){
			newPhotoHeight=contentHeight;
			newPhotoWidth=(photoWidth*newPhotoHeight)/photoHeight;
			left=((newPhotoWidth-contentWidth)/2)*(-1);
			top=0;
		}
	}else{
		if(photoWidth<contentWidth){
			newPhotoWidth=contentWidth;
			newPhotoHeight=(newPhotoWidth*photoHeight)/photoWidth;
			left=0;
			top=((newPhotoHeight-contentHeight)/2)*(-1);
		}
		if(newPhotoHeight<contentHeight){
			newPhotoHeight=contentHeight;
			newPhotoWidth=(newPhotoHeight*photoWidth)/photoHeight;
			left=((newPhotoWidth-contentWidth)/2)*(-1);
			top=0;
		}
	}
	$('#photo').width(newPhotoWidth).height(newPhotoHeight).css({'top':top, 'left':left});
	return false;
}

function photoClick(){
	$('#photo').click(function(){
		window.location = "http://www.ruidophoto.com/photo/";
	});
	return false;
}

window.onload=function(){
	//alert('onload');
}

window.onresize=function(){
	photoPlace();
	return false;
}

$(document).ready(function(){
	photoSrc='portada1b.jpg';
	imgPhoto=new Image();
	imgPhoto.onload = function(){
		photoWidth=$(this).attr('width');
		photoHeight=$(this).attr('height');
		var strPhoto='<div class="fons">'
		strPhoto+='<img id="photo" alt="photo" src="'+photoSrc+'"/>';
		strPhoto+='</div>';
		$('#obra').html(strPhoto);
		photoClick();
		photoPlace();
		$('#obra').fadeIn('fast');
	}
	imgPhoto.src=photoSrc;
	return false;
});
