window.addEvent("domready",function() {
	
	zIndexx = 4;
	
	$$('.contentbox').dispose();
	
	checkLinksEl();
	
	$each($$('.altvcontentbox'),function(el,i) {
		el.set('id','ax'+i);								   
		$('ax'+i).makeDraggable({
		  snap: 0, 
		  handle: $$('#'+'ax'+i+' .dragbut'),
		  limit: {y: [0,]},
		  onSnap: function() { $('ax'+i).setStyles({'z-index':zIndexx}); zIndexx++;  }
    	});
		
		$$('#ax'+i+' a.closebut').addEvent('click',function(event) {
			event.stop();
			$('ax'+i).fade('out');
		});
		
	});


});


function checkLinksEl() {

$each($$('a.tojs'),function(el,i) {
		
		var curID = el.get('id');
		
		new Element('div', {'id': curID+'_box', 'html': '<a href="#" class="closebut"></a><div class="dragbut"></div><div class="innercbox" id="'+curID+'_inner'+'"></div>','class': 'contentbox jsed'}).inject($('container'));
		
		$(curID+'_box').fade('out').get('tween').chain(function() { $(curID+'_box').setStyle('display','block'); });
		
		$(curID+'_box').makeDraggable({
		  snap: 0, 
		  handle: $$('#'+curID+'_box .dragbut'),
		  limit: {y: [0,]},
		  onSnap: function() { $(curID+'_box').setStyles({'z-index':zIndexx}); zIndexx++;  }
    	});
		
		
		
		//var aniMation = new Fx.Reveal($(curID+'_box'), {transition: Fx.Transitions.Back.easeOut, duration: 1100, mode: 'vertical'});
		var aniMation = function() { $(curID+'_box').setStyles({'z-index':zIndexx}); $(curID+'_box').fade('toggle'); zIndexx++; }
		if(el.get('html').match(/\<span\>/)) { 
			$$('a.tojs')[i].getChildren('span')[0].fade('out').get('tween').chain(function() { $$('a.tojs')[i].getChildren('span')[0].setStyle('display','block');  });
		el.addEvent('mouseenter',function(event) {
			$$('a.tojs')[i].getChildren('span')[0].fade('in');
		});
		
		el.addEvent('mouseleave',function(event) {
			$$('a.tojs')[i].getChildren('span')[0].fade('out');
		});
	
		}
		
		
		el.addEvent('click',function(event) {
			if(event) { event.stop(); }
			
			new Request.HTML({  
				method: 'get', 
				url: el.get('href')+'?q=moo',
				update: $(curID+'_inner'),
				onComplete: function() { 
					
					renameSomeIds(curID);
					
					if($(el.get('id')+'_inner').get('html').match(/secmenu/i)) 
					{
						//$$('#'+el.get('id')+'_inner #secmenu').set('id',el.get('id')+'_secmenu');
						//console.log($(el.get('id')+'_secmenu').getDimensions().y);
						//console.log($(el.get('id')+'_inner').getFirst('#secmenu').getDimensions().y);
						$(curID+'_scrollbox').setStyle('height',$(curID+'_scrollbox').getStyle('height').toInt()-40);
						$(curID+'_scrollbar').setStyles({'height':$(curID+'_scrollbar').getStyle('height').toInt()-40,'top':$(curID+'_scrollbar').getStyle('top').toInt()+51});
						secNavPoints(curID+'_inner');
					}
					
					
					new Request({  
						method: 'get',
						url: 'titlecon.php?q='+el.get('href'),
						onSuccess: function(txt,xxml)  {document.title = txt} // document.title = txt.textContent;
						}).send();
						
					
					aniMation(); 
					noSpam();
					//alert(($(curID+'_txtinner').getDimensions().y+$(curID+'_h2').getDimensions().y) + " out " + $(curID+'_inner').getDimensions().y);
					if(($(curID+'_txtinner').getDimensions().y+$(curID+'_h2').getDimensions().y)>$(curID+'_inner').getDimensions().y) {
					//$(curID+'_scrollbar').setStyle('display','block'); 
					
					makeScrollbar( $(curID+'_scrollbox'), $(curID+'_scrollbar'), $(curID+'_scrollh') ); 
					
					
					}
					else {$(curID+'_scrollbar').setStyle('display','none');}
					checkInnerlinks();
					milkbox.reloadGalleries();

				}
			}).send();

		});
		
		$$('#'+curID+'_box a.closebut').addEvent('click',function(event) {
			event.stop();
			aniMation();
			
			
		});
	});

}

