(function ($) {
	
	$.giftCard = function (options, productId) {
		return $.giftCard.impl.init(options, productId);
	};
	
	$.fn.giftCard = function (options) {
		return $.giftCard.impl.init(this, options);
	};
	
	/*
	 * default options
	 */
	$.giftCard.defaults = {
	    res:    {}
	};
	
	$.giftCard.impl = {
		
		/*
		 * options
		 */
		opts:           null,
		
		/*
		 * helper
		 */
		helper:         {},
		
		/*
		 * Initialize the page
		 */
		init: function (options, productId) {
            
            var self = this;
            
            this.opts = $.extend({}, $.giftCard.defaults, options);
            
            // set product ID
            self.productId = productId;
            
            // helpers
            self.helper.container = $('div.page-giftcard');
            self.helper.optionList = self.helper.container.find('ul.option-collection');
            self.helper.addProduct = self.helper.container.find('a.add-product');
            self.helper.productAdded = self.helper.container.find('ul.product-added');
            
            // initialize the select boxes
            self.initSelects();
            
            // cufon
            Cufon.replace(self.helper.container.find('h1'), {
                fontFamily: 'Helvetica Neue LT Std'
            });
            Cufon.now();
            
            // events
            self.bindAddProductClick();
            
			return self;
		},
		
		/*
		 * Binds the add product click
		 */
	    bindAddProductClick: function () {
	        
	        var self = this;
	        
            self.helper.addProduct
                .removeClass('disabled')
                .click(function(e){
                    e.preventDefault();
                    
                    $(this)
                        .addClass('disabled')
                        .unbind()
                        ;
                    
                    self.helper.productAdded
                        .css({
                            display:    'none'
                        })
                        ;
                    
                    var sizeCode = self.helper.optionList.find('.option-size a.select-selected').attr('value');
		            var variantCode = self.helper.optionList.find('.option-color a.select-selected').attr('value');
		            var quantity = self.helper.optionList.find('.option-quantity a.select-selected').attr('value'); 
                    
                    if (sizeCode == '' || sizeCode == 'null')
                    {
                        alert(self.opts.res.pleaseSelectSize);
                        return;
                    }
                    
                    $.shoppingBag.impl.addProduct(self.productId, sizeCode, variantCode, quantity, $.giftCard.impl.addProductCallback);
                    
                })
                ;
	        
	        return;
	    },
		
		/*
		 * addProduct callback
		 */
	    addProductCallback: function (data) {
	        
	        var self = $.productDetail.impl;
	        var count = (data > 0) ? data : 0;
	        
	        if (data < 0)
	        {
	            alert('An error has occurred. Please try again.');
	            
	            return;
	        }
	        
	        // re-bind the addproduct click
	        self.bindAddProductClick();
	        
	        // show the checkout button
            self.helper.productAdded
                .fadeIn(500)
                ;
            
            // update the shoppingbag count in header
            $('ul#nav-ecommerce .shoppingbag-count')
                .text(count)
                ;
	        
	        return;
	    },
		
		/*
		 * Initializes the select boxes
		 */
		initSelects: function () {
		    
		    var self = this;
		    
		    self.helper.optionList.find('select').each(function(i, item){
		        
		        var $og = $(item).hide();
		        var ogId = $og.attr('id');
		        var ogClass = $og.attr('class');
		        var selectedIndex = $og.children("option:selected").prevAll().length;
		        var values = [];
			    var texts = [];
			    
		        $og.children().each(function(j){
		        
				    values[j] = $(this).attr('value');
				    texts[j] = $(this).text();
				    
			    });
			    
			    var $sContainer = $('<div class="select-container"></div>').insertAfter($og);
		        var $sSelected = $('<a href="#" class="select-selected">' + texts[selectedIndex] + '</a>').appendTo($sContainer);
		        var $sOptions = $('<ul class="select-option-collection"></ul>').appendTo($sContainer);
		        
		        $.each(values, function(j){
		            
		            var $o = $('<li class="select-option"><a href="#" value="' + values[j] + '">' + texts[j] + '</a></li>');
		            
		            $sOptions.append($o);
		            
		        });
		        
		        // selected click (i.e. open/close)
		        $sSelected.click(function(e){
		            e.preventDefault();
		            
		            if (!$sContainer.hasClass('select-container-show'))
		            {
		                $sContainer
		                    .addClass('select-container-show')
		                    ;
		                
		                $sContainer.find('a').blur(function(){
		                    $sContainer
		                        .removeClass('select-container-show')
		                        ;
		                });
		            }
		            else
		            {
		                $sContainer
		                    .removeClass('select-container-show')
		                    ;
		            }
		        });
		        
		        // option click
		        $sOptions.find('a').click(function(e){
		            e.preventDefault();
		            
		            if ($sSelected.text() != $(this).text())
		            {
		                $sSelected.text($(this).text());
		            }
		            
		            $sContainer
                        .removeClass('select-container-show')
                        ;
		        });
		        
		        // link hover
		        $sContainer.find('a').hover(
		            function(){
		                $sContainer.find('a').unbind('blur');
		            },
		            function(){
		                $sContainer.find('a').blur(function(){
		                    $sContainer
		                        .removeClass('select-container-show')
		                        ;
		                });
		            }
		        );
		    });
		    
		    return;
		}
		
	};
})(jQuery);