/*
* jQuery content search plugin
* 
* @version  0.1
* @homepage ??
* @author   Boz Kay (http://www.boz.co.uk)
*
* Copyright (c) 2009 Nowhere
*/

(function($){

	$.fn.contentSearch = function(options){
    
		var opts = $.extend({}, $.fn.contentSearch.defaults, options);
		
		$CS = $(opts.CSElement);


		// bind events to the container
		$CS.unbind();
		$CS.bind('close.CS',close);
		$CS.bind('next.CS',next);
		$CS.bind('previous.CS',previous);
		$CS.bind('loadItems.CS',loadItems);

		if($(opts.CSElement+' #CSWrap').length == 0) {

			// setup initial html elements
		    $CS.append('<div id="CSWrap">'+
		    	'<div id="CSLoading"></div><div id="CSNav">'+
		    	'<a class="previous"><img src="/images/shared/contentPrevious.png" /></a> <span class="number"></span> <a class="next"><img src="/images/shared/contentNext.png" /></a><div class="clear"></div></div>'+
		    	'<div id="CSItems"></div>'+
		    '</div>');
			$(opts.CSElement+' #CSLoading').hide();
	
			if(opts.newSearch == true) {
				$(opts.CSElement+' #CSWrap').append('<div class="new"><a class="close buttonSubtle"><span class="arrow">New search</span></a></div>');
			
			}
			
			if(opts.view == 'thumbs') {
				$('#CSWrap').addClass('viewThumbs');
			} else if(opts.view == 'full') {
				$('#CSWrap').addClass('viewFull');
			}

			if(opts.viewDirection == 'vertical') {
				$('#CSWrap').addClass('viewVertical');
			} else if(opts.viewDirection == 'horizontal') {
				$('#CSWrap').addClass('viewHorizontal');
			}

			$(opts.CSElement+' #CSNav .number').html($CS.contentSearch.currentSet+opts.limit);

			// start with inital images from lib
			$CS.trigger('loadItems.CS',[0]);
	
			// trigger events from elements within container
			$(opts.CSElement+' .close').click(function(){
				$CS.trigger('close.CS');
			});
			
			$CS.fadeIn();
			
			
			
		} else if(opts.reLaunch) {
		
			$CS.trigger('loadItems.CS',[0]);
			$CS.fadeIn();
			
		}

		// the event functions ...
		function close() {
			$CS.fadeOut(500,function() {
				opts.close();
			});
		}
		function next() {
			$CS.trigger('loadItems.CS',[$CS.contentSearch.currentSet+opts.limit]);
		}
		function previous() {
			$CS.trigger('loadItems.CS',[$CS.contentSearch.currentSet-opts.limit]);
		}

		
		function loadItems(e, fromNo) {
			
		    // add images
		    $(opts.CSElement+' #CSLoading').show();
		    $(opts.CSElement+' #CSItems').hide().load(opts.getItemsPHP, {
		    	limit: opts.limit, 
		    	from: fromNo, 
		    	userId: opts.userId, 
		    	type: opts.type, 
		    	tags: opts.tags
		    }, function(){
		    	$(opts.CSElement+' #CSLoading').hide();
		    	setArrows();
		    	if(opts.view == 'thumbs') {
		    		$('.details').hide();
		    	}
				var total = $(opts.CSElement+' #contentTotal').html();			
				var toNo = fromNo+opts.limit;
				var to = 0;
				total < toNo ? to=total : to=toNo;
				$(opts.CSElement+' #CSNav .number').html(fromNo+"-"+to+" of "+total);
		    	$(this).fadeIn();
			});


			$CS.contentSearch.currentSet = fromNo;

		};
		
		function setArrows() {

			if(opts.noNav == true) {
				$(opts.CSElement+' #CSNav').hide();
			} else {
				
				if($(opts.CSElement+' #contentTotal').length == 0) {
					var total = 0;
				} else {
					var total = $(opts.CSElement+' #contentTotal').html();			
				}
				
				if(total == 0) {
					$(opts.CSElement+' #CSNav').hide();
				} else {
				
					if($CS.contentSearch.currentSet == 0) {
						$(opts.CSElement+' .previous').unbind().css({opacity:'0.2',cursor:'default'});
					} else {
						$(opts.CSElement+' .previous').unbind().click(function(){
							$CS.trigger('previous.CS');
						}).css({opacity:'1',cursor:'pointer'});
					}
					if(($CS.contentSearch.currentSet+opts.limit) < total) {
						$(opts.CSElement+' .next').unbind().click(function(){
							$CS.trigger('next.CS');
						}).css({opacity:'1',cursor:'pointer'});
					} else {
						$(opts.CSElement+' .next').unbind().css({opacity:'0.2',cursor:'default'});
					}
					
				}

			}
			
		}
		

	}
	
	
	$.fn.contentSearch.defaults = {
		CSElement: '#contentSearch',
		getItemsPHP: '/contentSearch/getItems.php',
		limit: 3,
		tags: 'w',
		type: 'hb',
		userId: '',
		reLaunch: true,
		noNav: false,
		newSearch: true,
		view: 'thumbs', // 'full' option
		viewDirection: 'vertical',
		close: function(){}
	};        


})(jQuery);

/*function contentLoad(c_id) {

	// create container
	if($('#contentItem').length == 0) {
		$('#quest').append('<div id="contentItem"><a class="close"></a><div id="contentLoading"><img src="/images/shared/ajax-loader.gif" /></div><div id="contentItemWrap"></div></div>');
		$('#contentItem').center().addClass('popup');
		$('#contentItem .close').click(function() {
			$('#contentItem').fadeOut();
		});
	}
	// load content into it
	$('#contentLoading').show();
	$('#contentItem').fadeIn();
	$('#contentItemWrap').hide().load('/contentSearch/getItem.php', {
		c_id: c_id
	}, function(){
		$('#contentLoading').hide();
		$('#contentItemWrap').show();
	});	
	// setup controls

}*/

function contentLoad(c_id) {

	// create container
	if($('#contentItem').length == 0) {
		$('#pagewrapper').append('<div id="contentItem"><div id="contentLoading"></div><div id="contentItemWrap"></div></div>');		
	}
	// make it popup
	$().showPage({
		show:'#contentItem',
		showNow: true,
		noClick: true
	});
	// load content into it
	$('#contentLoading').show();
	$('#contentItem').fadeIn();
	$('#contentItemWrap').hide().load('/contentSearch/getItem.php', {
		c_id: c_id
	}, function(){
		$('#contentLoading').hide();
		$('#contentItemWrap').show();
	});	
	// setup controls

}
