(function ($) {
	
	$.textual = function (options) {
		return $.textual.impl.init(options);
	};
	
	$.fn.textual = function (options) {
		return $.textual.impl.init(this, options);
	};
	
	/*
	 * default options
	 */
	$.textual.defaults = {
	    res:    {}
	};
	
	$.textual.impl = {
		
		/*
		 * options
		 */
		opts:           null,
		
		/*
		 * helper
		 */
		helper:         {},
		
		/*
		 * Initialize the page
		 */
		init: function (options) {
            
            var self = this;
            
            this.opts = $.extend({}, $.textual.defaults, options);
            
            var $content = $('div.page-textual');
            var $container = $content.find('div.textual-container');
            var $primary = $container.find('div.textual-primary');
            var $secondary = $container.find('div.textual-secondary');
            var contentH = $content.outerHeight();
            var containerH = $container.outerHeight();
            
            // initialize the image viewer
            self.images();
            
            if (containerH > contentH)
            {
                $content
                    .css({
                        height:     $container.outerHeight() + 'px'
                    })
                    ;
            }
            
            $container
                .css({
                    height:     contentH - (2*parseInt($container.css('padding-top'))) + 'px'
                })
                ;
            
            // initialize cufon
            Cufon.replace($('h1'), {
                fontFamily: 'Helvetica Neue LT Std'
            });
            Cufon.now();
            
			return self;
		},
		
		/*
		 * Images
		 */
		images: function () {
		    
		    var self = this;
		    
		    // initialize image viewer
            var $container = $('div.newsitem-images');
            var $list = $container.find('ul.image-collection');
            var $details = $container.find('ul.image-collection img.image-detail');
            var $thumbs = $container.find('ul.image-collection img.image-thumb');
            var $selected = $('<div class="selected"></div>').appendTo($container);
            var $selectors = $('<ul class="selector-collection"></ul>').appendTo($container);
            
            $list.children()
                .each(function(i, item){
                    
                    var $li = $('<li class="selector"></li>');
                    
                    $li
                        .append($thumbs[i])
                        .appendTo($selectors)
                        ;
                    
                    if (i == 0)
                    {
                        $li
                            .addClass('selector-active')
                            ;
                        
                        image(0);
                    }
                    
                })
                ;
            
            $selectors.children()
                .hover(
                    function(){
                        $(this)
                            .addClass('selector-hover')
                            ;
                    },
                    function(){
                        $(this)
                            .removeClass('selector-hover')
                            ;
                    }
                )
                .click(function(e){
                    e.preventDefault();
                    
                    if ($(this).hasClass('selector-active'))
                        return;
                    
                    $(this)
                        .addClass('selector-active')
                        .siblings()
                        .removeClass('selector-active')
                        ;
                    
                    image($(this).prevAll().length);
                    
                })
                ;
            
            // initialize the carousel
            $selectors
                .jcarousel({
                    
                })
                ;
		    
		    function image(i) {
		        
		        var $img = $($details[i]);
		        
		        $selected
		            .empty()
		            ;
		        
		        $(new Image()).load(function(){
		            
		            $selected
		                .append($(this))
		                ;
		            
		            $(this)
		                .fadeIn(1000)
		                ;
		            
		        }).attr('src', $img.attr('src'));
		        
		    }
		    
		    return;
		}
		
	};
})(jQuery);
