$(document).ready(function(){
	initExpanders();
	initScrollers();
});

function toggleCookieSet(name){
	return readCookie('tog_'+name);
}
function setToggleCookie(name){
	createCookie('tog_'+name,'1');
}
function unsetToggleCookie(name){
	eraseCookie('tog_'+name);
}

function initExpanders(){
	$('.expand').each(function(){
		var sel = '#'+(this.getAttribute('title'));
		if(!toggleCookieSet(this.getAttribute('title'))){
			$(sel).hide();
		}
	});
	$('.expand').click(function(){
		var title = this.getAttribute('title');
		var el = $('#'+title);
		if(el.is(':hidden')){
			setToggleCookie(title);
			el.show();
		}else{
			unsetToggleCookie(title);
			el.hide();
		}
	});
}

function initScrollers(){
	var j_timer = null;
	var p_timer = null;
	/* Scrolling Articles */
	var j_api = $('#scrollable-jobs').scrollable({
		vertical:true,
		clickable:false,
		speed:1000,
		steps:1
	}).circular(
	).autoscroll({
		steps:1,
		interval:5000,
		autoplay:false,
		api:true
	});
	
	var p_api = $('#scrollable-profiles').scrollable({
		vertical:true,
		clickable:false,
		speed:1000,
		steps:1
	}).circular(
	).autoscroll({
		steps:1,
		interval:5000,
		autoplay:false,
		api:true
	});
	
	/* Toggles */
	$('#toggle-jobs').click(function(){
		var sc = $('#scrollable-jobs');
		if(sc.is(':hidden')){
			sc.show();
			setToggleCookie(sc.attr('id'));
			j_timer = window.setTimeout(function(){
				j_api.play();
			},5000);
		}else{
			sc.hide();
			window.clearTimeout(j_timer);
			unsetToggleCookie(sc.attr('id'));
			j_api.stop();
		}
	});
	
	$('#toggle-profiles').click(function(){
		var sc = $('#scrollable-profiles');
		if(sc.is(':hidden')){
			sc.show();
			setToggleCookie(sc.attr('id'));
			p_timer = window.setTimeout(function(){
				p_api.play();
			},5000);
		}else{
			sc.hide();
			window.clearTimeout(p_timer);
			unsetToggleCookie(sc.attr('id'));
			p_api.stop();
		}
	});

	// Hide Togglers
	$('.scrollable').each(function(){
		if(!toggleCookieSet(this.id)){
			$(this).hide();
		}
	});
	
	if(toggleCookieSet('scrollable-jobs')){
		j_timer = window.setTimeout(function(){
			j_api.play();
		},5000);
		//alert('jobs should be scrolling');
	}
	if(toggleCookieSet('scrollable-profiles')){
		p_timer = window.setTimeout(function(){
			p_api.play();
		},5000);
	}
}
