/**
 * @author SimonDevil
 */

function newsView(){
	
	var news = {
	
		pos: 1,
		sNum:4,//defauld value
		init: function(){
			this.newsConf = arguments[0];
			
			this.sNum = (codexd.defined(this.newsConf.sNum))?parseInt(this.newsConf.sNum):this.sNume;
			if(this.sNum>15){alert(w18.alerts.TMS); return;}
			if (document.getElementById(this.newsConf.prefix + '_mainWndID')== null) {
				this.mainWnd = document.createElement('div');
				this.mainWnd.id = this.newsConf.prefix + '_mainWndID';
				this.mainWnd.className = this.newsConf.mwC || 'mainWndClass';
				this.cont = document.createElement('div');
				this.cont.id = this.newsConf.contID || this.newsConf.prefix + '_innerContID';
				this.cont.className = this.newsConf.contC || 'innerContClass';
				this.nav = document.createElement('div');
				this.nav.id = this.newsConf.mwID || this.newsConf.prefix + '_mainNavID';
				this.nav.className = this.newsConf.nC || 'navClass';
				this.mainWnd.appendChild(this.cont);
				this.mainWnd.appendChild(this.nav);
				this.buildSlids();
				this.buildNav();
				
				if (codexd.defined(this.newsConf.newsParent)) 
					codexd.$cc(this.newsConf.newsParent).appendChild(this.mainWnd);
				else 
					document.body.appendChild(this.mainWnd);
				if (codexd.defined(this.newsConf.slideshow)) 
					this.autoclick();
			}
		},
		
		buildSlids:function(){
			for(var i=1;i<(news.sNum+1);i++){
				var slide = document.createElement('div');
					slide.id = this.newsConf.prefix+"_slide_"+i;
					slide.style.display = (i==1)?"":"none";
					slide.innerHTML = (i);
				this.cont.appendChild(slide);	
			}			
			
		},
		
		buildNav: function(){
			this.btn = document.createElement('div');
			this.btn.className = this.newsConf.btnC || 'navBtnClass';
			for (var i = 0; i <= (news.sNum+3); i++) {
				var btn = new Object();
				btn = this.btn.cloneNode(true);
				btn.id = this.newsConf.prefix + "_navBtn_" + ((i == 0) ? "prev" : (i == (news.sNum+1)) ? "next" :(i==(news.sNum+2))?"stop":(i==(news.sNum+3))?"play":i);
				btn.innerHTML = (i == 0) ? " &lt; " : (i == (news.sNum+1)) ? " &gt; " :(i==(news.sNum+2))?"auto off": (i==(news.sNum+3))?"auto on":i;
				btn.className = btn.className + ((i == 1)?" navBtnClass_clicked":" navBtnClass_out");
				btn.style.display = (i==0 || i==(news.sNum+1) || i==(news.sNum+3))?"none":"";
				btn.style.width = (i==(news.sNum+2) || i==(news.sNum+3))?"50px":"";
				btn.onmouseover = function(){
					if (this.className.indexOf('clicked') == -1) 
						this.className = this.className.replace(/clicked|out/, 'over');
				}
				btn.onmouseout = function(){
					if (this.className.indexOf('clicked') == -1) 
						this.className = this.className.replace('over', 'out');
					
				}
				
				btn.onclick = function(){
					if (this.id.indexOf('next') == -1 
						&& this.id.indexOf('prev') == -1 
						&& this.id.indexOf('play') == -1 
						&&  this.id.indexOf('stop') == -1) {
						this.className = this.className.replace(/over|out/, 'clicked');
						news.declick(this);
					}else{	
						if (this.id.indexOf('stop') != -1){
							clearInterval(news.autoSet);
							document.getElementById(this.id.replace("stop","play")).style.display = "";
							document.getElementById(this.id.replace("stop","prev")).style.display = "";
							document.getElementById(this.id.replace("stop","next")).style.display = "";
							this.style.display = "none";
						}
						if (this.id.indexOf('play') != -1){
							news.autoclick();
							document.getElementById(this.id.replace("play","stop")).style.display = "";
							document.getElementById(this.id.replace("play","prev")).style.display = "none";
							document.getElementById(this.id.replace("play","next")).style.display = "none";
							this.style.display = "none";
						}
						if (this.id.indexOf('next') != -1) news.next();
						if(this.id.indexOf('prev') != -1) news.prev();
					}
				}
				
				this.nav.appendChild(btn);
			}
			
		},
		
		next: function(){
			if ( this.pos >= 1 && this.pos < news.sNum) {
				this.pos++;
			}else {
				this.pos = 1;
			}
			var newsID = codexd.$cc(news.newsConf.prefix+"_navBtn_"+news.pos) 
			newsID.className = newsID.className.replace(/over|out/, 'clicked');
			news.declick(newsID);
		},
		
		prev:function(){
			if (this.pos < (news.sNum+1) && this.pos > 1) {
				this.pos--;
			}else {
				this.pos = news.sNum;
			}
			var newsID = codexd.$cc(news.newsConf.prefix+"_navBtn_"+news.pos) 
			newsID.className = newsID.className.replace(/over|out/, 'clicked');
			news.declick(newsID);
		},
		
		autoclick:function(){
			news.autoSet = setInterval(function(){
					if (document.getElementById(news.newsConf.prefix + '_mainWndID') != null) {
						news.next()
					}else{
							clearInterval(news.autoSet);	
					}
				}, parseInt(news.newsConf.delay || 5000));
		},
		
		declick:function(){
			var none = arguments[0];			
			for(var i=1;i<(news.sNum+1);i++){
				var match = codexd.$cc(news.newsConf.prefix+"_navBtn_"+i);
				var slide = codexd.$cc(news.newsConf.prefix+"_slide_"+i);
				if(none != match){
					match.className = match.className.replace(/over|clicked/, 'out');
					slide.style.display = "none";
				}else{
					this.pos = i;
					slide.style.display = "";
				}
			}
		}
	}
	this.news = news;
	this.conf = arguments[0]||{};
	
	this.add2Conf = function(){
		var key = arguments[0];
		var value = arguments[1];
		this.conf[key] = value;
	}
	
	news.init(this.conf||{});
}