﻿/* JavaScript Core 1.0 */
var _=function( id ){return new _.fn.init( id )}
_.fn = _.prototype = {
    $:null,
    isS:function(){return (this.$.style.display != 'none')},//Is Show
    sh:function(show){if(show)return this.s();else return this.h()},//Hide
	h:function(){this.$.style.display = 'none';return this},//Hide
	s:function(){this.$.style.display = '';return this},//Show
	s2:function(){this.$.style.display = 'block';return this},//Show
	c:function(n){if(n==null)return this.$.className;else this.$.className = n;return this},//Class
	t:function(t){if(t==null)return this.$.innerHTML;else this.$.innerHTML = t;return this},//HTML
	v:function(vel){if(vel==null)return this.$.value;else this.$.value = vel;return this},//Value
	id:function(vel){if(vel==null)return this.$.id;else this.$.id = vel;return this},//ID
	a:function(n,v){if(v==null)return this.$.getAttribute(n);else this.$.setAttribute(n,v);return this},//Attribute
	ae:function(n,v){if(v==null)return eval('this.$.'+n);else eval('this.$.'+n+'='+v);return this},//Attribute EVAL
	p:function(){return new _.fn.init( this.$.parentNode )}, // Parent node
	tn:function(n){return this.$.getElementsByTagName(n)}, // Get Element by TagName
	c_af:function(el){this.$.insertBefore(el,this.$.firstChild)}, // Insert control at 0
	init:function(id){if(typeof id == "string")this.$ = document.getElementById(id); else this.$ = id} // constructor
}
_.fn.init.prototype = _.fn;

/*** ajax ***/
_.ajaxObj = null;
_.ajaxQueue = new Array();
_.get = function(url,func,f) {
    if(!f) _.ajaxQueue.push({u:url,f:func});
    if(_.ajaxObj!=null && !f)return;
    if (window.XMLHttpRequest){_.ajaxObj = new XMLHttpRequest()} else if (window.ActiveXObject) {_.ajaxObj = new ActiveXObject("Microsoft.XMLHTTP")}
    _.ajaxObj.onreadystatechange = _.got;
    _.ajaxObj.open("GET", url, true);
    _.ajaxObj.send(null);
}
_.got = function() {
  if (_.ajaxObj.readyState == 4) {
      if (_.ajaxObj.status == 200) {
	     if(_.ajaxQueue[0].f!=null)
		    _.ajaxQueue[0].f(_.ajaxObj.responseText)
      }
      _.ajaxQueue.splice(0, 1);
      if(_.ajaxQueue.length>0){
	    _.get(_.ajaxQueue[0].u,_.ajaxQueue[0].f,true);
      }else{
	    _.ajaxObj = null
      }
  }
}
// ajax
_.url = function(u){window.location.href = u}

_.wnd = function(a_str_windowURL, a_str_windowName, a_int_windowWidth, a_int_windowHeight, a_bool_scrollbars, a_bool_resizable, a_bool_menubar, a_bool_toolbar, a_bool_addressbar, a_bool_statusbar, a_bool_fullscreen,left,top) {
	var int_windowLeft = (left==null)?(screen.width - a_int_windowWidth) / 2:left;
	var int_windowTop = (top==null)?(screen.height - a_int_windowHeight) / 2:top;
	var str_windowProperties = 'height=' + a_int_windowHeight + ',width=' + a_int_windowWidth + ',top=' + int_windowTop + ',left=' + int_windowLeft + ',scrollbars=' + a_bool_scrollbars + ',resizable=no,menubar=' + a_bool_menubar + ',toolbar=' + a_bool_toolbar + ',location=' + a_bool_addressbar + ',statusbar=' + a_bool_statusbar + ',fullscreen=' + a_bool_fullscreen + '';
	var obj_window = window.open(a_str_windowURL, a_str_windowName, str_windowProperties)
	if (parseInt(navigator.appVersion) >= 4) {
		obj_window.window.focus();
	}
	return false;
}

function foreach(arr, f){
    for(var _i=0;_i<arr.length;_i++)
        f(arr[_i])
}

if(!Array.indexOf){
    Array.prototype.indexOf = function(item, from){
		var len = this.length;
		for (var i = (from < 0) ? Math.max(0, len + from) : from || 0; i < len; i++){
			if (this[i] === item) return i;
		}
		return -1;
    }
}

