/**
 * @author Mihai Varga
 */



(function($) {
$.widget("ui.gridview", {

	_init: function() {
			this.element.wrap("<div></div>").parent().addClass("ui-widget ui-gridview");								

			this.gridtable={
				obj:this,
				header:"",
				pages:"",
				rows:"",
				loader:""
			};			
			
			var self=this,index;
			this.gridtable.header=$(this.element.children("thead").children("tr")[0]).children();
			this.gridtable.header.each(function(i, el){
					$(el).attr("sortdir","0");
					$(el).css({
						"cursor": 'pointer'
					});
					
					$(el).css("width",$(el).width()+"px");
					$(el).addClass("ui-widget-header ui-gridview-header-col");
					$(el).click(function()
					{	
						self.options.sortby=$(this).attr("sortby");
						$(el).next().each(function(){
							$(this).removeClass("ui-gridview-header-col-desc");
							$(this).removeClass("ui-gridview-header-col-asc");
						});
						$(el).prevAll().each(function(){
							$(this).removeClass("ui-gridview-header-col-desc");
							$(this).removeClass("ui-gridview-header-col-asc");
						});
						if ($(this).attr("sortdir") <= 0) {
							$(this).attr("sortdir", 1);
							self.options.sortdir=1;
							$(this).removeClass("ui-gridview-header-col-desc");
							$(this).addClass("ui-gridview-header-col-asc");
						}
						else {
							$(this).attr("sortdir", -1);
							self.options.sortdir=-1;
							$(this).removeClass("ui-gridview-header-col-asc");
							$(this).addClass("ui-gridview-header-col-desc");
						}	
						self._refresh();
					}
					);
				}
			);
			
			this.gridtable.rows=this.element.children("tbody").children("tr");
			this.gridtable.rows.each(function(i,el)
			{				
				$(el).addClass("ui-gridview-row")
				if((i%2))					
					$(el).addClass("ui-gridview-row-alt");	
				
				if(self.options.rowclick!='')							
					$(el).click(
						function(){
							self.options.rowclick(this);
						}
					);
			});
			
			if (this.options.no_results > this.options.no_rows) {
				htmlitem = "<div class='ui-widget-content ui-gridview-pages'></div>";
				this.element.after(htmlitem);
				this.gridtable.pages = this.element.next();
				
				this.options.no_pages = Math.floor(this.options.no_results / this.options.no_rows);
				if (this.options.no_results % this.options.no_rows != 0) 
					this.options.no_pages++;
				this._show_pages();		
			}				
			
			this.element.before("<div class='ui-gridview-loader'>Loading...</div>");
			this.gridtable.loader=this.element.prev()[this.element.prev().size()-1];
			$(this.gridtable.loader).css({"position":"absolute","background":"#eee"});
			$(this.gridtable.loader).hide();
			
			this._refresh();
	},
	
	_displayLoader:function(){		
		$(this.gridtable.loader).width(this.element.width());
		$(this.gridtable.loader).height(this.element.height());
		$(this.gridtable.loader).css("line-height",this.element.height()+"px");
		$(this.gridtable.loader).show();
	},

	_refresh:function()
	{
		this._displayLoader();
		var self=this,index;
		$.ajax({
			url: this.options.url,
			data: "sortBy=" + (this.options.sortby?this.options.sortby:"") + "&sortAscending=" + (this.options.sortdir > 0 ? "true" : "false")+"&numberOfRows="+this.options.no_rows+"&startIndex="+(this.options.current_page*this.options.no_rows),
			cache: true,
			success: function(response){
				self._display(response);
				self._show_pages();
				$(self.gridtable.loader).hide();
			}
		});
	},
	
	_display:function(content){
		list=content.split("|||");		
		
		for(i=0;i<list.length;i++)
		{
			if (this.gridtable.rows.size()>i) {
				if (list[i]) {
					row = list[i].split("###");					
					if (row.length== this.gridtable.header.size()) {						
						for (j = 0; j < this.gridtable.header.size(); j++) {		
							if($($(this.gridtable.rows[i]).children("td")[j]))					
								$($(this.gridtable.rows[i]).children("td")[j]).html(row[j]);							
						}						
					}
					else {												
						for (j = 0; j < this.gridtable.header.size(); j++) {
							if($($(this.gridtable.rows[i]).children("td")[j]))
								$($(this.gridtable.rows[i]).children("td")[j]).html("&nbsp;");
						}						
					}
				}
				else {					
					for (j = 0; j < this.gridtable.header.size(); j++) {
						if($($(this.gridtable.rows[i]).children("td")[j]))
							$($(this.gridtable.rows[i]).children("td")[j]).html("&nbsp;");
					}
				}
			}			
		}
		if(list.length<this.gridtable.rows.size()){
			for(i=list.length;i<this.gridtable.rows.size();i++)
				for (j = 0; j < this.gridtable.header.size(); j++)
					if($($(this.gridtable.rows[i]).children("td")[j]))
						$($(this.gridtable.rows[i]).children("td")[j]).html("&nbsp;");
		}
	},
	
	_show_pages : function(){
		
		if (this.gridtable.pages != '') {
			
			$(this.gridtable.pages).html("");
			if (this.options.current_page > this.options.max_pages / 2 - 1) {
				$(this.gridtable.pages).append("<a class='ui-state-default ui-corner-all ui-gridview-page' page='" + 0 + "'><<</a>");
				$(this.gridtable.pages).append("<a class='ui-state-default ui-corner-all ui-gridview-page' page='" + (this.options.current_page - 1) + "'><</a>");
			}
			
			var start = Math.floor(this.options.current_page - this.options.max_pages / 2 + 1) >= 0 ? Math.floor(this.options.current_page - this.options.max_pages / 2 + 1) : 0;
			var end = Math.floor(this.options.current_page + this.options.max_pages / 2) <= this.options.no_pages ? Math.floor(this.options.current_page + this.options.max_pages / 2) : this.options.no_pages;
			end = end + (this.options.max_pages - (end - start));
			if (end > this.options.no_pages) {
				end = this.options.no_pages;
				start = start - (this.options.max_pages - (end - start));
			}
			if (start < 0) 
				start = 0;
			
			for (i = start; i < end; i++) {
				cls = "class='ui-state-default ui-corner-all ui-gridview-page'";
				if (i == this.options.current_page) 
					cls = "class='ui-state-active ui-corner-all ui-gridview-page-selected'";
				htmlitem = "<a " + cls + " page='" + i + "'>" + (i + 1) + "</a>";
				$(this.gridtable.pages).append(htmlitem);
			}
			
			if (end < this.options.no_pages) {
				$(this.gridtable.pages).append("<a class='ui-state-default ui-corner-all ui-gridview-page' page='" + (this.options.current_page + 1) + "'>></a>");
				$(this.gridtable.pages).append("<a class='ui-state-default ui-corner-all ui-gridview-page' page='" + (this.options.no_pages - 1) + "'>>></a>");
			}
			var self=this,index;
			$(this.gridtable.pages).children().each(function(i, el){
				$(el).click(function(){
					self.options.current_page = parseInt($(el).attr("page"));
					self._refresh();
				});
				$(el).mouseover(function(){
					$(this).toggleClass("ui-state-hover");
				});
				$(el).mouseout(function(){
					$(this).toggleClass("ui-state-hover");
				});
			});
			
			$(this.gridtable.pages).append("<br/>");
			$(this.gridtable.pages).append("<div class='ui-widget-header'>Result: " + (this.options.current_page * this.options.no_rows + 1) + " to " + (this.options.current_page * this.options.no_rows + this.options.no_rows < this.options.no_results ? this.options.current_page * this.options.no_rows + this.options.no_rows : this.options.no_results) + " of " + this.options.no_results + "</div>");
		}
	},
	
	refresh:function(){
		this._refresh();
	},
	
	destroy: function() {

		this.element
			.removeClass("ui-progressbar"
				+ " ui-widget"
				+ " ui-widget-content"
				+ " ui-corner-all")
			.removeAttr("role")
			.removeAttr("aria-valuemin")
			.removeAttr("aria-valuemax")
			.removeAttr("aria-valuenow")
			.removeData("progressbar")
			.unbind(".progressbar");

		this.valueDiv.remove();

		$.widget.prototype.destroy.apply(this, arguments);

	}
});

$.extend($.ui.gridview, {
	version: "1.7",
	defaults: {
		url:'',
		no_rows:10,
		no_results:1,
		current_page:1,
		max_pages:10,					
		rowclick:''
	}
});

})(jQuery);

