<?php 
if ( is_admin() && isset($_GET['activated'] ) && $pagenow == "themes.php" )wp_redirect( 'themes.php?page=nusantara' );

	// -- Function Name : nusantara_admin_init
	// -- Params : 
	// -- Purpose : 
	function nusantara_admin_init() {
		// Rev up the Options Machine
		global $nusantara_options, $options_machine;
		$options_machine = new Options_Machine($nusantara_options);
		
		
	   }	

	// -- Function Name : nusantara_add_admin
	// -- Params : 
	// -- Purpose : 
	function nusantara_add_admin() {
		$nusantara_page = add_theme_page( NUSANTARA_THEME, 'Nusantara options', 'edit_theme_options', 'nusantara', 'nusantara_options_page');
		// functionaily to the head individually
		add_action("admin_print_scripts-$nusantara_page", 'nusantara_load_only');
		add_action("admin_print_styles-$nusantara_page",'nusantara_style_only');
		add_action( "admin_print_styles-$nusantara_page", 'nusantara_mlu_css', 0 );
		add_action( "admin_print_scripts-$nusantara_page", 'nusantara_mlu_js', 0 );
	}

	

	// -- Function Name : nusantara_options_page
	// -- Params : 
	// -- Purpose : 
	function nusantara_options_page(){
		global $options_machine;
		/*
	//for debugging
	$nusantara_options = get_option(OPTIONS);
	print_r($nusantara_options);
	*/
		?>
<div class="wrap" id="nusantara_container">
       <div id="save_float"> 
		<ul>
                        <li><a href="#wpbody">top</a></li>	                	
			<li id ="nusantara_save"><?php  _e('Save', 'nusantara'); ?></li>			
			<li><a href="#footer">bottom</a></li>	   
                </ul>	
	</div>
	<div id="nusantara-popup-save" class="nusantara-save-popup">
		<div class="nusantara-save-save"><?php  _e('Nusantara Options Updated', 'nusantara'); ?></div>
	</div>
	<div id="nusantara-popup-reset" class="nusantara-save-popup">
		<div class="nusantara-save-reset"><?php  _e('Nusantara Options Reset', 'nusantara'); ?></div>
	</div>
	<div id="nusantara-popup-fail" class="nusantara-save-popup">
		<div class="nusantara-save-fail"><?php  _e('Nusantara Options Error!', 'nusantara'); ?></div>
	</div>
	<span style="display: none;" id="hooks"><?php  echo json_encode(nusantara_get_header_classes_array()); ?></span>
	<input type="hidden" id="reset" value="<?php  if(isset($_REQUEST['reset'])) echo $_REQUEST['reset']; ?>" />
	<input type="hidden" id="security" name="security" value="<?php  echo wp_create_nonce('nusantara_ajax_nonce'); ?>" />
	<form id="nusantara_form" method="post" action="<?php  echo esc_attr( $_SERVER['REQUEST_URI'] )  ?>" enctype="multipart/form-data" >
		<div id="header">
			<div class="logo">
				<h2><?php  echo NUSANTARA_THEME; ?></h2>
				<span><?php  echo (''. NUSANTARA_VERSION); ?></span>
			</div>
			<div id="js-warning"><?php  _e('Warning- This options panel will not work properly without javascript!', 'nusantara'); ?></div>
			<div class="icon-option"></div>
			<div class="clear"></div>
    	</div>
		<div id="info_bar">
			<a>
				<div id="expand_options" class="expand"><?php  _e('Expand', 'nusantara'); ?></div>
			</a>
			<img style="display:none" src="<?php  echo nusantara_DIR; ?>assets/images/loading-bottom.gif" class="ajax-loading-img ajax-loading-img-bottom" alt="Working..." />
			<button id="nusantara_save" type="button" class="button-primary">
				<?php  _e('Save All Changes', 'nusantara'); ?>
			</button>
		</div><!--.info_bar--> 	
		<div id="main">
			<div id="nusantara-nav">
				<ul>
				  <?php  echo $options_machine->Menu  ?>
				</ul>
			</div>
			<div id="content">
		  		<?php  echo $options_machine->Inputs /* Settings */ ?>
		  	</div>
			<div class="clear"></div>
		</div>
		<div class="save_bar"> 		
			<img style="display:none" src="<?php  echo nusantara_DIR; ?>assets/images/loading.gif" class="ajax-loading-img ajax-loading-img-bottom" alt="Working..." />
			<button id ="nusantara_save" type="button" class="button-secondary"><?php  _e('Save All Changes', 'nusantara'); ?></button>			
			<button id ="nusantara_reset" type="button" class="button submit-button reset-button" ><?php  _e('Options Reset', 'nusantara'); ?></button>
			<img style="display:none" src="<?php  echo nusantara_DIR; ?>assets/images/loading.gif" class="ajax-reset-loading-img ajax-loading-img-bottom" alt="Working..." />
		</div><!--.save_bar--> 
	</form>
	<div style="clear:both;"></div>
</div><!--wrap-->	
<?php
	}

	

	// -- Function Name : nusantara_style_only
	// -- Params : 
	// -- Purpose : 
	function nusantara_style_only(){
		wp_enqueue_style('admin-style', nusantara_DIR . 'assets/css/admin-style.css');
		wp_enqueue_style('color-picker', nusantara_DIR . 'assets/css/colorpicker.css');
	}

	

	// -- Function Name : nusantara_load_only
	// -- Params : 
	// -- Purpose : 
	function nusantara_load_only() {
		add_action('admin_head', 'nusantara_admin_head');
		wp_enqueue_script('jquery-ui-core');
		wp_enqueue_script('jquery-ui-sortable');
		wp_enqueue_script('jquery-input-mask', nusantara_DIR .'assets/js/jquery.maskedinput-1.2.2.js', array( 'jquery' ));
		wp_enqueue_script('tipsy', nusantara_DIR .'assets/js/jquery.tipsy.js', array( 'jquery' ));
		wp_enqueue_script('color-picker', nusantara_DIR .'assets/js/colorpicker.js', array('jquery'));
		wp_enqueue_script('ajaxupload', nusantara_DIR .'assets/js/ajaxupload.js', array('jquery'));
		wp_enqueue_script('cookie', nusantara_DIR . 'assets/js/cookie.js', 'jquery');
		wp_enqueue_script('nusantara-admin', nusantara_DIR .'assets/js/nusantara.js', array( 'jquery' ));
	}
	
	// -- Function Name : nusantara_admin_head
	// -- Params : 
	// -- Purpose : 
	function nusantara_admin_head() {
		?>		
	<script type="text/javascript" language="javascript">
	jQuery.noConflict();
	jQuery(document).ready(function($){
		// COLOR Picker			
		$('.colorSelector').each(function(){
			var Othis = this; //cache a copy of the this variable for use inside nested function		
			$(this).ColorPicker({
					color: '<?php  if(isset($color)) echo $color; ?>',
					onShow: function (colpkr) {
						$(colpkr).fadeIn(500);
						return false;
					},
					onHide: function (colpkr) {
						$(colpkr).fadeOut(500);
						return false;
					},
					onChange: function (hsb, hex, rgb) {
						$(Othis).children('div').css('backgroundColor', '#' + hex);
						$(Othis).next('input').attr('value','#' + hex);
					}
			});
		}); //end color picker
	}); //end doc ready
	</script>
<?php 
	}

	

	// -- Function Name : nusantara_ajax_callback
	// -- Params : 
	// -- Purpose : 
	function nusantara_ajax_callback() {
		global $options_machine, $nusantara_options;
		$nonce=$_POST['security'];
		
		if (! wp_verify_nonce($nonce, 'nusantara_ajax_nonce') ) die('-1');
		//get options array from db
		$all = get_option(OPTIONS);
		$save_type = $_POST['type'];
		//echo $_POST['data'];
		//Uploads
		
		if($save_type == 'upload'){
			$clickedID = $_POST['data'];
			// Acts as the name
			$filename = $_FILES[$clickedID];
			$filename['name'] = preg_replace('/[^a-zA-Z0-9._\-]/', '', $filename['name']);
			$override['test_form'] = false;
			$override['action'] = 'wp_handle_upload';
			$uploaded_file = wp_handle_upload($filename,$override);
			$upload_tracking[] = $clickedID;
			//update $options array w/ image URL			  
			$upload_image = $all;
			//preserve current data
			$upload_image[$clickedID] = $uploaded_file['url'];
			update_option(OPTIONS, $upload_image ) ;
			
			if(!empty($uploaded_file['error'])) {
				echo 'Upload Error: ' . $uploaded_file['error'];
			} else {
				echo $uploaded_file['url'];
			}

			// Is the Response
		}

		elseif($save_type == 'image_reset'){
			$id = $_POST['data'];
			// Acts as the name
			$delete_image = $all;
			//preserve rest of data
			$delete_image[$id] = '';
			//update array key with empty value	 
			update_option(OPTIONS, $delete_image ) ;
		}

		elseif($save_type == 'backup_options'){
			$backup = $all;
			$backup['backup_log'] = date('r');
			update_option(BACKUPS, $backup ) ;
			die('1');
		}

		elseif($save_type == 'restore_options'){
			$nusantara_options = get_option(BACKUPS);
			update_option(OPTIONS, $nusantara_options);                                                 
			die('1');
		}

		elseif ($save_type == 'save'){
			wp_parse_str(stripslashes($_POST['data']), $nusantara_options);
			unset($nusantara_options['security']);
			unset($nusantara_options['nusantara_save']);
			update_option(OPTIONS, $nusantara_options);
                        
			die('1');
		}

		elseif ($save_type == 'reset'){
                        update_option(OPTIONS,$options_machine->Defaults);
			die('1');
			//options reset
		}

		die();
	}