function _sel(id){
	if(id!='rated'){
		_('rated').h()
		_('arated').c('')
		
		_('com').s()
		_('acom').c('tc')
	}else{
		_('com').h()
		_('acom').c('')
		
		_('rated').s()
		_('arated').c('tc')
	}
	return false
}

function fixDescription(){
	var arr = _('fulltext').tn("strong");
	foreach(arr,function(i){wrapnode(i)})
}

function wrapnode(el) { 
	if(el.parentNode.tagName=="STRONG")
		return;
	var newnode = document.createElement("strong"); 
	var parent = el.parentNode; 
	newnode.appendChild(el.cloneNode(true)); 
	parent.replaceChild(newnode, el);  
} 

var _r = {
	arr:['Ужасно','Плохо','Неплохо','Хорошо','Отлично'],
	isOver:false,
	clicked:false,
	over:function(v){
		if(_r.clicked)return
		_r.isOver=true
		for(var i=1;i<6;i++){
			_('r'+i).c(v+1>i?'s1':'s0')
		}
		_('rdesc').t(_r.arr[v-1])
	},
	out:function(){
		if(_r.clicked)return
		_r.isOver=false
		setTimeout('_r._out()',500)
	},
	_out:function(){
		if(!_r.isOver){
			for(var i=1;i<6;i++)
				_('r'+i).c('s0')
			_('rdesc').t('')
		}
	},
	click:function(id,v){
		if(_r.clicked)return
		_r.clicked = true
		_.get('/handler/rateNews.ashx?id='+id+'&v='+v,_r._click)
	},
	_click:function(d){
		eval('var o='+d)
		if(!o.error){
			_('rCount').t(o.count)
			for(var i=1;i<6;i++){
				_('_r'+i).c( (o.value>=i*1.0-0.001)?"s1":(o.value>(i*1.0-0.51)?"s05":"s0") )
			}
			_('rdesc').t(o.message)
		}else{
			_('rdesc').c('err').t(o.message)
		}
	}
}

// Core End
window.location.querystring = (function() {   
    var result = {};   
    var querystring = window.location.search;   
    if (!querystring)   
        return result;   
    var pairs = querystring.substring(1).split("&");   
    var splitPair;   
    for (var i = 0; i < pairs.length; i++) {   
        splitPair = pairs[i].split("=");   
        result[splitPair[0]] = splitPair[1];
    }   
    return result;   
})();

_.qs = function(name){
    var val = window.location.querystring[name];
    if(val==null||val=='')
        val = '0';
    var filter = /^\d*$/i
    if(!filter.test(val))
        val = '0';
    return eval(val)
}
_.qsStr = function(name){
	var val = window.location.querystring[name];
    if(val==null||val=='')
        return '';
    else
		return decodeURI(val.replace(/\+/g,"%20"));
}


/*** Comments ***/
var arrErr=['Введите имя','Введите комментарий','Код неверный']
function InitCommment(){
	var r = 0; while(r < 0.1){r = Math.random()} 
	var d = Math.floor(r*100000000)
	document.write("<input type='hidden' name='d' value='"+d+"' />")
	document.write("<img src='/handler/ch.ashx?d="+d+"' class='codeImg' />")
	_('author').v(_.qsStr('auth'))
	_('text').v(_.qsStr('text'))
	var er = _.qs('error')
	if(4>er&&er>0){
		_('errorComment').s().t(arrErr[er-1])
	}
}
function trim(string){return string.replace(/(^\s+)|(\s+$)/g, "")}
function validate(){
	_('errorComment').h()
	if(trim(_('author').v()).length==0) {_('errorComment').s().t(arrErr[0]);return false}
	if(trim(_('text').v()).length==0) {_('errorComment').s().t(arrErr[1]);return false}
	_('addComment').$.submit();
	return false;
}
function comentParent(id,e,page){
	if(id == _('parent').v() && !page){
		var ac = _('addComment');
		if(ac.isS()) ac.h(); else ac.s();
		if(id==0) _('btnCom').c(_('btnCom').c()=='opn'?'cls':'opn');
		return false
	}
	if(_('parent').v()==0)_('btnCom').c('cls');
	_('parent').v(id);
	if(id==0)_('btnCom').c('opn')
	if(!page)_('addComment').s();
	var tmp = _("addComment").$;
	_("addComment").$.parentNode.removeChild(_("addComment").$);
	e.parentNode.parentNode.appendChild(tmp);
	return false;
}
var curP = 1;
function rate(id,b){_.get('/handler/rateComment.ashx?nid='+_('nid').v()+'&cid='+id+'&p='+curP+(b?'&positive=1':''),rateEnd)}
function rateEnd(d){eval('var i = '+d);_('t'+i.id).t(i.t);_('p'+i.id).t(i.p);_('n'+i.id).t(i.n)}
function commPage(p){
	curP = p;
	var url = '/news/'+_('nid').v()+'/comments'+((p==1)?('.htm'):('/'+p+'.htm'))
	_.get(url,commPageEnd); 
	_.url("#comment")
}
function commPageEnd(d){comentParent(0, _('btnCom').$,true);_('addComment').h();_('comBody').t(d)}
// Comments

