$(document).ready(function(){
	var delta = $('#latestJobsScrollContent').height() - $('#latestJobsScrollWrapper').height();
	if(delta > 0)
	{
		$('#latestJobsSlider').slider({
			orientation: "vertical",
			min: 0,
			max: delta,
			value: delta,
			slide: function( event, ui ) {
				$( "#latestJobsScrollContent" ).css( 'top', ui.value - delta );
			}
		});
	}
	$('.lightbox').lightBox();
	$('.ref_lightbox').lightBox();
	
	//alert($('#job_imag').width());
	//hide the image untill it loads and is resized
	if($('#job_imag').width() > 35){
		var dim = resizeRectToLimit($('#job_imag').width(),$('#job_imag').height(),216,162); 
		$('#job_imag').css({'width':dim.width,'height':dim.height});
	}
	
	$('#job_imag').load(function(){ 
		var dim = resizeRectToLimit($(this).width(),$(this).height(),216,162); 
		$(this).css({'width':dim.width,'height':dim.height});
	});
	
	$('.reference_img').each(function(){
		//hide the image untill it loads and is resized
		if($(this).width() > 35){
			var dim = resizeRectToLimit($(this).width(),$(this).height(),216,162); 
			$(this).css({'width':dim.width,'height':dim.height});
		}
		$(this).load(function(){ 
			var dim = resizeRectToLimit($(this).width(),$(this).height(),118,89); 
			$(this).css({'width':dim.width,'height':dim.height}); 
		});
	});
	fileInputs = function() {
	  var $this = $(this),
		  $val = $this.val(),
		  valArray = $val.split('\\'),
		  newVal = valArray[valArray.length-1],
		  $button = $this.siblings('.button'),
		  $fakeFile = $this.siblings('.file-holder');
	  if(newVal !== '') {
		/*$button.text('Photo Chosen');
		if($fakeFile.length === 0) {
		  $button.after('' + newVal + '');
		} else {
		  $fakeFile.text(newVal);
		}*/
		$('#sf_filename').text(newVal);
	  }
	};

	$(document).ready(function() {
	  $('.file-wrapper input[type=file]')
	  .bind('change focus click', fileInputs);
	});
});
/* return the dimension of the resized to the maximum values rectangle */
function resizeRectToLimit(wid ,heig , wMax , hMax )
{
	var coef = 1;
	var wCoef = wid/wMax;
	var hCoef = heig/hMax;
	if (wCoef > 1 || hCoef > 1)
	{
		coef =  (wCoef > hCoef) ? wMax/wid : hMax/heig;
		var tWidth =(coef * wid);
		var tHeight = (coef * heig); 
		return {'width' : tWidth, 'height': tHeight};
	}
	return {'width' : wid, 'height': heig};
}
