$(document).ready(function() {
	$(function(){
		site = new Site ;
		site.init();
	});
});

function Site() {
	this.global_width = 980;
	this.portfolio_aniamtion_speed = 400;
	this.portfolio_z_index = 999;
	this.did_alert = false;
}

Site.prototype = {
		
	/////////////////////////////////////////////////////
	//INITIALIZERS
	/////////////////////////////////////////////////////
	init:function(){
		this.initListeners();
		this.resize();
		
	},	
	
	initListeners:function(){
		$('#portfolios .item').stop().mouseover(site.openPortfolioEntry);
		$('#portfolios .item').stop().mouseleave(site.closePortfolioEntry);
		$('#portfolios .item').stop().click(site.clickPortfolioEntry);
		$(window).resize(site.resize);
	},
	
	
	resize:function() { 
		var w = $(window).width();
		w-=200;//ensure 100 margins
		if (w<500) w=500;
		if (w>1200) w=1100;
		
		site.global_width = w;
		
				
		$('#wrapper').width(w);
		$('#centercol').width(w);
		$('#portfolios').width(w);
		$('#portfolios .item').width(w);
		$('#portfolios .item .scrim img').each(function(){
			var thew = parseFloat($(this).attr('thewidth'));
			var theh = parseFloat($(this).attr('theheight'));
			var ratio = thew/theh;
			var h = Math.ceil(site.global_width/ratio);	
			//if (site.did_alert==false) alert(theh);
			//site.did_alert=true;
			$(this).width(site.global_width);
			$(this).height(h);
		});
		$('#portfolios .item .bg img').each(function(){
			var thew = parseFloat($(this).attr('thewidth'));
			var theh = parseFloat($(this).attr('theheight'));
			var ratio = thew/theh;
			var h = Math.ceil(site.global_width/ratio);
			//if (site.did_alert==false) alert(theh);
			//site.did_alert=true;
			$(this).width(site.global_width);
			$(this).height(h);
		});		
	},
	
	openPortfolioEntry:function() { 
		site.portfolio_z_index++;
		$(this).css('zIndex',site.portfolio_z_index);//pop to top
		$(this).attr('isopen',true); //set open
		var h = $('.bg img',this).height(); //get height
		$(this).stop().animate({height:h,top:0},site.portfolio_aniamtion_speed,"easeOutQuad"); //open up to height, move to top 0
		$('.scrim',this).stop().fadeTo(site.portfolio_aniamtion_speed,0); //fade out scrim
		$('.title',this).stop().fadeTo(site.portfolio_aniamtion_speed*.5,0); //fade out title
	},
	closePortfolioEntry:function() { 
		$(this).attr('isopen',false); //set to closed
		var ogh = $(this).attr('orig_top');
		$(this).stop().animate({height:30,top:ogh},site.portfolio_aniamtion_speed,"easeInQuad"); //brin gback to 30
		$('.scrim',this).stop().fadeTo(site.portfolio_aniamtion_speed,1); //fade in scrim
		$('.title',this).stop().fadeTo(site.portfolio_aniamtion_speed,1); //fade in title
	},
	clickPortfolioEntry:function() { 
		var isopen = $(this).attr('isopen');
		if (isopen) { 
			$(this).trigger('mouseleave');
		} else { 
			//$(this).trigger('mouseover');
		}
	}

}// p


