dojo.declare('newsticker', null, {
	// public variables - can be changed during widget initialization
	previous: "Previous",
	next: "Next",
	id: "ibm-content-sidebar",
	time: "6",
	
	
	// private variables
	_n: 0,
	_c: 0,  
	_interval: null,
	_holder: '',
	_holder_2: '',
	_holder_3: '',
	_elements: [],
	_id: null,
		
		
	// functions ->
	getData:  function () {
		dojo.xhrGet({
			url: this.url,
			handleAs: 'xml',
			preventCache: true,
			error: dojo.hitch(this, function(){
				console.error('Newsticker: File couldn\'t be found at following url: "' + this.url + '". Please check before publishing!');
			}),
			load: dojo.hitch(this, function(xml) {
				if(xml == null){
					console.error('Newsticker: Incorrect document returned');
				}
				
				this._n = dojo.query('news', xml).length;;
				
				this.createBox();
				this.createButtons();
				
				dojo.query('news', xml).forEach( dojo.hitch(this, function(node, i){
					var img_width = node.getAttribute('width') || 172;
         			var img_height = node.getAttribute('height') || '';
					var img_alt = node.getAttribute('alt') || '#';
					var img_src = node.getAttribute('image');
					var sub_title = node.getAttribute('newstitle');
					var ticker_text = node.getAttribute('newstext');
					var ticker_link = node.getAttribute('url');
					var ticker_icon = node.getAttribute('linktype') || 'ibm-forward-link';
          			var ticker_info = node.getAttribute('span');
					
					var div = dojo.create('div', { 
						style: 'display: ' + (i == 1 ? 'block' : 'none')
					}, this._holder_2);
					
          			if(img_src && img_src.length > 1){
          				dojo.create('img', {
          					height: img_height, 
          					width: img_width, 
          					alt: img_alt, 
          					src: img_src
          				}, div ); 
          			}
          			
          			if(sub_title && sub_title.length > 1){
          				dojo.create('p', {
          					innerHTML: '<strong>' + sub_title + '</strong>'
          				}, div);
          			}
          			 
					if(ticker_text && ticker_text.length > 1){
						dojo.create('p', {
							innerHTML:ticker_text
						}, div);
					}
					
					if(ticker_link && ticker_link.length > 1){
						var p = dojo.create('p', {
							className: 'ibm-ind-link'
						}, div);
											
						var anchor = dojo.create('a', {
							className: ticker_icon + ' ticker-span',
							href: ticker_link,
							innerHTML: node.childNodes[0].nodeValue
						}, p);
						                                                   
						if(ticker_info && ticker_info.length > 1){
							dojo.create('span', {
								className: 'ibm-item-note', 
								innerHTML: ticker_info
							}, anchor);
						}
					}
				}));
				
				this._elements = dojo.query('> div',this._holder_2);
				this.run();
			})
		});
	},
	
	createBox: function () {
		this._holder = dojo.create('div', {
			className: 'ibm-container ibm-newsticker', 
			id: this._id 
		}, this.id);
		
    	if(this.boxtitle){
	      	dojo.create('h2', {
	      		innerHTML: this.boxtitle
	      	}, dojo.query(this._holder)[0]);  
	    }
	    
   		this._holder_2 = dojo.create('div', {
   			className: 'ibm-container-body'
   		}, this._holder);
   		
   		
		this._holder_3 = dojo.create('div', {
			className: 'ibm-container-buttons'
		}, this._holder);
	},
	
	createButtons: function () {
		dojo.place('<div class="ibm-alternate"><hr /></div><span><a href="#previous" class="prev_button">' + this.previous + '</a> | <a href="#next" class="next_button">' + this.next + '</a></span>', this._holder_3);
	},
	
	run: function () {
    	this.rotate();
    	
		this._interval = setInterval(dojo.hitch(this, this.rotate), this.time * 1000); 
		
		dojo.connect(dojo.byId(this._id), 'onmouseenter', dojo.hitch(this, function(){
			clearInterval(this._interval);
		}));  
		  
		dojo.connect(dojo.byId(this._id), 'onmouseleave', dojo.hitch(this, function(){
			this._interval = setInterval(dojo.hitch(this, this.rotate), this.time * 1000); 
		}));

		dojo.connect(dojo.query('#' + this._id + ' .next_button')[0], 'onclick', this, 'rotate');
		dojo.connect(dojo.query('#' + this._id + ' .prev_button')[0], 'onclick', this, 'rotatePrevious');
	},
	
	rotate: function() {
		var n = (this._c + 1) % this._n;
		this._elements.forEach(function(node){    
			node.style.display = 'none';
		})
		
		this._elements[n].style.display = 'block';
		this._elements[n].style.opacity = '0';
		
		dojo.anim(this._elements[n], { 
			opacity: 1 
		}, 600);
		
		this._c = n;                                      
	},
	
	rotatePrevious: function() {
		var n = (this._c - 1) % this._n;
		if (n == -1) {
			n = this._n - 1;
		}
		this._elements.forEach(function(node){
			node.style.display = 'none';
		})
		
		this._elements[n].style.display = 'block';
		this._elements[n].style.opacity = '0';
		dojo.anim(this._elements[n], { opacity: 1 }, 600);
		this._c = n;  
	},
	
	// program entry
	constructor: function(args){	
		// loop through all CSS includes on page
		var found = false;
		dojo.query('link').forEach(function(i){ 
			// check if one of them includes our css
			var href = i.getAttribute('href');
			if(href.indexOf('newsticker_v17.css') > -1){
				found = true;
			}
		});
		
		if(!found){
			dojo.create('link', {
				type: 'text/css',
				rel: 'stylesheet',
				title: 'www',
				href: '/software/uk/itsolutions/js/nt/newsticker_v17.css'
			}, dojo.query('head')[0]);
		}
		
		// generate random ID for this widget
		this._id = 'ibm-newsticker-' + ibmweb.util.generateId();
		
		dojo.safeMixin(this, args);
		
		// check if target ID exists on page
		if(!dojo.byId(this.id)){
			return console.error("Target ID doesn't exist on page");
		}
		
		this.getData();
	}
});       
