// Date: 2010-08-10 17:46
// File: jquery.brthTv.js
// The jquery plugin for slide-show widget
// All rights reserved
//

;(function($) {
	$.extend($, {actionLink:{
		loading: '<img src="'+(typeof(pathPrefix)=='undefined'?'':pathPrefix)+(typeof(staticPrefix)=='undefined'?'':staticPrefix)+'images/loading.gif" alt="loading..." title="loading..."/>'
		,hoverStyle: 'ui-state-hover'
		,action: function( evt ){
			//Handling locking
			if($(this).attr('locked')=='true')return;
			$(this).attr('locked', 'true');
			//Handling action
			act=$(this).attr('action');
			if(act=='link'){//Hyperlink
				link=$(this).attr('content');
				if(typeof(link)=='undefined'||link=='')
					link==$(this).attr('href');
				target=$(this).attr('target');
				target=(typeof(target)=='undefined')||(target=='')?'#bodyPnl':target;
				if(target.match(/^_.*/gi))
					window.open(link, target, $(this).attr('specs'));
				else{
					if(target.indexOf('!')>=0){
						target=target.substring(0, target.indexOf('!'));
						frame=target.substring(target.indexOf('!')+1);
						iframe=$('<iframe></iframe>').attr({src:link,width:'100%',height:'500px',id:frame,frameborder:0,marginheight:'0px',marginwidth:'0px'});
						$(target).empty().append(iframe);
					}else
						$(target).html(jQuery.actionLink.loading).load(link);
				}	
				$(this).attr('locked', null);
			}else if(act=='script'){//Execute script
				script=$(this).attr('content');
				eval(script);
				$(this).attr('locked', null);
			}else if(act=='msg'){//Prompt message
				alert($(this).attr('content'));
				$(this).attr('locked', null);
			}else if(act=='submit'){
				$(this).parents('form:first').submit();
			}else if(act=='ajax'){//Submit data
				//Init parameter
				target=$(this).attr('target');
				target=(typeof(target)=='undefined')||(target=='')?'#bodyPnl':target;
				method=$(this).attr('method');
				method=(typeof(method)=='undefined')?'GET':method;
				data=$(this).attr('data');
				data=(typeof(data)=='undefined')?null:eval('('+data+')');
				//Ajax loading
				$(target).html(jQuery.actionLink.loading);
				$.ajax({
					url: $(this).attr('content')
					, type: method
					, data: data
					, context: this
					, success: function( data, status ){ $(target).html(data); $(this).attr('locked', null); }
					, error: function( req, status ){ $(target).text(status); $(this).attr('locked', null); }
				});
			}else{
				$(this).attr('locked', null);
				return false; //unknow action
			}
		}
	}});
	
	// jQuery plugin definition
	$.fn.actionLink = function(params) {
		// merge default and user parameters
		params = $.extend({target:'#bodyPnl',action:'link',content:'',hoverStyle:jQuery.actionLink.hoverStyle}, params);
		
		// traverse all nodes
		this.each(function() {
			$(this)
				.attr('hoverStyle', params.hoverStyle)
				.hover(function(){$(this).addClass($(this).attr('hoverStyle'));}, function(){$(this).removeClass($(this).attr('hoverStyle'));})
				.css('cursor', 'pointer')
				.click(jQuery.actionLink.action)
			;
		});
		// allow jQuery chaining
		return this;
	};
})(jQuery);

