/** 
 * 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: true,


	/**
	 * 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"});
		
		// 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

}


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