(function($){	
		
function open_modal(){
	$('body').prepend('<div id="modal"><div id="mask" class="close_modal"></div><div class="modal_window"></div></div>');
	$('#modal , #mask , .modal_window').fadeIn(500);
}

function close_modal(){
	$('#modal , #mask , .modal_window').fadeOut(500, function(){ 
	  $('.modal_window .modal_content').appendTo('.hidden'); 
	  $('#modal').remove(); 
	});
}

function open_gallery_modal(){
	$('body').prepend('<div id="modal"><div id="mask" class="close_gallery_modal"></div><div class="modal_window"></div><div class="logo">Sanctuary South Beach</div></div>');
	$('#modal , #mask , .modal_window').fadeIn(500);
}

function close_gallery_modal(){
	$('#modal , #mask , .modal_window').fadeOut(500, function(){ $('#modal').remove(); } );
	$('.gallery_item').removeClass('current_gallery_img');
	$('.gallery_item').removeClass('next_gallery_img');
	$('.gallery_item').removeClass('prev_gallery_img');
}

var repositionModal = function() {
    $('.modal_window').each(function(){
        var window_height = $(window).height();
		var window_width = $(window).width();
        var modal_height = $(this).height();
        var modal_width = $(this).width();
        var top = (window_height - modal_height)/2;
        var left = (window_width - modal_width)/2;
		$(this).css({'top' : top , 'left' : left});
    });
};

var resizeImage = function() {
  var picW = $('.modal_window img').width();
  var picH = $('.modal_window img').height();
  var conW =  $(window).width() - 180;
  var conH = $(window).height() - 180;
  var picRatio = picW / picH;
  var conRatio = conW / conH;
  var ratio  = (picRatio > conRatio)? picW / conW : picH / conH;
  
  if(picW > conW || picH > conH){
  $(".modal_window").css({
    "width": picW / ratio,
    "height": picH / ratio
  });
  $(".modal_window img").css({
    "width": picW / ratio,
    "height": picH / ratio
  });
  }
};      	

jQuery(document).ready(function(){
	$('a.open_modal').live('click', function(e){
		e.preventDefault();
		open_modal();
		var link = $(this).attr('rel');
		$('#' + link).appendTo('.modal_window');
		repositionModal();
	});
	
	$('a.open_specials_modal').live('click', function(e){
		e.preventDefault();
		open_modal();
		var link = $(this).attr('rel');
		$('#' + link).appendTo('.modal_window');
		repositionModal();
		$("#specials_content").mCustomScrollbar("vertical",0,"easeOutCirc",1,"fixed","yes","yes",10);
	});
	
	$('.close_modal').live('click', function(e){ 
		close_modal();
    });
	
	
	$('.gallery_item a').live('click', function(e){
		e.preventDefault();
		open_gallery_modal();
		repositionModal();
		$('.modal_window').html('<div class="loader">Loading...</div>');
		var img_url = $(this).attr('href');
        var img = new Image();
        $(img).load(function(){
            $('.loader').remove();
            $('.modal_window').append(img);
			resizeImage();
			repositionModal();
        }).attr('src',img_url).attr('class', 'gallery_img').attr('alt','Sanctuary South Beach');
		
		$(this).parent().prev().addClass('prev_gallery_img');
		var prev = $('.prev_gallery_img a').clone();
		$(prev).text(function(index) { return '<'; });
		if ($(prev).length ) {
		  $('#modal').append('<div class="prev_img fontface"></div>');
		  $('.prev_img').append(prev);
		};
		
		$(this).parent().next().addClass('next_gallery_img');
		var next = $('.next_gallery_img a').clone();
		$(next).text(function(index) { return '>'; });
		if ($(next).length ) {
		  $('#modal').append('<div class="next_img fontface"></div>');
		  $('.next_img').append(next);
		};
	});
	
	$('.next_img a').live('click', function(e){
		e.preventDefault();
		$('.prev_img , .next_img').remove();
		$('.gallery_item').removeClass('current_gallery_img');
		$('.next_gallery_img').addClass('current_gallery_img');
		$('.gallery_item').removeClass('next_gallery_img');
		$('.gallery_item').removeClass('prev_gallery_img');
		repositionModal();
		$('.modal_window').html('<div class="loader">Loading...</div>');
		var img_url = $(this).attr('href');
        var img = new Image();
        $(img).load(function(){
            $('.loader').remove();
            $('.modal_window').append(img);
			resizeImage();
			repositionModal();
        }).attr('src',img_url).attr('class', 'gallery_img').attr('alt','Sanctuary South Beach');
		
		$('.current_gallery_img').prev().addClass('prev_gallery_img');
		var prev = $('.prev_gallery_img a').clone();
		$(prev).text(function(index) { return '<'; });
		if ($(prev).length ) {
		  $('#modal').append('<div class="prev_img fontface"></div>');
		  $('.prev_img').append(prev);
		}
		
		$('.current_gallery_img').next().addClass('next_gallery_img');
		var next = $('.next_gallery_img a').clone();
		$(next).text(function(index) { return '>'; });
		if ($(next).length ) {
		  $('#modal').append('<div class="next_img fontface"></div>');
		  $('.next_img').append(next);
		}
	});
	
	$('.prev_img a').live('click', function(e){
		e.preventDefault();
		$('.prev_img , .next_img').remove();
		$('.gallery_item').removeClass('current_gallery_img');
		$('.prev_gallery_img').addClass('current_gallery_img');
		$('.gallery_item').removeClass('next_gallery_img');
		$('.gallery_item').removeClass('prev_gallery_img');
		repositionModal();
		$('.modal_window').html('<div class="loader">Loading...</div>');
		var img_url = $(this).attr('href');
        var img = new Image();
        $(img).load(function(){
            $('.loader').remove();
            $('.modal_window').append(img);
			resizeImage();
			repositionModal();
        }).attr('src',img_url).attr('class', 'gallery_img').attr('alt','Sanctuary South Beach');
		
		$('.current_gallery_img').prev().addClass('prev_gallery_img');
		var prev = $('.prev_gallery_img a').clone();
		$(prev).text(function(index) { return '<'; });
		if ($(prev).length ) {
		  $('#modal').append('<div class="prev_img fontface"></div>');
		  $('.prev_img').append(prev);
		}
		
		$('.current_gallery_img').next().addClass('next_gallery_img');
		var next = $('.next_gallery_img a').clone();
		$(next).text(function(index) { return '>'; });
		if ($(next).length ) {
		  $('#modal').append('<div class="next_img fontface"></div>');
		  $('.next_img').append(next);
		}
	});
	
	$('.close_gallery_modal').live('click', function(e){ 
		close_gallery_modal();
    });
});

$(window).resize(function() {
	repositionModal();
});

})(jQuery);
