function submitForm(e) {
	e.preventDefault();
	$.ajax({
	        'type': 'POST',
	        'url': $('#modal-box-popup').find('form').first().attr('action'),
	        'data': $('#modal-box-popup').find('form').first().serialize(),
	        'dataType': 'html',
	        cache: false,
	        'success': function (data) {
	                if(!$(data).find('form').length) {
	            		if($('#popup-container').hasClass('ordered')) {
	            			$('#popup-container').removeClass('ordered');
	            		}
	                	$('#popup-container').delay(1500).fadeOut(function(){
	    		    		if($('#popup-container').hasClass('ordered')) {
	    		    			$('#popup-container').removeClass('ordered');
	    		    		}
	                	});
	                	$('#shadow').fadeOut();
	                	$('#modal-box-popup').append(data);
	                } else {
	                	$('#modal-box-popup').html(data);
	                	$('#modal-box-popup').find('form').first().find('input:submit').removeAttr('disabled');
	                	$('#modal-box-popup').find('form').first().bind('submit', submitForm);
	                	$('#modal-box-popup').find('.popup-close').click(function(){
	                		$('#popup-container').fadeOut(function(){
	        		    		if($('#popup-container').hasClass('ordered')) {
	        		    			$('#popup-container').removeClass('ordered');
	        		    		}
	                		});
	                		$('#shadow').fadeOut();
	                	});
	                }
	        },
	        'beforeSend': function () {
	                $('#popup-container > img').show();
	                $('#modal-box-popup').find('form').first().find('input:submit').attr('disabled','disabled');

	        },
	        'complete': function () {
	                $('#popup-container > img').hide();
	        }
		});
		return false;
}

function bindFormCloser() {
	var mouse_is_inside = false;

	$('#popup-container').hover(function(){
	    mouse_is_inside=true;
	}, function(){
	    mouse_is_inside=false;
	});
	
	$('.portfolio-full').hover(function(){
	    mouse_is_inside=true;
	}, function(){
	    mouse_is_inside=false;
	});

	$("body").mouseup(function(){
		if(! mouse_is_inside) {
			$('#popup-container').fadeOut(function(){
	    		if($('#popup-container').hasClass('ordered')) {
	    			$('#popup-container').removeClass('ordered');
	    		}
			});
			$('.portfolio-full').fadeOut(0);
			$('#shadow').fadeOut(0);
		}
	});

	$(document).keyup(function(e) {
		  if (e.keyCode == 27) {
			  $('#popup-container').fadeOut(function(){
		    		if($('#popup-container').hasClass('ordered')) {
		    			$('#popup-container').removeClass('ordered');
		    		}
			  });
			  $('#shadow').fadeOut(0);
			  $('.portfolio-full').each(function(){
				$(this).hide();
			  });

		  }   // esc
	});
}

function loadForm(e) {
	e.preventDefault();
	var leftModalPos = $(e.target).offset()['left'] - 130 > 0 ? $(e.target).offset()['left'] - 130 : 10;
	var topModalPos = 10;
	if($(e.target).offset()['top'] - 130 > 0) { topModalPos = $(e.target).offset()['top'] - 130}
	if($(document).height().toString().replace('px', '') < topModalPos + 355)
		topModalPos = $(document).height().toString().replace('px', '') - 365;
	$('#modal-box-popup').find('form').remove();
	$('#popup-container').css({position: 'absolute', left: leftModalPos, top: topModalPos });
	$('#popup-container').fadeIn(200);
	$('#shadow').fadeIn();
	$.ajax({
        'type': 'GET',
        'url': $(e.target).get(0).tagName == "A" ? $(e.target).attr('href') : $(e.target).closest('li').find('a').first().attr('href'),
        'dataType': 'html',
        'success': function (data) {
			$('#modal-box-popup').html(data);
			if($('#modal-box-popup').find('#order-form').length) {
				$('#popup-container').addClass('ordered');
				leftModalPos = $(e.target).offset()['left'] - 260 > 0 ? $(e.target).offset()['left'] - 260 : 10;
				topModalPos = 10;
				if($(e.target).offset()['top'] - 260 > 0) { topModalPos = $(e.target).offset()['top'] - 260}
				if($(document).height().toString().replace('px', '') < topModalPos + 355)
					topModalPos = $(document).height().toString().replace('px', '') - 365;
				$('#popup-container').css({position: 'absolute', left: leftModalPos, top: topModalPos });
			}
			$('#modal-box-popup').find('form').first().bind('submit', submitForm);
			$('#modal-box-popup').find('.popup-close').click(function(){
        		$('#popup-container').fadeOut(function(){
		    		if($('#popup-container').hasClass('ordered')) {
		    			$('#popup-container').removeClass('ordered');
		    		}
			    });
        		$('#shadow').fadeOut();
        	});
        },
        'beforeSend': function () {
            $('#popup-container > img').show();

	    },
	    'complete': function () {
	           $('#popup-container > img').hide();
	    }
	});
}

$(function() {
	$('.click').bind('click', loadForm);
	bindFormCloser();
});

