(function($) {
	/**
	 * Slider constructor
	 * The slider is used to implement slideshows
	 * 
	 * @param DOMElement element
	 */
	var slider = function(element){
		//members init
		var _this = this;
		var element = $(element);
		var content = element.find('.home-slider-content');
		var nav = element.find('.home-slider-nav');
		this.switchTo = function(blockId){
			nav.find('li.active').removeClass('active');
			$('a[href=' + blockId + ']').parent('li').addClass('active');
			content.find('li.active').hide();
			$('#' + blockId).addClass('active').show();
		}
		/**
		 * Initialize the slider
		 */
		this.init = function(){
			content.find('li:not(:first-child)').hide();
			content.find('li:first-child').addClass('active');
			nav.find('li:first-child').addClass('active');
			nav.find('a').click(function(event){
				event.preventDefault();
				_this.switchTo($(this).attr('href'));
			});
		};
		this.init();
	};
	/**
	 * Namespace in jQuery
	 */
	$.fn.slider = function(){
       return this.each(function(){
           new slider(this);
       });
	};
	/**
	 * Newsletter Form constructor
	 * Basic fine-tuning
	 * 
	 * @param DOMElement element
	 */
	var newsletterForm = function(element){
		//members init
		var _this = this;
		var element = $(element);
		var label = element.find('label');
		var input = element.find('input[type=text]');
		label.children().remove();
		var labelText = $.trim(label.html());
		label.remove();
		input.val(labelText);
		input.focus(function(event){
			if(input.val().trim() === labelText){
				input.val('');
			}
		});
		input.blur(function(event){
			if(input.val().trim() === ''){
				input.val(labelText);
			}
		});
	};
	/**
	 * Namespace in jQuery
	 */
	$.fn.newsletterForm = function(){
		 return this.each(function(){
           new newsletterForm(this);
       });
	};
	/**
	 * Expandable constructor
	 * 
	 * @param DOMElement element
	 */
	var expandable = function(element){
		var _this = this;
		var element = $(element);
		/**
		 * Open the expandable element
		 * 
		 */
		this.open = function(){
			element.find('.view').removeClass('closed').addClass('open').show();
			element.find('.expandable-trigger')
				.html('-');
		};
		/**
		 * Close the expandable element
		 * 
		 */
		this.close = function(){
			element.find('.view').removeClass('open').addClass('closed').hide();
			element.find('.expandable-trigger')
				.html('+');
		};
		/**
		 * Switch state
		 * 
		 */
		this.swap = function(){
			if(element.find('.view').hasClass('open')){
				_this.close();
			}
			else{
				_this.open();
			}
		};
		/**
		 * Instance initialization
		 * 
		 */
		this.init = function(){
			var trigger = $('<a>')
				.attr('href', '#')
				.addClass('expandable-trigger');
			element.find('h3').prepend(trigger);
			element.find('h3').click(function(event){
				event.preventDefault();
				_this.swap();
			});
			_this.close();
		};
		this.init();
	}
	/**
	 * Namespace in jQuery
	 */
	$.fn.expandable = function(){
       return this.each(function(){
           var expandableInstance = new expandable(this);
       });
	};
	/**
	 * TagCloud constructor
	 * 
	 * @param DOMElement element
	 */
	var tagCloud = function(element, limit){
		var _this = this;
		var element = $(element);
		var count = element.find('li').length;
		if(count > limit){
			var more = $('<a>').addClass('tag-cloud-more').attr('href', '#').html('More tags');
			more.click(function(event){
				event.preventDefault();
				limit += 8;
				element.find('li:lt(' + (limit) + ')').fadeIn();
				if(element.find('li:not(:visible)').length === 0){
					more.hide();
				}
			});
			element.after(more);
			element.find('li:gt(' + (limit - 1) + ')').hide();
		}
	};
	/**
	 * Namespace in jQuery
	 */
	$.fn.tagCloud = function(limit){
       return this.each(function(){
           var tagCloudInstance = new tagCloud(this, limit);
       });
	};
    /**
	 * faqList constructor
	 *
	 * @param DOMElement element
	 */
    var faqList = function(element){
        var _this = this;
        var element = $(element);
        this.observeItems = function(){
            element.data('observed', true);
            element.find('.view-content li h2 > a').click(function(event){
                event.preventDefault();
                $(this).parent('h2').siblings('.content').toggle();
            });
        };
        this.init = function(){
            _this.observeItems();
            $('#page-title').bind('ajaxSuccess', function(event){
                element = $('.view-faq');
                if(element.data('observed') !== true){
                    _this.observeItems();
                }
            });
        };
        this.init();
    };
    /**
	 * Namespace in jQuery
	 */
    $.fn.faqList = function(){
        return this.each(function(){
            var faqListInstance = new faqList(this);
        });
    };

    /**
	 * faqList constructor
	 *
	 * @param DOMElement element
	 */
    var faqFilter = function(element){
        var _this = this;
        var element = $(element);
        var list;
        this.change = function(event){
            event.preventDefault();
            var item = $(this).parent('li');
            if(item.hasClass('active')){
                if(list.hasClass('open')){
                    _this.close();
                }
                else{
                    _this.open();
                }
            }
            else{
                _this.select($(this));
                element.change();
                element.val($(this).attr('href'));
            }
        };
        this.open = function(){
            list.find('li').show();
            list.addClass('open');
        };
        this.close = function(){
            list.find('li:not(.active)').hide();
            list.removeClass('open');
        };
        this.select = function(selected){
            var selectedItem = selected.parent('li');
            selectedItem.data('position', selectedItem.index());
            list.find('.active').removeClass('active');
            selectedItem.addClass('active');
            selectedItem.show();
            _this.close();
        };
        this.init = function(){
            list = $('<ul>').addClass('faq-filter');
            element.find('option').each(function(offset, option){
                var listItemClass = $(option).html().toLowerCase().replace('- ', '').replace(' -', '');
                var listItem = $('<li>').attr('class', listItemClass);
                listItem.hide();
                var link = $('<a>').attr('href', $(option).val()).html($(option).html());
                link.click(_this.change);
                listItem.append(link);
                list.append(listItem);
                $('#page-title').bind('ajaxSuccess', function(event){
                    element = $('#edit-field-category-sector-tid');
                    element.val(list.find('li.active a').attr('href'));
                });
            });
            _this.select(list.find('.any a'));
            element.before(list);
        };
        this.init();
    };
    /**
	 * Namespace in jQuery
	 */
    $.fn.faqFilter = function(){
        return this.each(function(){
            var faqFilterInstance = new faqFilter(this);
        });
    };
	//FIRE !
	$(document).ready(function(){
		$('#home-slider').slider();
		$('.expandable').expandable();
		$('#mailchimp-newsletter-block-form').newsletterForm();
		$('.tag-cloud').tagCloud(8);
        $('.view-faq').faqList();
        //$('#edit-field-category-sector-tid').faqFilter();
	});
})(jQuery);
;

