// newsslideshow 345

function xmlNewsToJson(xmlUrl) {
	var newsData = "[";
	var xmlg;
	$.ajax({
		async: false,
		url: xmlUrl + '?' + Math.random(111),
		dataType: ($.browser.msie) ? "text" : "xml",
		success: function(data){
			if (typeof data == "string") {
				xmlg = new ActiveXObject("Microsoft.XMLDOM");
				xmlg.async = false;
				xmlg.loadXML(data);
			} else {
				xmlg = data;
			}
			$('var[name=Content] > array > struct',xmlg).each(function(i, _struct){
				if (i != 0) {
					newsData+= ",";
				}
				newsData+= "{";
          //console.log(i, _struct)
				$('var',$(_struct)).each(function(j,_var){
					switch ($(_var).attr("name")){
						case 'id': newsData+= '\'url\' : \'/news/' + lng.lang + '/' + $(_var).text() + '.html\',';
							break;
						case 'title': newsData+= '\'title\' : \'' + $(_var).text().replace(/[\s]/g,' ') + '\',';
							break;
						case 'date': newsData+= '\'date\' : \'' + $(_var).text() + '\',';
							break;
						case 'short_text': newsData+= '\'description\' : \'' + $(_var).text().replace(/[\s]/g,' ') + '\',';
							break;
						case 'name': var src =$(_var).text(); src = (src != '') ? src : lng.zaglp+'news_main_zagl.jpg';
									 newsData+= '\'ico\' : \'' + src.substr(0,src.indexOf('.')) + '_15' + src.substr(src.indexOf('.')) + '\',';
									 //newsData+= '\'ico\' : \'../../../loading.gif\',';
									 newsData+= '\'img\' : \'' + src.substr(0,src.indexOf('.')) + '_10' + src.substr(src.indexOf('.')) + '\',';
							break;
                        case 'source': newsData+= '\'source\' : \'' + $(_var).text() + '\'';
							break;
					    case 'links': {
					    newsData+= ', \'links\' : {';
					    $('struct',$(_var)).each(function(j,_link){
					      //console.log(j)
					      if (j != 0){
					        newsData+= ',';
					      }
                        newsData+= '\'' + $('[name="link_url"] string',$(_link)).text() + '\' : {' ;
					      newsData+= '\'name\' : \'' + $('[name="link_name"] string',$(_link)).text() + '\',';
					      newsData+= '\'type\' : \'' + $('[name="link_type"] string',$(_link)).text() + '\'}';
					    })
					    newsData+= '}';
					  }
					}
				});
				newsData+= "}";
			});
			newsData+= "]";
	//console.log(newsData);
		}
	});
	newsData = eval('('+newsData+')');
	//console.dir(newsData);
	return newsData;
};

var newsSlideshow = function (selectorBlock, xmlUrl) {
	var nsBlock = $(selectorBlock);
	var self = this;
	this.data = xmlNewsToJson(xmlUrl);
	this.currentNews = nsBlock.find("div.news_current a.news_text");
	this.date = self.currentNews.find("span.date");
	this.title = self.currentNews.find("p.title");
	this.description = self.currentNews.find("p.description");
    this.links = nsBlock.find("div.news_current p.links");
	this.txtC = nsBlock.find("div.news_current div.text");
	this.cur_pos = 0;
	this.navigation = nsBlock.find("ul.news_list")
	this.countElem = self.data.length;
	this.elems;
	this.timer;
	this.timerTimeout;
	this.timer_delay = 5500;
	this.timer_stop = 0;
	this.fade = 0;
	this.init = function(){
		for (elem in self.data){
		    if (elem == 3) {
		      li_class = " class=\"last\"";   
            }  else {
              li_class = "";   
            }
			self.navigation.append("<li"+li_class+"><a href=\"#\"><img src=\"/img/gallery/images/thumb/" + self.data[elem].ico + "\" border=\"0\" /><span><strong>"+self.data[elem].title+"</strong></span></a></li>");
			$("<img>").attr("src","/img/gallery/images/thumb/" + self.data[elem].img);
		}
		self.elems = self.navigation.find("a");
		self.elems.click(function(e){
			if (self.cur_pos != self.elems.index($(this))) {
				clearInterval(self.timer);
				clearTimeout(self.timerTimeout);
				self.cur_pos = self.elems.index($(this));
				self.reloadCurNews(self.cur_pos);
                ++self.cur_pos;
				self.timerTimeout = setTimeout(function(){self.timer = setInterval(function(){self.play()},self.timer_delay);}, self.timer_stop);
			}
			return false;
		})
		self.reloadCurNews(0);
		self.cur_pos = 1;
		this.fade = 300;
		self.timer = setInterval(function(){self.play()},self.timer_delay);
	}
	this.play = function(){
		self.reloadCurNews(self.cur_pos);
		if (++self.cur_pos >= self.countElem) self.cur_pos = 0;
	}
	this.reloadCurNews = function(index){
	  //console.log(self.currentNews)
	  //return;
        self.links.fadeOut(self.fade);
		self.currentNews.fadeOut(self.fade, function () {
			self.date.text(self.data[index].date);
			self.title.text(self.data[index].title);
			self.description.text(self.data[index].description);
			self.currentNews.attr("href",self.data[index].url).css("background-image","url(/img/gallery/images/thumb/" + self.data[index].img + ")");
            self.links.text("");
            
            for (var link in self.data[index].links){
                self.links.append("<a class=\"type_"+self.data[index].links[link].type+"\" href=\""+link+"\">"+self.data[index].links[link].name+"</a>");
                //console.log(link);
                //console.log(self.data[index].links[link].name);
            }
            self.elems.parents().filter(".current").removeClass("current");
			self.elems.filter(".current").removeClass("current");
            self.elems.eq(index).parent().addClass("current");
			self.elems.eq(index).addClass("current");
			self.currentNews.fadeIn(self.fade);
            self.links.fadeIn(self.fade);
			self.txtC.css('padding-bottom', self.links.innerHeight() + 20);
		});/*
		self.date.html(self.data[index].date);
		self.title.html(self.data[index].title);
		self.description.html(self.data[index].description);
		self.currentNews.attr("href",self.data[index].url).css("background-image","url(/img/gallery/images/thumb/" + self.data[index].img + ")");
		self.elems.filter(".current").removeClass("current");
		self.elems.eq(index).addClass("current");*/
	}
}
