/* navHistory version 1.0 01/30/2009
 * By Daniel Lowetz
 * email: daniel at daniel lowetz dot com
 *
 * navHistory is a simple pluggin that displays a list of recently clicked links on a page.
 * It is especially handy if you are loading content into an element via ajax from a 
 * lengthy menu list and want to give users the option to easily reference links they have 
 * already clicked on.  A product catalog might use such a feature.
 *
 * It might not be written in the simplest form ever, so if you can improve it, by all 
 * means, please do.  I wrote it upon request for a client who wanted it on their site.
 *
 ****************************************[ USAGE ]****************************************
 *
 * You will need:
 * 	1.) jquery pluggin
 * 	2.) navHistory pluggin
 *  3.) link to the "cross.gif" image,used as "delete" button to right of each listed item 
 *
 * Adding to your page:
 *
 *  1.) Create an empty DIV where you want the button to appear and give it a unique ID
 *		i.e., <div id="navHistory"></div> 
 *	
 *		Reserved ID and Classes:  
 *			#navHistBtn, #navHistInner, #navHistClear, #histCount, .histList, .histDel
 *    
 *   2.) attach the navHistory pluggin to the element:
 *
 *	Simple mode:
 *		$.('#navHistory').navHistory(); // this will use all default settings - no ajax. 
 *
 *	Advanced mode:
 *		$.('#navHistory').navHistory({
 *			btnTitle: 'Recently Viewed',
 *			btnWidth: 150,   = button width in pixels
 *  		listWidth: 200,   = dropdown list width in pixels
 *			listOffset: -15, = adjusts X axis of dropdown box in relation to the main button
 * 			maxCount: 15,   = maximum number of records to be stored before deleting the oldest record each time a new one is added
 *			countOn: 'true', = true or false: determines whether or not to show "items stored" at bottom of list
 * 			ajaxOn: 'true',  = true or false: determines whether link will be loaded via ajax or as a normal link 
 *			linkTarget: "#content", = for ajax, specify a containing element selector.  Otherwise, use "_blank", "_self", etc...  
 *			buttonColor: "#0080C6", = hexidecimal value or color name for background color of button
 *			borderColor: "#0080C6', = hexidecimal value or color name for border color of dropdown
 *			ignore: ['divId', 'divClass', 'anchorTagClass'] = Array: list ID and/or Class attributes of to exclude.  More info below.  
 *		});
 *
 *	Ignoring Links:
 *			This can be the Id or Class attribute of the parent, parent of a parent, or of the <a> tag itself. 
 *			Since it is an array, each item must be in single quotes and seperated by commas, as shown in the
 *			above example.  DO NOT include the ID (#) or class (.) selectors. 
 *************************************************************************************************************
 */