function checkInnerlinks() {

	$each($$('a.tojsinner'),function(el,i) {
		el.addEvent('click',function(event) {
			if(event) { event.stop(); }
			
			new Request.HTML({  
				method: 'get', 
				url: el.get('href')+'?q=moo',
				update: $('null_inner'),
				onComplete: function() { 
					
					renameSomeIds('null');
					
					if($('null_inner').get('html').match(/secmenu/i)) 
					{
						//$$('#'+el.get('id')+'_inner #secmenu').set('id',el.get('id')+'_secmenu');
						//console.log($(el.get('id')+'_secmenu').getDimensions().y);
						//console.log($(el.get('id')+'_inner').getFirst('#secmenu').getDimensions().y);
						$('null_scrollbox').setStyle('height',$('null_scrollbox').getStyle('height').toInt()-40);
						$('null_scrollbar').setStyles({'height':$('null_scrollbar').getStyle('height').toInt()-40,'top':$('null_scrollbar').getStyle('top').toInt()+51});
						secNavPoints('null_inner');
					}
					
					
					new Request({  
						method: 'get',
						url: 'titlecon.php?q='+el.get('href'),
						onSuccess: function(txt,xxml)  {document.title = txt} // document.title = txt.textContent;
						}).send();
						
					
					$('null_box').setStyles({'z-index':zIndexx}); $('null_box').fade('toggle'); zIndexx++;
					noSpam();
					//alert(($(curID+'_txtinner').getDimensions().y+$(curID+'_h2').getDimensions().y) + " out " + $(curID+'_inner').getDimensions().y);
					if(($(curID+'_txtinner').getDimensions().y+$(curID+'_h2').getDimensions().y)>$('null_inner').getDimensions().y) {
					//$(curID+'_scrollbar').setStyle('display','block'); 
					makeScrollbar( $('null_scrollbox'), $('null_scrollbar'), $('null_scrollh') ); 
					
					
					}
					else {$('null_scrollbar').setStyle('display','none');}
					checkInnerlinks();
					milkbox.reloadGalleries();

				}
			}).send();
			
		});
	});
}

function secNavPoints(divID) {
	var cutID = divID.replace(/(.*)_.*/,"$1");
	var tmpScSz = $(cutID+'_scrollbar').getStyle('top');
	
	$each($$('#'+divID+' .secmenu a'),function(el,i) {
		
		el.addEvent('click',function(event) {

			event.stop();
			new Request.HTML({  
				method: 'get', 
				url: el.get('href')+'?q=moo',
				update: $(cutID+'_inner'),
				onSuccess: function() { 
					
					renameSomeIds(cutID);
					
					
					
					if($(cutID+'_inner').get('html').match(/secmenu/i)) 
					{
						$(cutID+'_scrollbox').setStyle('height',$(cutID+'_scrollbox').getStyle('height').toInt()-40);
						$(cutID+'_scrollbar').setStyles({'height':$(cutID+'_scrollbar').getStyle('height').toInt()-40,'top':tmpScSz});
					}
					
					if(($(cutID+'_txtinner').getDimensions().y+$(cutID+'_h2').getDimensions().y)>$(cutID+'_scrollbox').getDimensions().y) {
					makeScrollbar( $(cutID+'_scrollbox'), $(cutID+'_scrollbar'), $(cutID+'_scrollh') ); 
					}
					else {$(cutID+'_scrollbar').setStyle('display','none');}
					
					new Request({  
						method: 'get',
						url: 'titlecon.php?q='+el.get('href'),
						onSuccess: function(txt,xxml)  {document.title = txt} // document.title = txt.textContent;
						}).send();
					
					noSpam();
					checkInnerlinks();
					secNavPoints(cutID+'_inner');
					milkbox.reloadGalleries();

				}
			}).send();

		});
	});
	
}


function renameSomeIds(elID) {
	$$('#'+elID+'_box #scrollbox').set({'id':elID+'_scrollbox','class':'scrollbox'});
	$$('#'+elID+'_box #scrollbar').set({'id':elID+'_scrollbar','class':'scrollbar'});
	$$('#'+elID+'_box #scrollh').set({'id':elID+'_scrollh','class':'scrollh'});
	$$('#'+elID+'_box .textinner').set({'id':elID+'_txtinner'});
	$$('#'+elID+'_scrollbox h2').set({'id':elID+'_h2'});
}





function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse){
	var steps = (horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y))
	var slider = new Slider(scrollbar, handle, {	
		steps: steps,
		mode: (horizontal?'horizontal':'vertical'),
		onChange: function(step){
			var x = (horizontal?step:0);
			var y = (horizontal?0:step);
			content.scrollTo(x,y);
		}
	}).set(0);
	
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 30;	
			slider.set(step);					
		});
	
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
	
}
