<?php

/* * ************************* Localisation ************************** */

load_theme_textdomain('gp_lang', TEMPLATEPATH . '/languages');
$locale = get_locale();
$locale_file = TEMPLATEPATH . "/languages/$locale.php";
if (is_readable($locale_file))
    require_once($locale_file);


/* * ************************* Theme Information ************************** */

$themename = __('Score', 'gp_lang'); // Theme Name
$dirname = "score"; // Directory Name

/* * ************************* Dedication Information ********************* */

if ( !defined( 'DEDICATION_CATEGORY_SLUG' ) )
    define( 'DEDICATION_CATEGORY_SLUG', 'dedication' );


/* * ************************* File Directories ************************** */

define("ghostpool", TEMPLATEPATH . '/');
define("ghostpool_inc", TEMPLATEPATH . '/lib/inc/');
define("ghostpool_scripts", TEMPLATEPATH . '/lib/scripts/');
define("ghostpool_admin", TEMPLATEPATH . '/lib/admin/inc/');
define("ghostpool_bp", TEMPLATEPATH . '/lib/buddypress/');
define("BP_THEME_URL", get_template_directory_uri());


/* * ************************* Additional Functions ************************** */

// Main Theme Options
require_once(ghostpool_admin . 'theme-options.php');
require(ghostpool_inc . 'options.php');

// Meta Options
require_once(ghostpool_admin . 'theme-meta-options.php');

// Widgets
require_once(ghostpool_admin . 'theme-widgets.php');

// Sidebars
require_once(ghostpool_admin . 'theme-sidebars.php');

// Shortcodes
require_once(ghostpool_admin . 'theme-shortcodes.php');

// Custom Post Types
require_once(ghostpool_admin . 'theme-post-types.php');

// TinyMCE
require_once (ghostpool_admin . 'tinymce/tinymce.php');

// Update Notification
require_once(ghostpool_admin . 'theme-update-notification.php');

// WP Show IDs
require_once(ghostpool_admin . 'wp-show-ids/wp-show-ids.php');

// Import/Export Widgets
require_once(ghostpool_admin . 'widget-settings-importexport/widget_data.php');

// Image Resizer
require_once(ghostpool_scripts . 'image-resizer.php');

// Follow Item
if ($theme_reviews_follow == "0") {
    require_once(ghostpool_scripts . 'wp-favorite-posts/wp-favorite-posts.php');
}

// BuddyPress Functions
if (function_exists('bp_is_active') && file_exists(ghostpool_bp . 'functions-buddypress.php')) {
    require_once(ghostpool_bp . 'functions-buddypress.php');
}

/* * ************************* Flags img function *********************** */
function gp_get_flag_img($country_code = '', $flagsize=32) {
    $src = get_template_directory_uri() . '/lib/images/flags/'.$flagsize.'/';
    if ($country_code) {
        $src .= strtolower($country_code) . '.png';
    } else {
        $src .= 'none.png';
    }
    return '<img class="dedicationFlag" src="' . $src . '" alt="' . $country_code . '"/>';     
}

function gp_get_the_flag($userid, $flagsize=32) {    
    if ($userid) {
        if (bp_has_profile('user_id=' . $userid)) {
            while (bp_profile_groups()) {
                bp_the_profile_group();
                while (bp_profile_fields()) {
                    bp_the_profile_field();
                    if (bp_field_has_data()) {
                        $fieldname = strip_tags(bp_get_the_profile_field_name());
                        if ($fieldname == 'Country') {                        
                            $value = strip_tags(bp_get_the_profile_field_value(), '<img>');
                            $value = str_replace('/flags/32/', '/flags/'.$flagsize.'/', $value);
                            if ($value == '') {
                                return gp_get_flag_img('', $flagsize); // Bandera desconocida
                            } else {
                                return $value;
                            }
                        }
                    }
                }
            }
        }
    } else {
        return gp_get_flag_img('', $flagsize); // Bandera desconocida 
    }
}


function gp_the_flag($userid, $flagsize=32) {
    echo gp_get_the_flag($userid, $flagsize);
}

/* * ************************* Enqueue Styles ************************** */

