// $Id: general.js 2233 2009-02-18 16:18:52Z alexc $

/**
 * Zula Framework General JS
 *
 * @patches submit all patches to patches@tangocms.org
 *
 * @author Alex Cartwright
 * @copyright Copyright (C) 2007, 2008, Alex Cartwright
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU/GPL 2
 * @package Zula
 */

 	RegExp.escape = function( text ) {
						if (!arguments.callee.sRE) {
							var specials = [
						  		'/', '.', '*', '+', '?', '|', '^', '$',
						  		'(', ')', '[', ']', '{', '}', '\\'
							];
							arguments.callee.sRE = new RegExp( '(\\' + specials.join('|\\') + ')', 'g' );
					  	}
					  	return text.replace(arguments.callee.sRE, '\\$1');
					}

	/**
	 * jQuery plugin to add in or remove the AJAX throbber
	 *
	 * [@param bool $enable]
	 * [@param string $size]
	 * @return bool
	 */
	jQuery.fn.ajax_throbber = function() {
								this.html( '<span class="ajax_throbber"><img src="'+zula_dir_icon+'/misc/throbber.gif"></span>' );
							};

	
	/* Provides a way of including Javascript files into the head */
	function include(script_filename) {
		document.write('<' + 'script');
		document.write(' language="javascript"');
		document.write(' type="text/javascript"');
		document.write(' src="' + script_filename + '">');
		document.write('</' + 'script' + '>');
	}

 	/**
 	 * Toggle function for the ACL form that is displayed,
 	 * will toggle all of the Role/Resource checkboxes for a given
 	 * resource or Role
 	 *
 	 * @param string $class
 	 * @return void
 	 */
	function acl_toggle( name ) {
		var check_boxes = $( "."+name+":checkbox" );
		if ( check_boxes.size() != check_boxes.filter( ":checked" ).size() ) {
			// Check boxes need to be checked
			$( "."+name+":checkbox" ).each(
											function() { $(this).attr( "checked", "checked" ); }
											);
		} else {
			$( "."+name+":checkbox" ).each(
											function() { $(this).removeAttr( "checked" ); }
											);
		}
	}

	/**
	 * Sets the behaviour to be used for "a" elements that have
	 * a class of "confirm_del".
	 */
	function confirm_delete() {
		$( ".confirm_del" ).click(
									function() {
										var answer = confirm( "Are you sure you wish to continue?" );
										return answer;
									}
								);

	}

	/**
	 * Sets up the flash player to play a media item, such as
	 * a video or audio file
	 */
	function init_media_player() {
		$( "a.media_player" ).each(
									function() {
										video_file = $(this).attr( "href" );
										flashembed( this,
														{
														src: flash_media_player,
														width: 470,
														height: $(this).hasClass( "audio" ) ? 28 : 380,
														bgcolor: "#ffffff"
														},

														{config: {
														videoFile: video_file,
														initialSacle: "scane",
														useNativeFullScreen: true,
														loop: false
														}}
													);
										$(this).replaceWith( '<div class="media_player">'+$(this).html()+'</div>' );
									}
								);
	}

	$( document ).ready( confirm_delete );
	$( document ).ready( init_media_player );


