<?php

add_action( 'after_setup_theme', 'awesome_theme_setup' );

function awesome_theme_setup() {

	global $themename, $shortname;
	
	$themename = "Awesome";
	$shortname = "awesome";

	/* Make Awesome available for translation.
	 * Translations can be added to the /languages/ directory.
	 */
	load_theme_textdomain( 'awesome', get_template_directory() . '/languages' );

	// Add theme support
	add_theme_support( 'menus' );
	add_theme_support( 'post-thumbnails' );
	
	// This theme uses wp_nav_menu() in one location.
	register_nav_menu( 'primary', __( 'Primary Menu', 'awesome' ) );			

	require('functions/hook.php');


}

/*********************** After Theme Setup **************************************/

/**
 * Get our wp_nav_menu() fallback, wp_page_menu(), to show a home link.
 */
function awesome_menu_args( $args ) {
	$args['show_home'] = true;
	return $args;
}
add_filter( 'wp_page_menu_args', 'awesome_menu_args' );

/**
 * Awesome menu fallback
 */
function awesome_menu_fallback() {
	require_once (get_template_directory() . '/includes/default-menu.php');
} 

/*-----------------------------------------------------------------------------------*/
/*	Javascsript
/*-----------------------------------------------------------------------------------*/

function awesome_scripts_function() {

	wp_deregister_script('jquery');	
	wp_register_script( 'aw_jquery', 'http://code.jquery.com/jquery.min.js', false, '1.4.2', true ); 
	wp_enqueue_script( 'aw_jquery' );	
	
	// Site js
	wp_register_script( 'mobilemenu', get_bloginfo( 'template_directory') . '/javascripts/jquery.mobilemenu.js', false, '', true );  
	wp_register_script( 'scrollto', get_bloginfo( 'template_directory') . '/javascripts/jquery.scrollTo.js', false, '1.4.3', true );  
	wp_register_script( 'nav', get_bloginfo( 'template_directory') . '/javascripts/jquery.nav.js', false, '0.9', true );  
	wp_register_script( 'quicksand', get_bloginfo( 'template_directory') . '/javascripts/jquery.quicksand.js', false, '1.2.2', true );  
	wp_register_script( 'flexslider', get_bloginfo( 'template_directory') . '/javascripts/jquery.flexslider.js', false, '2.1', true );  
	wp_register_script( 'custom', get_bloginfo( 'template_directory') . '/javascripts/custom.js', false, '1.4.2', true ); 
	wp_register_script( 'rundonload', get_bloginfo( 'template_directory') . '/javascripts/runonload.js.js', false, '1.4.2', true ); 
	wp_register_script( 'awesome-cform', get_bloginfo( 'template_directory') . '/javascripts/awesome-cform.js', false, '1.4.2', true ); 

	wp_enqueue_script( 'mobilemenu' );		
	wp_enqueue_script( 'scrollto' );		
	wp_enqueue_script( 'nav' );		
	wp_enqueue_script( 'quicksand' );		
	wp_enqueue_script( 'flexslider' );		
	wp_enqueue_script( 'custom' );		
	wp_enqueue_script( 'rundonload' );		
	wp_enqueue_script( 'awesome-cform' );		
	
} 
add_action('wp_enqueue_scripts','awesome_scripts_function');

/* Add Script in Admin */
function awesome_admin_register() {

	wp_register_style( 'awesome-admin-style', get_template_directory_uri().'/stylesheets/admin-styles.css', array(), '20120208', 'all' ); 	
	wp_enqueue_style( 'awesome-admin-style' );

}
add_action( 'admin_enqueue_scripts', 'awesome_admin_register' ); 

/**
 *
 *	FUNCTIONS
----------------------------------------------------------------------------------------------------------- */ 
	
/* Include Post Type Function */
require_once (get_template_directory() . '/functions/post-type.php');	
	
/* Meta Boxes Function */
// require_once (get_template_directory() . '/functions/meta-boxes.php');	
// require_once (get_template_directory() . '/functions/meta-boxes-tagline-page.php');	

