// JavaScript Document

//firebug logging function
if (!window.console || !console.firebug)
{
    var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml",
    "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];

    window.console = {};
    for (var i = 0; i < names.length; ++i)
        window.console[names[i]] = function() {}
}

//custom scrollbars
function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse){
	var steps = (horizontal?(scrollbar.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y))
	//if(scrollbar.slider != null) {console.log('detach scrollbar'); scrollbar.slider.detach();}
	console.log('new slider, steps: '+steps);
	scrollbar.slider = new Slider(scrollbar, handle, {	
		steps: steps,
		mode: (horizontal?'horizontal':'vertical'),
		onChange: function(step){
			// Scrolls the content element in x or y direction.
			var x = (horizontal?step:0);
			var y = (horizontal?0:step);
			content.scrollTo(x,y);
		}
	}).set(0);
	if( !(ignoreMouse) ){
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = scrollbar.slider.step - e.wheel * 30;	
			scrollbar.slider.set(step);					
		});
	}
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){scrollbar.slider.drag.stop()});
	
	//hide scrollbar if unnecessary
	if(steps == 0){
		scrollbar.fade('out');
	} else {
		scrollbar.fade('in');
	}
}

//login window
window.addEvent('domready',function(){
	$('login-blinder').fade('hide');
	$('login-window').fade('hide');
});
function showLogin(){
	$('login-blinder').fade('in');
	$('login-window').fade('in');
}
function hideLogin(){
	$('login-blinder').fade('out');
	$('login-window').fade('out');
}

//querystring variables
function $get(key,url){
	if(arguments.length < 2) url =location.href;
	if(arguments.length > 0 && key != ""){
		if(key == "#"){
			var regex = new RegExp("[#]([^$]*)");
		} else if(key == "?"){
			var regex = new RegExp("[?]([^#$]*)");
		} else {
			var regex = new RegExp("[?&]"+key+"=([^&#]*)");
		}
		var results = regex.exec(url);
		return (results == null )? "" : results[1];
	} else {
		url = url.split("?");
		var results = {};
			if(url.length > 1){
				url = url[1].split("#");
				if(url.length > 1) results["hash"] = url[1];
				url[0].split("&").each(function(item,index){
					item = item.split("=");
					results[item[0]] = item[1];
				});
			}
		return results;
	}
}

//form labels
window.addEvent('domready',doFormLabels);
function doFormLabels(){
	$each($$('.tiptext'),formLabels);
}
function formLabels(object){
	if(object.formlabelsetup != true){
		object.temptext = object.getProperty('title');
		if(object.getProperty('type') == 'password'){
			object.setProperty('type','text');
			object.origpassword = true;
		}
		if(object.value == ""){object.value = object.temptext;}else{object.removeClass('tiptext');}
		object.addEvent('focus',function(){
			if(this.value == this.temptext){
				this.value = "";
				this.removeClass('tiptext');
				if(this.origpassword){this.setProperty('type','password');}
			}
		});
		object.addEvent('blur',function(){
			if(this.value == ""){
				this.addClass('tiptext');
				this.value = this.temptext;
				if(this.origpassword){this.setProperty('type','text');}
			}
		});
	}
	object.formlabelsetup = true;
}

//ajax form submissions
window.addEvent('domready',doAjaxForms);
function doAjaxForms(){
	$each($$('.ajaxform'),ajaxForms);
}
function ajaxForms(object){
	if(object.ajaxFormSetup != true){
		console.log('ajax form setup: ' + object.id);
		object.spinner = new Spinner(object.getParent(),{fxOptions:{duration: 100}});
		object.set('send',{onComplete: function(result){
			object.spinner.hide();
			eval(result);
		}});
		object.doSend = function(){
			object.spinner.show();
			object.send();
		};
		object.addEvent('submit', function(e) {
			console.log('ajax form submit: ' + object.id);
			new Event(e).stop();
			object.doSend();
		});
	}
	object.ajaxFormSetup = true;
}

//newsfeed
window.addEvent('domready',function(){
	var NewsfeedSlider = new SlidePanel("template-newsfeed");
	//NewsfeedSlider.SlideMe();
	ScrollNewsfeed.periodical(10000);
});
function ScrollNewsfeed(){
	$('template-newsfeed').doSlide();;
}

//slide panels
function SlidePanel(MainDivName){
	if($(MainDivName+'-more') != null){
		$(MainDivName+'-more').addEvent('click',SlideMe);
	} else {
		$(MainDivName).doSlide = SlideMe;
	}
	var scroll = new Fx.Scroll(MainDivName, {
		wait: false,
		duration: 500,
		transition: Fx.Transitions.Quad.easeInOut
	});

	Reset();
	
	function SlideMe(){
		AreaHeight = $(MainDivName).getHeight();
		CurrentScroll = $(MainDivName).getScrollTop();
		NewScroll = AreaHeight + CurrentScroll;
		if(NewScroll >= $(MainDivName).getScrollHeight()){NewScroll = 0;}
		console.log(NewScroll);
		scroll.start(0,NewScroll)
	}
	
	function Reset(){
		scroll.set(0,0)
	}
}
