<?php
  
class ffOptionsInstall{

    function replace($content){
        global $wpdb;
        $prefix = $wpdb->prefix;
        $tablename = $prefix.'gen_options';

        $to_replace = array();
        $replace_with = array();

        $to_replace[]   = 'XXXTEMPLATEURLXXX';
        $replace_with[] = get_template_directory_uri();

        $to_replace[]   = 'XXXTABLENAMEXXX';
        $replace_with[] = $tablename;

        $content = str_replace( $to_replace, $replace_with, $content);

        return $content;
    }

    function runSQLfromFile( $file_path ){
        $content = file_get_contents($file_path);
        $content = ffOptionsInstall::replace($content);
        mysql_query( $content );
    }
    
    function insertSlider(){

        $slider = (object) array();
        $slider->name = 'default';
        $slider->typeName = 'Default';
        $slider->options = (object) array(
             'slider_type' => 'Default',
             'slider-autoslide-enable' => 1,
             'slider-autoslide-interval' => '5000',
        );

        $slider->slides = array();
        
        for ($i=1;$i<8;$i++) {
            $slider->slides[] = (object) array(
                 'imageUrl' => get_template_directory_uri().'/images/default/placeholder.gif',
                 'imageLink' => get_template_directory_uri().'/images/default/placeholder.gif',
                 'action' => NULL,
                 'title' => 'Slide #'.$i.' Title',
                 'description' => 'Slide #'.$i.' Description',
                 'transition' => NULL,
                 'clickAction' => 'url',
            );
        }

        global $wpdb;
        $prefix = $wpdb->prefix;
        $tablename = $prefix.'gen_options';
        
        $data = $wpdb->escape( serialize( $slider ) );

        $data = str_replace(
                    'O:8:"stdClass":4:{',
                    'O:30:"fSLiderManagerSliderDataObject":4:{',
                    $data
        );

        $data = str_replace(
                    'O:8:"stdClass":7:{',
                    'O:19:"fSliderManagerSlide":7:{',
                    $data
        );

        $SQL = "INSERT IGNORE INTO `$tablename` (`namespace`, `name`, `value`) VALUES (".
               "'slidermanager_sliders', 'default', '$data' )";

        mysql_query( $SQL );
    }
    
    function defaults(){
        ffOptionsInstall::runSQLfromFile( dirname(__FILE__).'/data/tables.frs' );
        ffOptionsInstall::runSQLfromFile( dirname(__FILE__).'/data/db_all_default.frs' );
        ffOptionsInstall::insertSlider();
    }
}