﻿

jQuery.fn.extend({

    lightBox: function(options){
        var options = jQuery.extend({
            position: "",
            hiddenElement:".hide-light",
			callback:function(){},
			closeElement:".detailviewer,#bg-alpha"
        }, options);
        
        var uiDialog = jQuery(this);
        if (!document.getElementById("bg-alpha")) 
            jQuery('body').append('<div id="bg-alpha" ></div><div class="detailviewer" ></div>');
        jQuery("#bg-alpha").css({
            height: jQuery(document).height()
        });
        jQuery(".detailviewer").html(this);
        
        if (jQuery.browser.msie && jQuery.browser.version < 7) { // fix bug  Select alway show on top in ie6
            jQuery('select').css({visibility: "hidden"});
        }
        jQuery(options.hiddenElement).css({visibility: "hidden"});
        
        jQuery("#bg-alpha").fadeTo("normal", 0.75, 
        function(){
        jQuery(".detailviewer").show();           
		
		var wnd = jQuery(window), doc = jQuery(document), _top = doc.scrollTop(), _left = doc.scrollLeft();
		
				switch (options.position) {
					case 'center':
						_top += (wnd.height() / 2) - (uiDialog.height() / 2);
						_left += (wnd.width() / 2) - (uiDialog.width() / 2);
						break;
					case 'top':
						_top += 0;
						_left += (wnd.width() / 2) - (uiDialog.width() / 2);
						break;
					case 'right':
						_top += (wnd.height() / 2) - (uiDialog.height() / 2);
						_left += (wnd.width()) - (uiDialog.width());
						break;
					case 'bottom':
						_top += (wnd.height()) - (uiDialog.height());
						_left += (wnd.width() / 2) - (uiDialog.width() / 2);
						break;
					case 'left':
						_top += (wnd.height() / 2) - (uiDialog.height() / 2);
						_left += 0;
						break;
					default:
						//center
						_top += (wnd.height() / 2) - (uiDialog.height() / 2);
						_left += (wnd.width() / 2) - (uiDialog.width() / 2);
				}
			
			_top = _top < doc.scrollTop() ? doc.scrollTop() : _top;
			
			jQuery(".detailviewer").css({top:_top}).animate({
				opacity:"show",display:"block"
			},400,options.callback);
			
			var closeBox = function(){
                jQuery(".detailviewer").fadeOut("normal");
                jQuery("#bg-alpha").fadeOut("normal",endRemoveBox);
			}
			var endRemoveBox = function(){
				  jQuery(options.hiddenElement).css({visibility:"visible"});
				  if (jQuery.browser.msie && jQuery.browser.version < 7) { // fix bug  Select alway show on top in ie6
                        jQuery('select').css({
                            visibility: "visible"
                        });
                    }
			}
            jQuery(options.closeElement).click(closeBox);
        });
    },
    jPaging:function(settings){
    	var s = jQuery.extend(
		{
			url : "getContent.aspx",
			contianer:"#rs-content",
			tatalrows:23,
			pageSize:5,
			currentPage:1,
			page:"page",
			hoverClass:"paging-hover",
			currentColor:"#F7001F",
			success:function(){}
		}, settings);
		
		return this.each(
		function(){
			var $this = jQuery(this);
			var rsContent = jQuery(s.contianer||$this.parent().prev());
			$this.totalpage = Math.ceil(s.tatalrows/s.pageSize);
			
			getContent(s.currentPage);
			
			function genPaging(){
				var sDot = "<span> ... </span>"
				var $totalPage = $this.totalpage;
				$this.empty();
				if($totalPage > 11){
					// in case 
					var $currentPage = parseInt(s.currentPage);
					if($currentPage < 6){
						for(var i = 1; i < ($currentPage+3);i++){
							$this.append(getAnchors(i));
						}
						$this.append(sDot);
						for(var i=($totalPage-2);i<=$totalPage;i++){
							$this.append(getAnchors(i));
						}
					}else if($currentPage > ($totalPage-5) ){
						for(var i = 1; i < 4 ; i++){
							$this.append(getAnchors(i));
						}
						$this.append(sDot);
						for(var i = ( $totalPage-(($totalPage-$currentPage)+2) );i<=$totalPage;i++){
							$this.append(getAnchors(i));
						}
					}else{ 
						for(var i = 1 ; i < 4 ; i++){
							$this.append(getAnchors(i));
						}
						$this.append(sDot);
						for(var i = $currentPage-1;i < ($currentPage+2);i++){
							$this.append(getAnchors(i));
						}
						$this.append(sDot);
						
						for(var i = $totalPage-2 ; i <= $totalPage ; i++){
							$this.append(getAnchors(i));
						}
						
						
					}
				}else{
					for(var i = 1 ; i<=$this.totalpage ; i++ ){
						$this.append(getAnchors(i));
					}
				}
				
				/* gen label next and previous button */
				if( $this.totalpage > 1){
					$this.append("<label class='jpaging_next'><span "+((s.currentPage != $this.totalpage)?"style='cursor:pointer'":"style='color:#555'" )+"> ถัดไป ></span></label>");
				
					$this.prepend("<label class='jpaging_prev'><span "+((s.currentPage != 1)?"style='cursor:pointer'":"style='color:#555'")+">< ย้อนกลับ </span> </label>");
				}
					
			}
			function txtOver(elm){
				this.style.color = s.currentColor;
			}
			function getAnchors(i){
				return "<a class='link' href='"+ s.url +"&"+s.page+"="+i+"' style='"+((i==(s.currentPage||1))?"color:"+s.currentColor:"")+"' >"+i+" </a>";
			}
			function getContent(page){
				rsContent.html("<img src='images/bg_white_loader.gif' style='margin:20px' /><strong> Fetching data ... </strong>");
				jQuery.ajax({
					url:s.url,
					data:s.page +"="+ page,
					success:function(msg){
						
						rsContent.html(msg);
						s.success();
						genPaging();
						$this.find("a.link").hover(
							function(){
								jQuery(this).addClass(s.hoverClass);
							},function(){
								jQuery(this).removeClass(s.hoverClass);
							}
						).bind("click",function(){
							var $me = jQuery(this);
							
							s.currentPage = $me.text();
							getContent(s.currentPage);
							
							return false;
						})
						
						$this.find(".jpaging_prev").bind("click",function(){
							
							if(s.currentPage > 1){
								s.currentPage = Number(s.currentPage||1)-1;
								getContent(s.currentPage);
							}
							
						})
						$this.find(".jpaging_next").bind("click",function(){
							
							if(s.currentPage < $this.totalpage){
								s.currentPage = Number(s.currentPage||1)+1;
								getContent(s.currentPage);
							}
							
						})
						
						
					}
				});
			}//--#genContent paging
		
						
		});
		// end function paging
	
        
        
    }
	
});