if(!w3d) var w3d = {};
if(!w3d.fn) w3d.fn = {};

$(document).ajaxSend(function(event, xhr, settings) {
		function getCookie(name) {
				var cookieValue = null;
				if (document.cookie && document.cookie != '') {
						var cookies = document.cookie.split(';');
						for (var i = 0; i < cookies.length; i++) {
								var cookie = jQuery.trim(cookies[i]);
								// Does this cookie string begin with the name we want?
								if (cookie.substring(0, name.length + 1) == (name + '=')) {
										cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
										break;
								}
						}
				}
				return cookieValue;
		}
		function sameOrigin(url) {
				// url could be relative or scheme relative or absolute
				var host = document.location.host; // host + port
				var protocol = document.location.protocol;
				var sr_origin = '//' + host;
				var origin = protocol + sr_origin;
				// Allow absolute or scheme relative URLs to same origin
				return (url == origin || url.slice(0, origin.length + 1) == origin + '/') ||
						(url == sr_origin || url.slice(0, sr_origin.length + 1) == sr_origin + '/') ||
						// or any other URL that isn't scheme relative or absolute i.e relative.
						!(/^(\/\/|http:|https:).*/.test(url));
		}
		function safeMethod(method) {
				return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
		}

		if (!safeMethod(settings.type) && sameOrigin(settings.url)) {
				xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
		}
});

jQuery.fn.ajaxSubmit = function(type, dataType, handler) {
	$.ajax({
		type: type,
		cache: false,
		url: $(this).attr('action'),
		data: $(this).serialize(),
		dataType: dataType,
		success: handler,
		error: function() { fh.saveError('jQuery.fn.ajaxSubmit -> Ajax Submit Failed!'); alert('새로 고침 후에 다시 시도해 주십시오.'); }
	});
};

/*
/ imageObject : jQuery Object [<img style='display:block;'/>]
/ parentSize : {width: <value-x>, height: <value-y>}
*/
w3d.fn.fitImageIntoParent = function(imageObjects) {
	function fitImage(imgObj) {
		// get image size information
		var width = imgObj.width();
		var height = imgObj.height();
		
		var paddingX = imgObj.outerWidth() - width;
		var paddingY = imgObj.outerHeight() - height;
		
		// get parent div information
		var parWidth = imgObj.parent().width() - paddingX;
		var parHeight = imgObj.parent().height() - paddingY;
		
		// if image is smaller than parent
		if (width <= parWidth && height <= parHeight) {
			var top = Math.floor(( parHeight - height ) / 2);
			
			// set width and height
			imgObj.css({marginTop: top});
		}
		else {
			// get zoom ratio
			var widthRatio = parWidth / width;
			var heightRatio = parHeight / height;
			var ratio = widthRatio > heightRatio ? heightRatio : widthRatio;
			
			// calculate width and height
			width = widthRatio == ratio ? parWidth : Math.round(width * ratio);
			height = heightRatio == ratio ? parHeight : Math.round(height * ratio);
			
			var top = Math.floor(( parHeight - height ) / 2);
			
			// set width and height
			imgObj.css({width: width, height: height, marginTop: top});
		}
		imgObj.show();
	}
	
	imageObjects.each(function() {
		var imageObject = $(this);
		
		imageObject.load(function() {
			fitImage($(this));
		});	
	});
	
	return imageObjects;
};