/*
* 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);
		
		if(opts.CSElement == false) {
			opts.CSElement = '#'+this.attr('id');
		}

		// so that any previously bound events are cleared
		$(opts.CSElement+' .inner').remove();
		$(opts.CSElement).html('<div class="inner"></div>').show();
		
		var CS = $(opts.CSElement+' .inner');

		var namespace = '.'+opts.type;

		CS.unbind();
		CS.bind('close'+namespace,close);
		CS.bind('next'+namespace,next);
		CS.bind('previous'+namespace,previous);
		CS.bind('loadItems'+namespace,loadItems);

		// setup initial html elements
	    CS.hide().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="search">New search</span></a></div>');
		
		}
		
		if(opts.view == 'thumbs') {
			$(opts.CSElement+' #CSWrap').addClass('viewThumbs');
		} else if(opts.view == 'full') {
			$(opts.CSElement+' #CSWrap').addClass('viewFull');
		}

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

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

		// start with inital images from lib
		CS.triggerHandler('loadItems'+namespace,[0]);

		// trigger events from elements within container
		$(opts.CSElement+' .close').click(function(){
			CS.triggerHandler('close'+namespace);
		});
		
		CS.fadeIn();
		
		// the event functions ...
		function close() {
			CS.fadeOut(500,function() {
				opts.close();
			});
		}
		function next() {
			CS.triggerHandler('loadItems'+namespace,[CS.contentSearch.currentSet+opts.limit]);
		}
		function previous() {
			CS.triggerHandler('loadItems'+namespace,[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') {
		    		$(opts.CSElement+' .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);
				
				$(opts.CSElement+' #CSItems .icon').each(function(index) {
					$(this).hover(function() {
			    		$(opts.CSElement+' .details:eq('+index+')').show();
					},function() {
			    		$(opts.CSElement+' .details:eq('+index+')').hide();
					})
				});
				
		    	$(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.triggerHandler('previous'+namespace);
						}).css({opacity:'1',cursor:'pointer'});
					}
					if((CS.contentSearch.currentSet+opts.limit) < total) {
						$(opts.CSElement+' .next').unbind().click(function(){
							CS.triggerHandler('next'+namespace);
						}).css({opacity:'1',cursor:'pointer'});
					} else {
						$(opts.CSElement+' .next').unbind().css({opacity:'0.2',cursor:'default'});
					}
					
				}

			}
			
		}
		

	}
	
	
	$.fn.contentSearch.defaults = {
		CSElement: false,
		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,type) {

	// create container
	if($('#contentItem').length == 0) {
		$('#pagewrapper').append('<div id="contentItem" class="'+type+'"><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

}
