<?php
/**
 *
 * Adds the stylesheet for Swift admin pages
 */
function swift_admin_stylesheet(){
	wp_enqueue_style( 'swift-admin-stylesheet', get_template_directory_uri() .'/lib/admin/css/style.css' );

}

/**
 * Add JS scripts to Swift admin pages
 */
function swift_admin_scripts(){
	wp_enqueue_script( 'jquery' );
	wp_enqueue_script( 'jquery-ui-core' );
	wp_enqueue_script( 'jquery-ui-tabs' );
	wp_enqueue_script( 'jquery-ui-draggable' );
	wp_enqueue_script( 'jquery-ui-sortable' );
	wp_enqueue_script( 'jquery-ui-accordion' );
	wp_enqueue_script( 'jquery-ui-slider' );
	wp_enqueue_script( 'cookie', get_template_directory_uri() .'/lib/admin/js/jquery.cookie.js', array( 'jquery', 'jquery-ui-core','jquery-ui-tabs','file-uploader', 'jsColor' ) );
	wp_enqueue_script( 'admin-scripts', get_template_directory_uri() .'/lib/admin/js/scripts.js', array( 'jquery','cookie', 'jquery-ui-core','jquery-ui-tabs','file-uploader', 'jsColor' ) );
	wp_enqueue_script( 'file-uploader', get_template_directory_uri() .'/lib/admin/js/fileuploader.js', array( 'jquery' ) );
	wp_enqueue_script( 'jsColor', get_template_directory_uri() .'/lib/admin/js/jscolor/jscolor.js', array( 'jquery' ) );
}

/**
 * Gravatar JS
 */
function swift_gravatar_loading(){
	wp_enqueue_script( 'gravatarLoading', get_template_directory_uri() .'/lib/admin/js/gravatar-delayed-loading.js' );
}

// Add the options pages
add_action( 'admin_menu', 'swift_add_admin_pages' );

function swift_add_admin_pages(){
	$icon_url = get_template_directory_uri() .'/lib/admin/images/favicon.ico';
	$swift_options_page = add_menu_page( __( 'Swift Options', 'swift' ), __( 'Swift Options', 'swift' ), 'edit_theme_options', 'swift-options', 'swift_options_page', $icon_url, 62 );
	$swift_design_options_page = add_submenu_page( 'swift-options', __( 'Design Options', 'swift' ), __( 'Design Options', 'swift' ), 'edit_theme_options', 'swift-design-options', 'swift_design_options_page' );
	$swift_color_schemes_page = add_submenu_page( 'swift-options', __( 'Color Schemes', 'swift' ), __( 'Color Schemes', 'swift' ), 'edit_theme_options', 'swift-color-schemes', 'swift_color_schemes_page' );
	$swift_import_export = add_submenu_page( 'swift-options', __( 'Import / Export', 'swift' ), __( 'Import / Export', 'swift' ), 'edit_theme_options', 'swift-import-export', 'swift_import_export_page' );
	$swift_help = add_submenu_page( 'swift-options', __( 'Help', 'swift' ), __( 'Help', 'swift' ), 'edit_theme_options', 'swift-help', 'swift_help' );

	add_action( "admin_print_styles-$swift_options_page", 'swift_admin_stylesheet' );
	add_action( "admin_print_scripts-$swift_options_page", 'swift_admin_scripts' );

	add_action( "admin_print_styles-$swift_design_options_page", 'swift_admin_stylesheet', 1 );
	add_action( "admin_print_scripts-$swift_design_options_page", 'swift_admin_scripts', 2 );
	add_action( "admin_print_scripts-$swift_color_schemes_page", 'swift_admin_scripts', 2 );

	add_action( "admin_print_styles-$swift_import_export", 'swift_admin_stylesheet' );
	add_action( "admin_print_styles-$swift_help", 'swift_admin_stylesheet' );
	add_action( "admin_print_styles-$swift_color_schemes_page", 'swift_admin_stylesheet' );


	}

add_action( 'admin_init', 'swift_options_init');

