<?php
/******************* DEFINE NAME/VERSION ********************/

$themename = "Wecare";
$themefolder = "wecare";

define ('theme_name', $themename );
define ('theme_ver' , 1 );

/************************************************************/


/******************login page image*********************/
function my_login_logo() { 
	?><style type="text/css">body.login div#login h1 a {background-size:auto auto;}</style><?php 
}
add_action( 'login_enqueue_scripts', 'my_login_logo' );

/********************* DEFINE MAIN PATHS ********************/

define('Wecare_PLUGINS',  get_template_directory() . '/addons' ); // Shortcut to the /addons/ directory
$adminPath 	=  get_template_directory() . '/library/admin-panel/';
$funcPath 	=  get_template_directory() . '/library/functions/';
$incPath 	=  get_template_directory() . '/library/includes/';

require_once ($funcPath . 'helper-functions.php');
require_once ($incPath . 'the_breadcrumb.php');
require_once ($incPath . 'OAuth.php');
require_once ($incPath . 'twitteroauth.php');
require_once ($incPath . 'portfolio_walker.php');
require_once ($funcPath . 'post-types.php');
require_once ($funcPath . 'widgets.php');
require_once ($funcPath . '/shortcodes/shortcode.php');

include (get_template_directory() . '/library/admin-panel/admin-ui.php');
include (get_template_directory() . '/library/admin-panel/pricing-tables.php');
include (get_template_directory() . '/library/admin-panel/admin-functions.php');
include (get_template_directory() . '/library/admin-panel/post-options.php');
include (get_template_directory() . '/library/admin-panel/custom-slider.php');
include (get_template_directory() . '/library/admin-panel/notifier/update-notifier.php');

/************************************************************/


