$(document).ready(function(){
	
	if( !Modernizr.input.placeholder ) {
		$('form').placeholder();
	}
	
	$('.expanded h2, .collapsed h2' ).each(function(){
		$(this).expandable({
			target: $(this).parents('.expanded, .collapsed')
		});
	});
	
	$('.sticky ul').switchable({
		switchElement: 'a',
		activeClass: 'active',
		inactiveClass: ''
	});
	
	// Twitter feed	
	var twit = WebLab_TwitterAPI.create( 'Lexsellent', 'feed_tweets' );
	twit.setTweetCount( 1 );
	
	if( $.fn.corner ) {
		$('.page.contact .column.left nav li, ' +
				'.page.about .column.right li, ' +
				'.page.legal .column.right li, ' +
				'.page.post .column.right li').corner( "round 5px" );
		$('.btn a, .btn input').parent().corner( "round 5px" );
	}
	
	(function(){
		var originals,
			amount_visible = 20,
			list = $('.page.shop .column.left .expanded ul' );
		
		if( list.find('li').length > amount_visible ) {
			originals = list.find('li').slice( amount_visible );
			
			originals.remove();
			list.append( $( '<li><a href="#">Meer tonen ...</a></li>' )
					.click(function(){
						$(this).remove();
						list.append( originals );
					})
			);
		}
	}());

});

// Form placeholder
(function($){
	
	var onfocus = function(){
			var me = $(this);
			
			if( me.val() == me.attr('placeholder') ) {
				me.val('');
			};
		},
		onblur = function(){
			var me = $(this);
			
			if( me.val().length == 0 ) {
				me.val( me.attr('placeholder') );
			}
		};
	
	$.fn.placeholder = function() {
		this.find('input').each(function( index, item ){
			item = $(item);
			var placeholder = item.attr('placeholder');
			
			if( typeof placeholder === 'string' && placeholder.length > 0 ) {
				item.focus(onfocus).blur(onblur);
				
				if( item.val().length == 0 ) {
					item.val( placeholder );
				}
			}
		});
	};
	
}(jQuery));

(function($){
	
	var collapse = function() {
			this.removeClass('expanded')
				.addClass('collapsed');
		},
		expand = function() {
			this.removeClass('collapsed')
				.addClass('expanded');
		};
		
	$.fn.expandable = function( options ) {
		var target = options.target || this;
		if( typeof target === 'string' ) {
			target = $(target);
		}
		
		this.click(function(){
			if( target.hasClass( "expanded" ) ) {
				collapse.call( target );
			} else {
				expand.call( target );
			}
		});
	};
	
}(jQuery));

(function($){
	
	var deactivate = function( active, inactive ){
			this.removeClass( active )
				.addClass( inactive );
		},
		activate = function( active, inactive ) {
			this.addClass( active )
				.removeClass( inactive );
		};
		
		$.fn.switchable = function( options ) {
			
			options = options || {
				switchElement: 'li',
				activeClass: 'active',
				inactiveClass: 'inactive'
			};
			
			var list = this.find(options.switchElement || 'li' ),
				url = document.location + '',
				id = url.split('#')[1];
			
			list.each(function( index, elem ){
				elem = $(elem);
				
				if( elem.attr('href') == '#'+id ) {
					deactivate.call( list, options.activeClass, options.inactiveClass );
					activate.call( elem, options.activeClass, options.inactiveClass );
				}
			});
			
			list.click(function(){
				console.log('clicked');
				var $this = $(this);
				
				deactivate.call( list, options.activeClass, options.inactiveClass );
				activate.call( $this, options.activeClass, options.inactiveClass );
			});
			
		};
	
}(jQuery));
