// $Id: general.js 1679 2008-08-31 18:53:04Z alexc $

/**
 * Zula Framework General JS
 *
 * @patches submit all patches to patches@tangocms.org
 *
 * @author Alex Cartwright
 * @copyright Copyright (c) 2007, 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" ).flowplayer( flash_media_player );
	}
	
	$( document ).ready( init_media_player );
	$( document ).ready( confirm_delete );
	$( document ).ready(function() {
							$('#toc').hide();
							$('.toc_toggle').click( function() {
														$('#toc').toggle('slow');
													});
							$('a[rel*=lightbox]').lightBox({
								imageLoading: zula_base_dir + 'html/images/lightbox/loading.gif',
								imageBtnClose: zula_base_dir + 'html/images/lightbox/close.gif',
								imageBtnPrev: zula_base_dir + 'html/images/lightbox/prev.gif',
								imageBtnNext: zula_base_dir + 'html/images/lightbox/next.gif'
							});
							$('area[rel*=lightbox]').lightBox({
								imageLoading: zula_base_dir + 'html/images/lightbox/loading.gif',
								imageBtnClose: zula_base_dir + 'html/images/lightbox/close.gif',
								imageBtnPrev: zula_base_dir + 'html/images/lightbox/prev.gif',
								imageBtnNext: zula_base_dir + 'html/images/lightbox/next.gif'
							});
							/**
							 * Fix for worlds worse browser
							 */
							$('#main-menu ul li').hover(
											 function() {
												$(this).find( 'ul:hidden' ).show();
											},
											function() {
												$(this).find( 'ul:visible' ).hide();
											}
											);
						});