function gp_enqueue_styles() {
    if (!is_admin()) {

        global $post;
        require(ghostpool_inc . 'options.php');

        wp_enqueue_style('reset', get_template_directory_uri() . '/lib/css/reset.css');

        wp_enqueue_style('style', get_template_directory_uri() . '/style.css');

        wp_enqueue_style('prettyphoto', get_template_directory_uri() . '/lib/scripts/prettyPhoto/css/prettyPhoto.css');

        wp_enqueue_style('videoslider', get_template_directory_uri() . '/lib/scripts/videoslider/css/videoGallery.css');

        if (isset($_COOKIE['SkinCookie'])) {
            $skin = $_COOKIE['SkinCookie'];
        }

        if (isset($_GET['skin'])) {
            $skin = $_GET['skin'];
            setcookie('SkinCookie', $skin);
        }

        if ($_GET['skin'] == "default") {
            $skin = $theme_skin;
            setcookie('SkinCookie', $skin, time() - 3600);
        }

        if (isset($_GET['skin']) OR (isset($_COOKIE['SkinCookie']) && $_COOKIE['SkinCookie'] != "default")) {

            wp_enqueue_style('style-skin', get_template_directory_uri() . '/style-' . $skin . '.css');
        } else {

            if ((is_singular() && !is_attachment() && !is_404()) && (get_post_meta($post->ID, 'ghostpool_skin', true) && get_post_meta($post->ID, 'ghostpool_skin', true) != "Default")) {

                wp_enqueue_style('style-skin', get_template_directory_uri() . '/style-' . get_post_meta($post->ID, 'ghostpool_skin', true) . '.css');
            } else {

                wp_enqueue_style('style-skin', get_template_directory_uri() . '/style-' . $theme_skin . '.css');
            }
        }

        if ($theme_custom_stylesheet) {

            wp_enqueue_style('style-theme-custom', get_template_directory_uri() . '/' . $theme_custom_stylesheet);
        }

        if ((is_single() OR is_page()) && get_post_meta($post->ID, 'ghostpool_custom_stylesheet', true)) {

            wp_enqueue_style('style-custom', get_template_directory_uri() . '/' . get_post_meta($post->ID, 'ghostpool_custom_stylesheet', true));
        }
    }
}

add_action('wp_print_styles', 'gp_enqueue_styles');


/* * ************************* Enqueue Scripts ************************** */

function gp_enqueue_scripts() {
    if (!is_admin()) {

        wp_enqueue_script('jquery');
        wp_enqueue_script('jqueryui');

        //wp_enqueue_script('jqueryui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js', array('jquery'));

        if (is_singular())
            wp_enqueue_script('comment-reply');

        wp_enqueue_script('swfobject', 'http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js');

        wp_enqueue_script('jwplayer', get_template_directory_uri() . '/lib/scripts/mediaplayer/jwplayer.js', array('jquery'));

        //wp_enqueue_script('carousel', get_template_directory_uri().'/lib/scripts/jquery.carouFredSel.js', array('jquery'));

        wp_enqueue_script('prettyphoto', get_template_directory_uri() . '/lib/scripts/prettyPhoto/js/jquery.prettyPhoto.js', array('jquery'));

        wp_enqueue_script('jqtransform', get_template_directory_uri() . '/lib/scripts/jquery.jqtransform.js', array('jquery'));

        wp_enqueue_script('jqueryeasing', get_template_directory_uri() . '/lib/scripts/videoslider/js/jquery.easing.1.3.js', array('jquery'));

        wp_enqueue_script('froogaloop', get_template_directory_uri() . '/lib/scripts/videoslider/js/froogaloop.min.js', array('jquery'));
        wp_enqueue_script('apPlaylistManager', get_template_directory_uri() . '/lib/scripts/videoslider/js/jquery.apPlaylistManager.min.js', array('jquery'));
        wp_enqueue_script('apYoutubePlayer', get_template_directory_uri() . '/lib/scripts/videoslider/js/jquery.apYoutubePlayer.js', array('jquery'));
        wp_enqueue_script('apVimeoPlayer', get_template_directory_uri() . '/lib/scripts/videoslider/js/jquery.apVimeoPlayer.js', array('jquery'));
        //wp_enqueue_script('videoGallery', get_template_directory_uri().'/lib/scripts/videoslider/js/jquery.videoGallery.min.js', array('jquery'));
        wp_enqueue_script('videoGallery', get_template_directory_uri() . '/lib/scripts/videoslider/js/jquery.videoGallery.js', array('jquery'));
        wp_enqueue_script('mousewheel', get_template_directory_uri() . '/lib/scripts/videoslider/js/jquery.mousewheel.js', array('jquery'));
        //wp_enqueue_script('sharrre', get_template_directory_uri() . '/lib/scripts/videoslider/js/jquery.sharrre-1.3.0.min.js', array('jquery'));
    }
}

add_action('wp_print_scripts', 'gp_enqueue_scripts');


/* * ************************* WP Header Hooks ************************** */

