(function ($) {
	
	$.club = function (options) {
		return $.club.impl.init(options);
	};
	
	$.fn.club = function (options) {
		return $.club.impl.init(this, options);
	};
	
	/*
	 * club default options
	 */
	$.club.defaults = {
	    videoPath:  ''
	};
	
	$.club.impl = {
		
		/*
		 * club options
		 */
		opts: null,
		
		/*
		 * club helper
		 */
		helper: {},
		
		/*
		 * club index
		 */
		index: 0,
		
		/*
		 * Initialize the club
		 */
		init: function (template, options) {
            
            var self = this;
            
            self.opts = $.extend({}, $.club.defaults, options);
            
            // create the helper objects
            self.helper.container       = template;
            self.helper.body            = $('div.club-body');
            self.helper.list            = $('ul.clubitem-hero-collection');
            self.helper.hero            = $('<div class="clubitem-hero-container"></div>');
            self.helper.controls        = $('<div class="clubitem-hero-controls"><span class="clubitem-hero-status">Image <span class="current">&#8212;</span> of <span class="length">&#8212;</span></span></div>').appendTo(self.helper.hero);
            self.helper.prev            = $('<a class="prev-image" href="#" title="' + self.opts.prevText + '">' + self.opts.prevText + '</a>').prependTo(self.helper.controls);
            self.helper.next            = $('<a class="next-image" href="#" title="' + self.opts.nextText + '">' + self.opts.nextText + '</a>').appendTo(self.helper.controls);
            self.helper.video           = self.helper.container.find('div.clubitem-video');
            
            if (self.opts.videoPath != '')
            {
                self.initVideo();
            }
            else
            {
                if (self.helper.list.hasClass('clubitem-hero-portrait'))
                {
                    self.helper.body.addClass('club-body-portrait');
                    
                    // insert the thumb images in body (workaround for ie float bug)
//                    self.helper.body.find('div.clubitem-body p').each(function(i, item){
//                        
//                        if ($(item).offset().top >= 494)
//                        {
//                            self.helper.body.find('ul.clubitem-thumb-collection').insertBefore($(item));
//                            return false;
//                        }
//                        
//                    });
                }
                else
                {
                    self.helper.body.find('ul.clubitem-thumb-collection').insertBefore(self.helper.body.find('.clubitem-title'));
                }
                
                self.initHero();
                
                self.update();
            }
            
			return self;
		},
		
		/*
		 * Initialize the hero
		 */
		initHero: function () {
		    
		    var self    = this;
		    var images  = self.helper.list.find('img').clone();
		    
		    // replace list
		    self.helper.list
		        .css('display', 'none')
		        ;
		    
		    // append the images
		    self.helper.hero
		        .insertAfter(self.helper.list)
		        .prepend(images)
		        ;
		    
		    // show the first image
		    $(images[0]).css('display', 'block');
		    
		    // prev click
		    self.helper.prev.click(function(e){
		        e.preventDefault();
		        
		        if (self.index > 0)
		        {
		            self.index--;
		        }
		        
		        self.update();
		        
		    })
		    
		    // next click
		    self.helper.next.click(function(e){
		        e.preventDefault();
		        
		        if (self.index + 1 < self.helper.hero.find('img').length)
		        {
		            self.index++;
		        }
		        
		        self.update();
		        
		    })
		    
		    return;
		},
		
		/*
		 * Update
		 */
		update: function () {
		    
		    var self    = this;
		    
		    $(self.helper.hero.find('img')[self.index])
		        .css('display', 'block')
		        .siblings('img')
		        .css('display', 'none')
		        ;
		    
		    self.helper.controls.find('.current').text(self.index + 1);
		    self.helper.controls.find('.length').text(self.helper.hero.find('img').length);
		    
		    return;
		},
		
		/*
		 * Initialize the video
		 */
		initVideo: function () {
		    
		    var self    = this;
		    var fo      = new SWFObject('/swf/video_component.swf', 'foo', '100%', '100%', '9', '#000000', true);
		    
            fo.divId = 'video';
            
            fo.addParam('quality','high');
            fo.addParam('align','middle');
            fo.addParam('scale','noscale');
            fo.addParam('wmode','opaque');
            fo.addParam('salign','');
            fo.addParam('allowScriptAccess','sameDomain');
            fo.addParam('allowFullScreen','true');
            
            fo.addVariable('videoPath', self.opts.videoPath);

            hVars=location.hash.replace('#','').split('&');for(var i=0;i<hVars.length; i++){fo.addVariable(hVars[i].split('=')[0],hVars[i].split('=')[1]);}
            fo.write(self.helper.video.attr('id'));
            fo.SetReturnValue = function() { return; }
            var foo = document.getElementById('foo'); 
		    
		    return;
		}
		
	};
})(jQuery);