// -- Function Name : nusantara_head
	// -- Params : 
	// -- Purpose : 
	function nusantara_head() {
		do_action( 'nusantara_head' );
	}

	

	// -- Function Name : nusantara_option_setup
	// -- Params : 
	// -- Purpose : 
	function nusantara_option_setup(){
		global $nusantara_options, $options_machine;
		$options_machine = new Options_Machine($nusantara_options);
		
		if (!get_option(OPTIONS)){
                        update_option(OPTIONS,$options_machine->Defaults);
		}

	}
	
	
	// -- Function Name : nusantara_get_header_classes_array
	// -- Params : 
	// -- Purpose : 
	function nusantara_get_header_classes_array() {
		global $nusantara_options;
		foreach ($nusantara_options as $value) {
			
			if ($value['type'] == 'heading')$hooks[] = str_replace(' ','',strtolower($value['name']));
		}

		return $hooks;
	}

	$nusantara_options = get_option(OPTIONS);

// -- Required action filters
// -- uses add_action()
add_action('admin_head','nusantara_option_setup');
add_action('admin_init','nusantara_admin_init');
add_action('admin_menu', 'nusantara_add_admin');
add_action( 'init', 'nusantara_mlu_init');

// -- AJAX Saving Options
add_action('wp_ajax_nusantara_ajax_post_action', 'nusantara_ajax_callback');