/*********** LOAD ALL REQUIRED SCRIPTS AND STYLES ***********/
function wecare_load_scripts()
{
	if( $GLOBALS['pagenow'] != 'wp-login.php' && !is_admin())
	{           
		wp_enqueue_style('main-styles', get_stylesheet_directory_uri().'/style.css');
		wp_enqueue_style('zebra',  get_template_directory_uri().'/css/zebra.css');
		wp_enqueue_style('responsive-styles',  get_template_directory_uri().'/css/responsive.css');
		wp_enqueue_style('fancybox',  get_template_directory_uri().'/css/jquery.fancybox.css');
		wp_enqueue_style('flex-slider-styles',  get_template_directory_uri().'/css/flexslider.css');
		wp_enqueue_style('bx-slider-styles',  get_template_directory_uri().'/css/jquery.bxslider.css');
		wp_enqueue_style('font-awesome',  get_template_directory_uri().'/addons/fontawesome/css/font-awesome.min.css');
		wp_enqueue_style('jplayer-styles',  get_template_directory_uri().'/js/jplayer/skin/pink.flag/jplayer.pink.flag.css',false,'3.0.1','all');
		wp_enqueue_style('dynamic-styles',  get_template_directory_uri().'/css/dynamic-styles.php');
        wp_enqueue_style('bootstrap',  get_template_directory_uri().'/css/bootstrap.css');
		
		// Register or enqueue scripts
		wp_enqueue_script('jquery');
		wp_enqueue_script('core', get_template_directory_uri() .'/js/core.js', array('jquery'), '3.2', true);
		wp_enqueue_script('zebra-datapicker', get_template_directory_uri() .'/js/zebra_datepicker.js');
		wp_enqueue_script('jquery-ui-custom', get_template_directory_uri() .'/js/jquery-ui-1.10.3.custom.min.js', array('jquery'), '3.2', true);
		// wp_enqueue_script('modernizr', get_template_directory_uri() .'/js/modernizr.custom.79639.js');
		wp_enqueue_script('jquery-cond',  get_template_directory_uri(). '/js/jquery.ba-cond.min.js', array('jquery'), '3.2', true);
		wp_enqueue_script('isotope', get_template_directory_uri() .'/js/jquery.isotope.min.js', array('jquery'), '3.2', true);
		wp_enqueue_script('jplayer-audio',  get_template_directory_uri().'/js/jplayer/jquery.jplayer.min.js', array('jquery'), '3.2', true);
        wp_enqueue_script('flex-slider', get_template_directory_uri().'/js/jquery.flexslider.js', array('jquery'), '3.2', true);
		wp_enqueue_script('bx-slider', get_template_directory_uri().'/js/jquery.bxslider.min.js', array('jquery'), '3.2', true);
		wp_enqueue_script('testimonial-script', get_template_directory_uri().'/js/jquery.quovolver.js', array('jquery'), '3.2', true);
		wp_enqueue_script('bootstrap',  get_template_directory_uri(). '/js/bootstrap.min.js', array('jquery'), '3.0.1' );
		wp_enqueue_script('fancybox',  get_template_directory_uri().'/js/jquery.fancybox.js', array('jquery'), '3.2', true);
		wp_enqueue_script('fancybox-buttons', get_template_directory_uri() .'/js/jquery.fancybox-buttons.js', array('jquery'), '3.2', true);
		wp_enqueue_script('flickr', get_template_directory_uri().'/addons/flickr/jflickrfeed.min.js', array('jquery'), '3.2', true);
		wp_enqueue_script('Validate',  get_template_directory_uri().'/js/jquery.validate.min.js', array('jquery'), '3.2', true);
		wp_enqueue_script('custom-scripts', get_template_directory_uri() .'/js/script.js', array('jquery'), '3.2', true);
		wp_enqueue_script('custom-scripts2', get_template_directory_uri() .'/js/custom.js', array('jquery'), '3.2', true);
                  
		if (is_page_template('contact-template.php')){
			$address = weblusive_get_option('contact_address');
			if (!empty($address))
			{
				wp_enqueue_script('Google-map-api',  'http://maps.google.com/maps/api/js?sensor=false');
				wp_enqueue_script('Google-map',  get_template_directory_uri().'/js/gmap3.min.js', array('jquery'), '3.2', true);
			}			
		}		
		if (is_page_template('under-construction.php'))
		{
			wp_enqueue_script('Under-construction',  get_template_directory_uri().'/js/jquery.countdown.js', array('jquery'), '3.2', true);
		}
	}
}
add_action( 'wp_enqueue_scripts', 'wecare_load_scripts' ); //Load All Scripts

// Load Google Fonts
function Wecare_fonts() {
    $protocol = is_ssl() ? 'https' : 'http';
    wp_enqueue_style( 'Wecare-opensans', "$protocol://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,400,700,600,300" );
    wp_enqueue_style( 'Wecare-oswald', "$protocol://fonts.googleapis.com/css?family=Oswald:400,300,700" );
    wp_enqueue_style( 'Wecare-philosopher', "$protocol://fonts.googleapis.com/css?family=Philosopher:400,700,400italic" );
    }
add_action( 'wp_enqueue_scripts', 'Wecare_fonts' );

/************************************************************/
/*-----------------------------------------------------------------------------------*/
# Typography Elements Array
/*-----------------------------------------------------------------------------------*/
$custom_typography = array(
	"body"                                                       =>	"typography_general",
	".logo a.brand"                                              =>	"typography_site_title",
	".sf-menu > li > a"                                          =>	"typography_main_nav",
	".note, .recent-projects > h4, .latest-blog>h4, .title-block"=>"typography_page_title",
	".site-map, .site-map a"                                     => "typography_breadcrumbs",
	"#sidebar .widget h3"                                        =>	"typography_widgets_title",
	".footer-block h4"                                           =>	"typography_footer_widgets_title",
	"h1"                                                         =>	"typography_heading1",
	"h2"                                                         =>	"typography_heading2",
	"h3"                                                         =>	"typography_heading3",
	"h4"                                                         =>	"typography_heading4",
	"h5"                                                         =>	"typography_heading5",
);
	