(function($) {
	$.fn.navHistory = function(options) {
		
		var defaults = {
			btnTitle: 'Recently Viewed',
	    	btnWidth: 150,  
        	listWidth: 500,
			listOffset: -15, 
        	maxCount: 12, 
			countOn: 'true',
        	ajaxOn: 'false',  
        	linkTarget: "_blank", 
			buttonColor: "#0080C6",
			borderColor: "#0080C6",
			ignore: ''
   		}; 
		
        var options = $.extend(defaults, options);
		
		return this.each(function() {
			obj = $(this);
			histCount=0;
			
			//Creating the button and its content.
			obj.prepend('<div id="navHistBtn" style="padding:2px;width:'+options.btnWidth+'px; margin-bottom:5px; background-color:'+options.buttonColor+';color:#FFFFFF;text-align:center;cursor:pointer;">'+options.btnTitle+'</div><div id="navHistInner" style="position:absolute; z-index:0; margin-top:-10px;margin-left:'+options.listOffset+'px; background-color:#FFF; border:solid '+options.borderColor+' 1px; width:'+options.listWidth+'px;padding:5px;"><div id="navHistClear"style="margin:2px;background-color:'+options.buttonColor+';color:#FFF;cursor:pointer;text-align:center;">Clear List</div><div id="histCount" style="text-align:center;"></div></div>');			   
	
			if(options.countOn == 'true'){
				$('#histCount').append(histCount+' Items Stored');
			}
			// Altering some CSS properties and Rollovers:
			$('#navHistInner').css("visibility","hidden");
			
			var delay;
			$('#navHistBtn').mouseover(function(){								
				delay=setTimeout(function(){$('#navHistInner').css("visibility","visible")},500);	
			});
			$('#navHistBtn').mouseout(function(){
				clearTimeout(delay);								
			});
	
			$('#navHistInner').hover(function(){},function(){							 
				$('#navHistInner').css("visibility","hidden");	
			});

			$('#navHistClear').hover(function(){
				$('#navHistClear').css("backgroundColor","red");							  
			},function(){							 
				$('#navHistClear').css("backgroundColor",options.buttonColor);	
			});

			// Clearing all records from the list
			$('#navHistClear').click(function(){
				$('#navHistInner').children('.histList').remove();	
				histCount = 0;
				if(options.countOn == 'true'){
					$('#histCount').replaceWith('<div id="histCount" style="text-align:center;">'+histCount+' Items Stored</div>');
				}
			});
	
			function runHistory(linkUrl, linkTitle){
				histCount = histCount + 1;
				if(options.countOn == 'true'){
					$('#histCount').replaceWith('<div id="histCount" style="text-align:center;">'+histCount+' Items Stored</div>');
				}
				$('#navHistInner').prepend('<div class="histList" style="margin:2px;border-bottom:solid #ccc 1px;"><div style="display:block; float:left;"><a href="'+linkUrl+'" target="'+options.linkTarget+'">'+linkTitle+'</a></div><div class="histDel" style="display:block; float:right;cursor:pointer;"><img  src="js/akmodal/cross.gif" title="Delete This Item"/></div><div style="clear:both;"></div></div>');
	
				// when delete "X" button us clicked, do this:
				$('.histDel').click(function(){											 
					$(this).parent('.histList').remove();
					histCount = histCount - 1;
					if(options.countOn == 'true'){
					 $('#histCount').replaceWith('<div id="histCount" style="text-align:center;">'+histCount+' Items Stored</div>');
					}
				});	
									
				//if AjaxOn is set to true, use .load to load url into specified element
				if(options.ajaxOn == 'true'){
					$('.histList a').click(function(){
						var target = $(this).attr('href');
						$(options.linkTarget).load(target);	
						return false;
					});   
				}	
			  // set the max number of items in list
				if(histCount > options.maxCount){
					$('#navHistInner .histList:last').remove();
					histCount = histCount - 1;
					$('#histCount').replaceWith('<div id="histCount" style="text-align:center;">'+histCount+' Items Stored - Memory Full</div>');
				};	
			};// end runhistory function
		
			// Iterate through "ignore" content and block items-----------------
			function blockList(thisClick){
				igArray = options.ignore;
				igArrayLength = igArray.length;
				var acceptLink = true;
				
				//get all possible attributes 
				var thisParentId = thisClick.parent().attr('id');
				var thisParentClass = thisClick.parent().attr('class');
				var thisParentParentId = thisClick.parent().parent().attr('id');
				var thisParentParentClass = thisClick.parent().parent().attr('class');
				var thisId = thisClick.attr('id');
				var thisClass = thisClick.attr('class');
				
				//For loop that tests each value in the block list array against the clicked item
				var i=0;
				for (i=0;i<=igArrayLength && acceptLink == true;i++){
					var ignore = igArray[i];	
					if(ignore == thisParentId || ignore == thisParentClass || ignore == thisParentParentId || ignore == thisParentParentClass || ignore == thisId || ignore == thisClass){
						acceptLink = false;
						}else{
						acceptLink = true;
					}					
				}//end for loop
				
				if(acceptLink){
					linkUrl = thisClick.attr('href');
					linkTitle = thisClick.text();
					if(linkTitle.length == 0){linkTitle = thisClick.attr('title');};
					if(linkTitle.length == 0){linkTitle =('Unknown Page');};
					runHistory(linkUrl, linkTitle);	
				}
			}//end blocklist function
						
			$('a').click(function(){
				var thisClick = $(this);
				blockList(thisClick);
			});
		});
	};		
})(jQuery);				
