/*
* Copyright (C) 2009 Joel Sutherland.
* Liscenced under the MIT liscense

Mod by iWaKThemes
------------------------------------------------
Add support for easing
Add support for lightbox rel
Include margin in animation
-------------------------------------------------
$easing_list = array('linear'=>'linear', 
                        'swing'=>'swing', 
                        'easeInBounce'=>'easeInBounce',
                        'easeOutBounce'=>'easeOutBounce', 
                        'easeInOutBounce'=>'easeInOutBounce', 
                        'easeInQuad'=>'easeInQuad',
                        'easeOutQuad'=>'easeOutQuad', 
                        'easeInOutQuad'=>'easeInOutQuad', 
                        'easeInCubic'=>'easeInCubic',
                        'easeOutCubic'=>'easeOutCubic', 
                        'easeInOutCubic'=>'easeInOutCubic', 
                        'easeInQuart'=>'easeInQuart',
                        'easeOutQuart'=>'easeOutQuart', 
                        'easeInOutQuart'=>'easeInOutQuart', 
                        'easeInQuint'=>'easeInQuint',
                        'easeOutQuint'=>'easeOutQuint', 
                        'easeInOutQuint'=>'easeInOutQuint', 
                        'easeInSine'=>'easeInSine',
                        'easeOutSine'=>'easeOutSine', 
                        'easeInOutSine'=>'easeInOutSine', 
                        'easeInExpo'=>'easeInExpo',
                        'easeOutExpo'=>'easeOutExpo', 
                        'easeInOutExpo'=>'easeInOutExpo', 
                        'easeInCirc'=>'easeInCirc',
                        'easeOutCirc'=>'easeOutCirc', 
                        'easeInOutCirc'=>'easeInOutCirc', 
                        'easeInElastic'=>'easeInElastic',
                        'easeOutElastic'=>'easeOutElastic', 
                        'easeInOutElastic'=>'easeInOutElastic', 
                        'easeInBack'=>'easeInBack',
                        'easeOutBack'=>'easeOutBack', 
                        'easeInOutBack'=>'easeInOutBack', 
                    );
*/

