/* Copyright (c) 2010 i010.com (International) Limited
 * Last update: 11/06/2010
 * Requires: jquery.1.4+
 */

;if(window.jQuery) (function($){

	//**** Error Massage ****//
	$.fn.ierrorMessage = function(settings){

		var _defaultSettings = {
			message: '',
			error_class: 'message-error'
		};

		var _settings = $.extend(_defaultSettings, settings);

		var showMessage = function(f){
			if(!$(f).find("."+_settings.error_class).html()){
				var msg=jQuery('<li />').html(_settings.message).addClass(_settings.error_class);
				var d = jQuery('<div />').html(msg).addClass("message message-float");
				//$(f).find(".input").append(d);
				$(f).prepend(d);
				$(f).bind('focusout', function() {
					//$(d).delay(1000).remove();
					$(d).fadeOut('slow').delay(1000).queue(function(){ $(this).remove(); $(this).dequeue();});
				});
			}
		};

		return showMessage(this);
	};

	//**** Form Post ****//
	$.fn.iformPost = function(settings){

		var verifyForm = function(el){
			var url = $(el).attr("action");
			if($(el).find(".editor")){
				$(el).find(".editor").each(function(){
					tinyMCE.triggerSave();
				});
			}
			var data = $(el).serialize();
			var submit_name = $(el).find("button[type='submit']").attr("id");
			$.post(url, data+'&'+submit_name+'=1&ajax=1',function(data){ $('#form-ajax-post').html(data); } );
		};

		return this.each(function(){
			var id_form=$(this);
			$(this).find("#submit-verify").val('-1');

			$(this).find("button").click(function(){
				$(id_form).find("#submit-value").val($(this).attr("value"));
			});

			$(this).submit(function(){
				if($(id_form).attr("target")!="_preview"){ //preview
					var verified=$(this).find("#submit-verify").val();
					if(verified=="-1"){
						verifyForm($(this));
						return false;
					}
					else{
						return true;
					}
					return false;
				}
			});
		});
	};

	//**** TableEditable ****//
	$.fn.itableEditable = function(settings){

		var _defaultSettings = {
			editTitle: ''
		};

		var _settings = $.extend(_defaultSettings, settings);

		var returnText = function(f){
			$(f).find(".text").delay(1000).queue(function(){ $(this).fadeIn('slow'); $(this).dequeue();});
			$(f).find(".update-field").delay(1000).queue(function(){ $(this).hide(); $(this).dequeue();});
		};

		var setEditable = function(f){

			$(f).dblclick(function(){
				$(f).find(".text").hide();
				$(f).find(".update-field").show();
			});

			if($(f).find("input[type='text']")){
				var ostr=$(f).find("input[type='text']").val();
				$(f).find("input[type='text']").bind('change mouseup',function(){
					var str=$(this).val();
					$(f).find(".text").text(str);
					if(ostr!=str) $(f).parent().find('.checkchildren').attr("checked","ture");
				}).focusout(function(){
					returnText(f);
				});
			}

			if($(f).find("select")){
          			var ostr = $(f).find("select option:selected").val();
				$(f).find("select").change(function(){
					var str=$(this).find("option:selected").text();
					$(f).find(".text").text(str);
					if(ostr!=str) $(f).parent().find('.checkchildren').attr("checked","ture");
				}).focusout(function(){
					returnText(f);
				});
			}
		};

		return this.each(function(){
			$(this).find(".editable").each(function(){
				setEditable(this);
			});
		});
	};

	//**** DragDropTable - requires: jquery.tablednd_0_5.js ****//
	$.fn.itableDragDrop = function(settings){

		var _defaultSettings = {
			startnumber: '1'
		};

		var _settings = $.extend(_defaultSettings, settings);

		var resetTableOrder = function(tbody){
			var position=_settings.startnumber;

			$(tbody).find("tr").each(function(){
				if($('td.sort .text', $(this)).text()!=position){
					$('td.sort .text', $(this)).text(position);
					$('td.sort input', $(this)).val(position);
					if($(this).find('.checkchildren')){
						$(this).find('.checkchildren').attr("checked","ture");
					}
				}
				$(this).removeClass('row1 row2').addClass( position % 2 ? 'row1' : 'row2');
				position += 1;
			});
		};

		return this.each(function(){
			var tbody=this;
			$(this).tableDnD({
				onDrop: function(table, row) {
					resetTableOrder(tbody);
				},
				dragHandle: "sort"
			});

			$(this).find(".sort").hover(function() {
				$(this).css('cursor', 'move');
			}, function() {
				$(this).css('cursor', '');
			});

		});
	};

})(jQuery);


