// State Scope Code
(function(){d=document;e=d.documentElement;c="images-on";i=new Image();t=i.style;s=d.enableStateScope=function(s,o){if(o)e.className+=" "+s;else e.className=e.className.replace(new RegExp("\\b"+s+"\\b"),"");};if(t.MozBinding!=null){t.backgroundImage="url("+d.location.protocol+"//0)";b=window.getComputedStyle(i,'').backgroundImage;if(b!="none"&&b!="url(invalid-url:)"||d.URL.substr(0,2)=="fi")s(c,true);}else{t.cssText="-webkit-opacity:0";if(t.webkitOpacity==0){i.onload=function(){s(c,i.width>0);};i.src="data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==";}else{i.onerror=function(){s(c,true);};i.src="about:blank";}}})();

function SetRegistrationOptions() {
	if ($('registration_type').value == 'standard') {
		$('standard_registrations').setStyle('display', 'table-row');
		$('student_registrations').setStyle('display', 'none');
	}
	else {
		$('student_registrations').setStyle('display', 'table-row');
		$('standard_registrations').setStyle('display', 'none');	
	}
}

function UpdateTwitterTemplate() {
	$('twitter_actuator').setProperty('href','http://www.addthis.com/bookmark.php?v=250&winname=addthis&pub=dekode&source=tbx-250&lng=en-us&s=twitter&url=http%3A%2F%2F2011.oslofreedomforum.com%2Fspeakers%2Flech_walesa.html&title=Oslo%20Freedom%20Forum%20-%20Lech%20Walesa&ate=AT-dekode/-/fs-0/4d673b9bee19ef57/1&template=' + $('twitter_text').value + '%20%7B%7Burl%7D%7D');
}

function UpdateList(_source,_custom) {
	if ( $$('.selectors').length == 0) {
		return;
	}

	// If this is the "view all button", clear all the rest
	if (_source.name == 'sort-all' && _source.checked) {
		$$('.selectors input').each(function(input) {
			if (input.name != 'sort-all') {
				input.checked = false;
			}
		});
		_custom.clear();
		
		$$('.list_items li').each(function(list_item) {
			list_item.addClass('active');
		});
	}
	// Otherwise, make sure the "all" button is unchecked
	else {
		$$('.selectors input[name="sort-all"]')[0].checked = false;
		_custom.clear();
		
		// Figure out which classes are selected
		var SelectedClasses = new Hash();
		$$('.selectors input').each(function(input) {
			if (input.checked) {
				var group = input.name.split('-')[0];
				if (!SelectedClasses[group]) {
					SelectedClasses[group] = new Array();
				}
				SelectedClasses[group].push(input.name);
			}
		});
		
		// Deactivate all the currently visible records
		$$('.list_items li').each(function(list_item) {
			list_item.removeClass('active');
			list_item.removeClass('active-years');
			list_item.removeClass('active-regions');
			list_item.removeClass('active-themes');
		});
		
		
		// Build up the ActiveGroups array and mark those list items that match the specific subset
		var ActiveGroups = new Array();
		SelectedClasses.each(function(group, group_name) {
			ActiveGroups.push('.active-'+group_name);
			group.each(function(subset_name) {
				$$('.list_items li.'+subset_name).each(function(list_item) {
					list_item.addClass('active-'+group_name);
				});
			});
		});
		
		// Activate only those that match ALL active groups
		var ActiveClasses = ActiveGroups.join('');
		$$('.list_items li'+ActiveClasses).each(function(list_item) {
			list_item.addClass('active');
		});
		

		// If none are selected, switch back the "all" button
		if ( !ActiveGroups.length ) {
			$$('.selectors input[name="sort-all"]')[0].checked = true;
			UpdateList($$('.selectors input[name="sort-all"]')[0],_custom);
		}
		
		// Paginate the results
		PaginateSpeakers(1);
	}
}
function markMissing() {
	for (i = 0; i < missing.length; i++) {
		target = missing[i];
		document.getElementsByName(target)[0].className = 'missing';
	}
}
function PaginateSpeakers(_start) {
	var myURI = new URI();
	if (_start) {
		myURI.set('fragment',_start).go();
	}
	var start = _start || myURI.get('fragment') || 1;
	var count = 12;
	var visible = $$('.list_items ul li.active');
	var total = visible.length;
	var end = (start - 1 + count) < total ? (start - 1 + count) : total;
	
	// Hide the paginator if there is only one page worth of entries
	$('pagination').setStyle('display', total <= count ? 'none' : 'block');
	
	// Set the status area
	$('pagination_start').innerHTML = start;
	$('pagination_end').innerHTML = end;
	$('pagination_total').innerHTML = total;
	
	// Loop through the active items, hiding those outside the visible range
	for (var i = 0; i < total; i++) {
		var show = (i+1 >= start && i+1 <= end);
		if (show) {
			visible[i].removeClass('hidden');
			visible[i].addClass('visible');
		}
		else {
			visible[i].removeClass('visible');
			visible[i].addClass('hidden');
		}
	}
	
	// Generate the page links
	var pages = new Array();
	for (var i = 1; i <= Math.ceil(total / count); i++) {
		var page_class = (i-1) * count + 1 == start ? 'current' : 'off';
		var page_link = new Element('span', {
			html: i,
			'class': page_class,
			onclick: 'PaginateSpeakers(' + ((i-1) * count + 1) + ');'
		});
		pages.push(page_link);
		// Add the divider
		if (i < Math.ceil(total / count)) {
			pages.push(new Element('span', {
				html: ' | '
			}));
		}
	}
	$('pagination_pages').empty().adopt(pages);
	
	// Set the prev and next actions
	var prev = (start - count) > 1 ? start - count : 1;
	var next = (start - 1 + count + 1) < total ? (start - 1 + count + 1) : start;
	
	$('pagination_prev').setProperty('onclick', 'PaginateSpeakers(' + prev + ');');
	$('pagination_next').setProperty('onclick', 'PaginateSpeakers(' + next + ');');
}