(function() {
	jQuery.fn.filterable = function(settings) {
		settings = jQuery.extend({
			useHash: true,
			animationSpeed: 1000,
            easingShow: 'easeInCubic',
            easingHide: 'easeOutCubic',
			show: { width: 'show', marginRight: '20px', opacity: 'show' },
			hide: { width: 'hide', marginRight: '0', opacity: 'hide' },
			useTags: true,
			tagSelector: '#portfolio-filter a, #kaartje area, #footer_portfolio a',
			selectedTagClass: 'current',
			allTag: 'all'
		}, settings);
		
		return jQuery(this).each(function(){
		
			/* FILTER: select a tag and filter */
			jQuery(this).bind("filter", function( e, tagToShow ){
				
				var tagsToShow = tagToShow.substr( 1 ).split( ',' );
				
				if(settings.useTags){
					
					//lus door alle tags
					var processed = 0;
					jQuery.each( tagsToShow, function( i, v ){
						processed++;
						var myTagParts = v.split( '_' );
						//console.log( 'tag: ' + v );
						var myClassList = new Array();
						//vind alle items met in ieder geval de huidge tag uit de lus
						jQuery( 'li.' + v ).each( function(){
							var myClass = jQuery( this ).attr( 'class' ).split( ' ' );
							jQuery.each( myClass, function( j, w ){
								//voeg de overige classen toe aan een lijst
								var myClassParts = w.split( '_' );
								if( w != ' ' && w != '' && w != 'odd' && w != 'even' && w != 'portfolio-entry' && w != v && myTagParts[0] != myClassParts[0] ){
									myClassList.push( w );
								}
							} );
						} );
						var myClasses = '.' + myClassList.join( '.' );
						//console.log( 'classes: ' + myClasses );

						//ben ik all dan alles aan
						if( v == settings.allTag ){
							//en maak de url gelijk aan mijn rel tag
							jQuery( settings.tagSelector ).each( function(){
								jQuery( this ).removeClass( 'off' ).addClass( 'on' ).attr( 'href', '#' + jQuery( this ).attr( 'rel' ) );
							} );
						} else {
							//lus door alle links
							jQuery( settings.tagSelector ).each( function(){
								var rel = jQuery( this ).attr( 'rel' );
								if( rel != undefined ){
									var myRelParts = rel.split( '_' );
									//ben ik mezelf zet me dan aan
									if( rel == v ){
										//en zet mijn tag op all
										jQuery( this ).removeClass( 'off' ).addClass( 'on' );//.attr( 'href', '#' + settings.allTag );
									} else {
										//ben ik niet all
										if( rel != settings.allTag ){
											if( processed == 1 ){
												//kom ik niet voor in de lijst van classen zet me dan uit
												if( myClasses.indexOf( '.' + rel ) == -1 ){
													jQuery( this ).removeClass( 'on' ).addClass( 'off' ).attr( 'href', '#' + rel )
												} else {
													//en anders aan//hier de juiste url toekennen aan de link
													jQuery( this ).removeClass( 'off' ).addClass( 'on' ).attr( 'href', '#' + v + ',' + rel );
												}
											}
										}
									}
								}
							} );
						}
					} );
				}
				jQuery( this ).trigger( "filterportfolio", [ tagToShow.substr(1) ] );
			});
			
			
			/* FILTERPORTFOLIO: pass in a class to show, all others will be hidden */
			jQuery( this ).bind( "filterportfolio", function( e, classToShow ){
				
				//console.log( classToShow );
				//multiple classes at once!
				var selectors = classToShow.split( ',' );
				
				if( classToShow == settings.allTag ){
					jQuery( this ).trigger( 'show' );
				}else{
					jQuery( this ).trigger( 'show', [ '.' + selectors.join( '.' ) ] );
					jQuery( this ).trigger( 'hide', [ ':not(.' + selectors.join( '.' ) + ')' ] );
				}
				
				if( settings.useHash ){
					//console.log( classToShow );
					location.hash = '#' + classToShow;
				}
			});
			
			
			/* SHOW: show a single class*/
			jQuery( this ).bind( 'show', function( e, selectorToShow ){
				jQuery( this ).children( selectorToShow ).each( function(){
					jQuery( this ).animate( settings.show, settings.animationSpeed, settings.easingShow );
					imagelink = jQuery( this ).find( '.entry-thumb a' );
					imagelink.attr( 'rel', imagelink.attr( 'rel' ).replace(/(\d)\]/, '$1_active]' ) );
				});
			});
			
			/* SHOW: hide a single class*/
			jQuery(this).bind("hide", function( e, selectorToHide ){
				jQuery(this).children(selectorToHide).each(function(){
					jQuery(this).animate(settings.hide, settings.animationSpeed, settings.easingHide);	
					imagelink = jQuery(this).find('.entry-thumb a');
					imagelink.attr('rel', imagelink.attr('rel').replace('_active', ''));
				});
			});
			
			/* ============ Check URL Hash ====================*/
			if( settings.useHash ){
				//console.log( 'Check URL Hash' );
				if(location.hash != '')
					jQuery( this ).trigger( "filter", [ location.hash ] );
				else
					jQuery( this ).trigger( "filter", [ '#' + settings.allTag ] );
			}
			
			/* ============ Setup Tags ====================*/
			if( settings.useTags ){
				//console.log( 'Setup Tags' );
				jQuery( settings.tagSelector ).click( function(){
					jQuery('#portfolio-list').trigger("filter", [ jQuery(this).attr('href').replace('/wp.php/','') ]);
				});
			}
		});
	}
})(jQuery);


jQuery( document ).ready(function(){
	
	jQuery('#portfolio-list').filterable();

});