/*** All Projects ***/
var _svs = {
	show:function(){
		_('svs').h()
		_('svs2').s()
		_('svsList').s2()
	},
	hide:function(e){
		if(_('svs').$==null) return;
		
		if(_('svs').isS())
			return;
		e = e || event;
		var t = e.target || e.srcElement;
		while(t!=null){
			if(t.id=="svsList")
				return;
			t=t.parentNode
		}
		_('svs').s()
		_('svs2').h()
		_('svsList').h()
	}
}
document.onclick = _svs.hide;
// All Projects 

/*** Main item ***/
var _mi = {
	over:function(e){
		var si = _(e)
		var siImg = _(si.tn('img')[0])
		_('milia').a('href',si.a('href'))
		_('milt').t(siImg.a('title'))
		_('mili').a('alt',siImg.a('title')).a('title',siImg.a('title')).a('src', siImg.a('src').replace("90x60_","300x200_"))
	},
	out:function(e){}
}
// Main item

/*** Search ***/
var _sch = {
	select:function(e){
		if(_(e).c()=='sel')
			return;
		if(_(e).id()=='_ssite'){
			_(e).c('sel')
			_('_sweb').c('')
		}else{
			_(e).c('sel')
			_('_ssite').c('')
		}
	},
	click:function(){
		var svalue = escape(_('_stxt').v())
		if(_('_ssite').c()=='sel')
			_.url('/searchonsite.htm?text='+svalue);
		else
			window.open('http://search.oboz.ua/search.aspx?k='+svalue, '_blank');
	}
}
// Search

/*** Vote ***/
var _v = {
	'switchdiv':function(){
		if(_('_vResult').isS()){
			_('_vResult').h()
			document.getElementById("_switchDiv").innerHTML = 'Результаты голосования';
			//_('_vres').h()
			_('_vbtn').s()
			_('_vQuestino').s()
		}else{
			_v.showRes()
		}
	},
	click:function(){
		var checkedID = 0;
		foreach( _('_vQuestino').tn('input'), function(e){if(e.checked)checkedID=eval(e.value)})
		if(checkedID==0){
			_('voteError').s().t('Не выбран вариант ответа')
		}else{
			_.get('/handler/answerVote.ashx?aid='+checkedID,_v._click);
		}
	},
	_click:function(d){
		if(d.length==1){
			_('voteError').s().t('Вы уже голосовали, Ваш голос не принят!')
		} else {
			_('voteBody').p().t(d)
			_v.showRes()
		}	
	},
	showRes:function(){
		_('_vResult').s()
		document.getElementById("_switchDiv").innerHTML = 'Вернуться к голосованию';
		//_('_vres').s2()
		_('_vbtn').h()
		_('_vQuestino').h()
	}
}
// Vote

/*** Slide show ***/
var needSlide = false;
function slideClick(u){
	if(!needSlide){
		setTimeout("slide('"+u+"')",4000);
		_('fpbtn').c('fps')
	}else
		_('fpbtn').c('fpss')
	needSlide=!needSlide;
	return false
}
function slide(url){
	if(needSlide)
		_.url(url);
}
// Slide show

var _pu = {
	photo:function(e){return _.wnd(e.href, 'photo_popup', 978, 680, 1, 0, 0, 0, 0, 1, 0)},
	video:function(e){return _.wnd(e.href, 'video_popup', 978, 630, 1, 0, 0, 0, 0, 1, 0)}
}
