/*
 * v16-xml-news-ticker.js,v 1.0 2009/02/15 15:30:07 janetka
 * News ticker, data pulled from XML. 
 * Owner: Jan Janetka/Slovakia/Contr/IBM
*/

(function ($) {
	// namespace 'news' for news ticker	
	var bulletin = {
		
		preferences: {
			// translation for link 'Previous',
			previous: "poprzednia",
			// translation for link 'Next',
			next: "następna",
			// 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,
			obj: null,
			// interval ID
			interval: null,
			h2: null
		},
		
		// load XML data from file
		init: function (el) {
			bulletin.storage.obj = el;
			var temp = $(el).parent();
			bulletin.storage.h2 = $('h2', temp);
			
			$ ('h3', bulletin.storage.obj).hide();
			
			$ (bulletin.storage.obj).css ({width: "528", height: "95px", overflow: "hidden", zIndex: "0", position: "relative"});
			
			bulletin.storage.n = $ ('ul.ibm-portrait-module-list', bulletin.storage.obj).length;
			if (bulletin.preferences.pn) {
				bulletin.createButtons ();
			}
			
			$ ("ul.ibm-portrait-module-list", bulletin.storage.obj).css ({ position: "absolute", top: "0px", left: "-530px", zIndex: "0", width: "528px" });		
						
			// everything is ready
			// now we will display the first news
			bulletin.adjustHeight (0);
			$("ul.ibm-portrait-module-list:eq(0)", bulletin.storage.obj).animate ({left: 0}, 1000);
			bulletin.storage.h2.text( $("ul.ibm-portrait-module-list:eq(0) h3").text() );
			// and we can initialize the ticker
			bulletin.run ();
		},
		
		// will create and apped 'Next' / 'Previous' buttons
		createButtons: function () {
			$ ('#ibm-rotating-box').after ('<div class="ibm-container-body" id="v16-bulletin-buttons">');
			$ ("#v16-bulletin-buttons").css ({textAlign: "center"}).append ('<p><a class="bulletin-previous" href="#' + bulletin.preferences.previous + '">' + bulletin.preferences.previous + '</a><span class="ibm-sep"> | </span><a class="bulletin-next" href="#' + bulletin.preferences.next + '">' + bulletin.preferences.next + '</a>');
			
			// set CSS and attach event handler for each link
			$ ("a.bulletin-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 () { bulletin.rotatePrevious () });
			$ ("a.bulletin-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 () { bulletin.rotate () });
		},
		
		// this function will take care of showing the first news and cycling through them
		run: function () {
			bulletin.storage.interval = setInterval (this.rotate, this.preferences.time * 1000);
			// make sure we stop hovering once user is over the box with mouse
			var parent = $(bulletin.storage.obj).parent ();
			$ (parent).hover (function () {
				// mouse over -> clear interval
				clearInterval (bulletin.storage.interval);														  
			}, function () {
				// mouse out -> set interval again and do one rotation right now
				bulletin.storage.interval = setInterval (bulletin.rotate, bulletin.preferences.time * 1000);
			});
		},
		
		// the cool efect for cycling through news
		rotate: function () {
			// which box should we display now
			var n = (bulletin.storage.c + 1) % bulletin.storage.n;		
			// move current box to right, so it will dissapear
			$("ul.ibm-portrait-module-list:eq(" + bulletin.storage.c + ")", bulletin.storage.obj).animate ({left: 530}, 1000, function () {
			// now that it's in right, we will return it to basic position, left from the box, hidden
				$ (this).css ({ left: "-530px" });																											
			});
			
			// at the same time, move new box to center, so we can see it
			$("ul.ibm-portrait-module-list:eq(" + n + ")", bulletin.storage.obj).animate ({left: 0}, 1000);
			bulletin.storage.h2.text( $("ul.ibm-portrait-module-list:eq(" + n + ") h3").text() );

			bulletin.adjustHeight (n);
			
			// set current values to storage
			bulletin.storage.c = n;
		
		},
		
		// and go back
		rotatePrevious: function () {
			// which box should we display now
			var n = (bulletin.storage.c - 1) % bulletin.storage.n;
			if (n == -1) {
				n = bulletin.storage.n - 1;
			}			
			// move the new box to the right
			$("ul.ibm-portrait-module-list:eq(" + n + ")", bulletin.storage.obj).css ({left: 530});		
			// move current box to left, so it will dissapear
			$("ul.ibm-portrait-module-list:eq(" + bulletin.storage.c + ")", bulletin.storage.obj).animate ({left: -530}, 1000);		
			// at the same time, move new box to center, so we can see it
			$("ul.ibm-portrait-module-list:eq(" + n + ")", bulletin.storage.obj).animate ({left: 0}, 1000);
			bulletin.storage.h2.text( $("ul.ibm-portrait-module-list:eq(" + n + ") h3").text() );
			
			bulletin.adjustHeight (n);	
			
			// set current values to storage
			bulletin.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
			$ (bulletin.storage.obj).height ($("ul.ibm-portrait-module-list:eq(" + n + ")", bulletin.storage.obj).height () );
		},
		
		// function for displaying error messages.
		error: function (t) {
			$ ("#ibm-content-main > *:first").before ('<p class="ibm-ind-error">' + t);	
			return;
		}		
	};
	
	$ (document).ready (function () {
		bulletin.init ($ ('#ibm-rotating-box'));
	});

}) (jQuery);