jQuery.fn.work_paging = function(options){
	
	//Defaults
	var options = jQuery.extend({
		target: '',
		total: '',
		page_number: 0,
		total_rows: 2,
		paging_on: true,
		page_results: 9,
		total_pages: 1,
		url_root: 'http://www.krakencreative.com',
		firstText: 'First',
		nextText: 'Next',
		prevText: 'Prev',
		lastText: 'Last',
		paging_active: true,
		category: null,
		keyword:null,
		image_width: 185,
		image_height: 185,
		target_height: 205,
		current_total: 9
	},options);

	return this.each(function(){
	
		
	
		var target_ul = jQuery(options.target);
	
		var firstLink = jQuery(this).children('.paging_first');
		var prevLink = jQuery(this).children('.paging_prev');
		var nextLink = jQuery(this).children('.paging_next');
		var lastLink = jQuery(this).children('.paging_last');
		var info = jQuery(this).children('.paging_info');
		
		if(options.category != null){
			if(isNaN(options.category) == true){
				options.category = null;
			}
		}
	
		if(options.keyword != null){
			if(isNaN(options.keyword) == true){
				options.keyword = null;
			}
		}
		
		target_ul.css('height',options.target_height)
		
		loadWorkImages(target_ul);
		
		function loadWorkImages(target){
			//alert("loadWorkImages");
			target.css({opacity: 0});
			var target_lis = target.children('li');
			var loadArray = new Array();
			target_lis.each(function(i){
				loadArray[i] = jQuery(this).children('a').children('img').attr('src');
				jQuery(this).children('a').children('img').remove();
				//jQuery(this).css('opacity',0);
				var loadtarget = jQuery(this).children('a');
				var parent = jQuery(this);
				loadImage(parent, loadtarget, loadArray[i]);
			});
			options.current_total = loadArray.length;
			changeTargetHeight();
			target.css({opacity: 1});
		}
				
		function changeTargetHeight(){
			var targetHeight = 215;
			switch(options.current_total){
				case 1:
					targetHeight = 215;
					break;
				case 2:
					targetHeight = 215;
					break;
				case 3:
					targetHeight = 215;
					break;
				case 4:
					targetHeight = 430;
					break;
				case 5:
					targetHeight = 430;
					break;
				case 6:
					targetHeight = 430;
					break;
				case 7:
					targetHeight = 645;
					break;
				case 8:
					targetHeight = 645;
					break;
				case 9:
					targetHeight = 645;
					break;
			}
			target_ul.animate( { height:targetHeight }, { queue:false, duration:1000 } );
		}		
				
		// Get total rows
		if(nextLink.children('a').attr('href') != undefined){
			var queryString = nextLink.children('a').attr('href');
			var splitString = queryString.split('?');
			options.url_root = splitString[0];
			var pageString = splitString[1].split('=');
			var pageNo = pageString[1].split('&');
			var newPageNo = parseInt(pageNo[0]);
			options.page_number = newPageNo -1;
			var newPageTotal = parseInt(pageString[2]);
			options.total_rows = newPageTotal;
			options.total_pages = Math.ceil(options.total_rows/options.page_results);
		}else if(prevLink.children('a').attr('href') != undefined){
			var queryString = prevLink.children('a').attr('href');
			var splitString = queryString.split('?');
			options.url_root = splitString[0];			
			var pageString = splitString[1].split('=');
			var pageNo = pageString[1].split('&');
			var newPageNo = parseInt(pageNo[0]);
			options.page_number = newPageNo +1;
			var newPageTotal = parseInt(pageString[2]);
			options.total_rows = newPageTotal;
			options.total_pages = Math.ceil(options.total_rows/options.page_results);
		}else{
			options.page_number = 0;
			options.paging_on = false;
		}
		
		if(options.paging_on == false){
			jQuery(this).hide();
		}
		
		updatePaging();

		function updatePaging(){
			if(options.page_number != 0){
				addRemoveLinks(firstLink, options.firstText, true);				
			}else{
				addRemoveLinks(firstLink, options.firstText, false);							
			}

			if(options.page_number > 0){
				addRemoveLinks(prevLink, options.prevText, true);				
			}else{
				addRemoveLinks(prevLink, options.prevText, false);							
			}

			if(options.page_number < options.total_pages-1){
				addRemoveLinks(nextLink, options.nextText, true);				
			}else{
				addRemoveLinks(nextLink, options.nextText, false);							
			}

			if(options.page_number != options.total_pages-1){
				addRemoveLinks(lastLink, options.lastText, true);				
			}else{
				addRemoveLinks(lastLink, options.lastText, false);							
			}
			
			pagingInfo();	
		}
		
/*

		alert("FIRST"+(firstLink).children('a').attr('href'));				
		alert("NEXT"+(nextLink).children('a').attr('href'));				
		alert("LAST"+(lastLink).children('a').attr('href'));
		alert(window.location.pathname);		
		
*/
		nextLink.click(function(){
			if(options.page_number < options.total_pages-1 && options.paging_active == true){		
				options.paging_active =  false;				
				jQuery.get("pages.php",{ page_num: options.page_number+1, total_rows: options.total_rows, id: options.category, keyword: options.keyword }, function(data)
				{
					ajaxEvent(data,"next");
				});
			}
			return false;
		});
		
		prevLink.click(function(){
			if(options.page_number > 0 && options.paging_active ==  true){				  
				options.paging_active =  false;								
				jQuery.get("pages.php",{ page_num: options.page_number-1, total_rows: options.total_rows, total_rows: options.total_rows, id: options.category, keyword: options.keyword }, function(data)
				{
					ajaxEvent(data,"prev");
				});
			}
			return false;
		});

		firstLink.click(function(){
			if(options.page_number > 0 && options.paging_active ==  true){				   
				options.paging_active =  false;								
				jQuery.get("pages.php",{ page_num: 0, total_rows: options.total_rows, total_rows: options.total_rows, id: options.category, keyword: options.keyword }, function(data)
				{
					ajaxEvent(data,"first");
				});
			}
			return false;
		});

		lastLink.click(function(){
			if(options.page_number < options.total_pages-1 && options.paging_active ==  true){		
				options.paging_active = false;				
				jQuery.get("pages.php",{ page_num: options.total_pages-1, total_rows: options.total_rows, total_rows: options.total_rows, id: options.category, keyword: options.keyword }, function(data)
				{
					ajaxEvent(data,"last");
				});
			}
			return false;
		});		
		function removeCurrent(){
			target_ul.children('li').fadeOut(function(){
				jQuery(this).remove();		
			});
		}
		
		function addRemoveLinks(target, text, active){
			target.empty();
			if(active == true){
				var newLink = jQuery('<a href="#">'+text+'<a/>');					
				target.prepend(newLink);
				target.css('opacity',1);
			}else{
				var targetText = text;
				target.prepend(targetText);
				target.css('opacity',0.25);
			}
			
		}
		
		function pagingInfo(){
			var pagingText = 'Page '+(options.page_number+1)+' of '+options.total_pages;
			info.empty();
			info.prepend(pagingText);
		}
		
		function ajaxEvent(newdata,dir){
			switch(dir){
				case 'next':
					options.page_number+=1;
					break;
				case 'prev':
					options.page_number-=1;
					break;
				case 'first':
					options.page_number = 0;
					break;
				case 'last':
					options.page_number = options.total_pages-1;
					break;			
			}
			
			if(newdata != ''){
				removeCurrent();
				setTimeout(function(){
					target_ul.append(newdata);
					loadWorkImages(target_ul);	
					updatePaging();
					options.paging_active =  true;								
					reBindEvents();				
				},1000);
			}else{
				target_ul.append(newdata);
				loadWorkImages(target_ul);	
				updatePaging();
				options.paging_active =  true;								
				reBindEvents();		
			}
		}
				
		function reBindEvents(){
			//alert("BIND"+jQuery('#browse'));
			jQuery(options.target).work_browse();		
		}

		function loadImage(parent,target,url){
			//alert("TARGET"+target+" PARENT:"+parent+" URL:"+url);	
			var ieRandom = Math.round(Math.random()*999999);	
			url = url+"?random="+ieRandom		
			var targetWidth = jQuery(target).css('width');
			var targetHeight = jQuery(target).css('height');
			jQuery('<img />')
				.attr('src', url)
				.load(function(){
					//alert("LOADED"+url);	
					parent.css('opacity',0);			
					target.append(jQuery(this));
					parent.animate({opacity: 1}, 800);
					var imageWidth = jQuery(this).css('width');
					var imageHeight = jQuery(this).css('height');
					jQuery(this).css({
						width: imageWidth,
						height: imageHeight
					});
					//alert(targetWidth+":"+targetHeight+":"+imageWidth+":"+imageHeight);						
			}).error(function(){
				//alert("Error Loading: "+url);
			});
		}		
			
	});	
};
	