/**
 * @author Juan Andrade
 */
 
 window.ScionBlog = function($){
 	$(onDocumentReady);
 	
 	function onDocumentReady(){
 		if ($("#share-widget-5").length) {
 			new ScionShareWidget("#share-widget-5");	
 		}
 		
		//	comments
		if ($("#commentlist").length) {
			new ScionExpandComments("#content", 10, {more:"Show more comments", less:"Show less comments", containerSelector: ".comment", buttonSelector:".more_comments"});	
 		}
		
 		$("select").customselect();
		
		if ($('#search_Form').length) {
			new ScionSearch('#search_Form');
		}
	}
	
	/**
	 * Validate search
	 * @param {String} el	The search form selector
	 */
	var ScionSearch = function(el) {
		var element, 
			searchInput,
			//	constants
			INPUT_REQUIRED = 'keyword is required';
		
		/**
		 * constructor
		 */
		(function(){
			element = $(el);
			
			searchInput = element.find('#Search');
			
			if (searchInput.val() === INPUT_REQUIRED) {
				searchInput.val('');
			}
			//	submit
			element.unbind("submit").bind("submit",function() {
			    if(validateForm($(this))) {
			    	//	Disable binding
                	element.unbind("submit");
                    $(this).submit();
                }
                else {
                    return false;
                }
            });
			
			//	focus
			searchInput.unbind("focusin").bind("focusin", searchInput_focusinHandler);
			searchInput.unbind("focusout").bind("focusout", searchInput_focusoutHandler);
		}());
		
		function validateForm(form) {	
		    var flag = false;
			var needle = stringFilter($.trim(searchInput.val()));
	
			if(needle.length == 0){
				searchInput.attr('value',INPUT_REQUIRED);				
				flag = false;
			}else{
				if(needle.length >0 && needle != INPUT_REQUIRED){											
					flag = true;
					return flag;
				}
			}

            return flag;
        }
		
		function stringFilter ( s ) {
			filteredValues = "/+*@()[]{}<>"; 
			var i;
			var returnString = "";
			for (i = 0; i < s.length; i++) {
				var c = s.charAt(i);
				if (filteredValues.indexOf(c) == -1) returnString += c;
			}
			return returnString;
	   }
	   
	   function searchInput_focusinHandler(e) {
	   		if ($(e.currentTarget).val() === INPUT_REQUIRED) {
				$(e.currentTarget).val('');
			}
	   }
	   
	   function searchInput_focusoutHandler(e) {
	   		if ($(e.currentTarget).val().replace(/[' ']{1,}/, '') == '') {
				$(e.currentTarget).val(INPUT_REQUIRED);
			}
	   }
	}
 	
 	var ScionShareWidget = function(el) {
		var element,
			container, 
			toggleButton;
		/**
		 * constructor
		 */
		(function(){
			element = $(el);
			container = element.find(".socialwrap").hide();
			toggleButton = element.find(".widgettitle");
			
			toggleButton.click(toggleContainer);
		}());
		
		function toggleContainer(e) {
			container.toggle();
			return false;
		}
	};
	
	/**
 	 * Recent comments module
 	 */
 	var ScionExpandComments = function(el, numVisibleItems, options) {
		var element,
			container, 
			visibleItems = (numVisibleItems) ? numVisibleItems : 3,
			toggleButton,
			options = (options) ? options : {},
			containerSelector = (options.containerSelector) ? options.containerSelector : ".recentcomments",
			buttonSelector = (options.buttonSelector) ? options.buttonSelector : ".view_more_comments",
			LABEL_VIEW_MORE = (options.more) ? options.more : "VIEW MORE COMMENTS",
			LABEL_VIEW_LESS = (options.less) ? options.less : "VIEW LESS COMMENTS";
		/**
		 * constructor
		 */
		(function(){
			element = $(el);
			container = element.find(containerSelector);
			toggleButton = element.find(buttonSelector);
			collapseItems(false);
			
			toggleButton.click(toggleContainer);
		}());
		
		function toggleContainer(e) {
			if (container.is(":hidden")) {
				container.show();	
				toggleButton.text(LABEL_VIEW_LESS);
			} else {
				collapseItems(true);
			}
			
			
			return false;
		}
		
		function collapseItems(scroll) {
			if (scroll) {
				$(window).scrollTop($(container.get(visibleItems)).offset().top);	
			}
			
			toggleButton.html(LABEL_VIEW_MORE);
			$.each(container, function(index, item) {
				if (index >= numVisibleItems) {
					$(item).hide();
				}
			});
		}
	};
 }(jQuery);
	
(function($){
	
}(jQuery));
