// turn on noConflict mode
var $j = jQuery.noConflict();

// Taken from: http://ejohn.org/blog/simple-javascript-inheritance/
// Inspired by base2 and Prototype
(function(){
  var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;

  // The base Class implementation (does nothing)
  this.Class = function(){};
 
  // Create a new Class that inherits from this class
  Class.extend = function(prop) {
    var _super = this.prototype;
   
    // Instantiate a base class (but only create the instance,
    // don't run the init constructor)
    initializing = true;
    var prototype = new this();
    initializing = false;
   
    // Copy the properties over onto the new prototype
    for (var name in prop) {
      // Check if we're overwriting an existing function
      prototype[name] = typeof prop[name] == "function" &&
        typeof _super[name] == "function" && fnTest.test(prop[name]) ?
        (function(name, fn){
          return function() {
            var tmp = this._super;
           
            // Add a new ._super() method that is the same method
            // but on the super-class
            this._super = _super[name];
           
            // The method only need to be bound temporarily, so we
            // remove it when we're done executing
            var ret = fn.apply(this, arguments);       
            this._super = tmp;
           
            return ret;
          };
        })(name, prop[name]) :
        prop[name];
    }
   
    // The dummy class constructor
    function Class() {
      // All construction is actually done in the init method
      if ( !initializing && this.init ) {
        this.init.apply(this, arguments);
      }
    }
   
    // Populate our constructed prototype object
    Class.prototype = prototype;
   
    // Enforce the constructor to be what we expect
    Class.constructor = Class;

    // And make this class extendable
    Class.extend = arguments.callee;
   
    return Class;
  };
})();

// OnelessAct

var Panel = Class.extend({
    defaults: {
        panel_id: '#panel',
        button_id: '#panel_btn'
    },
    
    init: function(options) {
        var self = this;
        
        // merge defaults with options
        this.options = $j.extend({}, this.defaults, options);
        
        // bind button to panel
        $j(this.options.button_id).bind('click', function(e){

            // trigger panel update
            $j(document).trigger('panel_update.ola', [self]);
        });
    },
    
    isVisible: function() {
        return ($j(this.options.panel_id).css('display') != 'none') ? true : false;
    },
    
    show: function() {
        $j(this.options.button_id).parents('li').addClass('active');
        $j(this.options.panel_id).show();
    },
    
    hide: function() {
        $j(this.options.button_id).parents('li').removeClass('active');
        $j(this.options.panel_id).hide();
    }
});

var InfoPanelContainer = Class.extend({
    defaults: {
        panel_controller_id: '#panel_content',
        close_button_id: '#panel_close_btn'
    },
    
    init: function(options) {
        var self = this;
        
        // merge defaults with options
        this.options = $j.extend({}, this.defaults, options);
        
        // init panels
        self.panels = {
            about_panel: new Panel({panel_id: '#about_panel', button_id: '#about_btn'})
        };
        
        
        // bind actions
        self.current_panel = self.panels.about_panel; // set a default
        $j(document).bind('panel_update.ola', function(e, panel){
            self.panelUpdateHandler(panel);
        });

        $j(this.options.close_button_id).bind('click', function(e){
            self.closeHandler();
            return false;
        });
    },
    
    panelUpdateHandler: function(next_panel) {
        // check if this is a new panel
        var is_new_panel = (this.current_panel != next_panel ? true : false);
        
        // check if we need to swap panels
        if (is_new_panel) {
            this.current_panel.hide();
            this.current_panel = next_panel;
            this.current_panel.show();
        }
        
        // toggle panel container visibility if necessary
        if (!is_new_panel && this.isVisible()) {
            this.hide();
        } else if  (!this.isVisible()) {
            if (!this.current_panel.isVisible()) {
                this.current_panel.show();
            }
            this.show();
        }
        
    },
    
    closeHandler: function() {
        if (this.isVisible()) {
            this.hide();
        }
    },
        
    isVisible: function() {
        return ($j(this.options.panel_id).css('display') != 'none') ? true : false;
    },
    
    show: function() {
        $j(this.options.button_id).addClass('active');
        $j(this.options.panel_id).slideDown('normal', 'easeOutQuart');
    },
    
    hide: function() {
        $j(this.options.button_id).removeClass('active');
        $j(this.options.panel_id).slideUp('normal', 'easeInQuart');
        this.current_panel.hide();
    }
});

var TagPanel = Class.extend({
    defaults: {
        panel_id: '#panel_content',
        button_id: '#panel_btn'
    },
    
    init: function(options) {
        var self = this;
        
        // merge defaults with options
        this.options = $j.extend({}, this.defaults, options);
        
        // bind actions
        $j(this.options.button_id).bind('click', function(e){
            self.buttonHandler();
            return false;
        });
    },
    
    buttonHandler: function() {
        if (!this.isVisible()) {
            this.show();
        } else {
            this.hide();
        }
    },
    
    isVisible: function() {
        return (parseInt($j(this.options.panel_id).css('marginLeft')) == 0) ? true : false;
    },
    
    show: function() {
        $j(this.options.panel_id).animate({marginLeft:0}, 'fast', 'easeOutQuart');
    },
    
    hide: function() {
        $j(this.options.panel_id).animate({marginLeft:-$j(this.options.panel_id).outerWidth()}, 'fast', 'easeInQuart');
    }
});


