window.addEvent('domready', function() {

	resize();
	window.addEvent('resize', resize);
	
	var loc = location.href;
	if(strpos(loc,'#')>2){loc = loc.substr(strpos(loc,'#')+1,100);}else{loc='';};
	if(loc.length>2){
		var sizex = window.getSize();
		var el = loc
		var sizey =  $(el).getSize();
		var posy = $(el).getPosition();
		var posz = posy.x - sizex.x/2+sizey.x/2;
		var myFx = new Fx.Scroll(window,{duration: 1800}).start(posz,0); 
	}
	
	// resize
	function resize(){

		var total = $('container').getSize();
		var maxy = 0;
			
		$$('#content .wall').each(function(el) {
			
			var size  = el.getSize();
			var mid   = total.y/2 - size.y/2 - 50; 
			var pos   = el.getPosition();
			maxtmp = pos.x+size.x;
			el.setStyle('margin-top',mid+'px');
			if(maxtmp>maxy){maxy=maxtmp+350};
		})
		
		document.body.setStyle('width',maxy+'px');
		
		nrpk = 1;
		for(i=400;i<(maxy);i+=1000){	
		if(nrpk>4) break;
			var silimg = new Element ('img' , {'src' : '/wp-content/themes/laipa/img/silhuette-'+nrpk+'.png' , 'class' : 'silhuette' , 'id' : 'silhuette-'+nrpk});
			silimg.injectInside($('footer'));
			nrpk++;
		}
	};
	

	// move
	 $$('#menu a').addEvent('click', function(el) {
		var sizex = window.getSize();
		var el = this.get('href').substr(1)
		var sizey =  $(el).getSize();
		var posy = $(el).getPosition();
		var posz = posy.x - sizex.x/2+sizey.x/2;
		var myFx = new Fx.Scroll(window,{duration: 1800}).start(posz,0); 
		
		return false;
	});
	
	// move
	if($('galerija-link')){
	 $('galerija-link').addEvent('click', function(el) {
		var sizex = window.getSize();
		var sizey =  $('galerija').getSize();
		var posy = $('galerija').getPosition();
		var posz = posy.x - sizex.x/2+sizey.x/2;
		var myFx = new Fx.Scroll(window,{duration: 1800}).start(posz,0); 		
		return false;
	});
	};
	 
	//request
	
	
	$$('.subcats a').each(function(el) {
		var parent = el.getParent();
		parent = parent.getParent();
		parentid = parent.id.substr(5,200);

		el.addEvent('click', function() {
		
			var parent = this.getParent();
			parent = parent.getParent();
			parentid = parent.id
		
			req.container = parentid.substr(5,100);
			req.send({url:'/ajax.php?id='+this.id});

		    $$('#'+parent.id+' a').each(function(el) {el.removeClass('active')});
			this.addClass('active');			
			return false;
		});	
	});
	
	var req = new Request.HTML({ 
		onSuccess: function(html) {
			$(req.container+'-container').set('text', '');
			$(req.container+'-container').adopt(html);
		},
		onFailure: function(t) {	
			$(req.container+'-container').set('text', '');
			resp = t.responseText;
			$(req.container+'-container').innerHTML =  resp;
		}
	});
	
	if($('box')){var scr = new ScrollBar('box', 'jaunumi-container');};
	if($('box-jaunumi')){var scr = new ScrollBar('box-jaunumi', 'jaunumi-container');};
	if($('box-pargaleriju')){var scr = new ScrollBar('box-pargaleriju', 'pargaleriju-container');};
	if($('box-arhivs')){var scr = new ScrollBar('box-arhivs', 'arhivs-container');};

		
	document.addEvent('mousewheel', function(event) {
		event = new Event(event);
	 
		if (event.wheel > 0) {
			window.scrollBy(-event.wheel*30,0);
		} 
		else if (event.wheel < 0) {
			window.scrollBy(-event.wheel*30,0);
		}
	});
	

})



    var luna = {
      src: '/js/luna.swf'
    };
    sIFR.activate(luna);
    
    sIFR.replace(luna, {
    		selector: 'h3',
    		wmode: 'transparent',
    		css: [
        		'.sIFR-root { text-align: center; font-size: 16px; color:#4e4e4e }'
      		]   		
	});
	
	function strpos( haystack, needle, offset){
	    var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
	    return i === -1 ? false : i;
	}

	
