/** 
 * Netlash General
 * 
 * @author Bram Van Damme	<bram@netlash.com>
 * @author Bert Pattyn		<bert@netlash.com>
 */

/**
 * JS_NETLASH Object
 */
if (!JS_NETLASH) { var JS_NETLASH = new Object(); }


/**
 * JS_NETLASH - general JS object
 */
JS_NETLASH.general = {

	/**
	 * Datamembers
	 */
	debug: false,


	/**
	 * hook the clicks
	 * 
	 * @return void
	 */
	init: function() {

		JS_NETLASH.general._setExternalFavicons();

		JS_NETLASH.general._hookModalBoxes();
		
		JS_NETLASH.general._setTopBar();
		
		JS_NETLASH.general.colorbox();
	},


	/**
	 * attach favicons to external links (inside object with id #external-links-list)
	 * 
	 * @return void
	 */
	_setExternalFavicons	: function() {

		$('#external-links-list a[href^="http://"]').each(function() {
			var faviconIMG 	= $('<img width="16" height="16" alt="' + $(this).attr('title') + '" title="' + $(this).attr('title') + '" class="externalLinkImage" />').prependTo($(this));
			var extImg 		= new Image();

			extImg.src 		= $(this).attr('href').replace(/^(http:\/\/[^\/]+).*$/, '$1') +'/favicon.ico';

			extImg.onload = function() { faviconIMG.attr('src', extImg.src); }
			extImg.onerror = function() { faviconIMG.attr('src', '/modules/core/layout/images/externalLink.gif'); }
			extImg.onabort = function() { faviconIMG.attr('src', '/modules/core/layout/images/externalLink.gif'); }
		});

	},
	
	
	/**
	 * handle the value of the loginfields in the topbar
	 * 
	 * @return void
	 */
	_setTopBar: function() {
		
		// clear username 
		$('#login_username').bind('focus', function() {
			
			if($(this).val() == 'username') $(this).val('');
			
		});
		
		// set username if it is empty
		$('#login_username').bind('blur', function() {
			
			if($.trim($(this).val()) == '') $(this).val('username');
			
		});
		
		
	},

	
	/**
	 * adds (or removes) an overlay to the website
	 * 
	 * @return void
	 */
	_hookOverlay : function(show) {
		
		if($('#overlay').length == 0) $('#container').prepend('<div id="overlay"></div>');

		// set height
		$('#overlay').height($(document).height());
		$('#overlay').width($(document).width());

		if(show) $('#overlay').show();
		else $('#overlay').hide();
	},

	
	/**
	 * bind events to close and open modal boxes
	 * 
	 * @return void
	 */
	_hookModalBoxes	: function() {
		
		$('.modal-box-close, .modal-box-close-link').bind('click', function(evt) {
			
			// prevent default
			evt.preventDefault();

			// get id
			var id = $(this).attr('rel');

			// show box
			$('#' + id).hide();

			// hide overlay
			JS_NETLASH.general._hookOverlay(false);

		});

		$('.modal-box-toggle').bind('click', function(evt) {
			
			// get id
			var id = $(this).attr('rel');

			// show overlay
			JS_NETLASH.general._hookOverlay(true);

			// show box
			$('#' + id).show();

			// break the event
			evt.stopPropagation();
			evt.preventDefault();

			// hook click to box itself, so we can click in it.
			$('#' + id).bind('click', function(evt) {
				evt.stopPropagation();
			});

			$(document.body).bind('click', function(evt) {
				$('#' + id).hide();

				// hide overlay
				JS_NETLASH.general._hookOverlay(false);
			});

			// ESC key pressed
			$(document.body).bind("keypress", function(evt) {
				if (evt.keyCode == 27) {
					$('#' + id).hide();

					// hide overlay
					JS_NETLASH.general._hookOverlay(false);
				}
			});
		});
	},
	
	/**
	 * enable the colorbox popup for the images
	 */
	colorbox: function() {
		// images in content
		$('#storyContent img').parent('a[href$="jpg"]').colorbox({transition:"elastic"});
		$('#storyContent img').parent('a[href$="JPG"]').colorbox({transition:"elastic"});
		$('#storyContent img').parent('a[href$="gif"]').colorbox({transition:"elastic"});
		$('#storyContent img').parent('a[href$="GIF"]').colorbox({transition:"elastic"});
		$('#storyContent img').parent('a[href$="jpeg"]').colorbox({transition:"elastic"});
		$('#storyContent img').parent('a[href$="JPEG"]').colorbox({transition:"elastic"});
		$('#storyContent img').parent('a[href$="png"]').colorbox({transition:"elastic"});
		$('#storyContent img').parent('a[href$="PNG"]').colorbox({transition:"elastic"});
		
		// frontiersman
		$('#frontierMan ul.preview a').colorbox({transition:'elastic', rel:'frontiersman'});
		
		// main popup class
		$('#storyContent a.popup').colorbox({transition:"elastic"});
		
		// terms and conditions
		$('.termsLink').colorbox({fixedWidth:"600px", fixedHeight:"80%", iframe:true});
		
		// share this links
		/*$("a[href^='http://delicious.com/save?']").colorbox({fixedWidth:"80%", fixedHeight:"80%", iframe:true});
		$("a[href^='http://digg.com/submit']").colorbox({fixedWidth:"80%", fixedHeight: "80%", iframe:true});
		$("a[href^='http://www.facebook.com/sharer.php?']").colorbox({fixedWidth:"600px", fixedHeight: "80%", iframe:true});
		$("a[href^='http://friendfeed.com/?url=']").colorbox({fixedWidth:"80%", fixedHeight: "80%", iframe:true});
		$("a[href^='http://www.myspace.com/Modules/PostTo/Pages/?t=']").colorbox({fixedWidth:"80%", fixedHeight: "80%", iframe:true});
		$("a[href^='http://www.stumbleupon.com/submit?url=']").colorbox({fixedWidth:"80%", fixedHeight: "80%", iframe:true});
		$("a[href^='http://twitter.com/home?status=']").colorbox({fixedWidth:"80%", fixedHeight: "80%", iframe:true});*/

	},


	/**
	 * end of object
	 */
	_eoo			: true

}


/**
 * The ID must be put on any element, acting as a container. An unordered list will be generated
 * It will ignore all list items with a rel 'ignore'
 * 
 * Example: 
 *   <ul id="shareList" rel="title={$pageTitle}|url={$SITE_URL}{$SELF}">
 *   	<li rel="ignore"></li>
 *   	<li rel="linkedin"></li>
 *   	<li rel="facebook"></li>
 *   </ul>
 * 
 * Note that the type must be the first argument
 */
JS_NETLASH.sharelist = {
	id: '#shareList',
	params: {},
	
	init: function() {
		// cache object
		var oContainer = $(JS_NETLASH.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
			JS_NETLASH.sharelist.params[item.split('=')[0]] = item.split('=')[1];
		});
		
		// loop the share list items
		oContainer.children().each(function(i, item)
		{	
			// cache item
			var oItem = $(this);
			
			// if the rel is ignore, we don't continue
			if(oItem.attr('rel') != 'ignore')
			{
				// check if it has an ID set, if not add one
				if(oItem.attr('id') == '') oItem.attr('id', oItem.attr('rel'));
				
				// create new link
				JS_NETLASH.sharelist.setLink(this);
			}
		});
	},
	
	setLink: function(element) {
		// store element object
		var oElement = $(element);
		
		// reserve some vars
		var params = JS_NETLASH.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
}


/**
 * Thunderbirds.are.go!
 */
jQuery(function($) {
	JS_NETLASH.general.init();
	JS_NETLASH.sharelist.init();
});