add_action('wp_enqueue_scripts', 'weblusive_typography');
function weblusive_typography(){
	global $custom_typography;
        require get_template_directory() . '/library/admin-panel/google-fonts-function.php';
	foreach( $custom_typography as $selector => $value){
		$option = weblusive_get_option( $value );
		
		if( !empty($option['font']))
			weblusive_enqueue_font( $option['font'] ) ;
	}
}

add_action( 'after_setup_theme', 'wecare_setup' );
function wecare_setup() {

	/************** ADD SUPPORT FOR LOCALIZATION ***************/

	load_theme_textdomain( 'Wecare',  get_template_directory() . '/languages' );

		$locale = get_locale();

		$locale_file =  get_template_directory() . "/languages/$locale.php";
		if ( is_readable( $locale_file ) )
			require_once( $locale_file );

	/************************************************************/


	/************* ADD SUPPORT FOR WORDPRESS 3 MENUS ************/

	add_theme_support( 'menus' );

	//Register Navigations
	add_action( 'init', 'my_custom_menus' );
	function my_custom_menus() {
		register_nav_menus(
			array(
				'top_nav'     => __('Top Navigation', 'Wecare'),
				'primary_nav' => __( 'Primary Navigation', 'Wecare'),
			)
		);
	}
	/************************************************************/


	/**************** ADD SUPPORT FOR POST THUMBS ***************/

	add_theme_support( 'post-thumbnails');

	// Define various thumbnail sizes
	add_image_size('portfolio-4-col', 270, 190, true);
	add_image_size('portfolio-3-col', 370, 282, true); 
	add_image_size('blog-list', 275, 193, true);
	add_image_size('blog-thumb', 59, 59, true);
	add_image_size('blog-thumb-3', 270, 180, true);

	/************************************************************/
}

/******* FIX THE PORTFOLIO CATEGORY PAGINATION ISSUE ********/

$option_posts_per_page = get_option( 'posts_per_page' );
add_action( 'init', 'my_modify_posts_per_page', 0);
function my_modify_posts_per_page() {
    add_filter( 'option_posts_per_page', 'my_option_posts_per_page' );
}
function my_option_posts_per_page( $value ) {
    global $option_posts_per_page;
    if ( is_tax( 'portfolio_category') ) {
		$pageId = get_page_ID_by_page_template('portfolio-template3.php');
		if ($pageId)
		{
			$custom =  get_post_custom($pageId);
			$items_per_page = isset ($custom['_page_portfolio_num_items_page']) ? $custom['_page_portfolio_num_items_page'][0] : '777';
			return $items_per_page;
		}
		else
		{
			return 4;
		}
    } else {
        return $option_posts_per_page;
    }
}

/************************************************************/


function weblusive_get_option( $name ) {
	$get_options = get_option( 'weblusive_options' );
	
	if( !empty( $get_options[$name] ))
		return $get_options[$name];
		
	return false ;
}

//Docs Url
$docs_url = "http://weblusive-themes.com/documentation/".$themefolder;

// Redirect To Theme Options Page on Activation
if (is_admin() && isset($_GET['activated'])){
	wp_redirect(admin_url('admin.php?page=panel'));
}

/************************************************************/


/****************** REGISTER SIDEBARS ***********************/

if ( function_exists('register_sidebar') )
{
    $footer_widget_count = weblusive_get_option('footer_widgets');
	if($footer_widget_count !== 'none')
	{
		$columns = 'column3';
		switch($footer_widget_count)
		{
			case '4':
			$columns = 'column3';
			break;
			case '3':
			$columns = 'column4';
			break;
			case '2':
			$columns = 'column6';
			break;
		}
		for($i = 1; $i<= $footer_widget_count; $i++)
		{
			unregister_sidebar('Footer Widget '.$i);
			if ( function_exists('register_sidebar') )
			register_sidebar(array(
				'name' => 'Footer Widget '.$i,
				'id'	=> 'footer-sidebar-'.$i,
				'before_widget' => '<div class="'.$columns.' widget footer-block">',
				'after_widget' => '</div>',
				'before_title' => '<h3>',
				'after_title' => '</h3>',
			));
		}
	}
}