function gp_font_head() {
    require(ghostpool_inc . 'options.php');    

    $fonts = array(
        'Bad+Script' => "font-family: 'Bad Script', sans-serif;",
        'Give+You+Glory' => "font-family: 'Give You Glory', sans-serif;",
        'Handlee' => "font-family: 'Handlee', sans-serif;",
        'Indie+Flower' => "font-family: 'Indie Flower', sans-serif;",
        'Patrick+Hand' => "font-family: 'Patrick Hand', sans-serif;",
        'Shadows+Into+Light' => "font-family: 'Shadows Into Light', sans-serif;",
        'Shadows+Into+Light+Two' => "font-family: 'Shadows Into Light Two', sans-serif;",
    );

    $body_key = 'Bad+Script';

    if (isset($theme_font_dedication))
        $body_key = $theme_font_dedication;

    if (isset($fonts[$body_key])) {
        $body_font = $fonts[$body_key];

        wp_enqueue_style('googleFonts', 'http://fonts.googleapis.com/css?family=' . $theme_font_dedication);
        echo '<style>';
        echo '#dedicationTitle { ' . $body_font . ' } ';
        echo '</style>';
    }
}

function gp_wp_header() {

    require(ghostpool_inc . 'options.php');

    if ($theme_favicon_ico)
        echo '<link rel="shortcut icon" href="' . $theme_favicon_ico . '" /><link rel="icon" href="' . $theme_favicon_ico . '" type="image/vnd.microsoft.icon" />';

    if ($theme_favicon_png)
        echo '<link rel="icon" type="image/png" href="' . $theme_favicon_png . '" />';

    if ($theme_apple_icon)
        echo '<link rel="apple-touch-icon" href="' . $theme_apple_icon . '" />';


    if ($theme_custom_css)
        echo '<style>' . stripslashes($theme_custom_css) . '</style>';

    echo stripslashes($theme_scripts);

    echo '
	<!--[if lte IE 8]>
	<style>
	#page-shadow, #nav, .post-thumbnail, #footer-bottom, #footer-bottom .columns.separate > div {
		behavior: url("' . get_template_directory_uri() . '/lib/scripts/pie/PIE.php");
	}
	</style>
	<![endif]-->';

    gp_font_head();
}

add_action('wp_head', 'gp_wp_header');


/* * ************************* Featured Image Sizes ************************** */

add_theme_support('post-thumbnails');
set_post_thumbnail_size(150, 150, true);


/* * ************************* bbPress Support ************************** */

if (is_admin()) {
    add_theme_support('bbpress');
}


/* * ************************* Navigation Menus ************************** */

add_action('init', 'register_my_menus');

function register_my_menus() {
    register_nav_menus(array(
        'header-nav' => __('Header Navigation', 'gp_lang')
    ));
}

/* * ************************* Dedication category ********************* */

add_action('init', 'register_dedication_category');
function register_dedication_category() {   
    
    if (!get_category_by_slug(DEDICATION_CATEGORY_SLUG)) {
        $cat = array(
            'cat_name' => 'Dedication', 
            'category_nicename' => DEDICATION_CATEGORY_SLUG);
        
        wp_insert_category($cat);
    }
}


/* * ************************* Other Features ************************** */

// Add support for custom backgrounds
add_custom_background();

// This theme styles the visual editor with editor-style.css to match the theme style.
add_editor_style();

// Set the content width based on the theme's design and stylesheet.
if (!isset($content_width))
    $content_width = 590;

// Add default posts and comments RSS feed links to <head>.
add_theme_support('automatic-feed-links');


function remove_private_prefix($title) {
    $title = str_replace(
            'Private:', '', $title);
    return $title;
}

add_filter('the_title', 'remove_private_prefix');

/* * ************************* Excerpts ************************** */

// Excerpt Length
function new_excerpt_length($length) {
    return 500;
}

add_filter('excerpt_length', 'new_excerpt_length');

function 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;
}

// Replace excerpt ellipsis
function new_excerpt_more($more) {
    return '...';
}

add_filter('excerpt_more', 'new_excerpt_more');
remove_filter('the_excerpt', 'wpautop');


/* * ************************* Add Excerpt Support To Pages ************************** */

add_action('init', 'my_add_excerpts_to_pages');

function my_add_excerpts_to_pages() {
    add_post_type_support('page', 'excerpt');
}

/* * ************************* Shortcode Support For Text Widget ************************** */

add_filter('widget_text', 'do_shortcode');


/* * ************************* Page Navigation ************************** */