/* Meta Slider */
require_once (get_template_directory() . '/functions/meta-slider.php');	

/* Meta Page */
require_once (get_template_directory() . '/functions/meta-page.php');	

/* Meta Price Tabble */
require_once (get_template_directory() . '/functions/meta-pricetable.php');	

// Include shortcodes function
require_once (get_template_directory() . '/includes/shortcodes/zilla-shortcodes.php');

/* Include Theme Options */
if ( !function_exists( 'optionsframework_init' ) ) {
	define( 'OPTIONS_FRAMEWORK_DIRECTORY', get_template_directory_uri() . '/admin/' );
	require_once dirname( __FILE__ ) . '/admin/options-framework.php';
}

/** 
* Post Excerpt Limit
**/
// Limit post content or excerpt
function awesome_excerpt($limit) {
  $excerpt = explode(' ', get_the_excerpt(), $limit);
  if (count($excerpt)>=$limit) {
	array_pop($excerpt);
	$excerpt = implode(" ",$excerpt);
  } else {
	$excerpt = implode(" ",$excerpt);
  }	
  $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
  return $excerpt;
}

/** 
 * Custom Image Size
**/
if ( function_exists('add_image_size')) {
	add_image_size( 'full-size',  9999, 9999, false );
	add_image_size( 'slider-image', 580, 300, false );
	add_image_size( 'featured-image', 482, 235, true );
	add_image_size( 'post-thumb-grid', 298, 148, true );
	add_image_size( 'portfolio-thumb', 212, 140, true );
}

/**
 * Sidebars
**/
if ( function_exists('register_sidebar') )
	register_sidebar(array(
		'name' => 'Home Sidebar',
		'id' => 'home-sidebar',
		'description' => __('Widgets in this area will be shown in the homepage.','awesome'),
		'before_widget' => '<div  id="%1$s" class="home-sidebar">',
		'after_widget' => '</div>',
		'before_title' => '<h4 class="sidebar-title">',
		'after_title' => '</h4>',
));

/**
 * Hooks
**/

function awesome_contact_script() {
	do_action('awesome_contact_script');
}

add_action('awesome_contact_script', 'contact_form_functions', 1);
function contact_form_functions() { ?>

	<!-- this is from wp_hook -->

<?php 
//If the form is submitted
if(isset($_POST['submitted'])) {

	//Check to see if the honeypot captcha field was filled in
	if(trim($_POST['checking']) !== '') {
		$captchaError = true;
	} else {
	
		//Check to make sure that the name field is not empty
		if(trim($_POST['contactName']) === '') {
			$nameError = 'You forgot to enter your name.';
			$hasError = true;
		} else {
			$name = trim($_POST['contactName']);
		}
		
		//Check to make sure sure that a valid email address is submitted
		if(trim($_POST['email']) === '')  {
			$emailError = 'You forgot to enter your email address.';
			$hasError = true;
		} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
			$emailError = 'You entered an invalid email address.';
			$hasError = true;
		} else {
			$email = trim($_POST['email']);
		}
			
		//Check to make sure comments were entered	
		if(trim($_POST['comments']) === '') {
			$commentError = 'You forgot to enter your comments.';
			$hasError = true;
		} else {
			if(function_exists('stripslashes')) {
				$comments = stripslashes(trim($_POST['comments']));
			} else {
				$comments = trim($_POST['comments']);
			}
		}
			
		//If there is no error, send the email
		if(!isset($hasError)) {

			$mailadmin = of_get_option('aw_mailadmin'); 
		
			$emailTo = $mailadmin;
			$subject = 'Contact Form Submission from '.$name;
			$sendCopy = trim($_POST['sendCopy']);
			$body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
			$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
			
			mail($emailTo, $subject, $body, $headers);

			if($sendCopy == true) {
				$subject = 'You emailed Your Name';
				$headers = 'From: Your Name <kharisblank@gmail.com>';
				mail($email, $subject, $body, $headers);
			}

			$emailSent = true;

		}
	}
} ?>
	
<?php }

?>