/*----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------*/
$(document).ready(function() {
	
	function show() {
		var menu = $(this);
		menu.children(".submenulinks").show();
	}

	function hide() { 
		var menu = $(this);
		menu.children(".submenulinks").hide();
	}
	
	$(".submenulinks").hide();
	
	
	$("#topmenu_links > li").hoverIntent({
		sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
		interval: 50,   // number = milliseconds for onMouseOver polling interval
		over: show,     // function = onMouseOver callback (required)
		timeout: 1000,   // number = milliseconds delay before onMouseOut
		out: hide       // function = onMouseOut callback (required)
	});

});

/*----------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------*/
var importantNews = function() {
	var displayPosition = 0;
	var maxPosition = 0;
	var News = '';
	var displayNews = '';
	var playerInterval;
	
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	importantNews.prototype.init = function() {
		
		this.initData();
		this.setMaxPostion(this.News.length - 1);
		this.generateLinks();
		this.setData(0);
		
		this.setContent(0);
	}

	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	importantNews.prototype.initData = function() {
		this.News = importantnewsArray;
	}

	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	importantNews.prototype.setMaxPostion = function(_position) {
		this.maxPosition = _position;
		
		if (importantnewsArray && importantnewsArray.length < _position) {
			this.maxPosition = (this.News.length - 1);
		}
	}

	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	importantNews.prototype.setDisplayPosition = function(_position) {
		this.displayPosition = _position;
	}
	
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	importantNews.prototype.setData = function(_position) {
		clearTimeout(this.playerInterval);
		this.displayPosition = _position;
		this.displayNews = this.News[this.displayPosition];
	}

	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	importantNews.prototype.setPreviousContent = function() {
		if ( (this.displayPosition - 1) < 0 ) {
			this.setContent(this.maxPosition);
		} else {
			this.setContent( (this.displayPosition - 1) );
		}
			
	}
	
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	importantNews.prototype.setNextContent = function() {
		if ( (this.displayPosition + 1) > this.maxPosition) {
			this.setContent(0);
		} else {
			this.setContent( (this.displayPosition + 1) );
		}
			
	}
	
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	importantNews.prototype.setContent = function(_position) {
		document.getElementById('number-' + this.displayPosition).className = 'numberbox';
		
		if (_position != 'undefined') {
			this.setData(_position);
		}
		
		textEnd = this.textEndPosition();
		var dotts = '';
		if (textEnd < this.displayNews['Advert']['address'].length) {
			dotts = '...';
		} 
		
		try {document.getElementById("important-picture").src = this.displayNews['Media']['media_path'] + this.displayNews['Media']['media_name'] + '.' + this.displayNews['Media']['media_extension'];} catch(e) {}   
		try {document.getElementById("important-title").innerHTML = this.displayNews['Advert']['name'];} catch(e) {}
		try {
			if(this.displayNews['Media']['orig']) {
				//document.getElementById("important-link").href = this.displayNews['Media']['media_path'] +  this.displayNews['Media']['media_name'] + '_orig.' + this.displayNews['Media']['media_extension'];
				document.getElementById("important-link").title = this.displayNews['Advert']['address'].substr(0,textEnd) + dotts;
			}
		} catch(e) {}
		try {document.getElementById("important-description").innerHTML = this.displayNews['Advert']['address'].substr(0,textEnd) + dotts;} catch(e) {}
		//try {document.getElementById("important-link").href = '/advert/free_advert/'} catch(e) {}
		//try {document.getElementById("more-info-from-news").href = '/news/view/' + this.displayNews['News']['news_link'];} catch(e) {}
		
		document.getElementById('number-' + _position).className = 'numberbox selected';
		
		this.autoPlayer();
	}

	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	importantNews.prototype.textEndPosition = function() {
		var text = this.displayNews['Advert']['address'];
			text = text.substr(150);
	
			if (text) {
				if (((120 + text.indexOf('.')) + 1) > 250) {
					return (150 + text.indexOf(',')) + 1;
				} else {
					return (150 + text.indexOf('.')) + 1;
				}
			}
			return this.displayNews['Advert']['address'].length;
	}
	
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	importantNews.prototype.generateLinks = function() {
		for (var i=0; i <= this.maxPosition; i++ ) {
			this.generateLink(i);
		}
	}
	
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	importantNews.prototype.generateLink = function(_number) {
		var link = document.createElement('a');
		
		link.setAttribute('href','javascript: void(0)');
		link.setAttribute('id','number-' + _number);

		link.onclick = function() {importantNews.setContent(_number)};
		link.innerHTML = (_number + 1);
		link.className = 'numberbox';
		document.getElementById('generatedLinks').appendChild(link);
	}
	
	////////////////////////////////////////////////////////////////////////
	////////////////////////////////////////////////////////////////////////
	importantNews.prototype.autoPlayer = function() {
		this.playerInterval = setTimeout('importantNews.setNextContent()',6000);
	}
}
