/**
 * sfSlider
 *
 * @version: 1.0
 *
 * Required settings:
 *  display 	- provide number of items displayed at once
 *
 * Other settings:
 *  time 		- transition time
 *  easing 		- easing for the transition
 *  width 		- width of the scrolled area (by default visible area + right margin on the last visible item)
 *  previous	- previous link text
 *  next		- next link text
 *  wrap		- wrap container selector
 *  slider		- items container selector
 *  items		- items selector
 */ 
eval(function($){jQuery.fn.sfSlider=function(options){var defaults={width:0,display:6,time:500,easing:'swing',previous:'Previous',next:'Next',wrap:'div.wrap',slider:'ul.items',items:'ul.items li'};var settings=$.extend({},defaults,options);return this.each(function(){var $root=$(this);var $wrap=$root.find(settings.wrap);var $slider=$root.find(settings.slider);var $items=$root.find(settings.items);var all=$items.size();var pages=Math.ceil(all/settings.display);if($items.size()<=settings.display)return false;var width=settings.width;if(settings.width===0)width=$wrap.width()+parseInt($items.css('margin-right'),10);var current=1;$root.append('<ul class="index"><li class="prev"><a href="#previous" class="off">'+settings.previous+'</a></li><li class="next"><a href="#next">'+settings.next+'</a></li></ul>');var $controls=$root.find('ul.index');var check=function(){if(current===1){$controls.find('li.prev a').addClass('off');}else{$controls.find('li.prev a').removeClass('off');}if(current===pages){$controls.find('li.next a').addClass('off');}else{$controls.find('li.next a').removeClass('off');}};$controls.find('a').click(function(){var direction=$(this).parent().attr('class');if($slider.is(':animated')||(current==1&&direction=='prev')||(current==pages&&direction=='next'))return false;if(direction=='next'){move='-='+width+'px';current++;}else{move='+='+width+'px';current--;}$slider.animate({'marginLeft':move},settings.time,settings.easing);check();return false;});});};})(jQuery);

/**
 * Product Filter
 * Version: 1.0
 * 
 *  
 * Plugin should be applied to the products list and can be modified with following options
 *  - checkboxContainer : selector for container that will hold filter checkboxes, by default "#checkboxcontainer"
 *  - item : selector for product item withing the list, by default "li"
 *  - tags : selector for tags wrapper within the product item element, by default ".tags"
 *  - itemsFound : selector for the "items found" message, by default ".itemsFound". It should have at least one <span /> containing the results number
 *  - hideOnLoad : should all items be displayed or hidden when page is loaded, by default "true" (hidden)
 *  - hiddenMessage : selector for the element with message displayed when all items are hidden, by default ".hideMessage"
 *  - delimiter : delimiter used to separate tags, by default a comma
 *  - createDynamicCheckboxes : should script dynamically create filter checkboxes, by default true
 */
