$(document).ready(function(){
	SMessages.error.text = function(message){ 
		if (!message) return;
		var reg=/errortip{(\w+)}(.+)/; 
		var chunks = message.split(';');
		var restMsg = [];
		for (var i=0; i < chunks.length; i++){
			var result=reg.exec(chunks[i]);
			if (result == null) {
				restMsg.push(chunks[i]);
				continue;
			}
			var obj = $('#' + result[1]);
			var message = result[2];
			if(obj.size() > 0){
				obj.after('<span class="errorTip">' + message + '</span>').addClass('errorTip');
			}else{
				restMsg.push(message);
			}
		}
		SMessages.error.toShow = (restMsg.length > 0);
		if (SMessages.error.toShow){
			$('#error span.text').text(restMsg.join('<br/>'));
		}
	};
	SMessages.hide = function(){
		SMessages.error.hide();
		SMessages.status.hide();
		$('span.errorTip').remove();
		$('.errorTip').removeClass('errorTip');
	}
	SMessages.status.text = function(message){ $('#status span.text').text(message); };
	SMessages.ttl = 10000;
	SMessages.status.onShow = SMessages.error.onShow = function(){
		if ((this.id == '#error') && (this.toShow == false)) return;
		var top = $(window).scrollTop();
		var left = $(window).scrollLeft() + $(window).width() / 2 - $(this.id).width() / 2;
		var arrangeF = this.arrange;
		$(this.id).stop().css({opacity: 1, left: left, top: top - 50}).show().animate({top:top + 10}, {complete:function(){$(window).bind('scroll', arrangeF);}  });
	}
	SMessages.status.onHide = SMessages.error.onHide = function(){
		$(window).unbind('scroll', this.arrange);
		$(this.id).fadeOut();
	}
	SMessages.arrange = function(){
		if (!this.isShown) return;
		var top = $(window).scrollTop();
		var left = $(window).scrollLeft() + $(window).width() / 2 - $(this.id).width() / 2;
		$(this.id).stop().animate({top: top + 10,  left: left}, {duration: 'fast'});
	}
	SMessages.error.arrange = jQuery.proxy(SMessages.arrange, SMessages.error);
	SMessages.status.arrange = jQuery.proxy(SMessages.arrange, SMessages.status);
	
	$('a.ajax').each(
		function(a, b){
			$(b).attr('url', b.href).bind("click", function(){ $('#rightcolumn').overlay(); $('#rightcolumn').load( $(b).attr('url'), function(){$('#overlay').hide();} ); })
			b.href = "javascript:void(0)";
		}
	);
});
jQuery.fn.overlay = function (overlay){
	if (overlay == undefined) overlay = $('#overlay');
	overlay.css({
		position: "absolute", 
		top: this.position().top, 
		left: this.position().left,
		width: this.width(),
		height: this.height()
	});
	this.after(overlay);
	if(jQuery.support.opacity){
		overlay.fadeIn();
	}else{
		overlay.show();
	}
	return this;
}

jQuery.fn.highlight = function (id){
	var originalBG = this.css("backgroundColor");
	var originalC = this.css("color");
	this.animate({backgroundColor: '#FFE8E8', color: '#ff0000'}, 600)
		.animate({backgroundColor: originalBG, color: originalC}, 1200);
   	return this;
}
jQuery.fn.checkAll = function(status){
	var allChecked = false;
	$('input:checkbox', this).each(
		function(i, item){
			if(item.name != 'ids[]') return;
			item.checked = status;
		}
	);
	if (status) $('button:#delete').show(); else $('button:#delete').hide();
	return this;
}

function scanCheckboxes(){
	 var checkedStatus = 'none';
	 var checkedCount = 0;
	 var allCount;
	 $('input:checkbox').each(
			 function(i, item){
				 if (item.name != 'ids[]') return;
				 allCount++;
				 if (item.checked) checkedCount++;
			 }
	 );
	 if (checkedCount == allCount) return 'all';
	 if (checkedCount > 0) return 'some';
	 return 'none';
}
function deleteBtnDriver(){
	var result = scanCheckboxes();
	switch(result){
		case 'none':
			$('button:#delete').fadeOut();
			break;
		default:
			$('button:#delete').fadeIn();
			break;
	}
}