function swift_options_init(){
	register_setting( 'swift_options', 'swift_options', 'swift_validate_options');
	register_setting( 'swift_design_options', 'swift_design_options', 'swift_validate_options');

	add_settings_section( 'swift-options', NULL, 'swift_admin_header', 'swift-options');
	add_settings_section( 'swift-design-options', NULL, 'swift_admin_header', 'swift-design-options');

	add_settings_field( '','','swift_options_input','swift-options', 'swift-options');
	add_settings_field( '','','swift_design_options_input','swift-design-options', 'swift-design-options');
}

function swift_options_page(){
	$msg=swift_write_file();
	if ( $msg && $msg===TRUE )
		return;
	elseif($msg)
		echo '<div id="message" style="width:94%;" class="message updated"><p>' . $msg . '</p></div>';
?>
	<div class="swift-wrap clearfix">
	<form action="options.php" method="post">
	<?php settings_fields( 'swift_options' )?>
	<?php do_settings_sections( 'swift-options' )?>
	<div id="swift-options-buttons" class="clearfix">
	<input name="swift_options[reset]" type="submit" onClick="return confirmation()" value="<?php _e( 'Reset options', 'swift'); ?>" class="button-secondary alignleft" />
	<input name="swift_options[save]" type="submit" value="<?php _e( 'Save options', 'swift' ); ?>" class="button-primary alignright" />
	</div>
	</form>
	</div>
	<?php
}

function swift_design_options_page(){
	$msg=swift_write_file();
	if ( $msg && $msg===TRUE )
		return;
	elseif($msg)
		echo '<div id="message" class="message updated"><p>' . $msg . '</p></div>';
?>
	<div class="swift-wrap clearfix">
	<form action="options.php" method="post">
	<?php settings_fields( 'swift_design_options' )?>
	<?php do_settings_sections( 'swift-design-options' )?>
	<div id="swift-options-buttons" class="clearfix">
	<input name="swift_design_options[reset]" type="submit" onClick="return confirmation()" value="<?php _e( 'Reset options', 'swift'); ?>" class="button-secondary alignleft" />
	<input name="swift_design_options[save]" type="submit" value="<?php _e( 'Save changes', 'swift' ); ?>" class="button-primary alignright" />
	</div>
	</form>
	</div>
	<?php
}

function swift_import_export_page(){
	if(isset($_POST))
	swift_import_options($_POST);

	//if ( swift_write_file(TRUE) ) return;
	$msg=swift_write_file(TRUE);
	if ( $msg && $msg===TRUE )
		return;

?>
	<div class="swift-wrap clearfix">
	<form action="" method="post">
	<?php swift_import_export_form()?>
	<div id="swift-options-buttons" class="clearfix">
	<input name="swift_import_export" type="submit" value="<?php _e( 'Save changes', 'swift' ); ?>" class="button-primary alignright" />
	</div>
	</form>
	</div>
	<?php
}

function swift_help(){
	include( SWIFT_ADMIN.'/swift-help.php');
}
function swift_admin_header(){
?>


	<div id="header" class="clearfix">
		<div id="theme-info">
		<?php
			$temp=get_theme_data(TEMPLATEPATH.'/style.css');
			$temp2=get_theme_data(STYLESHEETPATH.'/style.css');
			/* Should use this with wp verison 3.4
			$ct = wp_get_theme();
			if(is_child_theme()){
				$pt = $ct->parent();
				echo $pt->get('Version');


			}*/


		?>
			<h3><?php echo $temp['Name'].' <strong>v'.$temp['Version'].'</strong>';?></h3>
			<span><?php echo __( 'Location:', 'swift') . str_replace( WP_CONTENT_DIR, '', TEMPLATEPATH ); ?></span>
		    <?php if( TEMPLATEPATH != STYLESHEETPATH ){?>
			<h4><?php echo $temp2['Name'].' <strong>v'.$temp2['Version'].'</strong>';?></h4>
		    <span><?php echo __( 'Location:', 'swift') . str_replace( WP_CONTENT_DIR, '', STYLESHEETPATH ); ?></span>
		    <?php } ?>

	    </div>

		<div id="logo"><a href="http://SwiftThemes.Com"></a></div>
	</div>
  	<ul id="swift-nav" class="clearfix">
 		<li id="hire" class="alignleft"><a href="http://swiftthemes.com/hire-me/" title="<?php printf( __( 'Hire %s for a custom theme modification', 'swift' ), 'Satish Gandham' ); ?>"><?php _e( 'Request a custom theme modification', 'swift' ); ?></a></li>
    	<li id="manual" ><a href="http://swiftthemes.com/wordpress-themes/swift/swift-user-guide/" title="<?php _e( 'A complete guide to installing and customizing SWIFT', 'swift' ); ?>"><?php _e( 'User Guide', 'swift' ); ?></a></li>
 		<li id="support" ><a href="http://swiftthemes.com/forums" title="<?php _e( 'Need more help? Check out support forum.', 'swift' ); ?>"><?php _e( 'Support forum', 'swift' ); ?></a></li>
 		<li id="testimonial" ><a href="http://swiftthemes.com/testimonials/" title="<?php _e( 'Write a testimonial for SWIFT', 'swift' ); ?>"><?php _e( 'Write a Testimonial', 'swift' ); ?></a></li>
  	</ul>

<?php
}

