;(function($){
	$.fn.ajaxPagi = function(options) {
		var defaults = {
			url: "jquery/ajax.asp",
			data:"",
			method: "POST",
			dataType: "json",
			busy: false,
			items_per_page:10,
			//start_index:0,
			current_page:1,
			link_to:"#",
			prev_text:"&laquo;",
			next_text:"&raquo",
			ellipse_text:"...",
			content_id:$(this).attr("id"),
			callback:function(){return false;}
		};
		var opts = jQuery.extend(defaults,options);
		var id = "#"+$(this).attr("id");
		
		if(!$("#"+opts.content_id).length){	
			return;
		}
		var getAjax = function(current){
			var current_page = current?current:opts.current_page;
			$.ajax({
				type: opts.method,
				url: opts.url,
				data: "page="+current_page+"&"+opts.data+"&offset="+opts.items_per_page,
				processData: false,
				dataType: opts.dataType,
				beforeSend: function(){
					if(opts.busy)
					$("#"+opts.content_id).html('<img src="'+opts.busy+'" border="0" />');
				},
				success: function(data){
						responseHtml(data, opts.url,opts.method,opts.busy,id, current_page);
				},
				error:function (XMLHttpRequest, textStatus, errorThrown) {
					//alert("error");
				}
			});		
		}
		
		var responseHtml = function(data, url, method, busy, id, current){
			var rowPerPage = opts.items_per_page;
			var html = new String();
			var totalPage = $(data).find("span[class='totalPage']").text();
			// call callback function;
			opts.callback(data, this);	
			//server side draw pagination;
			bindPagination(totalPage);			
		}
		
		var bindPagination = function(totalPage){					
			var record = 5;
			var currentNow = new String();
			$("#pagi div a").click(function(){
				currentNow = $(this).attr("name").substr(4);
				if($(this).attr("name").length > 0)
				{
					getAjax(currentNow);
				}				
				return false;
			});
			$("#pagiform").submit(function(){
				var requestedGotoPage = $("#gotofield").val();
				if(isNaN(requestedGotoPage)){$("#gotofield").val("");return false;};
				requestedGotoPage = Math.floor(requestedGotoPage);
				var gotoPage = requestedGotoPage>totalPage?totalPage:requestedGotoPage;
				gotoPage = gotoPage<1?1:gotoPage;
				if(isNaN(gotoPage)){return false;};
				getAjax(gotoPage);
				return false;
			});			
		}
		return $("#"+opts.content_id).each(function() {	
			getAjax(opts.current_page);
		});
		
	}
})(jQuery);

/*Help Dialog*/

;(function($){
	$.fn.helpDialog = function(options) {
		var defaults = {
				autoOpen: true,
				closeOnEscape: true,
				draggable: false,
				minHeight: 20,
				maxWidth: 240,
				maxHeight: 600,
				//position:"center",
				dialogClass: 'ui-helpDialog',
				resizable: false,
				bgiframe:true
		};
		var opts = jQuery.extend(defaults,options);
		var isFocus=false;
		var isDialogOpen =false;
		var dialogId= "helpDialogBox";
		this.each(function(i){
			bindDialog(this, dialogId);	
		});

		function delayClose(elem){	
			setTimeout(function(){
				if(isFocus){
					return false;
				}
				else{					
					$(elem).dialog("destroy");		 //close any leftover help dialogs
					$("."+opts.dialogClass).each(function(i){	
						$(this).remove();
					});
					isDialogOpen =false;
				}					
			}, 500);
		}
		function initDialog(elem, dialogId){
			if(isDialogOpen){return false};
			$(elem).after("<div id='" +dialogId+"'></div>");
			var helpId = $(elem).attr("id");
			$("#"+dialogId).load("/help/services/popUp", {nodeDesc: helpId}, function(){
				dialogWidth= $(this).width()>200?240:$(this).width()+40;
				var helpContent = "";
				if($("#"+helpId+"Dialog") && $("#"+helpId+"Dialog").attr("class") && $("#"+helpId+"Dialog").attr("class").indexOf("helpDialog")!=-1 && $("#"+helpId+"Dialog").html()) {
					helpContent = $("#"+helpId+"Dialog").html();
				} else {
					helpContent = $(this).html();
				}
				$(this).html(		
						"<div class='helpDialog'><div class='content'><div class='t'></div>" +
						helpContent +
						"</div><div class='b-l'><div class='b'><div></div></div></div></div>" 
						);
				$(this).css({width:dialogWidth});
				dialogHeight=$(this).outerHeight();

				$(this).dialog({
					autoOpen: opts.autoOpen,
					closeOnEscape: opts.closeOnEscape,
					draggable: opts.draggable,
					minHeight: opts.minHeight,
					maxWidth: opts.maxWidth,
					width: dialogWidth,
					maxHeight: opts.maxHeight,
					dialogClass: opts.dialogClass,				
					resizable: opts.resizable,
					bgiframe: opts.bgiframe
				});
				$(this).mouseout(function() {
					isFocus=false;
					delayClose(this);
				})
				$(this).mouseover(function() {
					isFocus= true;
				})
				isDialogOpen =true;
				$(".phoenix, ."+opts.dialogClass).css({top: $(elem).offset().top-dialogHeight+7, left: $(elem).offset().left-3});
			});
			// Prevent click eventpropagation;
			return false;
		}
		function bindDialog(elem, dialogId){
			height=0;
			$(elem).mouseover(function(e) {
				if(!isDialogOpen){
					initDialog(this, dialogId);
				}
				isFocus= true;
			})
			$(elem).mouseout(function() {
				isFocus= false;
				delayClose($("#"+dialogId));
				
			})
			// Prevent click eventpropagation;
			return false;
		}		
		
	}
})(jQuery);