var ProductFilter = Class.extend({
    defaults: {
        product_class: '.product',
        page_url: ''
    },
    
    init: function(options) {
        var self = this;
        
        // merge defaults with options
        this.options = $j.extend({}, this.defaults, options);
        
        // on document ready, filter products
        $j(document).ready(function(){
            $j(self.options.product_class).not(self.getFiltersAsString()).hide();

            // fix column spacing
            $j(self.options.product_class+':visible').each(function(index) {
                if (index % 3 == 2) {
                    $j(this).addClass('last');
                }
            });
            
            if ($j(self.options.product_class+':visible').length == 0) {
                $j('#no_products_found').show();
            }
            
        });
        
    },
    
    getFiltersAsString: function() {
        var filter_string = '.cat-'+this.getFilters().join('.cat-');
        
        return filter_string;
    },
    
    getFilters: function() {
        var tags = [];
        
        // get main category as first filter
        var s = this.options.page_url.match(/\/category\/([a-zA-Z0-9-]+)/);
        if (s != null) {
            tags.push(s[1]);
        }
        
        var filters = $j.query.get('filter');
        if (filters.length > 0) {
            tags = tags.concat(filters);
        }

        return tags;
    }
    
});



// plugins
$j.preloadImages = function() {
	for (var i = 0; i<arguments.length; i++) {
		img = new Image();
		img.src = arguments[i];
	}
};


// INIT PAGE

$j(document).ready(function(){
    
    var is_ie6 = false;
    
    if(typeof document.body.style.maxHeight === "undefined") {
        is_ie6 = true;
    }
    
    // preload images
    $j.preloadImages('http://onelessact.com/store/images/buttons/view_cart-hover.gif',
    'http://onelessact.com/store/images/buy_bubble-hover.gif',
    'http://onelessact.com/store/images/buy_bubble-hover.png',
    'http://onelessact.com/store/images/bubble_black-hover.gif',
    'http://www.onelessact.com/store/images/logo-hover.gif',
    'http://www.onelessact.com/store/images/tag_panel_btn-hover.gif',
    'http://www.onelessact.com/store/images/search_bg-hover.gif'
    );
    
    if (is_ie6) {
        $j('.carousel_btn, .front_graphic_buy_bubble').each(function(){
            var img_src = $j(this).attr('src').replace(/.png/, '.gif');
            $j(this).attr({'src': img_src});
        });
    }
    
    new InfoPanelContainer({
        panel_id: '#info_panel',
        button_id: '#info_panel_btn',
        close_button_id: '#info_panel_close'
    });

    new TagPanel({
        panel_id: '#tag_panel',
        button_id: '#tag_panel_btn'
    });
  
    $j('.product').each(function(index) {
        $j(this).click(function(){
            location.href = $j(this).find('.product_link').attr('href');
        }).hover(function(){
            $j(this).find('.actions a img').each(function(){
                var img_src = $j(this).attr('src');
                var img_hover_src = img_src.replace(/.gif/, '-hover.gif');
                
                $j(this).attr({'src':  img_hover_src});
                
                $j(this).hover(function(){
                  $j(this).attr({'src':  img_src});
                }, function(){
                  $j(this).attr({'src': img_hover_src});
                });
            });
            
            $j(this).addClass('hover');

        },function(){
            $j(this).find('.actions a img').each(function(){
                $j(this).attr({'src':  $j(this).attr('src').replace(/-hover.gif/, '.gif')});
            });
            
            $j(this).removeClass('hover');
        });
    });

   $j('#tag_panel_btn').hover(function(){
       var img_src = $j(this).children('img').attr('src');
       $j(this).children('img').attr({'src': img_src.replace(/.gif/, '-hover.gif')});
   },function(){
       var img_src = $j(this).children('img').attr('src');
       $j(this).children('img').attr({'src': img_src.replace(/-hover.gif/, '.gif')});
   });
   
   $j('#logo').hover(function(){
       var img_src = $j(this).attr('src');
       $j(this).attr({'src': img_src.replace(/.gif/, '-hover.gif')});
   },function(){
       var img_src = $j(this).attr('src');
       $j(this).attr({'src': img_src.replace(/-hover.gif/, '.gif')});
   });
   
   $j('#search-input').focus(function(){
       $j(this).addClass('hover');
   }).blur(function() {
       $j(this).removeClass('hover');
   });
   
   $j('.product_image a').each(function(){
       $j(this).click(function(){
           var class_name = $j(this).attr('id');
           $j('#product_image_window').scrollTo('#large_'+class_name, {
               easing: 'easeOutQuart',
               duration: 800
           });
           return false;
       });
   });
  
   $j("#carousel").jCarouselLite({
        btnNext: "#right_carousel_btn",
        btnPrev: "#left_carousel_btn",
        visible: 1,
        speed: 800,
        auto: 3000,
        easing: 'easeOutQuart'
    });

    $j("#font_carousel").jCarouselLite({
         btnNext: "#font_next_btn",
         btnPrev: "#font_back_btn",
         visible: 1,
         speed: 800,
         easing: 'easeOutQuart'
     });

     $j("#front_graphic1 div").jCarouselLite({
          btnNext: "#front_graphic1 .front_graphic_next_arrow",
          btnPrev: "#front_graphic2 .front_graphic_prev_arrow",
          visible: 1,
          speed: 800,
          easing: 'easeOutQuart'
      });
     
      $j("#front_graphic2 div").jCarouselLite({
           btnNext: "#front_graphic2 .front_graphic_next_arrow",
           btnPrev: "#front_graphic2 .front_graphic_prev_arrow",
           visible: 1,
           speed: 800,
           easing: 'easeOutQuart'
       });

    
    $j("#contact-content input, #contact-content textarea").focus(function(){
        $j(this).addClass('hover');
    }).blur(function() {
        $j(this).removeClass('hover');
    });
  
});
