/*
 * 	MDB Right Click - jQuery plugin
 * 	Disable right click and add custom context menu
 *	Written for Million Dollar Baby
 *
 *	Cross browser issues:
 *	- Safari does not allow disabling of the context menu. 
 * 	- Opera does not allow Javascript 'bookmark this' links
 * 	- Firefox only allows 'bookmark this' links by opening the page in the sidebar
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */
 
(function( $ ){
	$.fn.mdbRightClick = function(options) {

		var settings = $.extend( {
			offsetx	: 2,
			offsety	: 2,
			id		: 'contextmenu',
			links	: {
				1: {
					type: 'back'
				},
				2: {
					type: 'forward'
				},
				3: {
					type: 'bookmark'
				}
			}
			
		}, options);
		
		/*
		TYPES:
		
			'url'
				Requires 'text', 'link'
				Optional: 'target'
				
			'link'	:	To be used on A elements or elements wrapped in A
				Requires: none
				Optional: 'text', 'target'
				Nonfunctional: 'link'
			
			'bookmark'
				Optional: 'text'
				Nonfunctional: 'target', 'link'
		
			'bookmarklink'	:	To be used on A elements or elements wrapped in A
				Optional: 'text'
				Nonfunctional: 'target', 'link'
				
			'back'
				Optional: 'text'
				Nonfunctional: 'target', 'link'
				
			'forward'
				Optional: 'text'
				Nonfunctional: 'target', 'link'
		
		*/
		
		return this.each(function() {		
			$(this).mouseup(function(event) {
				if(event.which == 3) {
					var norightclick = function() {
						$(document).unbind('click', norightclick).unbind('keypress');
						menu.remove();
						return false;
					};
					event.stopPropagation();
					
					// Disable the browser's context menu
					this.oncontextmenu = function() {
						return false;
					} 
					
					// If menu exists, remove it
					if ($('#' + settings.id).length ) { 						
						$('#' + settings.id).remove();					
					} 
					
					// If menu doesn't exist					
					$('body').append('<div id="' + settings.id + '"></div>');
					var menu = $('#' + settings.id);
					$(document).unbind('click', norightclick);
					menu.append('<ul></ul>');
					
					// Position it
					menu.css('left', event.pageX + settings.offsetx);
					menu.css('top', event.pageY + settings.offsety);
					
					// Check if this is for a link or not 
					var anchor;
					var anchortitle;
					if($(this).is('a') || $(this).parent().is('a')) {
						if($(this).is('a')) {
							anchor = $(this).attr('href');
							anchortitle = $(this).attr('title');
						} else if($(this).parent().is('a')) {
							anchor = $(this).parent().attr('href');
							anchortitle = $(this).parent().attr('title');
						}	
						if (anchor.substring(0,6) != 'http:/' && anchor.substring(0,6) != 'https:' && anchor.substring(0,6) != 'ftp://' && anchor.substring(0,6) != 'mailto') {
							var domain = window.location.hostname;
							anchor = 'http://' + domain + anchor;
						}
						if(anchortitle == '' || anchortitle === undefined) {
							anchortitle = anchor;
						}	
					}					
					
					// Add items
					jQuery.each(settings.links, function(i, obj){
						switch (obj.type) {
							case 'link':	
							
								var thetext = (obj.text !== undefined) ? obj.text : 'Open Link in New Window';
								var thetarget = (obj.target !== undefined) ? ' target="' + obj.target + '"' : ' target="_blank"';
								
								menu.find('ul').append('<li></li>');
								menu.find('li:last-child').append('<a href="' + anchor + '" ' + thetarget + '>' + thetext + '</a>');
								
								break;
								
							case 'url': 
							
								if(obj.text !== undefined && obj.link !== undefined) {
									var thetarget = (obj.target !== undefined) ? ' target="' + obj.target + '"' : '';
									
									menu.find('ul').append('<li></li>');
									menu.find('li:last-child').append('<a href="' + obj.link + '" ' + obj.target + '>' + obj.text + '</a>');
								}
								
								break;
								
							case 'bookmark':
							
								if(window.sidebar || window.external || opera.external) { // (Firefox | IE | Opera) - Safari doesn't support
									var path = location.href;
									var title = document.title;
									var thetext = (obj.text !== undefined) ? obj.text : 'Bookmark This Page';

									menu.find('ul').append('<li></li>');
									menu.find('li:last-child').append('<a href="#" class="mdbbookmark">' + thetext + '</a>');
									
									if(window.opera) {
										if ($('.mdbbookmark').attr("rel") != '') { 
											$('.mdbbookmark').attr("rel","sidebar");
										}										
									}
									$('.mdbbookmark').unbind('click').bind('click', function(ev) {
										if(window.sidebar) { 
											window.sidebar.addPanel(title, path, '');
										} else if(window.external) { 
											window.external.AddFavorite(path, title);
										} else if(window.opera) { 
											return false; 
										} 
										ev.preventDefault();
									});	
								}
							
								break;
								
							case 'bookmarklink':
							
								if(window.sidebar || window.external || opera.external) { // (Firefox | IE | Opera) - Safari doesn't support
									var thetext = (obj.text !== undefined) ? obj.text : 'Bookmark This Link';

									menu.find('ul').append('<li></li>');
									menu.find('li:last-child').append('<a href="#" class="mdbbookmark">' + thetext + '</a>');
									
									if(window.opera) {
										if ($('.mdbbookmark').attr("rel") != '') { 
											$('.mdbbookmark').attr("rel","sidebar");
										}										
									}
									$('.mdbbookmark').unbind('click').bind('click', function(ev) {
										if(window.sidebar) { 
											window.sidebar.addPanel(anchortitle, anchor, '');
										} else if(window.external) { 
											window.external.AddFavorite(anchor, anchortitle);
										} else if(window.opera) { 
											return false; 
										} 
										ev.preventDefault();
									});	
								}
							
								break;
								
							case 'back':
							
								var thetext = (obj.text !== undefined) ? obj.text : 'Back';
								menu.find('ul').append('<li></li>');
								menu.find('li:last-child').append('<a href="#" class="mdbnavback">' + thetext + '</a>');
								
								$('.mdbnavback').unbind('click').bind('click', function(ev) {
									history.back(1);
									ev.preventDefault();
								});								
								
								break;
							
							case 'forward':
							
								var thetext = (obj.text !== undefined) ? obj.text : 'Forward';
								menu.find('ul').append('<li></li>');
								menu.find('li:last-child').append('<a href="#" class="mdbnavforth">' + thetext + '</a>');
								
								$('.mdbnavforth').unbind('click').bind('click', function(ev) {
									history.forward(1);
									ev.preventDefault();
								});								
								
								break;
								
							default: 
							
								break;
								
						}
					});
					
					// Hover events - thanks to http://abeautifulsite.net/blog/2008/09/jquery-context-menu-plugin/
					menu.find('a').mouseover(function() {
						menu.find('li.hover').removeClass('hover');
						$(this).parent().addClass('hover');
					}).mouseout(function() {
						menu.find('li.hover').removeClass('hover');
					});
						
					// Keyboard
					$(document).keypress(function(e) {
						switch(e.keyCode) {
							case 38: // up
								if( menu.find('li.hover').size() == 0 ) {
									menu.find('li:last').addClass('hover');
								} else {
									menu.find('li.hover').removeClass('hover').prevAll().eq(0).addClass('hover');
									if( menu.find('li.hover').size() == 0 ) menu.find('li:last').addClass('hover');
								}
							break;
							case 40: // down
								if( menu.find('li.hover').size() == 0 ) {
									menu.find('li:first').addClass('hover');
								} else {
									menu.find('li.hover').removeClass('hover').nextAll().eq(0).addClass('hover');
									if( menu.find('li.hover').size() == 0 ) menu.find('li:first').addClass('hover');
								}
							break;
							case 13: // enter
								menu.find('li.hover a').trigger('click');
							break;
							case 27: // esc
								$(document).trigger('click');
							break
						}
						e.preventDefault();
					});					

					// When items are selected
					menu.find('li a').not('.mdbbookmark, .mdbnavback, .mdbnavforth').unbind('click');
					menu.find('li a').not('.mdbbookmark, .mdbnavback, .mdbnavforth').click( function() {
						$(document).unbind('click', norightclick).unbind('keypress');
						menu.remove();
					});					
					
					// Bind document clicks to remove menu
					setTimeout( function() {
						$(document).bind('click', norightclick);
					}, 0); 
					
				} // end if event 3 			
			}); // end this.mouseup
		}); // end this.each
	};
})( jQuery );