/* custom extensions */
;(function($){
	$.fn.extend({
		collapse: function(callback) {
			return this.slideUp('normal',callback);
		},
		expand: function(callback) {
			return this.slideDown('normal',callback);
		},
		addHoverClass: function(){
			return this.addClass('hover');
		},
		removeHoverClass: function(){
			return this.removeClass('hover');
		},
		addSelectedClass: function(){
			return this.addClass('selected');
		},
		removeSelectedClass: function(){
			return this.removeClass('selected');
		}
	});
})(jQuery);


/* ui widget defaults */
;(function($){
	$.extend($.ui.accordion, {
		defaults: {
			selectedClass: "selected",
			alwaysOpen: false,
			collapsible: true,
			event: "click",
			header: "dt",
			active: false,
			icons: {
				header: "icon-arrow-e",
				headerSelected: "icon-arrow-s"
			},
			autoHeight: false,
			running: 0
		}
	});
	$.datepicker.setDefaults({
		showOn: 'button',
		buttonImage: '/resources/mojito/img/common/calendar_sm.gif',
		buttonImageOnly: true,
		dayNamesMin: ['S','M','T','W','T','F','S'],
		prevText:'',
		nextText: '',
		minDate: new Date(),
		onClose: function() {this.focus();}
	});
})(jQuery);

/* validate extention with instruction text*/
;(function($) {
	var validate = $.fn.validate;
	$.fn.validate = function(options) {
		// create settings for compatibility with validate
		opts = $.extend(options, $.extend({}, $.fn.validate.defaults, options));
		//run original validate function
		var validator = validate.apply(this, arguments);
		
		//start of custome code for instructionText
		//addMethod
		jQuery.validator.addMethod("instructionText", function(value, element, param) { 
			 return value!=param; 
			});
		var instruction = opts.instruction;
		//populate instruction text
		if(instruction){
			$.each(instruction, function(key, value) {
				var elem = $("#"+key);
				$(elem).attr("value", value);
				$(elem).addClass('readonlystyle');
				$(elem).focus(function(){
					this.value='';
					$(this).removeClass('readonlystyle');
					$(this).unbind();
				});
				//add rules for instructionText
				$(elem).rules("add", {instructionText: value});
			});
		}
		return validator;
	};
})(jQuery);

/* ======+====== Bindings ======+====== */

(function($) {
	$(function() {
		// put bindings here;

		$(".helpIcon").livequery( function() {
			$(this).helpDialog();
		});

	  $(".ui-accordion-nav div.ui-accordion-header a").livequery(function(){
		    $(this).addClass("accordionL1Link");        
		  });
		  $(".ui-accordion-nav .ui-accordion-content ul li a").livequery(function(){
		    $(this).addClass("accordionL2Link");        
		  });
		  $("a.accordionL1Link,a.accordionL2Link").livequery('click',function(){
		    $("a.accordionL1Link").removeSelectedClass();
		    $("a.accordionL2Link").removeSelectedClass();               
		    $(this).addSelectedClass();
		  });
		 
		$("dt.ui-accordion-header").live('mouseover',function(){$(this).addHoverClass();});
		$("dt.ui-accordion-header").live('mouseout',function(){$(this).removeHoverClass();});
		
		// move the "errors" div to a location right below the title h1 and rename the id to "errorIntro";		
		$("form .errors").livequery( function() {
			$('#errorIntro').remove();
			$(this).removeClass('hidden')
				.insertAfter('#GridMiddle h1')
				.roundedCorners({wrapperClass:'rc-panel4 rc-panel4-all hidden',wrapperId:'errorIntro'});
			$('#errorIntro').hide().removeClass('hidden');
		});
		// check if there's any form with errors, and if so, show the errorIntro element. Once errors are resolved, collapse it again;
		$("form:has(label.error:visible)").livequery( 
			function() {
				$('#errorIntro').expand();
				$('#GridMiddle').addClass('hasErrorIntro');
			}, 
			function() {
				$('#errorIntro').collapse();
				$('#GridMiddle').removeClass('hasErrorIntro');
			}
		);
	});
})(jQuery);		

