/**
 * @require jQuery 1.4.2+
 * 
 * @version 1.0.0
 */
jQuery.util = {};

/**
 * IE6・IE7 browser check
 */
jQuery.util.browser = {
	ie6flg: null,
	isIE6: function() {
		if (this.ie6flg !== null) return this.ie6flg;
		return this.ie6flg = /.*MSIE\s*6\.[0-9].*/.test(navigator.userAgent);
	},
	ie7flg: null,
	isIE7: function() {
		if (this.ie7flg !== null) return this.ie7flg;
		return this.ie7flg = /.*MSIE\s*7\.[0-9].*/.test(navigator.userAgent);
	},
	ieflg: null,
	isIE: function() {
		if (this.ieflg !== null) return this.ieflg;
		return this.ieflg = /.*MSIE\s*[0-9]+\.[0-9].*/.test(navigator.userAgent);
	}
};

/**
 * form submit
 */
jQuery.util.submitForm = function(formSelector, url, type){
	j$(formSelector).attr('method', type || 'post').attr('action', url).submit();
};


/**
 * unescape(&は最後)
 */
jQuery.util.unescape = function(str) {
	str = str.replace(/&quot;/g,"\"");
	str = str.replace(/&#034;/g,"\"");
	str = str.replace(/&#039;/g,"'");
	str = str.replace(/&lt;/g,"<");
	str = str.replace(/&gt;/g,">");
	str = str.replace(/&amp;/g, "&");
	return str;
};

/**
 * escape(&は最初)
 */
jQuery.util.escape = function(str) {
	str = str.replace("&", "&amp;");
	str = str.replace("\"", "&quot;");
	str = str.replace("'", "&#039;");
	str = str.replace("<", "&lt;");
	str = str.replace(">", "&gt;");
	return str;
};

/**
* overlay
*/
jQuery.util.overlay = {

	show: function(target, option, isParent){
		option = option ? option : {};
		option.color = option.color ? option.color : '#000'; 
		option.opacity = option.opacity ? option.opacity : '0.5';
		option.filter = option.filter ? option.filter : 'alpha(opacity=50)';
		option.zIndex = option.zIndex ? option.zIndex : '1000';
		
		if(!target.data('visible')){
			overlayPoint(target, option, isParent);
		}
		
		j$(window).bind('resize.overlay', function(){
			overlayPoint(target, option, isParent);
	 		return false;
		});
		
		function overlayPoint(target, option, isParent){
			target.css({backgroundColor: option.color, opacity: option.opacity, filter: option.filter, 
				top:'0px', left: '0px', zIndex: option.zIndex});
			if(j$.util.browser.isIE6()){
				var doc = isParent ? j$(parent.document.body) : j$(document.body);
				
				var width = doc.width();
				var height = doc.height();
				
				target.css({position: 'absolute', width: width, height: height});
				j$('select').not('.showSelect').css('visibility', 'hidden');
			} else {
				target.css({position: 'fixed', width: '100%', height: '100%'});
			}
			target.data('visible', true).show();
		}
	},
	hide: function(target){
		if(j$.util.browser.isIE6()) j$('select').not('.showSelect').css('visibility', 'visible');
		j$(window).unbind('resize.overlay');
		target.data('visible', false).hide();
	},
	is: function(target){
		return (target.data('visible'));
	}
};


/**
 * parameter IO
 */
jQuery.util.form = function(formId) {
	this.formId = formId;
	this.form = j$(formId);
	this.get = function(){
		return this.form.serialize();
	};
	this.getVal = function(name){
		return this.form.find('[name='+name+']');
	};
	this.setVal = function(name, values){
		this.remove(name);
		this.form.append(values);
	};
	this.changeVal = function(name, value){
		this.form.find(':hidden[name='+name+']').val(value);
	};
	this.addVal = function(name, val, clazz){
		if(this.form.find(':hidden[name='+name+'][value='+val+']').length > 0){ return; }
			(clazz)?
					this.form.append('<input type="hidden" name="'+name+'" value="'+val+'" class="'+clazz+'" />'):
					this.form.append('<input type="hidden" name="'+name+'" value="'+val+'" />');
	};
	this.removeVal = function(name, val){
		this.form.find(':hidden[name='+name+'][value='+val+']').remove();
	};
	this.removeClazz = function(clazz){
		this.form.find(clazz).remove();
	};
	this.remove = function(name){
		this.form.find(':hidden[name='+name+']').remove();
	};
	this.size = function(name){
		return this.form.find('[name='+name+']').length;
	};
};

/**
* ウィンドウサイズが変わったら、ポップアップ表示場所を変更
*/
jQuery.util.popupResize = {

	bind : function popResize(target) {
		j$(window).bind('resize.popupResize', function() {
			target.setCenterPos();
			return false;
		});
	},
	unbind : function() {
		j$(window).unbind('resize.popupResize');
	},
	bindFixed : function popResize(target) {
		j$(window).bind('resize.popupResizeFixed', function() {
			target.setCenterPosFixed();
			return false;
		});
	},
	unbindFixed : function() {
		j$(window).unbind('resize.popupResizeFixed');
	}
};

/**
 * ウィンドウがスクロールしたら、ポップアップ表示場所を変更
 */
jQuery.util.popupScroll = {
	conf : {
		timer : null
	},
	bind : function popScroll(target, isParent) {
		j$(window).bind('scroll.popupScroll', function() {
			if (j$.util.popupScroll.conf.timer) {
				clearTimeout(j$.util.popupScroll.conf.timer);
				j$.util.popupScroll.conf.timer = null;
			}
			j$.util.popupScroll.conf.timer = setTimeout( function() {
				target.nextPos = isParent ? target.setParentCenterPosSet() : target.setCenterPosSet();
				target.animate(target.nextPos, "slow");
			}, 100);
			return false;
		});
	},
	unbind : function() {
		j$(window).unbind('scroll.popupScroll');
	}
};

/**
 * 親のスクロール（jqueryだと上手くいかない）
 */
jQuery.util.parentScroll = {
	conf : {
		timer : null
	},
	bind : function popScroll(target) {
		if (j$.util.browser.isIE6()) {
			parent.document.body.parentNode.onscroll = function() {
				if (j$.util.popupScroll.conf.timer) {
					clearTimeout(j$.util.popupScroll.conf.timer);
					j$.util.popupScroll.conf.timer = null;
				}
				j$.util.parentScroll.conf.timer = setTimeout( function() {
					target.nextPos = target.setCenterPos(true);
					target.animate(target.nextPos);
				}, 100);
				return false;
			};
		} else if (j$.util.browser.isIE()) {
			parent.document.body.onscroll = function() {
				if (j$.util.popupScroll.conf.timer) {
					clearTimeout(j$.util.popupScroll.conf.timer);
					j$.util.popupScroll.conf.timer = null;
				}
				j$.util.parentScroll.conf.timer = setTimeout( function() {
					target.nextPos = target.setCenterPosFixed(true);
					target.animate(target.nextPos);
				}, 100);
				return false;
			};
		} else {
			parent.document.onscroll = function() {
				if (j$.util.popupScroll.conf.timer) {
					clearTimeout(j$.util.popupScroll.conf.timer);
					j$.util.popupScroll.conf.timer = null;
				}
				j$.util.parentScroll.conf.timer = setTimeout( function() {
					target.nextPos = target.setCenterPosFixed(true);
					target.animate(target.nextPos);
				}, 100);
				return false;
			};
		}
	},
	unbind : function() {
		// TODO
		j$(window).unbind('scroll.popupScroll');
	}
};

/**
 * cumulativeOffset from prototype.js
 */
jQuery.cumulativeOffset = function(element) {
	var valueT = 0, valueL = 0;
	do {
		valueT += element.offsetTop || 0;
		valueL += element.offsetLeft || 0;
		element = element.offsetParent;
	} while (element);
	return {
		'top' : valueT,
		'left' : valueL
	};
};

/**
 * http://16c.jp/2008/0528214632.php
 */
jQuery.scope = function(target, func) {
	return function() {
		return func.apply(target, arguments);
	};
};

/**
 * リクエストのキャンセル処理
 */
jQuery.util.cancelRequest = function(xhr) {
	try{
		if(xhr){ xhr.abort(); }
	}catch(error){
		
	} finally{
		return xhr = null;
	}
};

/**
 * elem の value を配列でリターン
 */
jQuery.util.elemVal = function(selector) {
	var elem = j$(selector);
	var rtn = [];
	elem.each( function() {
		var val = j$(this).val();
		if (val)
			rtn.push(val);
	});
	return rtn || [];
};

/**
 * plug-in
 */
jQuery.fn.extend( {
	/** ポップアップを画面中央に表示する(absolute)　*/
	setCenterPos : function(isParent) {
		var target = j$(this);
		var win = isParent ? j$(parent.window) : j$(window);
		return target.css( {
			position : 'absolute', 
			left : (win.width() - target.width()) / 2 + win.scrollLeft(),
			top : (win.height() - target.height()) / 2 + win.scrollTop()
		});
	},
	/** ポップアップを画面中央に表示する(fixed)　*/
	setCenterPosFixed : function(isParent) {
		var target = j$(this);
		var win = isParent ? j$(parent.window) : j$(window);
		return target.css( {
			position : 'fixed', 
			left : (win.width() - target.width()) / 2,
			top : (win.height() - target.height()) / 2 * 0.8
		});
	},
	/** 画面中央のTOP、LEFTの値を設定　*/
	setCenterPosSet : function(isParent) {
		var target = j$(this);
		var win = isParent ? j$(parent.window) : j$(window);
		return {
			left : (win.width() - target.width()) / 2 + win.scrollLeft(),
			top : (win.height() - target.height()) / 2 + win.scrollTop()
		};
	},
	/** チェックボックスを全てチェック　*/
	check : function() {
		return this.each( function() {
			this.checked = true;
		});
	},
	/** チェックボックスを全てチェックを外す　*/
	uncheck : function() {
		return this.each( function() {
			this.checked = false;
		});
	}
});