(function(){	
	
	var SFProductFilter = function(productList, options){
		
		this.productList = $(productList);				
		this.checkboxContainer = $(options.checkboxContainer || '#checkboxcontainer');
		this.items = this.productList.find(options.item || 'li');		
		this.tags = {};
		
		this.itemsFound = $(options.itemsFound || '.itemsFound');		
		this.hideOnLoad = typeof options.hideOnLoad != 'undefined' ? !!options.hideOnLoad : true;
		this.hiddenMessage = $(options.hiddenMessage || '.hideMessage');
		this.createDynamicCheckboxes = typeof options.createDynamicCheckboxes != 'undefined' ? !!options.createDynamicCheckboxes : true;

		var delimiter = options.delimiter || ',';

		var self = this, tagElement = options.tags || '.tags';
				
		/* Travers all items and parse appended tags */
		$.each(this.items, function(index, item){
			var tags = $(item).find(tagElement).text().split(delimiter);
			
			//empty or missing tags on the element
			if (!tags) {
				return true;
			}
			
			//trim all whitespace from tags and add them to the global 
			tags = $.map(tags, function(item){
				var tag = $.trim(item);
				if (tag === ''){
					return null;
				}
				
				self.tags[tag] = tag;		
				return tag;
			});
						
			$(item).data('tags',tags);
		});
		
		if (this.hideOnLoad === true) {
			this.hiddenMessage.show();
			this.productList.hide();
		}
		
		if (this.createDynamicCheckboxes === true) {
			this.createCheckboxes();
		}
		
		this.checkboxContainer.change(function(){
			self.filter();
		});
		
		this.filter();
	};
	
	/*
	 * Parse all tags and create a list of checkboxes
	 */
	SFProductFilter.prototype.createCheckboxes = function(){
		var self = this;
		var i = 0;
		$.each(this.tags, function(index, item){
			var checkbox, label, p;
			checkbox = $('<input type="checkbox" value="' + item + '" id="product-list-tag-'+i+'"/>');
			
			label = $('<label for="product-list-tag-'+i+'">'+item+'</label>');
			p = $('<p/>');
			p.append(checkbox, ' ', label);
			self.checkboxContainer.append(p); 
			i++;
		});
	};
	
	/*
	 * Apply filters to the list of products
	 */
	SFProductFilter.prototype.filter = function(){
		//fetch currently
		var activeTags = this.checkboxContainer.find('option:selected').map(function(){
			return $(this).val();
		});

		//create an array of active elements (with all active tags)
		var activeElements = this.items.filter(function(){			

			var itemTags = $(this).data('tags');
			var flag = true;

			$.each(activeTags, function(index, tag){
				//active tag found on item, search for next inactive tag
				if ($.inArray(tag, itemTags) > -1) {
					return true;
				}
				//if we find at least one inactive tag then break the loop and deactive this item
				flag = false;
				return false;				
			});		
			return flag;
		});

		var resultsFound = activeElements.length;

		//no tags checked + hideOnLoad function is active
		if (this.hideOnLoad === true && activeTags.length === 0) {
			this.productList.hide();
			this.hiddenMessage.show();
			
			resultsFound = 0;
		}
		else {
			this.productList.show();
			this.hiddenMessage.hide();			
		}
		
		//update number of found items
		this.itemsFound.find('span').text(resultsFound);	
		
		//toggle active items
		this.items.hide(); 
		activeElements.show();
	};
	
	jQuery.fn.sfProductFilter = function(options){
		options = options || {};
		
		return this.each(function(){
			var pf = new SFProductFilter(this, options);
		});
	};
})();

/*
 * hrefID jQuery extention
 */
$.fn.extend({ hrefId: function() { return $(this).attr('href').substr($(this).attr('href').indexOf('#')); } });

/*
 * Scripts
 *
 */
jQuery(function($) {
 
	var Engine = {
		utils : {
			links : function(){
				$('a[rel*=external]').click(function(e){
					e.preventDefault();
					window.open($(this).attr('href'));						  
				});
			},
			mails : function(){
				$('a[href^=mailto:]').each(function(){
					var mail = $(this).attr('href').replace('mailto:','');
					var replaced = mail.replace('/at/','@');
					$(this).attr('href','mailto:'+replaced);
					if($(this).text() == mail) {
						$(this).text(replaced);
					}
				});
			}
		},
		forms : {
			labels : function(){
				var $elements = $('form.newsletter-a p input, form#topform p input');
				$elements.each(function(){
					if($(this).val() !== '') $(this).prevAll('label:first').hide();
				}).focus(function(){
					$(this).prevAll('label:first').hide();
				}).blur(function(){
					if($(this).val() === '') $(this).prevAll('label:first').show();
				});
			}
		},
		fixes : {
			blog : function(){
				// no comments/trackbacks + alternative
				var $comments = $('div.comments-a');
				$comments.each(function(){
					if($(this).find('div.comment').length == 0){
						var fixed = $(this).html().replace(/<\/h2>/i,'</h2><p class="empty">') + '</p>';
						$(this).html(fixed);
					} else {
						$(this).find('div.comment:odd').addClass('alt');
					}
				});
				
				// show/hide comments/trackbacks
				var $links = $('div.post-a p.info a.comments, div.post-a p.info a.trackbacks');
				$links.click(function(){
					$($(this).hrefId()).toggle();
					if($(this).is('.comments')) $($(this).hrefId()).next('div.add-comment-a:first').toggle();
					return false;
				});
				
				// single post (show trackbacks and comments)
				if($('div.post-a').length == 1){
					$('div.comments-a, div.add-comment-a').show();
				}
			},
			separators : function(){
				$('ul[class*=cols]').each(function(){
					var matches = /cols([0-9]+)/i.exec($(this).attr('class'))
					var row = matches[1];
					$(this).find('li').each(function(i){
						if((i+1) % row == 0) $(this).after('<li class="separate"><a href="#top">Back to top</a></li>');
					});
				});
			}
		},
		slider : function(){
			$('#slider ul.items li:nth-child(6n+1)').addClass('start');
			$('#slider').sfSlider({display: 6});
		}
	};

	Engine.utils.links();
	Engine.utils.mails();
	Engine.forms.labels();
	Engine.fixes.blog();
	Engine.fixes.separators();
	Engine.slider();
	
});
