<?php
// $Id$
// 
//  jqplug.module
//  
// 
// 
//  Created by Bach on 2008-04.
//  Modified by bach on 2009-09-09
//  Copyright 2009 gui. All rights reserved.
// 

/**
* @file
* Loads plugins for jQuery
*
*
*
*/
define('JQPLUG_PERM_ADMIN', 'administer jqplug');


/**
*
* hook_permission()
*
*/
function jqplug_permission(){
	return array('administer jqplug' => array(
		'title' => 'administer jqplug',
		'description' => 'administer what jquery plugin will be loaded',
		),
	);
}


/**
 * hook_menu()
 * 
 */
function jqplug_menu() {

  $items['admin/config/user-interface/jgplug'] = array(
    'title' => 'jq plug',
    'description' => 'Control which plugin are loaded by theme using jqplug.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('jqplug_admin_settings'),
    'access arguments' => array(JQPLUG_PERM_ADMIN),
    'type' => MENU_NORMAL_ITEM
  );

  return $items;
}


function jqplug_admin_settings() {
	
  $form = array();

  $form['jqplug'] = array(
    '#type' => 'fieldset',
    '#title' => t('jqplug settings'),
    '#description' => t('Control which plugin are loaded by theme using jqplug.')
  );


	$default = array();
	$options = array();
	
	$list = _get_plugList();
	
 ## DEFINE OPTIONS
	foreach($list as $plug){
		$options[$plug['file']] = t($plug['name']);
	}
	
	# config by theme, does'nt work yet 'cause of hook_init which does'nt have acces to global $theme
	/*
	$themes = list_themes();
	foreach($themes as $theme){
		if( $theme->status == 1){
			
			$form['jqplug']['jqplug_theme_' . $theme->name] = array(
		    '#type' => 'checkboxes',
		    '#title' => t($theme->name),
		    '#default_value' => variable_get('jqplug_theme_' . $theme->name, $default),
				'#options' => $options,
		    '#description' =>t('<b>' . $theme->name . ' :</b> selecte which plugins will be load for this theme.'),
		  );
		}
	}
	*/
	
	#same config for alla themes
	$form['jqplug']['jqplug'] = array(
    '#type' => 'checkboxes',
    '#title' => t('jQuery Plugins'),
    '#default_value' => variable_get('jqplug', $default),
		'#options' => $options,
    '#description' =>t('selecte which plugins will be load for all theme.'),
  );
 
  return system_settings_form($form);
}


// function jqplug_add($adds = array()){
// 	
// 	$activated = variable_get('jqplug', array());
// 	$list = _get_plugList();
// 
// 	$match = true;
// 	foreach ($adds as $add) {
// 		foreach($list as $plug){
// 			if($plug['name'] == $add){
// 				variable_set();
// 			} 
// 		}
// 	}
// 
// 
// 
// 	return $match;
// 	
// }

function _get_plugList(){
	$list = array();
	$mod_path = drupal_get_path('module', 'jqplug');
	$pluginsFolderPath = $mod_path.'/js/*';
	$strToRemove = array("jquery.", ".pack", "jQuery", ".js", ".compressed", ".minified", ".min");
	$strToSpace = array(".", "-");
	foreach(glob($pluginsFolderPath) as $file){
		$pathParts = explode("/", $file);
		$file = $pathParts[count($pathParts)-1];
		$fileParts = explode(".", $file);
		$extension = $fileParts[count($fileParts)-1];
		if($extension == "js"){
			$PlugName = str_replace($strToRemove, '', $file);
			$PlugName = str_replace($strToSpace, ' ', $PlugName);
			array_push($list, array("file"=>$file,"name"=>$PlugName));
		}
	}
	return $list;
}



/**
 * Implementation of hook_init().
 */
function jqplug_init() {
	# Add the JS for this module.
	$mod_path = drupal_get_path('module', 'jqplug');
	// dsm($mod_path);

	# config by theme, does'nt work yet 'cause of hook_init which does'nt have acces to global $theme
	// global $theme;
	// $theme_plugs = variable_get('jqplug_theme_'.$theme, array());
	// foreach($theme_plugs as $plug){
	// 	if( $plug != 0 ){
	// 		drupal_add_js($mod_path.'/js/'.$plug, 'module', 'header', FALSE, TRUE);
	// 	}
	// }
	
	#same config for alla themes
	$plugs = variable_get('jqplug', array());
	// dsm($plugs);
	foreach($plugs as $plug){
		if( $plug != "0" ){
				// dsm($mod_path.'/js/'.$plug);
			if(file_exists($mod_path.'/js/'.$plug))
				drupal_add_js($mod_path.'/js/'.$plug, array('scope'=>'header', 'group'=>JS_LIBRARY));	
				
			$cssFile = preg_replace("/\.js$/", '.css', $plug);
			$cssFile = str_replace(".min", '', $cssFile);
			if(file_exists($mod_path.'/js/'.$cssFile))
				drupal_add_css($mod_path.'/js/'.$cssFile, array('media'=>'screen', 'group'=>CSS_DEFAULT));
							
		}
	}
	
}