function gp_pagination($pages = '', $range = 2) {
    $showitems = ($range * 2) + 1;

    global $paged;

    if (get_query_var('paged')) {
        $paged = get_query_var('paged');
    } elseif (get_query_var('page')) {
        $paged = get_query_var('page');
    } else {
        $paged = 1;
    }

    if ($pages == '') {
        global $wp_query;
        $pages = $wp_query->max_num_pages;
        if (!$pages) {
            $pages = 1;
        }
    }

    if (1 != $pages) {
        echo "<div class='wp-pagenavi'>";
        echo '<span class="pages">' . __('Page', 'gp_lang') . ' ' . $paged . ' ' . __('of', 'gp_lang') . ' ' . $pages . '</span>';
        if ($paged > 2 && $paged > $range + 1 && $showitems < $pages)
            echo "<a href='" . get_pagenum_link(1) . "'>&laquo;</a>";
        if ($paged > 1 && $showitems < $pages)
            echo "<a href='" . get_pagenum_link($paged - 1) . "'>&lsaquo;</a>";

        for ($i = 1; $i <= $pages; $i++) {
            if (1 != $pages && (!($i >= $paged + $range + 1 || $i <= $paged - $range - 1) || $pages <= $showitems )) {
                echo ($paged == $i) ? "<span class='current'>" . $i . "</span>" : "<a href='" . get_pagenum_link($i) . "' class='inactive' >" . $i . "</a>";
            }
        }

        if ($paged < $pages && $showitems < $pages)
            echo "<a href='" . get_pagenum_link($paged + 1) . "'>&rsaquo;</a>";
        if ($paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages)
            echo "<a href='" . get_pagenum_link($pages) . "'>&raquo;</a>";
        echo "</div>\n";
    }
}

/* * ************************* Posts Per Page Fix ************************** */

function gp_posts_per_page($query_string) {

    require(ghostpool_inc . 'options.php');

    if (!is_admin()) {
        if ($page_var == "homepage") {
            $query_string['posts_per_page'] = $theme_homepage_posts_per_page;
        } elseif (is_page_template('blog.php')) {
            $query_string['posts_per_page'] = $theme_blog_posts_per_page;
        } else {
            $query_string['posts_per_page'] = $theme_cat_posts_per_page;
        }
    }
    return $query_string;
}

add_filter('request', 'gp_posts_per_page');


/* * ************************* Search Criteria ************************** */

function SearchFilter($query) {

    require(ghostpool_inc . 'options.php');

    if (!is_admin()) {
        if ($query->is_search) {
            if ($theme_search_criteria == "Review Posts Only") {
                $query->set('meta_key', 'ghostpool_post_type');
                $query->set('meta_value', 'Review');
            } elseif ($theme_search_criteria == "All Posts") {
                $query->set('post_type', 'post');
            }
        }
        return $query;
    }
}

add_filter('pre_get_posts', 'SearchFilter');


/* * ************************* Shortcode Empty Paragraph Fix ************************** */

// Plugin URI: http://www.johannheyne.de/wordpress/shortcode-empty-paragraph-fix/
add_filter('the_content', 'shortcode_empty_paragraph_fix');

function shortcode_empty_paragraph_fix($content) {
    $array = array(
        '<p>[' => '[',
        ']</p>' => ']',
        ']<br />' => ']'
    );

    $content = strtr($content, $array);

    return $content;
}

/* * ************************* Custom Media Gallery Field ************************** */

function ghostpool_attachment_fields_to_edit($form_fields, $post) {
    $form_fields["ghostpool_video_url"] = array(
        "label" => __('Audio/Video URL', 'gp_lang'),
        "input" => "text",
        "value" => get_post_meta($post->ID, "_ghostpool_video_url", true),
        "helps" => __('The URL of your video or audio file (YouTube/Vimeo/FLV/MP4/M4V/MP3).', 'gp_lang'),
    );
    return $form_fields;
}

add_filter("attachment_fields_to_edit", "ghostpool_attachment_fields_to_edit", null, 2);

function ghostpool_attachment_fields_to_save($post, $attachment) {
    if (isset($attachment['ghostpool_video_url'])) {
        update_post_meta($post['ID'], '_ghostpool_video_url', $attachment['ghostpool_video_url']);
    }
    return $post;
}

add_filter("attachment_fields_to_save", "ghostpool_attachment_fields_to_save", null, 2);


/* * ************************* Redirect to Theme Options after Activation ************************** */

if (is_admin() && isset($_GET['activated']) && $pagenow == "themes.php") {
    add_action('admin_head', 'ct_option_setup');
    header('Location: ' . admin_url() . 'themes.php?page=theme-options.php');
}
?>