<?php
/**
 * @package ZBefore_After
 * @version 2.1
 */

/*
Plugin Name: Before AFter
Plugin URI: http://zachfine.com/beforeafter
Description: This is hardly a plugin. It's just a way to get Wordpress to load the files required for "Catch My Fame"'s jQuery Before/After. Also now fixed to not clobber Wordpress's jquery Plugin. Created on 7/16/2010
Author: "Catch My Fame" admin@catchmyfame.com (for original jQuery Before/After Plugin), Zach Fine zach@zachfine.com (some alterations + wrapping that plugin into this Wordpress plugin
Version: 2.0
Author URI: http://zachfine.com/blog
*/

if (!class_exists("BeforeAfterPlugin")) {
    class BeforeAfterPlugin {
        function addBeforeAfterPlugin () {

// changed the selector here to look for the div's class rather than id.


?>

<script type="text/javascript">
//because of noconflict mode
var $j = jQuery.noConflict();

$j(function(){
   $j('div.container').each(function (index) {
      var $div = $j(this);

// I initially set a series of variables like the following and
// passed them to div.beforeAfter:
// var $wipeTo = $(this).attr("data-wipeto");

// but then I decided it's cleaner and more flexible to iterate
// through all custom attributes and send them all as arguments to
// beforeAfter

// set up object in which to store options pulled from the
// custom attributes in the html:
     var userOptions = {};

// iterate through all custom attributes in the div and add them to
// the userOptions object.

     $j.each(this.attributes, function(i, attrib) {
         if( /^data-/.test(attrib.name) ) {
          userOptions[attrib.name.replace(/data-/g, '')] = attrib.value;
          }
     });

// Adding just one more option to the object of arguments:
      userOptions['imagepath'] = '<?php echo WP_PLUGIN_URL . "/beforeafter/js/"; ?>';

// console.log("serialized: "+$.param(userOptions)); //for testing

      $div.beforeAfter(userOptions);
   });
});
</script>

            <?php
      }
    }
} //endif (!class_exists("BeforeAfterPlugin"))

if (class_exists("BeforeAfterPlugin")) {
    $ba_plugin = new BeforeAfterPlugin();
}

if (isset($ba_plugin)) {
// Load required scripts

//needs a change here, should check version of jquery before loading a different version
//Z re-enabled the deregister and register of jquery here as the plugin had stopped working.
//    wp_deregister_script(array('jquery'));


//Z disabled the following 3 lines as they're no longer needed and conflict with other tools/plugins
//    wp_enqueue_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');
//    wp_enqueue_script('jquery');
//    wp_enqueue_script('jquery-ui', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js', array('jquery'), '1.7.1' );


    wp_enqueue_script('beforeafter.js', WP_PLUGIN_URL . '/beforeafter/js/jquery.beforeafterZ.js', array('jquery','jquery-ui') );
//    wp_enqueue_script('beforeafter.js', 'http://www.catchmyfame.com/jquery/jquery.beforeafter.min.js', array('jquery','jquery-ui') );

// Add bit of javascript to header, with a very low priority (9) so
// that it gets added after the scripts the define the functions it
// uses have already been added.
    add_action('wp_head',array(&$ba_plugin, 'addBeforeAfterPlugin'), 9);

}

?>