/*
 * jQuery Util Plugin
 * @requires jQuery v1.3.2 or later
 * @author rcnboys
 */
(function($) {
/**
 * 각 엘리먼트에 대해 disabled 어트리뷰트가 있는지 확인하고 만약 있다면 값을 true로 설정한다.
 * @example $("form#FormList input.special").disable();
 * @name disable
 * @type jQuery
 */
$.fn.disable = function(){
	return this.each(function() {
		if(typeof this.disabled != "undefined"){
			this.disabled = true;
		}
	});
};

/**
 * 각 엘리먼트에 대해 disabled 어트리뷰트가 있는지 확인하고 만약 있다면 값을 true로 설정한다.
 * @example $(".req").displayRequired();
 * @name displayRequired
 * @type jQuery 
 */
$.fn.displayRequired = function(){
	this.each(function(){
		$(this).html($(this).text()+"<font color='red'>*</font>");
	});
};

/**
 * 각 엘리먼트에 대해 disabled 어트리뷰트가 있는지 확인하고 만약 있다면 값을 false로 설정한다.
 * @example $("form#FormList input.special").enable();
 * @name enable
 * @type jQuery
 */
$.fn.enable = function(){
	return this.each(function() {
		if(typeof this.disabled != "undefined"){
			this.disabled = false;
		}
	});
};

/**
 * 각 엘리먼트에 대해 SELECT 태그인지 확인하고 만약 맞다면 비운다.
 */
$.fn.emptySelect = function(){
	return this.each(function(){
		if(this.tagName == 'SELECT'){
			this.options.length = 0;
		}
	});
};

/**
 * optionsDataArry에 맞게 select option 추가
 */
$.fn.loadSelect = function(optionsDataArray){
	return this.emptySelect().each(function(){
		if(this.tagName == 'SELECT'){
			var selectElement = this;
			$.each(optionsDataArray, function(index, optionData){
				var option = new Option(optionData.caption, optionData.value);
				if($.browser.msie){
					selectElement.add(option);
				}else{
					selectElement.add(option, null);
				}
			});
		}
	});
};

/**
 * 새로 생성되는 html로 대체할 수 있다.
 */
$.fn.replaceWith = function(html){
	return this.after(html).remove();
};

/**
 * 폼태그 값 초기화
 */
$.fn.reset = function(){
	return this.each(function(){
		if(this.type == 'radio' || this.type == 'checkbox'){
			$(this).attr('checked', '');
		}else{
			var dv = $(this).attr("dv");
			if(dv != "undefined"){
				$(this).val(dv);
			}else{
				$(this).val('');
			}
		}
	});
};

$.fn.resizeImage = function(mWid, mHei, bScale){
	return this.each(function(){
		var maxWidth = mWid; // Max width for the image
        var maxHeight = mHei;    // Max height for the image
        var ratio = 0;  // Used for aspect ratio
        var width = $(this).width();    // Current image width
        var height = $(this).height();  // Current image height
 
        // Check if the current width is larger than the max
        if(width > maxWidth){
            ratio = maxWidth / width;   // get ratio for scaling image
            $(this).css("width", maxWidth); // Set new width
            $(this).css("height", height * ratio);  // Scale height based on ratio
            height = height * ratio;    // Reset height to match scaled image
            width = width * ratio;    // Reset width to match scaled image
        }else{
        	if(bScale == false){
        		ratio = maxWidth / width;   // get ratio for scaling image
                $(this).css("width", maxWidth); // Set new width
                $(this).css("height", height * ratio);  // Scale height based on ratio
                height = height * ratio;    // Reset height to match scaled image
                width = width * ratio;    // Reset width to match scaled image
        	}
        }
 
        // Check if current height is larger than max
        if(height > maxHeight){
            ratio = maxHeight / height; // get ratio for scaling image
            $(this).css("height", maxHeight);   // Set new height
            $(this).css("width", width * ratio);    // Scale width based on ratio
            width = width * ratio;    // Reset width to match scaled image
        }else{
        	if(bScale == false){
        		 ratio = maxHeight / height; // get ratio for scaling image
                 $(this).css("height", maxHeight);   // Set new height
                 $(this).css("width", width * ratio);    // Scale width based on ratio
                 width = width * ratio;    // Reset width to match scaled image
        	}
        }
	});
};

/**
 * Modal Window
 */
$.fn.mw = function(options, _func) {
	options = jQuery.extend({
		width : 400,
		height: 300,
		arg : {},
		scroll : 'on',
		status : 'off',
		sizeable : 'on',
		force : false
		
	}, options);
	return this.each( function() {
		var $item = jQuery(this);
			
		
		$item.click( function(e) {
			var _width = options.width;
			var _height = options.height;
			
			// IE 6.0이면
			if ( $.browser.msie && /MSIE 6.0/.test(navigator.userAgent) ) {
			}
			else if ( $.browser.msie && /MSIE 7.0/.test(navigator.userAgent) ) {	// IE. 7.0이면
				
			}
			else if ( $.browser.msie && /MSIE 8.0/.test(navigator.userAgent) ) {	// IE. 7.0이면
				_width = options.width+45;
				_height = options.height -50;			
			}
			
			e.preventDefault();
			
			var _url = $item.attr("href");
			
			var date = new Date().getTime();
			if( _url.indexOf("?") > -1) {
				_url += '&rt=' + date;
			}else {
				_url += '?rt=' + date;
			}

			
			var ret = window.showModalDialog(_url, options.arg, "dialogWidth:"+(_width)+"px; dialogHeight:"+(_height)+"px; scroll:"+options.scroll+"; status:"+ options.status+"; resizable :"+options.sizeable );

			if( options.force )
				_func(ret);
			
			if( jQuery.isFunction(_func) && ret != undefined ) {
				_func( ret );
			}	
		});
	});	
};

/**
 * Open Window
 */
$.fn.ow = function(options, _func) {
	options = jQuery.extend({
		width : 400,
		height: 300,
		arg : {},
		scroll : 'on',
		status : 'on',
		sizeable : 'on',
		force : false
		
	}, options);
	return this.each( function() {
		var $item = jQuery(this);
			
		
		$item.click( function(e) {
			var _width = options.width;
			var _height = options.height;
			
			// IE 6.0이면
			if ( $.browser.msie && /MSIE 6.0/.test(navigator.userAgent) ) {
			}
			else if ( $.browser.msie && /MSIE 7.0/.test(navigator.userAgent) ) {	// IE. 7.0이면
				
			}
			else if ( $.browser.msie && /MSIE 8.0/.test(navigator.userAgent) ) {	// IE. 7.0이면
				_width = options.width+45;
				_height = options.height -50;			
			}
			
			e.preventDefault();
			
			var _url = $item.attr("href");
			
			var date = new Date().getTime();
			if( _url.indexOf("?") > -1) {
				_url += '&rt=' + date;
			}else {
				_url += '?rt=' + date;
			}

			var winl = (screen.width - _width) / 2;
			var wint = (screen.height - _height) / 2;
			var winprops = 'height='+(_height)+',width='+(_width)+',top='+wint+',left='+winl+',scrollbars=1,resizable';
			var z = window.open(_url, '', winprops);

			if( jQuery.isFunction(_func) ) {
				_func();
			}
			
		});
	});	
};
})(jQuery);

