(function ($) {
		   
	// namespace 'news' for news ticker	
	var news = {
		
		preferences: {
			// path to xml file
			url: "nt/v16-xml-news-ticker.xml",
			// headline for merchandising module
			h2: "Bulletin d'info",
			// translation for link 'Previous',
			previous: "Précédent",
			// translation for link 'Next',
			next: "Suivant",
			// id where it should get loaded - should be'ibm-content-sidebar' or 'ibm-merchandising-module'
			//id: "ibm-content-sidebar",
			// rotation time in seconds
			time: "6",
			// should we generate also "previous" / "next" container ?
			pn: true
		},
		
		storage: {
			// number of boxes to display
			n: 0,
			// currently displayed box
			c: 0,
			// interval ID
			interval: null,
			headline: []
		},
		
		// load XML data from file
		getData: function () {
			$.ajax({
  				url: news.preferences.url,
  				cache: true,
				dataType: "xml",
  				success: function (x) {
					
					// xml was loaded successfully, so we can check now if there are any news
					// if there are non, we stop right here
					var n = $("news", x).length;
					
					if ( n == 0 ) { 
						return; 
					} else {
						news.storage.n = n;						
					}
					
					// create and append box to proper ID
					news.createBox ();
				
					if (news.preferences.pn) {
						news.createButtons ();
					}
		
					// append every news to created box in a separate 'div' tag
					$ ("news", x).each (function () {
						var url = $ (this).attr ("url");
						var text = $ (this).text();
						var aClass = $ (this).attr ("class");
						var image = $ (this).attr ("image");
						var alt = $ (this).attr ("alt") || "";
						var pClass = (aClass != "ibm-feature-link") ? "ibm-ind-link ibm-extend-bottom" : "";
						news.storage.headline.push ($(this).attr ("h2") );
						
						// create and append this news to box
						var html = '<div class="v16-newsTicker-news">';
						if ( image ) {
							html = html + '<img src="' + image + '" alt="' + alt + '" width="168" height="64">';
						}
						
						html = html + '<p class="' + pClass + '"><a class="' + aClass + '" href="' + url + '"';
						html += aClass == 'ibm-pdf-link' ? ' onclick="sa_onclick(this.href)" onkeypress="sa_onclick(this.href)"' : '';
						html += '>' + text;
						
						$ ("#v16-newsTicker-holder > div:first").append (html);
					});
					
					// set CSS to created divs
					$ ("div.v16-newsTicker-news").css ({ position: "absolute", top: "0px", left: "-170px", zIndex: "0", width: "168px" });
					$ ("div > p.ibm-extend-bottom").each (function () {
						var h = $(this).height();						
						$(this).height( h + 5);
					});
					
					// prepare 'leaving-ibm' popup
					if ( $( "a.ibm-external-link", "#v16-newsTicker-holder").length > 0) {
						if (typeof (ibmCommon.leavingIbm) !="object" || ibmCommon.leavingIbm == null) {
							jQuery.getScript("//www.ibm.com/common/js/leaving-ibm.js");
						}			
					}				
						
					// everything is ready
					// now we will display the first news
					news.adjustHeight (0);
					$("div.v16-newsTicker-news:eq(0)").animate ({left: 0}, "slow");
					$ ('#v16-newsTicker-holder h2:first').text (news.storage.headline[0]);
					// and we can initialize the ticker
					news.run ();

  				},
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					news.error ("XML file couldn't be parsed !<br>" + XMLHttpRequest + "<br>" + textStatus + "<br>" + errorThrown );
				}
			});
		},
		
		createBox: function () {
		$ ("#ibm-content-sidebar").append ('<div id="v16-newsTicker-holder" class="ibm-container">');
			$ ("#v16-newsTicker-holder").append ('<h2>' + news.preferences.h2 + '</h2><div class="ibm-container-body">');
			
			// box is ready
			// now we need to set some CSS to container-body			
			$ ("#v16-newsTicker-holder > div:first").css ({width: "168px", height: "70px", overflow: "hidden", zIndex: "0", position: "relative"});
		},
		
		// will create and apped 'Next' / 'Previous' buttons
		createButtons: function () {
			$ ("#v16-newsTicker-holder").append ('<div class="ibm-container-body" id="v16-newsTicker-buttons">');
			$ ("#v16-newsTicker-buttons").css ({textAlign: "center"}).append ('<p><a class="newsTicker-previous" href="#' + news.preferences.previous + '">' + news.preferences.previous + '</a><span class="ibm-sep"> | </span><a class="newsTicker-next" href="#' + news.preferences.next + '">' + news.preferences.next + '</a>');
			
			// set CSS and attach event handler for each link
			$ ("a.newsTicker-previous").css ({ background: "url(http://www.ibm.com/common/js/rssdisplaymodule/buttons/default/back.png) no-repeat 0 -2px", height: "1%", padding: "0px 0px 0px 18px"})
				.click (function () { news.rotatePrevious () });
			$ ("a.newsTicker-next").css ({ background: "url(http://www.ibm.com/common/js/rssdisplaymodule/buttons/default/next.png) no-repeat right -2px", height: "1%", padding: "0px 18px 0px 0px"})
				.click (function () { news.rotate () });
		},
		
		// this function will take care of showing the first news and cycling through them
		run: function () {
			news.storage.interval = setInterval (this.rotate, this.preferences.time * 1000);
			// make sure we stop hovering once user is over the box with mouse
			$ ("#v16-newsTicker-holder").hover (function () {
				// mouse over -> clear interval
				clearInterval (news.storage.interval);														  
			}, function () {
				// mouse out -> set interval again and do one rotation right now
				news.storage.interval = setInterval (news.rotate, news.preferences.time * 1000);
			});
		},
		
		// the cool efect for cycling through news
		rotate: function () {
			// which box should we display now
			var n = (news.storage.c + 1) % news.storage.n;		
			// move current box to right, so it will dissapear
			$("div.v16-newsTicker-news:eq(" + news.storage.c + ")").animate ({left: 180}, "slow", function () {
			// now that it's in right, we will return it to basic position, left from the box, hidden
				$ (this).css ({ left: "-170px" });																											
			});
			
			// at the same time, move new box to center, so we can see it
			$("div.v16-newsTicker-news:eq(" + n + ")").animate ({left: 0}, "slow");

			news.adjustHeight (n);
			
			$ ('#v16-newsTicker-holder h2:first').text (news.storage.headline[n]);
			
			
			// set current values to storage
			news.storage.c = n;
		
		},
		
		// and go back
		rotatePrevious: function () {
			// which box should we display now
			var n = (news.storage.c - 1) % news.storage.n;
			if (n == -1) {
				n = news.storage.n - 1;
			}			
			// move the new box to the right
			$("div.v16-newsTicker-news:eq(" + n + ")").css ({left: 180});		
			// move current box to left, so it will dissapear
			$("div.v16-newsTicker-news:eq(" + news.storage.c + ")").animate ({left: -180}, "slow");		
			// at the same time, move new box to center, so we can see it
			$("div.v16-newsTicker-news:eq(" + n + ")").animate ({left: 0}, "slow");
			
			news.adjustHeight (n);	
			
			$ ('#v16-newsTicker-holder h2:first').text (news.storage.headline[n]);
			
			// set current values to storage
			news.storage.c = n;
		},
		
		adjustHeight: function (n) {
			// now the tricky part
			// if text we are about to display is too long, some part of it might get lost beneath the box
			// so we need to get it's height and compare to current box height
			// and set it so it would fit
			$ ("#v16-newsTicker-holder > div:first").height ($("div.v16-newsTicker-news:eq(" + n + ")").height () );
		},
		
		// function for displaying error messages.
		error: function (t) {
			$ ("#ibm-content-main > *:first").before ('<p class="ibm-ind-error">' + t);	
			return;
		}		
	};
	
	$ (document).ready (function () {		
		// get XML and append news to created box.
		// after ajax is done, it will call everything else, so follow "success" function in getData
		news.getData ();								  
								  
	});
}) (jQuery);
