/*jslint undef: true, browser: true */

/*global jQuery*/

/**
 * Plugin for handling right-click event for jquery 1.3+
 *
 * Copyrighted by Morten Sjoegren <m_abs_AT_mabs.dk> Denmark 2009
 *
 * Licensed under MIT
 * ---------------------------------------------------------------------------------------------------
 *  Copyright (c) 2009 Morten Sjoegren <m_abs_AT_mabs.dk> 
 *  Permission is hereby granted, free of charge, to any person obtaining a copy
 *  of this software and associated documentation files (the "Software"), to deal
 *  in the Software without restriction, including without limitation the rights
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 *  copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *  The above copyright notice and this permission notice shall be included in
 *  all copies or substantial portions of the Software.
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 *  THE SOFTWARE.
 * ---------------------------------------------------------------------------------------------------
 */

/*****************************************************************************************************
 * Changelog:
 *		v1.0	Initial release.
 ****************************************************************************************************/


(function($){var left_button=0;var middel_button=1;var right_button=2;var clear_temp_events=function(){};var mouseup_event=function(e){var data=e.data;var evt=data.evt||e;clear_temp_events.call(this,evt);if(evt.button==right_button&&data&&$.isFunction(data.callback)){var res=data.callback.call(this,evt);if(typeof res=="boolean"){return res;}else{return false;}}else{return true;}};var mousedown_event=function(e){var data=e.data;if(data&&$.isFunction(data.callback)&&e.button==right_button){data.evt=e;$(this).bind("mouseup",data,mouseup_event).bind("mouseleave",clear_temp_events);}};clear_temp_events=function(){var el=$(this);el.unbind("mouseup",mouseup_event);el.unbind("mouseleave",clear_temp_events);};var no_contextmenu=function(e){var el=$(this);var condition_event=el.data("no_right_click_condition");var res;if($.isFunction(condition_event)){res=condition_event.call(this,e);}
if(typeof res=="boolean"){return res;}else{return false;}};$.fn.disableContextMenu=function(condition_event){$(this).each(function(){var el=$(this);if(el.data("stop_right_click_enabled")){return;}
el.data("stop_right_click_enabled",true);el.data("no_right_click_condition",condition_event);this.oncontextmenu=no_contextmenu;});return this;};$.fn.enableContextMenu=function(){$(this).each(function(){var el=$(this);if(el.data("stop_right_click_enabled")){el.removeData("stop_right_click_enabled");el.removeData("no_right_click_condition");this.oncontextmenu=undefined;}});return this;};$.fn.rightClick=function(callback,contextmenu_condition_event){if(!callback||!$.isFunction(callback)){return this;}
var els;$(this).each(function(){var el=$(this);if(el.data("right_click_enabled")){return;}
if(!els){els=el;}else{els=els.add(el);}});if(!els||els.size()===0){return this;}
els.bind("mousedown",{callback:callback},mousedown_event);els.disableContextMenu(contextmenu_condition_event);els.data("right_click_enabled",true);return els;};$.fn.removeRightClick=function(){$(this).each(function(){var el=$(this);if(el.data("right_click_enabled")){el.unbind("mousedown",mousedown_event);el.removeData("right_click_enabled");clear_temp_events.call(el);el.enableContextMenu();}});return this;};})(jQuery);