add_filter('widget_text', 'do_shortcode');
add_filter('the_excerpt', 'do_shortcode');

add_action( 'widgets_init', 'weblusive_widgets_init' );
function weblusive_widgets_init() {
	$before_widget =  '<div id="%1$s" class="widget %2$s">';
	$after_widget  =  '</div></div>';
	$before_title  =  '<h3>';
	$after_title   =  '</h3><div class="widget-container">';
					
	register_sidebar( array(
		'name' =>  __( 'Primary Widget Area', 'Wecare' ),
		'id' => 'primary-widget-area',
		'description' => __( 'The Primary widget area', 'Wecare' ),
		'before_widget' => $before_widget , 'after_widget' => $after_widget , 'before_title' => $before_title , 'after_title' => $after_title ,
	) );
	
	register_sidebar( array(
		'name' =>  __( 'Homepage top sidebar', 'Wecare' ),
		'id' => 'homepage-top-sidebar',
		'description' => __( 'Homepage top sidebar', 'Wecare' ),
		'before_widget' => '<div class="homepage-top-sidebar">' , 'after_widget' => '</div>' , 'before_title' => '' , 'after_title' => '' ,
	) );

	//Custom Sidebars
	$sidebars = weblusive_get_option( 'sidebars' ) ;
	if($sidebars){
		foreach ($sidebars as $sidebar) {
			register_sidebar( array(
				'name' => $sidebar,
				'id' => sanitize_title($sidebar),
				'before_widget' => $before_widget , 'after_widget' => $after_widget , 'before_title' => $before_title , 'after_title' => $after_title ,
			) );
		}
	}
}

/************************************************************/


/****************** CUSTOM LOGIN LOGO ***********************/

function weblusive_login_logo(){
	if( weblusive_get_option('dashboard_logo') )
    echo '<style  type="text/css"> h1 a {  background-image:url('.weblusive_get_option('dashboard_logo').')  !important; } </style>';  
}  
add_action('login_head',  'weblusive_login_logo'); 

/************************************************************/


/******************** CUSTOM GRAVATAR ***********************/

function weblusive_custom_gravatar ($avatar) {
	$weblusive_gravatar = weblusive_get_option( 'gravatar' );
	if($weblusive_gravatar){
		$custom_avatar = weblusive_get_option( 'gravatar' );
		$avatar[$custom_avatar] = "Custom Gravatar";
	}
	return $avatar;
}
add_filter( 'avatar_defaults', 'weblusive_custom_gravatar' ); 

/************************************************************/


/********************* CUSTOM TAG CLOUDS ********************/

function wecare_custom_tag_cloud_widget($args) {
	$args['number'] = 0; //adding a 0 will display all tags
	$args['largest'] = 18; //largest tag
	$args['smallest'] = 12; //smallest tag
	$args['unit'] = 'px'; //tag font unit
	$args['format'] = 'list'; //ul with a class of wp-tag-cloud
	return $args;
}
add_filter( 'widget_tag_cloud_args', 'wecare_custom_tag_cloud_widget' );

/************************************************************/


/****************** ENABLE SESSIONS *************************/

function cp_admin_init() {
	if (!session_id())
	session_start();
}

add_action('init', 'cp_admin_init');

/************************************************************/


/******************* FILL EMPTY WIDGET TITLE ****************/

function wecare_fill_widget_title($title){
	if( empty( $title ) )
		return ' ';
	else return $title;
}
add_filter('widget_title', 'wecare_fill_widget_title');

add_theme_support( 'automatic-feed-links' );
if ( ! isset( $content_width ) ) $content_width = 960;

/************************************************************/
?>