function swift_validate_options( $input ){

GLOBAL $swift_image_repeat, $swift_font_stack, $swift_font_unit, $swift_font_style, $swift_font_weight;
GLOBAL $swift_text_transform, $swift_design_options_init, $swift_options_init,$swift_font_defaults,$swift_design_options;

if( isset( $swift_design_opt['fontstack1'] ) || isset( $swift_design_opt['fontstack2'] ) )
	array_unshift($swift_font_stack, array($swift_design_opt['fontstack1'],''), array($swift_design_opt['fontstack2'], '') );

	if( $input['which'] == 'design_options' ){
		$options = apply_filters( 'swift_design_options_init', $swift_design_options_init);
	}elseif ( $input['which'] == 'options' ){
		$options = apply_filters( 'swift_options_init', $swift_options_init);
	}else{
		return $input;
	}
	$sanitized_options = array();

	if( isset( $input['reset'] ) ){
		foreach ( $options as $option ){
			if( $option['datatype'] != 'none' && $option['datatype'] !='font' ){
				$sanitized_options[ $option['id'] ] = $option['default'];
			}
			elseif($option['datatype'] =='font'){
			$sanitized_options[ $option['id'].'_enable' ] = $swift_font_defaults[$option['id'].'_enable'];
			$sanitized_options[ $option['id'].'_family' ] = $swift_font_defaults[$option['id'].'_family'];
			$sanitized_options[ $option['id'].'_size' ] = $swift_font_defaults[$option['id'].'_size'];
			$sanitized_options[ $option['id'].'_size_unit' ] = $swift_font_defaults[$option['id'].'_size_unit'];
			$sanitized_options[ $option['id'].'_lh' ] = $swift_font_defaults[$option['id'].'_lh'];
			$sanitized_options[ $option['id'].'_lh_unit' ] = $swift_font_defaults[$option['id'].'_lh_unit'];
			$sanitized_options[ $option['id'].'_weight' ] = $swift_font_defaults[$option['id'].'_weight'];
			$sanitized_options[ $option['id'].'_transform' ] = $swift_font_defaults[$option['id'].'_transform'];
			$sanitized_options[ $option['id'].'_style' ] = $swift_font_defaults[$option['id'].'_style'];
			}
		}

		if(is_array(get_option('SwiftOptions'))){
		$temp_options = get_option('SwiftOptions');
		}
		if( $input['which'] == 'options' ){
			$temp_options['site_options'] = $sanitized_options;
		}elseif( $input['which'] == 'design_options' ){
			$temp_options['design_options'] = $sanitized_options;
		}
		update_option('SwiftOptions', $temp_options);
		return NULL;
	}
	foreach ( $options as $option ){
		switch ( $option['datatype'] ){

			case 'none':
			case 'hidden':
			break;

			case 'predefined':
				$keys = array_keys( $option['options'] );
				if( isset($input[ $option['id']]) && in_array( $input[ $option['id'] ], $keys ) ){
					$sanitized_options[ $option['id'] ] = $input[ $option['id'] ];
				}else{
					$sanitized_options[ $option['id'] ] = $option['default'];
				}
			break;
			case 'int':
				$sanitized_options[ $option['id'] ] = absint( $input[ $option['id'] ] );

			break;

			case 'uri':
				$sanitized_options[ $option['id'] ] = esc_url( $input[ $option['id'] ] );

			break;

			case 'text':
				$sanitized_options[ $option['id'] ] = ( $input[ $option['id'] ] );

			break;

			case 'javascript':
				$sanitized_options[ $option['id'] ] = force_balance_tags( $input[ $option['id'] ] );

			break;


			case 'sortable_array':
				$temp =  array();
				$temp = split( ',', $input[ $option['id'] ] );
				$sanitized_options[ $option['id'] ] = $temp;

			break;

			case 'sortable':
				$i = 0;
				$temp = $temp2 = array();
				$temp = split( ',', $input[ $option['id'] ] );
				foreach( $temp as $value ){
					if( !in_array($value, $option['options'] ) && $value != ''  ){
						$temp2[$i++] = "text";
						$temp2[$i++] = $value;
					}
					else{
						$temp2[$i++] = $value;
					}
				}

				$sanitized_options[ $option['id'] ] = $temp2;

				$temp2 = $temp = NULL;
			break;

			case 'font':
				$id = $option['id'];
			if( isset( $swift_design_options['fontstack1'] ) || isset( $swift_design_options['fontstack2'] ) )
			array_unshift($swift_font_stack, array($swift_design_options['fontstack1'],''), array($swift_design_options['fontstack2'], '') );

				if(isset($input[ $option['id'].'_enable' ]))
				$sanitized_options[ $option['id'].'_enable' ] = (bool)( $input[ $option['id'].'_enable' ] );
				else
				$sanitized_options[ $option['id'].'_enable' ] = FALSE;
				/*
				$keys = array_keys( $font_stack );
				if( in_array( $input[ $option['id'].'_family' ], $keys ) )
					$sanitized_options[ $option['id'].'_family' ] = $input[ $option['id'].'_family'];
				else
					$sanitized_options[ $option['id'].'_family' ] = '';
				*/
				foreach( $swift_font_stack as $font ){
					if( $input[ $option['id'].'_family' ] == $font[0] ){
						$sanitized_options[ $option['id'].'_family' ] = $font[0];
						BREAK;
					}
					else{
						$sanitized_options[ $option['id'].'_family' ] = '';
					}

				}

				$keys = array_keys( $swift_font_unit );
				if( in_array( $input[ $option['id'].'_size_unit' ], $keys ) )
					$sanitized_options[ $option['id'].'_size_unit' ] = $input[ $option['id'].'_size_unit' ];
				else
					$sanitized_options[ $option['id'] ] = '';

				if( in_array( $input[ $option['id'].'_lh_unit' ], $keys ) )
					$sanitized_options[ $option['id'].'_lh_unit' ] = $input[ $option['id'].'_lh_unit' ];
				else
					$sanitized_options[ $option['id'] ] = '';

				$keys = array_keys( $swift_font_style );
				if( in_array( $input[ $option['id'].'_style' ], $keys ) )
					$sanitized_options[ $option['id'].'_style' ] = $input[ $option['id'].'_style' ];
				else
					$sanitized_options[ $option['id'] ] = '';

				$keys = array_keys( $swift_font_weight );
				if( in_array( $input[ $option['id'].'_weight' ], $keys ) )
					$sanitized_options[ $option['id'].'_weight' ] = $input[ $option['id'].'_weight' ];
				else
					$sanitized_options[ $option['id'] ] = '';

				$keys = array_keys( $swift_text_transform );
				if( in_array( $input[ $option['id'].'_transform' ], $keys ) )
					$sanitized_options[ $option['id'].'_transform' ] = $input[ $option['id'].'_transform' ];
				else
					$sanitized_options[ $option['id'] ] = '';


				$sanitized_options[ $option['id'].'_size' ] = (float)( $input[ $option['id'].'_size' ] );

				$sanitized_options[ $option['id'].'_lh' ] = (float)( $input[ $option['id'].'_lh' ] );


			break;

			case 'background':
				$id = $option['id'];
				$sanitized_options[ $id ] = esc_url( $input[ $id ] );
				$sanitized_options[ $id.'_p_t' ] = esc_attr( $input[ $id.'_p_t' ] );
				$sanitized_options[ $id.'_p_l' ] = esc_attr( $input[ $id.'_p_l' ] );
				$keys = array_keys( $swift_image_repeat );
				if( in_array( $input[ $option['id'].'_repeat' ], $keys ) )
					$sanitized_options[ $option['id'].'_repeat' ] = $input[ $option['id'].'_repeat' ];
				else
					$sanitized_options[ $option['id'] ] = '';


			break;

			case 'bool':
				if( isset($input[ $option['id'] ]) )
				$sanitized_options[ $option['id'] ] = (bool)( $input[ $option['id'] ] );
				else
				$sanitized_options[ $option['id'] ] = FALSE;

			break;

			default:
				if(isset($input[ $option['id'] ]))
				$sanitized_options[ $option['id'] ] = $input[ $option['id'] ];

			break;

		}
	}
	if(is_array(get_option('SwiftOptions'))){
		$temp_options = get_option('SwiftOptions');
	}else{
		$temp_options = array();
	}
	if( $input['which'] == 'options' ){
		$temp_options['site_options'] = $sanitized_options;
	}elseif( $input['which'] == 'design_options' ){
		$temp_options['design_options'] = $sanitized_options;
	}
	update_option('SwiftOptions', $temp_options);
	return  NULL;
}


