$(document).ready(function() {
	// insert div for greyLayer
	$("body").append('<div id="cje_greyLayer" style="display:none;position:absolute;top:0;left:0;z-index:1000;background:url(\''+cje.base_url+'/core/wait.gif\') #777 50% 50% no-repeat;"></div>');

	// insert div for dialog
	$("body").append('<div id="cje_dialogWindow" style="display:none"></div>');
	// initialize dialog window
	$("#cje_dialogWindow").dialog({
								autoOpen:false,bgiframe:true,
								resizable:false,width:400,height:250,modal:true,
								overlay:{backgroundColor:"#777",opacity:0.5}
							});
});

jQuery.fn.inputHint = function(options) {
	if (typeof(options)=='string') options = {hint:options}
	return this.each(function() {
		options = jQuery.extend({
			hint:'--- hint here ---',
			colorReal:jQuery(this).css('color'),
			colorHint:'#999'
		},options);
		jQuery(this).data('cje_hint',options.hint);
		if (jQuery(this).val()=='') { jQuery(this).css('color',options.colorHint).val(options.hint); }
		jQuery(this)
			.bind('focus',	function() { if (jQuery(this).val()==options.hint) { jQuery(this).val('').css('color',options.colorReal); } })
			.bind('blur',	function() { if (jQuery(this).val()=='') { jQuery(this).val(options.hint).css('color',options.colorHint); } })
		;
		if (jQuery(this).parents('form:first').data('cje_hint')!='cje_hint') {
//			window.alert('need to set submit callback! '+jQuery(this).parents('form:first').data('cje_hint'));
			jQuery(this).parents('form:first').bind('submit',function() {
					jQuery(this).find('input:text, textarea').each(function(){
						if (jQuery(this).val()==jQuery(this).data('cje_hint')) jQuery(this).val('');
					});
				});
			jQuery(this).parents('form:first').data('cje_hint','cje_hint');
		}
//		window.alert('next loop...');
	});
}

jQuery.fn.cjeToday = function(options) {
	if (options=="remove")
		return this.each(function(){
			var currObjects = jQuery(document).data('cjeTodayObjects');
			if (typeof(currObjects)=='object') {
				for (var i in currObjects)
					if (currObjects[i].object == this) {
						clearInterval(currObjects[i].timer);
						delete(currObjects[i]);
					}
			}
			jQuery(document).data('cjeTodayObjects',currObjects);
		});

	options = jQuery.extend({
		timeout: 1000,
		format: "hey!",
		week: ["Sunday","Monday","Tuesday","Wednesday","Thuesday","Friday","Saturday"],
		month: ["January","February","March","April","May","June","July","August","September","October","November","December"]
	},options);
	var currObjects = jQuery(document).data('cjeTodayObjects');
//	window.alert(typeof(currObjects));
	if (typeof(currObjects)!='object') { currObjects = new Array(); }
	return this.each(function(){
		var flag = undefined;
		for (var i in currObjects) {
//		window.alert(i+" : "+currObjects[i]);
			if (currObjects[i].object == this) flag = i;
		}
		if (flag===undefined) {
//			window.alert('not exists');
		} else {
//			window.alert('exists');
			clearInterval(currObjects[flag].timer);
			delete(currObjects[flag]);
		}
//		window.alert(("hey!").repeat(2));
		var currObj = new Object({object:this,format:options.format});
		var currTimer = setInterval(function() {
						(function(obj) {
							var now = new Date();
							var str = new String(obj.format);
							str = str.replace(/%Hour/, now.getHours().toFormat(2,0));
							str = str.replace(/%hour/, now.getHours());
							str = str.replace(/%Min/, now.getMinutes().toFormat(2,0));
							str = str.replace(/%min/, now.getMinutes());
							str = str.replace(/%Sec/, now.getSeconds().toFormat(2,0));
							str = str.replace(/%sec/, now.getSeconds());
							str = str.replace(/%Y/, now.getFullYear());
							str = str.replace(/%y/, now.getFullYear().toString().substr(-2));
							str = str.replace(/%M/, options.month[now.getMonth()]);
							str = str.replace(/%m/, now.getMonth()+1);
							str = str.replace(/%W/, options.week[now.getDay()]);
							str = str.replace(/%D/, now.getDate().toFormat(2,0));
							str = str.replace(/%d/, now.getDate());
							jQuery(obj.object).html(str);
						})(currObj);
					},options.timeout)
		currObj.timer = currTimer;
		currObjects.push(currObj);

		jQuery(document).data('cjeTodayObjects',currObjects);
	});
/*
	setInterval((function(obj) {
		window.alert(obj);
		jQuery(obj).html("yo!");
	})(this),3000);
*/
}

Number.prototype.toFormat = function(x,y) {
	return (new Array(x-this.toFixed(0).length+1)).join('0')+(new String(this.toFixed(y)));
}

function cje_showDialog(title,body,buttons) {
	$("#cje_dialogWindow").html(body)
		.dialog("option","title",title)
		.dialog("option","buttons",buttons).dialog("open");
}
function cje_greyLayer(opacity,speed) {
	if (!speed) speed="normal";
	$("#cje_greyLayer").css({width:$(window).width(),height:$(window).height()});
	if ($("#cje_greyLayer").is(":hidden"))
		$("#cje_greyLayer").css({display:"block",opacity:0});
	$("#cje_greyLayer").fadeTo(speed,opacity,(function(o) { if (!o) $("#cje_greyLayer").hide(); })(opacity));
}