jQuery(function($) {
	_internal = {
			options : jQuery.extend({
				width : 400,
				height: 300,
				arg : {},
				scroll : 'on',
				status : 'on',
				sizeable : 'on',
				force : false			
			}),
			mwManual : function(url, opts, _func) {
				
				$.extend(true, _internal.options, opts);
				
				var _width = _internal.options.width;
				var _height = _internal.options.height;
				
				// IE 6.0이면
				if ( $.browser.msie && /MSIE 6.0/.test(navigator.userAgent) ) {
					_width = _internal.options.width;
				}
				else if ( $.browser.msie && /MSIE 7.0/.test(navigator.userAgent) ) {	// IE. 7.0이면
					
				}
				else if ( $.browser.msie && /MSIE 8.0/.test(navigator.userAgent) ) {	// IE. 7.0이면
					_width = _internal.options.width+45;
					_height = _internal.options.height -50;			
				}
				
				var _url = url;
				
				var date = new Date().getTime();
				
				if( _url.indexOf("?") > -1) {
					_url += '&rt=' + date;
				}else {
					_url += '?rt=' + date;
				}
				
				var ret = window.showModalDialog(_url, _internal.options.arg, "dialogWidth:"+(_width)+"px; dialogHeight:"+(_height)+"px; scroll:"+_internal.options.scroll+"; status:"+ _internal.options.status+"; resizable :"+_internal.options.sizeable );
				
				if( jQuery.isFunction(_func) && ret != undefined ) {
					_func( ret );
				}
			}
	},
	$.extend({
		mwManual: _internal.mwManual
	});
});

