// this disables the fugly white space the first time you load a page
var addthis_config = {
  data_use_flash: false
}

if (!ibbt) { var ibbt = new Object(); }

ibbt.general = {
	init: function() {
		// a-img problem
		$(".content a > img").addClass('linkedImage');

		// input field class
		$('#searchForm .inputHolder input').focus(function() {
			$('#searchForm .inputHolder').addClass('inputHolderActive');
		});
		$('#searchForm .inputHolder input').blur(function() {
			$('#searchForm .inputHolder').removeClass('inputHolderActive');
		});

		// remove the values of these inputfields on focus
		$('#subscribe_email, #search').focus(function(){
			$(this).val('');
		});
		
		// add the emailencode to the 'mailto' class
		$('a[href^=mailto]').emailencode();
	},

	eof: true
}


ibbt.slideshow = {
	init: function() {
		$('#slides').slides({
			preload: true,
			preloadImage: '/images/loading.gif',
			play: 5000,
			pause: 2500,
			hoverPause: true,
			animationStart: function(current){
				$('.caption').animate({
					bottom:-35
				},100);
			},
			animationComplete: function(current){
				$('.caption').animate({
					bottom:0
				},200);
			},
			slidesLoaded: function() {
				$('.caption').animate({
					bottom:0
				},200);
			},
			next: 'slideshowNext',
			prev: 'slideshowPrev'
		});
		
		if($('.slides_container').find('.slide').length < 2)
		{
			$('.slideshowNavigation').hide();
		}
	}
}


ibbt.tooltips = {
	init: function() {
		// cache tooltip
		var element = $('.themeList li a');
		var tooltip = $('.tooltip');
		
		element.hover(
			function() { tooltip.show(); },
			function() { tooltip.hide(); }
		);
	
		element.mousemove(
			function(e) {
				var mousex = e.pageX + 10;
				var mousey = e.pageY + 5;
				
				tooltip.text($(this).attr('rel'));
				tooltip.css({ top: mousey, left: mousex });
			}
		);
	},

	eof: true
}


/**
 * The ID must be put on any element, acting as a container. An unordered list will be generated
 * 
 * Example: 
 *   <ul id="shareList">
 *   	<li rel="linkedin"></li>
 *   	<li rel="facebook"></li>
 *   </ul>
 * 
 * Note that the type must be the first argument
 */
ibbt.shareList = {
	id: '#shareList',
	params: {},
	
	init: function() {
		// cache object
		var oContainer = $(ibbt.shareList.id);
		
		// fetch the contents of the rel attribute
		var rel = new String(oContainer.attr('rel')).split('|');
		
		// loop the rel
		$.each(rel, function(i, item)
		{
			// store the parameters
			ibbt.shareList.params[item.split('=')[0]] = item.split('=')[1];
		});
	
		// loop the share list items
		oContainer.children().each(function(i, item)
		{
			// create new link for this one
			ibbt.shareList.setLink(this);
		});
	},
	
	setLink: function(element) {
		// store element object
		var oElement = $(element);
		
		// reserve some vars
		var params = ibbt.shareList.params;
		var url = '';

		// add an anchor to the li
		oElement.html('<a href="#"></a>');
		
		// make an object of the added element
		var oAnchor = $('#'+ oElement.attr('id') +' a');
		
		// check what type we're dealing with
		switch(oElement.attr('rel'))
		{
			case 'delicious':
				url = 'http://www.delicious.com/post';
				
				if(params['url'] != undefined) url += '?url='+ params['url'];
				if(params['title'] != undefined) url += '&title='+ params['title'];
				break;
			case 'facebook':
				url = 'http://www.facebook.com/share.php?src=bm';

				// add fb_share name to the anchor
				oAnchor.attr('name', 'fb_share');
				
				if(params['url'] != undefined) url += '&u='+ params['url'];
				if(params['title'] != undefined) url += '&t='+ params['title'];
				break;
			case 'google':
				url = 'http://www.google.com/bookmarks/mark?op=add';
				
				if(params['url'] != undefined) url += '&bkmk='+ params['url'];
				if(params['title'] != undefined) url += '&title='+ params['title'];
				if(params['text'] != undefined) url += '&annotation='+ params['text'];
				break;
			case 'linkedin':
				url = 'http://www.linkedin.com/shareArticle?mini=true';
				
				if(params['url'] != undefined) url += '&url='+ params['url'];
				if(params['title'] != undefined) url += '&title='+ params['title'];
				if(params['text'] != undefined) url += '&summary='+ params['text'];
				break;
			case 'netlog':
				url = 'http://www.netlog.com/go/manage/links/view=save&origin=external';
				
				if(params['url'] != undefined) url += '&url='+ params['url'];
				if(params['title'] != undefined) url += '&title='+ params['title'];
				if(params['text'] != undefined) url += '&description='+ params['text'];
				break;
			case 'stumbleupon':
				url = 'http://www.stumbleupon.com/submit';
				
				if(params['url'] != undefined) url += '?url='+ params['url'];
				if(params['title'] != undefined) url += '&title='+ params['title'];
				break;
			case 'twitter':
				url = 'http://twitter.com/home?status=';

				if(params['title'] != undefined && params['url'] != undefined) url += params['title'] +' @ '+ params['url'];
				break;
		}
		
		// change the href of the element
		oAnchor.attr('href', url);
	},
	
	eof: true
}


$(document).ready(function() {
	ibbt.general.init();
	ibbt.shareList.init();
	ibbt.slideshow.init();
	ibbt.tooltips.init();
});
