var Cookie = {
	get: function ( name ) {
		var start = document.cookie.indexOf( name + '=' );
		var len = start + name.length + 1;
		if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
			return null;
		}
		if ( start == -1 ) return null;
		var end = document.cookie.indexOf( ';', len );
		if ( end == -1 ) end = document.cookie.length;
		return unescape( document.cookie.substring( len, end ) );
	},
	set: function ( name, value, expires, path, domain, secure ) {
		var today = new Date(); today.setTime( today.getTime() );
		if ( expires ) {expires = expires * 1000 * 60 * 60 * 24;}
		var expires_date = new Date( today.getTime() + (expires) );
		document.cookie = name+'='+escape( value ) +
			( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) +
			( ( path ) ? ';path=' + path : '; path=/' ) +
			( ( domain ) ? ';domain=' + domain : '' ) +
			( ( secure ) ? ';secure' : '' );
	},
	del: function ( name, path, domain ) {
		if ( Cookie.get( name ) ){
			 document.cookie = name + '=' +
			( ( path ) ? ';path=' + path : ';path=/') +
			( ( domain ) ? ';domain=' + domain : '' ) +
			';expires=Thu, 01-Jan-1970 00:00:01 GMT';
		}
	}
}

var Gallery = function(id){
	var _ = this;
	this.photos 	= [];
    this.Photo  	= null;
    this.prevPhotoB = null;
    this.nextPhotoB = null;
	this.current 	= 1;
	this.obj 		= null;
	this.addPhoto	= function(url,text,comments){
		_.photos[_.photos.length] = {url:url, text:text}
		if(comments && comments.title) {
			_.photos[_.photos.length-1].title = comments.title;
		}
		if(comments && comments.descript) {
			_.photos[_.photos.length-1].descript = comments.descript;
		}
	}
	this.nextPhoto	= function(){
		_.current++;
		_.setPhoto();
	}
    this.prevPhoto	= function(){
		_.current--;
		_.setPhoto();
	}
	this.setPhoto	= function(){ 
		_.obj.down('.gall_s').update('<span>Ôîòî</span> '+_.current+' <span>èç</span> '+_.photos.length);
        _.photo.setStyle({background: 'url('+_.photos[_.current-1].url+') no-repeat 50% 50%'});
        _.photo.title 	= _.photos[_.current-1].text;
		if(_.current<=1){_.current = 1;_.prevPhotoB.hide();}else{_.prevPhotoB.show();}
        if(_.current>=_.photos.length){_.current = _.photos.length;_.nextPhotoB.hide();}else{_.nextPhotoB.show();}
		if(!_.photos[_.current-1].title && !_.photos[_.current-1].descript){
			_.commentDiv.hide();
		}
		 else {
		 	//_.commentTitle.update(_.photos[_.current-1].title);
            _.commentDescript.update(_.photos[_.current-1].descript);
			_.commentDiv.show();
		}
	}
	this.append		= function(){
		if(!$(id)){
			_.obj = new Element('DIV',{'id': id}).addClassName('gall');
			$(document.body).insert(_.obj);
		}
		 else { _.obj = $(id);}
			_.obj
			  .insert(new Element('DIV').addClassName('gall_s'))
              .insert(new Element('DIV').addClassName('butt_back').insert(
			  	   _.prevPhotoB = new Element('DIV').insert(new Element('IMG',{src:'/i/butt_back.gif'}))
													.setStyle({cursor:'pointer'})
								  					.observe('click',function(){_.prevPhoto();})
			  ))
              .insert(new Element('DIV').addClassName('butt_next').insert(
			  	   _.nextPhotoB = new Element('DIV').insert(new Element('IMG',{src:'/i/butt_next.gif'}))
													.setStyle({cursor:'pointer'})
								  					.observe('click',function(){_.nextPhoto();})
			  ))
              .insert(_.photo = new Element('DIV').setStyle({width:'612px',height:'349px'}))
			  .insert(new Element('DIV').setStyle({position:'absolute'}).insert(
			  		_.commentDiv = new Element('DIV').addClassName('shadow1').setStyle({position:'relative',top:'-100px',width:'445px'})
			  			//.insert(_.commentTitle = new Element('H1'))
                    	.insert(_.commentDescript = new Element('span'))
			  ))
			_.setPhoto();
	}
}
function toggleSubmenu(el){
	var sub = $(el).next();
	if(!sub.hasClassName('sub3')){return false;}
	if(sub.hasClassName('subShow')){sub.removeClassName('subShow');}
	 else {sub.addClassName('subShow');}
}
var TeleRotate = function (n){
	var _ = this;
	this.list = [];
	this.obj = null;
	this.curr = null;
	this.time = n;
	this.create = function(){
		var x = $$('div.ban_big');if(!x || !x[0]){return false;}
		_.obj = x[0];
	   setTimeout(function(){_.repl();},1000);
	}
	this.add = function(img, sleep, hd, text,url, dt, rzd){
		_.list[_.list.length] = {img: img, sleep: (sleep)?sleep*1000 : n, hd: hd, text: text, url: url,  dt: dt, rzd: rzd};
	}
	this.repl = function(){
		var i = _.curr||0; if(_.list.length<=i) i=0;
		if(_.list[i]){
			var o = _.list[i];
			_.obj.setStyle({background: "url("+o.img+") no-repeat 0 0"});
	        _.obj.down('.shadow').writeAttribute('lk',o.url);
	        _.obj.down('h1').update(o.hd);
	        _.obj.down('span').update(o.text);
            _.obj.previous('.b_cr').replaceChild(
				document.createTextNode(o.dt),
				_.obj.previous('.b_cr').firstChild
			);
            _.obj.previous('.b_cr').replaceChild(
				document.createTextNode(o.rzd),
				_.obj.previous('.b_cr').childNodes[2]
			);
			_.curr = i+1;
		}
		setTimeout(function(){_.repl();}, o.sleep || _.time);
	}
	this.create();
}
function jsDecode(str){
	str = str.gsub(/#(\d+);/,function(m){return String.fromCharCode(m[1]);});
	return str;
}
(function(){window.onload=function(){var x = $$('a');if(!x){return false;}x.each(function(s){if(s.hasAttribute('href') && s.readAttribute('href').substr(0,3)=='js:'){s.writeAttribute('href',jsDecode(s.readAttribute('href').substr(3)));}})}})();