var ScrollBar = new Class({

		Implements: [Events, Options],

		options: {
			maxThumbSize: 7,
			wheel: 8,
			arrows: true,
			hScroll: false // horizontal scrollbars
		},

		initialize: function(main, content, options){
			this.setOptions(options);
			
			this.main = $(main);
			this.content = $(content);
			
			if (this.options.arrows == true){
				this.arrowOffset = 0;
			} else {
				this.arrowOffset = 0;
			}
			
			this.vScrollbar = new Element('div', {
    				'class': 'vScrollbar'
				}).injectAfter(this.content);				

			if (this.options.arrows == true){				
				this.arrowUp = new Element('div', {
    					'class': 'arrowUp'
					}).injectInside(this.vScrollbar);
			}	

			this.vTrack = new Element('div', {
    				'class': 'vTrack'
				}).injectInside(this.vScrollbar);
				
			this.vThumb = new Element('div', {
    				'class': 'vThumb'
				}).injectInside(this.vTrack);

			if (this.options.arrows == true){				
				this.arrowDown = new Element('div', {
    					'class': 'arrowDown'
					}).injectInside(this.vScrollbar);
			}		
				
			
			this.bound = {
				'vStart': this.vStart.bind(this),			
				'end': this.end.bind(this),
				'vDrag': this.vDrag.bind(this),			
				'wheel': this.wheel.bind(this),
				'vPage': this.vPage.bind(this)			
			};

			this.vPosition = {};			
			this.vMouse = {};			
			this.update();
			this.attach();
		},

		update: function(){
			

			this.vTrack.setStyle('height', this.content.offsetHeight - this.arrowOffset);
		
			this.vContentSize = this.content.offsetHeight;
			this.vContentScrollSize = this.content.scrollHeight;
			this.vTrackSize = this.vTrack.offsetHeight;

			this.vContentRatio = this.vContentSize / this.vContentScrollSize;

			this.vThumbSize = (this.vTrackSize * this.vContentRatio).limit(this.options.maxThumbSize, this.vTrackSize);

			this.vScrollRatio = this.vContentScrollSize / this.vTrackSize;

			this.vThumb.setStyle('height', this.vThumbSize);

			this.vUpdateThumbFromContentScroll();
			this.vUpdateContentFromThumbPosition();
			
		},

		vUpdateContentFromThumbPosition: function(){
			this.content.scrollTop = this.vPosition.now * this.vScrollRatio;
		},
		

		vUpdateThumbFromContentScroll: function(){
			this.vPosition.now = (this.content.scrollTop / this.vScrollRatio).limit(0, (this.vTrackSize - this.vThumbSize));
			this.vThumb.setStyle('top', this.vPosition.now);
		},
		

		attach: function(){
			this.vThumb.addEvent('mousedown', this.bound.vStart);
			if (this.options.wheel) this.content.addEvent('mousewheel', this.bound.wheel);
			this.vTrack.addEvent('mouseup', this.bound.vPage);
			
			if (this.options.arrows == true){
				this.arrowUp.addEvent('mousedown', function(event){
						this.interval = (function(event){
						this.content.scrollTop -= this.options.wheel;
						this.vUpdateThumbFromContentScroll();
					}.bind(this).periodical(40))
				}.bind(this));
			
				this.arrowUp.addEvent('mouseup', function(event){
					$clear(this.interval);
				}.bind(this));
				
				this.arrowUp.addEvent('mouseover', function(event){
					this.arrowUp.addClass('active-up');
				}.bind(this));	
			
				this.arrowUp.addEvent('mouseout', function(event){
					$clear(this.interval);
					this.arrowUp.removeClass('active-up');
				}.bind(this));
						
				this.arrowDown.addEvent('mousedown', function(event){
						this.interval = (function(event){
						this.content.scrollTop += this.options.wheel;
						this.vUpdateThumbFromContentScroll();
					}.bind(this).periodical(40))
				}.bind(this));
			
				this.arrowDown.addEvent('mouseup', function(event){
					$clear(this.interval);
				}.bind(this));


				this.arrowDown.addEvent('mouseover', function(event){
					this.arrowDown.addClass('active-down');
				}.bind(this));			
				
										
			
				this.arrowDown.addEvent('mouseout', function(event){
					$clear(this.interval);
					this.arrowDown.removeClass('active-down');
				}.bind(this));
			
			}			
						
		},
		
		wheel: function(event){
			this.content.scrollTop -= event.wheel * this.options.wheel;
			this.vUpdateThumbFromContentScroll();
			event.stop();
		},

		vPage: function(event){
			if (event.page.y > this.vThumb.getPosition().y) this.content.scrollTop += this.content.offsetHeight;
			else this.content.scrollTop -= this.content.offsetHeight;
			this.vUpdateThumbFromContentScroll();
			event.stop();
		},		

		vStart: function(event){
			this.vMouse.start = event.page.y;
			this.vPosition.start = this.vThumb.getStyle('top').toInt();
			document.addEvent('mousemove', this.bound.vDrag);
			document.addEvent('mouseup', this.bound.end);
			this.vThumb.addEvent('mouseup', this.bound.end);
			event.stop();
		},	

		end: function(event){
			document.removeEvent('mousemove', this.bound.vDrag);
			document.removeEvent('mousemove', this.bound.hDrag);			
			document.removeEvent('mouseup', this.bound.end);
			this.vThumb.removeEvent('mouseup', this.bound.end);
			this.hThumb.removeEvent('mouseup', this.bound.end);			
			event.stop();
		},

		vDrag: function(event){
			this.vMouse.now = event.page.y;
			this.vPosition.now = (this.vPosition.start + (this.vMouse.now - this.vMouse.start)).limit(0, (this.vTrackSize - this.vThumbSize));
			this.vUpdateContentFromThumbPosition();
			this.vUpdateThumbFromContentScroll();
			event.stop();
		}

	});

