<?php
/**
 * Shashin is a WordPress plugin for displaying Picasa, Flickr, and Twitpic photos in WordPress.
 *
 * @author Michael Toppa
 * @version 3.0
 * @package Shashin
 * @subpackage Classes
 *
 * Copyright 2007-2009 Michael Toppa
 *
 * Shashin is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Shashin is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
/**
 * The main class - directs traffic for all incoming requests.
 *
 * @author Michael Toppa
 * @package Shashin
 * @subpackage Classes
 */

class Shashin {
    public $options;
    public $name = 'Shashin';
    public $version = '3.0';
    public $faq_url = 'http://www.toppa.com/shashin-wordpress-plugin/';
    public $picasa_sizes = array(32, 48, 64, 72, 144, 160, 200, 288, 320, 400, 512, 576, 640, 720, 800);
    public $picasa_crop = array(32, 48, 64, 160);
    public $flickr_sizes = array(75, 100, 240, 500, 1024);
    public $flickr_crop = array(75);
    public $twitpic_sizes = array(75, 150, 600);
    public $twitpic_crop = array(75, 150);

    /**
     * Get options, register hooks.
     *
     * @access public
     * @uses initAdminMenus
     * @uses getHeadTags
     */
    public function __construct() {
        global $wpdb;
        $this->options = unserialize(get_option('shashin_options'));
        load_plugin_textdomain('shashin', false, basename(SHASHIN_DIR) . '/languages/');

        // add actions and hooks
        add_action('admin_menu', array($this, 'initAdminMenus'));
        add_action('template_redirect', array($this, 'getHeadTags'));
        register_deactivation_hook(__FILE__, array($this, 'unscheduleUpdate'));

    }

    /**
     * Adds the Shashin management and option pages.
     *
     * @static
     * @access public
     * @uses getAdminMenu()
     * @uses getOptionsMenu()
     */
    public function initAdminMenus() {
        require_once(SHASHIN_DIR . '/ShashinAdmin.phl');
        $admin = new ShashinAdmin($this);
        $options_page = add_options_page('Shashin', 'Shashin', 'manage_options', 'ShashinBoot', array($admin, 'getSettingsMenu'));
        // from http://planetozh.com/blog/2008/04/how-to-load-javascript-with-your-wordpress-plugin/
        add_action("admin_print_styles-$options_page", array($admin, 'getHeadTags'));
        $tools_page = add_management_page('Shashin', 'Shashin', 'edit_posts', 'ShashinBoot', array($admin, 'getToolsMenu'));
        add_action("admin_print_styles-$tools_page", array($admin, 'getHeadTags'));
        return true;
    }
    
    /**
     * Removes the hook for scheduled album syncing.
     *
     * @access public
     */
    public function unscheduleUpdate() {
        wp_clear_scheduled_hook('shashin_scheduled_update_hook');
    }    

    /**
     * Gets the Shashin CSS file, and the optional Highslide CSS and
     * JS, for inclusion in the document head. Will get a copy of shashin.css
     * from your theme directory if you put a custom one there.
     *
     * @access public
     */
    public function getHeadTags() {
        $shashin_options = unserialize(SHASHIN_OPTIONS);

        if (file_exists(TEMPLATEPATH . '/shashin.css')) {
            $shashin_css = get_bloginfo('template_directory') . '/shashin.css';
        }

        else {
            $shashin_css = SHASHIN_DISPLAY_URL . '/shashin.css';
        }

        wp_enqueue_style('shashin_css', $shashin_css, false, SHASHIN_VERSION);

        if ($shashin_options['image_display'] == 'highslide') {
            if (file_exists(TEMPLATEPATH . '/highslide.css')) {
                $highslide_css = get_bloginfo('template_directory') . '/highslide.css';
            }

            else {
                $highslide_css = SHASHIN_DISPLAY_URL . '/highslide.css';
            }

            wp_enqueue_style('highslide_css', $highslide_css, false, '4.1.4');
            wp_enqueue_script('highslide', SHASHIN_DISPLAY_URL . '/highslide/highslide.js', false, '4.1.4');
            wp_enqueue_script('swfobject', SHASHIN_DISPLAY_URL . '/highslide/swfobject.js', false, '2.1');
            wp_enqueue_script('highslide_settings', SHASHIN_DISPLAY_URL . '/highslide_settings.js', false, SHASHIN_VERSION);
            wp_localize_script('highslide_settings', 'highslide_settings', array(
                'graphics_dir' => SHASHIN_DISPLAY_URL . '/highslide/graphics/',
                'outline_type' => $shashin_options['highslide_outline_type'],
                'dimming_opacity' => $shashin_options['highslide_dimming_opacity'],
                'interval' => $shashin_options['highslide_interval'],
                'repeat' => $shashin_options['highslide_repeat'],
                'position' => $shashin_options['highslide_v_position'] . ' ' . $shashin_options['highslide_h_position'],
                'hide_controller' => $shashin_options['highslide_hide_controller']
            ));
        }
    }
}

?>