/**
 * @author Administrator
 */
jQuery.fn.spinner=function()
{
	return this.each(function(){
		
		jQuery(this).parent().css("clear","both");
	    jQuery(this).css({'width':(jQuery(this).width()-16)+"px",'float':'left'});
		jQuery(this).attr('readonly','readonly');
		
		jQuery(this).attr("minvalue",jQuery(this).attr("minvalue")?this.attr("minvalue"):1);
		jQuery(this).attr("maxvalue",jQuery(this).attr("maxvalue")?this.attr("maxvalue"):"");
		jQuery(this).attr("increment",jQuery(this).attr("increment")?this.attr("increment"):1);
		
		htmlitem="<div id='buttons_"+this.id+"' class='spinner_handler'></div>";
		jQuery(this).after(htmlitem);
		this.btn_container=jQuery(this).next();
		this.btn_container.css({'float':'left','height':jQuery(this).height(),'width':'10px'});
		
		htmlitem="<button id='plus_"+this.id+"' class='spinner_handler-plus' type='button'></button>"
		jQuery(this.btn_container).append(htmlitem);
		this.btn_plus=jQuery(this.btn_container).children()[0];
		jQuery(this.btn_plus).css({
							'height': jQuery(this).height() / 2 + "px",
							'line-height': jQuery(this).height() / 2 + "px",
							'width': '16px',
							'font-size':'4px',
							'padding':'0',
							'margin':'0',
							'border':'none',
							'vertical-align':'top'
						});
						
		htmlitem="<button id='minus_"+this.id+"' class='spinner_handler-minus' type='button'></button>"
		jQuery(this.btn_container).append(htmlitem);
		this.btn_minus=this.btn_container.children()[1];
		jQuery(this.btn_minus).css({
							'height': jQuery(this).height() / 2 + "px",
							'line-height': jQuery(this).height() / 2 + "px",
							'width': '16px',
							'font-size':'4px',
							'padding':'0',
							'margin':'0',
							'border':'none'
						});
		
		jQuery(this.btn_plus).bind("click",function()
			{
				input=jQuery(this).parent().prev("input")[0]
				minvalue=jQuery(input).attr("minvalue");	
				maxvalue=jQuery(input).attr("maxvalue");
				increment=jQuery(input).attr("increment");
				
				if ((maxvalue != "" && jQuery(input).val() < maxvalue) || maxvalue == "") {
					jQuery(input).val(parseInt(jQuery(input).val()) + parseInt(increment));
				}
				if(input.onchange)
					input.onchange();					
			}
		);
		
		jQuery(this.btn_minus).bind("click",function()
			{
				input=jQuery(this).parent().prev("input")[0]
				minvalue=jQuery(input).attr("minvalue");	
				maxvalue=jQuery(input).attr("maxvalue");
				increment=jQuery(input).attr("increment");
				
				if ((minvalue != "" && jQuery(input).val() > minvalue) || minvalue == "") {
					jQuery(input).val(parseInt(jQuery(input).val()) - parseInt(increment));
				}
				if(input.onchange)
					input.onchange();					
			}
		);		
	});
};
