window.addEvent("domready",function(){
	
	var allstring = $('showall').getProperty('id2');
	$('showall').addEvent("click",function(e){ 
			new Event(e).stop();
			disChange(allstring, '?cuser=x'); 
			}); 	 
					
	var szNormal = 25, szSmall  = 20, szFull   = 110;	 
	var kwicks = $$("#kwicks .kwick");
	var fx = new Fx.Elements(kwicks, {wait: false, duration: 300, transition: Fx.Transitions.Back.easeOut});
	kwicks.each(function(kwick, i) {
		var usestring = kwick.getProperty('id2');
		var upstring = "cuser=" + usestring;
		var togstring = kwick.getProperty('id');
		kwick.addEvent("click",function(e){ 
			new Event(e).stop();
			disChange(togstring, upstring); 
		}); 	
	
		kwick.addEvent("mouseenter", function(event) {
			var o = {};
			o[i] = {width: [kwick.getStyle("width").toInt(), szFull]}
			kwicks.each(function(other, j) {
				if(i != j) {
					var w = other.getStyle("width").toInt();
					if(w != szSmall) o[j] = {width: [w, szSmall]};
				}
			});
			fx.start(o);
		});
	});
	 
	$("kwicks").addEvent("mouseleave", function(event) {
		var o = {};
		kwicks.each(function(kwick, i) {
			o[i] = {width: [kwick.getStyle("width").toInt(), szNormal]}
		});
		fx.start(o);
	})				

	boxUp = new Ajax('', { autoCancel: true, method:'post', update: $('inbox') });
	postReq = new Ajax('dischange.php', { autoCancel: true, method:'post', update: $('postlist'), onComplete: comLinks });
	commReq = new Ajax('ajaxcomment.php', { autoCancel: true, method:'post', onComplete: showCom });
	comTrUp = new Ajax('ctracker2.php', {method: 'post', update: $('ctracker')});
	CrazGal = new Ajax('gallery.php', { evalScripts: true, autoCancel: true, method:'post', update: $('postlist') });

	//zoomer = new SmoothScroll();
	
	comLinks(); 	
	
	HistoryManager.initialize();
				
		// no index now
		currentIndex = '0-0-0-0-DESC';
		
		reqHistory = HistoryManager.register(
		'page-index',
		['0-0-0-0-DESC'], // default, page 0
		function(values) {
			disChange(values[0]);
		},
		function(values) {
			return 'page-index(' + values[0] + ')';
		},
		/page-index\(([0-9a-zA-Z\-]+)\)/ // the regexp to match "page-index(0)"
	);		
	
	/**
	 * on start it starts checking the hash
	 */
	
	HistoryManager.start();
	
	$("crazygal").addEvent("click", function() {
			CrazGal.url = 'gallery.php';	
			CrazGal.request();
	});

	$("crazyview").addEvent("click", function() {
			CrazGal.url = 'gallery3.php';
			CrazGal.request();
	});	
	
	$("crazyups").addEvent("click", function() {
			CrazGal.url = 'testup.php';
			CrazGal.request();
	});	
	
});

function stopP(e){
	return false;	
}

function disChange(togstring, boxup) {
	
	if (currentIndex == togstring) return;	
	currentIndex = togstring;
	// updating the history
	reqHistory.setValue(0, togstring);
	var newNode = new Element('div');
	newNode.addClass('anibar');
	newNode.injectTop('postlist');
	var upstring = "upstring=" + togstring;				
	postReq.request(upstring);
	if (boxup){
		new Ajax('coninfo.php', {postBody: boxup, update: $('inbox'), onComplete: function () { FormBox("inbox"); } }).request();
	}	
	return false
}	


function FormBox(upitem) {
	$$("#" + upitem + " .ajform").each(function(el) {
		el.addEvent('submit', function(e) {
			new Event(e).stop();
			this.send({	update: $(upitem), onComplete: function() { FormBox(upitem); } });
		});
	});		

	$$("#" + upitem + " .ajpage").each(function(el) {
		el.addEvent('click', function() {
			var whichaj= el.getProperty('id');
			new Ajax(whichaj, {update: $(upitem), evalScripts: true, onComplete: function() { FormBox(upitem); } }).request();
		});
	});
}

function comLinks() {
	$$('#postlist a.comlink').each(function(el){
		var comid = el.getParent().getProperty('id');
		el.addEvent("click",function(e){ 
			new Event(e).stop();
			ajaxcomtog(comid); 
		});
	});
}


function ajaxcomtog(pid){
	
	if ($('comdis')) {
		var opencom = $('comdis').parentNode;		
		if (opencom != $(pid)) {
			$('comdis').remove();			
			var comString = "which=" + pid;
			comNode = new Element('div');
			comNode.setAttribute("id", "comdis");
			$(pid).appendChild(comNode);
			$('comdis').innerHTML = "<div class='anibar'> </div>";				
			commReq.request(comString);
		} 
		else {  
		$('comdis').remove();				
			//if ($('scrollset').checked){ 
			//	zoomer.toTop();
			//	}
		}		
	}
	else {
		var comString = "which=" + pid;
		comNode = new Element('div');
		comNode.setAttribute("id", "comdis");
		$(pid).appendChild(comNode);	
		$('comdis').innerHTML = "<div class='anibar'> </div>";			
		commReq.request(comString);	
	}

}
	
	
function showCom(request) {
	if ($('comdis')) {
		$('comdis').innerHTML = request;
		document.comment.comtext.focus();
		//if ($('scrollset').checked){ 
		//	var openercom = $('comdis').parentNode.parentNode;
		//	zoomer.toElement(openercom); 
		//}		
	}
}

function ajaxcoms(pid) {
	var comtextf = $('comtext').value;
	if (comtextf !=  "") { 
		if (!$('newcom')){
			newCom = document.createElement('div');
			newCom.setAttribute("id", "newcom");		
			$('comdiscoms').appendChild(newCom);
			$('newcom').addClass('newcomred'); 				
			newCom.innerHTML = "<div class='anibar'> </div>";
		}		
		if ($('csec').checked) { var sec ='yes' ; } else { var sec = ''; } 				
		var ucomString = "comtext=" + encodeURIComponent(comtextf) + "&sec=" + sec + "&when=" + $('cwhen').value;
		new Ajax('f_ajaxprevcom.php', {postBody: ucomString, onComplete: onPrevCom}).request();
	}
}

function onPrevCom(request) {					
	newCom.innerHTML = request;
	$('comtext').removeEvents('keydown');
	$('comtext').addEvent('keydown', function(){
		$('postcombutt').remove();
		$('comtext').removeEvents('keydown');
	});
	document.comment.comtext.focus();	
}


function onsubCom() {		
	$('postcombutt').innerHTML = "<div class='loader'> </div>";
	if ($('csec').checked) { var sec ='yes' ; } else { var sec = ''; } 				
	var ucomString = "comtext=" + encodeURIComponent($('comtext').value) + "&sec=" + sec + "&when=" + $('cwhen').value;
	new Ajax('f_ajaxupcom.php', {postBody: ucomString, onComplete: onReturnCom}).request();	
}

function onReturnCom(request) {
	var comnums = "con" + request + "num";
	$('postcombutt').remove();
	var myBox = new Fx.Styles('newcom', {duration: 500});
	myBox.start({ 'background-color': ['000000'] });	
	$('comtext').value = '';
	$('ajcomment').remove();
	var comm = $(comnums).innerHTML;			
	$(comnums).innerHTML = parseInt(comm) + 1;			
	$('newcom').setAttribute("id", "");
	comTrUp.request();				
}