/**
 *
 * Things to do when the theme is first installed
 */
if ( is_admin() && isset($_GET['activated'] ) ) {

	require_once get_template_directory().'/lib/load-core.php';
	swift_fucntions();
	if(!get_option('SwiftOptions')){

		GLOBAL $swift_options_init;
		foreach ( $swift_options_init as $option ) {
			if( $option['datatype'] != 'none')
			$sanitized_options[ $option['id'] ] = $option['default'];
		}
		//update_option('swift_options',$sanitized_options);
		$temp['site_options'] = $sanitized_options;

		GLOBAL $swift_design_options_init;
		foreach ( $swift_design_options_init as $option ) {
			if( $option['datatype'] != 'none'){
			$sanitized_options[ $option['id'] ] = $option['default'];
			}elseif( $option['datatype'] == 'font' ){
			$sanitized_options[ $option['id'].'_enable' ] = $font_defaults[$option['id'].'_enable'];
			$sanitized_options[ $option['id'].'_family' ] = $font_defaults[$option['id'].'_family'];
			$sanitized_options[ $option['id'].'_size' ] = $font_defaults[$option['id'].'_size'];
			$sanitized_options[ $option['id'].'_size_unit' ] = $font_defaults[$option['id'].'_size_unit'];
			$sanitized_options[ $option['id'].'_lh' ] = $font_defaults[$option['id'].'_lh'];
			$sanitized_options[ $option['id'].'_lh_unit' ] = $font_defaults[$option['id'].'_lh_unit'];
			$sanitized_options[ $option['id'].'_weight' ] = $font_defaults[$option['id'].'_weight'];
			$sanitized_options[ $option['id'].'_transform' ] = $font_defaults[$option['id'].'_transform'];
			$sanitized_options[ $option['id'].'_style' ] = $font_defaults[$option['id'].'_style'];
			}
		}
		//update_option('swift_design_options',$sanitized_options);
		$temp['design_options'] = $sanitized_options;
		update_option('SwiftOptions', $temp);
	}

	header( 'Location: '.admin_url().'admin.php?page=swift-help');
}

function download_css_js(){
	if (( $creds = request_filesystem_credentials('', '', false,false, null ) ) ) {
		if ( ! WP_Filesystem($creds) ) {
		// our credentials were no good, ask the user for them again
		return;

		}
	}


	if( isset($_POST['download_css']) ){

	header("Cache-Control: public");
	header("Content-Description: File Transfer");
	header("Content-Disposition: attachment; filename=custom-styles.css");
	header("Content-Type: application/octet-stream; ");
	header("Content-Transfer-Encoding: binary");


	echo swift_generate_stylesheet();
}

if( isset($_POST['download_js']) ){
	header("Cache-Control: public");
	header("Content-Description: File Transfer");
	header("Content-Disposition: attachment; filename=swift-scripts.js");
	header("Content-Type: application/octet-stream; ");
	header("Content-Transfer-Encoding: binary");

	echo swift_generate_js();
}
}
?>