var _zv_widget_jqLoaded = false;

var _zv_news_widget = {
	Widget: function(settings) {
		
		if (!settings.width)
			settings.width = '250px';
		
		this.settings = settings;
		this.getJQuery(_zv_news_widget.get);
		
		return this;
	},
	
	getJQuery: function(f) {
		$this = this;
		
		if (typeof jQuery == 'undefined')
			{
			LazyLoad.loadOnce('http://im1.zv.innovaterussia.ru/data/zv/widget/jQuery.js', function() { _zv_widget_jqLoaded = true; jQuery.noConflict(); f($this.settings.vars) } );
			}
		else
			{
			_zv_widget_jqLoaded = true;
			f($this.settings.vars);
			}
		
	},
	
	get: function(vars) {
		$this = this;
		jQuery('<link>', {rel:"stylesheet", type:"text/css", href:"http://im1.zv.innovaterussia.ru/data/zv/widget/zv_widget.css?v21"}).appendTo('head');
		
		jQuery.getJSON("http://zv.innovaterussia.ru/api/news.widget.api.php?callback=?", vars, function(data) {
			$this.dataIsGet = true;
			$this.response = data;
		});
		return this;
	},
	
	parse: function() {
		$this = this;
		_this = this
		var news;
		var out = '';

		var jqTimer = setInterval(function () {

			if (_zv_widget_jqLoaded)
				{
				var widgetHTML = jQuery('<div>', {className: 'zwnb_news_widget ie_png', style: 'width: '+_this.settings.width+' !important', id: 'zv_news_widget', html: '<div class="zwnb_news_widget_wrap"><div style="text-align: center !important; padding: 15px 0 10px !important;"><img alt="загрузка" src="http://im1.zv.innovaterussia.ru/data/zv/widget/pl.gif" /></div><small class="zwnb_news_widget_block_title zwnb_link"><a target="_blank" href="http://zv.innovaterussia.ru">zv.innovaterussia.ru</a></small></div>'});

				widgetHTML.insertBefore(jQuery('#_zv_news_widget_script'));
				clearInterval(jqTimer);
				}
		}
		, 1);
				
		
		
		var timer = setInterval(function() {
			if ($this.dataIsGet)
				{
				clearInterval(timer);
				news = $this.response;
				
				jQuery.each(news, function(key, value) {
					
					out += '<div class="zwnb_news_widget_wrap_news_block"><small class="zwnb_news_widget_block_title"><a target="_blank" href="http://zv.innovaterussia.ru/news/'+key+'">'+_zv_news_types_decode[key]+'</a></small>';
					
					jQuery.each(value, function(i, o) {
						out += '<div>';
						out += '<table cellspacing="0"><tr>';
						out += (o['file_id']?'<td><a target="_blank" href="http://zv.innovaterussia.ru/news/'+o['id']+'"><img alt="'+o['title']+'" src="http://zv.innovaterussia.ru/attach/'+o['file_id']+'/d60" /></a></td>':'')+'<td class="zwnb_news_widget_title"><small><i class="zwnb_news_widget_blue">'+decodeDate(o['date'])+'</i> <img alt="" src="http://im1.zv.innovaterussia.ru/images_ir/taxi.gif" /></small><h4><a target="_blank" href="http://zv.innovaterussia.ru/news/'+o['id']+'">'+o['title']+'</a></h4></td>';
						out += '</tr></table>';
						out += '</div>';
					})
					
					out += '</div>';
				});
				
				jQuery('#zv_news_widget div.zwnb_news_widget_wrap').html(out+'<small class="zwnb_news_widget_block_title zwnb_link"><a target="_blank" href="http://zv.innovaterussia.ru">zv.innovaterussia.ru</a></small>');

				}
		}, 1)
	}
}


var _zv_news_types_decode = {
	site: 'новости сайта',
	project: 'новости проекта',
	smi: 'пресса о нас'
}

var decodeDate = function(date) {
	
	var matches = /([0-9]+)-([0-9]+)-([0-9]+)/.exec(date);

	return matches[3]+' '+months[matches[2].replace(/^0/, '')];
}

var months = ['нул', 'янв', 'фев', 'мар', 'апр', 'мая', 'июн', 'июл', 'авг', 'сен', 'окт', 'ноя', 'дек',]

/*
Copyright (c) 2008 Ryan Grove <ryan@wonko.com>. All rights reserved.
Licensed under the BSD License:
http://www.opensource.org/licenses/bsd-license.html
Version: 1.0.4
*/
var LazyLoad=function(){var E=document,D=null,A=[],C;function B(){if(C){return }var G=navigator.userAgent,F;C={gecko:0,ie:0,webkit:0};F=G.match(/AppleWebKit\/(\S*)/);if(F&&F[1]){C.webkit=parseFloat(F[1])}else{F=G.match(/MSIE\s([^;]*)/);if(F&&F[1]){C.ie=parseFloat(F[1])}else{if((/Gecko\/(\S*)/).test(G)){C.gecko=1;F=G.match(/rv:([^\s\)]*)/);if(F&&F[1]){C.gecko=parseFloat(F[1])}}}}}return{load:function(K,L,J,I){var H=E.getElementsByTagName("head")[0],G,F;if(K){K=K.constructor===Array?K:[K];for(G=0;G<K.length;++G){A.push({url:K[G],callback:G===K.length-1?L:null,obj:J,scope:I})}}if(D||!(D=A.shift())){return }B();F=E.createElement("script");F.src=D.url;if(C.ie){F.onreadystatechange=function(){if(this.readyState==="loaded"||this.readyState==="complete"){LazyLoad.requestComplete()}}}else{if(C.gecko||C.webkit>=420){F.onload=LazyLoad.requestComplete;F.onerror=LazyLoad.requestComplete}}H.appendChild(F);if(!C.ie&&!C.gecko&&!(C.webkit>=420)){F=E.createElement("script");F.appendChild(E.createTextNode("LazyLoad.requestComplete();"));H.appendChild(F)}},loadOnce:function(N,O,L,P,G){var H=[],I=E.getElementsByTagName("script"),M,J,K,F;N=N.constructor===Array?N:[N];for(M=0;M<N.length;++M){K=false;F=N[M];for(J=0;J<I.length;++J){if(F===I[J].src){K=true;break}}if(!K){H.push(F)}}if(H.length>0){LazyLoad.load(H,O,L,P)}else{if(G){if(L){if(P){O.call(L)}else{O.call(window,L)}}else{O.call()}}}},requestComplete:function(){if(D.callback){if(D.obj){if(D.scope){D.callback.call(D.obj)}else{D.callback.call(window,D.obj)}}else{D.callback.call()}}D=null;if(A.length){LazyLoad.load()}}}}();
