swtc = function(){
	
	window.swtc = this;
	
	
	this.loading_posts = false;
	
	
	this.load_posts = function(dir){
		
		var offset = this.data.offset + (this.data.posts_per_page * dir);
		offset = offset < 0 ? 0 : offset;
		
		if(offset < this.data.posts_count){
			this.loading_posts = true;
			ijs.ajax.request({
					'url': ijs.config('template_directory') +'/load_posts.php?o='+ offset +'&c='+ this.data.posts_per_page,
					'dir': dir,
					'response_type': 'json',
					'callback': function(){
						swtc.load_posts_content(this.response.content, this.dir);
						swtc.data.offset = swtc.data.offset + ( this.dir * this.response.count );
						swtc.data.offset = swtc.data.offset < 0 ? 0 : swtc.data.offset;
						swtc.arrow_up.style.visibility = swtc.data.offset <= 0 ? 'hidden' : 'visible';
						swtc.arrow_down.style.visibility = swtc.data.offset + (swtc.data.posts_per_page * swtc.new_ul_dir) >= swtc.data.posts_count ? 'hidden' : 'visible';
						}
					}
				);
			}
		}
	
	
	this.load_posts_content = function(content, dir){
		
		this.old_ul = this.sidebar.getElementsByTagName('ul')[0];
		this.new_ul = document.createElement('ul');
		this.new_ul.innerHTML = content;
		this.new_ul.style.position = 'absolute';
		this.new_ul.style.left = '0px';
		this.new_ul.style.top = (dir * 100) + '%';
		this.new_ul_top = dir * 100;
		this.new_ul_dir = dir;
		this.sidebar.appendChild(this.new_ul);
		
		this.old_ul.style.position = 'absolute';
		this.old_ul.style.top = '0%';
		this.old_ul.style.left = '0px';
		
		this.v = 1;
		this.load_posts_anim();
		
		
		//
		
		}
	
	
	this.load_posts_anim = function(){
		
		
		if(this.new_ul_dir > 0){
			if(this.new_ul_top > 50) {
				this.v += 1;
				this.new_ul_top -= this.v;
				}
			else{
				this.new_ul_top -= this.new_ul_top / 2;
				
				}
			
			this.new_ul.style.top = parseInt(this.new_ul_top) + '%';
			this.old_ul.style.top = '-' + parseInt(100 - this.new_ul_top) + '%'
			
			}
		else{
			
			if(this.new_ul_top < -50) {
				this.v += 1;
				this.new_ul_top += this.v;
				}
			else{
				this.new_ul_top += Math.abs(this.new_ul_top) / 2;
				}
			
			if(this.new_ul_top > 0) this.new_ul_top = 0;
			
			this.new_ul.style.top = parseInt(this.new_ul_top) + '%';
			this.old_ul.style.top = parseInt(100 + this.new_ul_top) + '%'
			
			
			//this.new_ul.style.display = 'none';
			
			}
		
		
		
		if(parseInt(this.new_ul_top) != 0){
			if(this.new_ul_top >= -100 && this.new_ul_top <= 100)
				setTimeout(function(){ swtc.load_posts_anim() }, 50);
			}
		else{
			this.old_ul.parentNode.removeChild(this.old_ul);
			this.loading_posts = false;
			}
		}
	
	
	this.get_doc_height = function() {
			var D = document;
			return Math.max(
				Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
				Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
				Math.max(D.body.clientHeight, D.documentElement.clientHeight)
			);
		}
	
	
	this.get_scroll_xy = function (){
		var scrOfX = 0, scrOfY = 0;
		
		if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
			} 
		else 
			if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
				//DOM compliant
				scrOfY = document.body.scrollTop;
				scrOfX = document.body.scrollLeft;
				} 
			else 
				if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
					//IE6 standards compliant mode
					scrOfY = document.documentElement.scrollTop;
					scrOfX = document.documentElement.scrollLeft;
					}
		return { x: scrOfX, y: scrOfY };
		}
	
	
	this.loading_img = false;
	this.load_img = function(old_img){
		
		if(!swtc.img_h){
			this.loading_img = true;
			
			
			var img_h = document.createElement('div');
			var img = old_img.cloneNode(true);
			
			//img.src= img_url;
			
			swtc.img_h = img_h;
			img_h.style.position = 'absolute';
			img_h.style.top = '50%';
			img_h.style.left = '50%';
			//img_h.style.padding = '10px';
			img_h.style.zIndex = 9999;
			img_h.style.border = '1px solid #DAD9D0';
			img_h.style.background = '#FFF url('+ old_img.src +') center no-repeat';
			img_h.style.height = img_h.style.width = '0px';
			img_h._width = old_img.width + 10;
			img_h._height = old_img.height + 10;
			
			
			img_h._scroll_offset = this.get_scroll_xy();
			
			img_h._current_p = 0;
			//ijs.css
			
			img_h.onclick = this.unload_img;
			
			var cover = document.createElement('div');
			img_h.cover = cover;
			cover.style.position = 'absolute';
			cover.style.top = '0px';
			cover.style.left = '0px';
			cover.style.width = '100%';
			cover.style.height = this.get_doc_height() + 'px';
			cover.style.zIndex = img_h.style.zIndex - 2;
			cover.style.background = '#FFF';
			cover.onclick = this.unload_img;
			ijs.css.set_opacity(cover, 0);
			
			document.body.appendChild(cover);
			document.body.appendChild(img_h);
			
			img_h.v = 0;
			this.load_img_anim();
			
			}
		else{
			if(!swtc.loading_img){
				swtc.img_h.v = 0;
				swtc.loading_img = true;
				swtc.unload_img_anim();
				}
			}
			
		};
	
	
	this.unload_img_anim = function(){
		
		this.img_h.v += 1.5;
		this.img_h._current_p -= this.img_h.v;
		
		var height = parseInt( this.img_h._current_p / 100 * this.img_h._height );
		var width = parseInt( this.img_h._current_p / 100 * this.img_h._width );
		
		height = height > 0 ? height : 0;
		width = width > 0 ? width : 0;
		
		this.img_h.style.width = width + 'px';
		this.img_h.style.height = height + 'px';
		
		this.img_h.style.marginTop = Math.ceil( - height / 2) + this.img_h._scroll_offset.y + 'px';
		this.img_h.style.marginLeft = Math.ceil( - width / 2) + 'px';
		ijs.css.set_opacity(this.img_h, parseInt(this.img_h._current_p));
		
		ijs.css.set_opacity(this.img_h.cover, this.img_h._current_p > 0 ? this.img_h._current_p/100 * 75 : 0);
		
		
		
		if(this.img_h._current_p > 0)
			setTimeout(function(){ swtc.unload_img_anim(); }, 10);
		else{
			
			this.img_h.cover.parentNode.removeChild(this.img_h.cover);
			this.img_h.parentNode.removeChild(this.img_h);
			this.img_h = false;
			ijs.css.set_opacity(this.page_wrapper, 100);
			}
		
		};
	
	
	this.load_img_anim = function(){
		
		this.img_h._current_p += Math.ceil((100 - this.img_h._current_p) / 4);
		
		var height = parseInt( this.img_h._current_p / 100 * this.img_h._height );
		var width = parseInt( this.img_h._current_p / 100 * this.img_h._width );
		
		this.img_h.style.width = width + 'px';
		this.img_h.style.height = height + 'px';
		
	
		this.img_h.style.marginTop = Math.ceil( - height / 2) + this.img_h._scroll_offset.y + 'px';
		this.img_h.style.marginLeft = Math.ceil( - width / 2) + 'px';
		ijs.css.set_opacity(this.img_h, parseInt(this.img_h._current_p));
		
		ijs.css.set_opacity(this.img_h.cover, parseInt(this.img_h._current_p/100 * 75));
		
		
		if(this.img_h._current_p < 100)
			setTimeout(function(){ swtc.load_img_anim(); }, 10);
		else{
			
			var close_btn = document.createElement('div');
			close_btn.className = 'close_btn';
			close_btn.innerHTML = 'Close Me';
			close_btn.style.zIndex = this.img_h.style.zIndex + 1;
			document.body.appendChild(close_btn);
			close_btn.style.top = this.img_h.offsetTop - close_btn.offsetHeight + 2 + 'px';
			close_btn.style.left =  this.img_h.offsetLeft + this.img_h.offsetWidth - close_btn.offsetWidth + 'px';
			this.img_h.close_btn = close_btn;
			close_btn.onclick = this.unload_img;
			this.loading_img = false;
			}
		
		};
	
	
	this.unload_img = function(){
		
		if(!swtc.loading_img){
			swtc.load_img();
			swtc.img_h.close_btn.parentNode.removeChild(swtc.img_h.close_btn);
			swtc.img_h.close_btn = null;
			}
		return false;
		}
	
	
	this.sidebar_wrapper = ijs.$('sidebar_wrapper');
	this.sidebar = ijs.$('sidebar');
	ijs.$$('post_img', document, 'div', 1).first(function(){
			
			var plus = ijs.$$('plus', this, 'div', 1).first(function(){ this.style.display = 'block'; });
			
			var img = this.getElementsByTagName('img')[0];
			
			img.onclick = function(){
				swtc.load_img(this);
				return false;
				}
				
				
			plus._img = img;
			plus.onclick = function(){
				swtc.load_img(this._img);
				return false;
				}
			
			}
		);
	
	this.data = eval('('+ ijs.$('sidebar_data').innerHTML +')');
	this.data.li_height = parseInt(ijs.css.get(this.sidebar.getElementsByTagName('li')[0], 'height'));
	this.sidebar.style.height = this.data.li_height * this.data.posts_per_page + 'px'; 
	this.sidebar.style.overflow = 'hidden';
	
	this.page_wrapper = ijs.$('page_wrapper');
	
	this.arrow_up = ijs.$$('arrow_up', this.sidebar_wrapper, 'div', 1).first();
	this.arrow_down = ijs.$$('arrow_down', this.sidebar_wrapper, 'div', 1).first();
	this.arrow_up.onclick = function(){ if(!swtc.loading_posts) swtc.load_posts(-1); }
	this.arrow_down.onclick = function(){ if(!swtc.loading_posts) swtc.load_posts(1); }
	
	if(this.data.offset <= 0)	
		this.arrow_up.style.visibility = 'hidden';
	if(this.data.posts_per_page >= this.data.posts_count)	
		this.arrow_down.style.visibility = 'hidden';
	
	
	return this;
	
	};


ijs.onload(function(){
		
		new swtc;
		
	}
);

