/*TOPICAL MENU*/
function dropout() {
	$(this).children('ul').slideDown('fast');
}

function dropin() {
	$(this).children('ul').fadeOut('fast');
}

$(document).ready(function() {
	/*TABS, BANNER*/
	$('#tab-container-over ul#switch').tabs({ event: 'mouseover', fx: { opacity: 'toggle' }}) ;
	$('.slide').innerfade({ speed: 'slow', timeout: 20000, type: 'sequence', containerheight: '370px' });
	$('.sm-tabsVert').tabs();

	//png fix for ie6 nav-audience-bg
	$('#container-header, #nav-audience-bg, .lg-spotlight, #footer, .sm-textImageVert').pngFix();
	
	//topical menu
	var config = {    
		over: dropout, // function = onMouseOver callback (REQUIRED)    
		timeout: 400, // number = milliseconds delay before onMouseOut    
		out: dropin // function = onMouseOut callback (REQUIRED)    
	};
	$("#nav-topical li").hoverIntent(config);
	$("ul.dropdownnav li").hoverIntent(config);
	
	//resize ensemble videos to fit within the middle module block
	$('#col-main .ensembleEmbeddedContent').each(function() {
		h = $(this).css('height');
		w = $(this).css('width');
		r = h/w;  //ratio of height to width
		h = r*444;
		$(this).css('height', h);
		$(this).css('width', 444);
	});
	
	//get rid of margin for certain images, depending on the size
	$('.lg-textImageHoriz img').each(function() {
		if($(this).width() >= 420) $(this).css('margin-right', '0px');
	});
	$('.lg-article img').each(function() {
		if($(this).css('float') == 'right') $(this).css('margin', '5px 0px 10px 10px');
		if($(this).css('float') == 'left') $(this).css('margin', '5px 10px 10px 0px');
	});
     
	//FAQS
	$('.collapsed .faq-answer').hide();
	$(".collapsed .faq-quest").click(function () {
		$(this).next('.faq-answer').slideToggle('fast');
	});
	
	//News Archives
	$('.sm-newsarchive ul li ul').hide();
	$('.sm-newsarchive a.archive-month').click(function() {
		$(this).parent().parent().children('ul').slideToggle('fast');
	});
	
	//center gallery page images in their a blocks
	$('.cbox-images img').imgCenter({scaleToFit:false});
	
	//colorbox for image galleries
	$('.cbox-images').each(function() {
		gallery = $(this).children('a:first').attr('rel');
		$("a[rel='"+gallery+"']").colorbox({transition:"none", width:"960px", height:"660px"});
	});
	
	//colorbox for document images
	$('.cbox-item').colorbox({transition:"none", width:"960px", height:"660px"});
	
	//slideshow gallery
	$('.slideshow-images img').imgCenter({scaleToFit:false});
	$('.slideshow-images').innerfade({ speed: 'fast', timeout: 4000, type: 'sequence', containerheight: '320px' });
	
	//polls
	$('.poll input[name="submit"]').click(function(event) {
		event.preventDefault();
		theform = $(this).parent().attr('id');
		$.ajax({ 
			type: "POST", 
			url: "http://"+window.location.hostname+"/cascade-poll/process_results.php", 
			data: $('#'+theform).serialize(), 
			success: function(data){ 
					populatePoll(data, theform, 1000, true);
				}
		});
	});
	
	$('.view-results').click(function(event) {
		event.preventDefault();
		theform = $(this).parent().children('form').attr('id');
		$.ajax({ 
			type: "POST", 
			url: "http://"+window.location.hostname+"/cascade-poll/process_results.php", 
			data: 'getResults=true&poll_name='+theform, 
			success: function(data){ 
					populatePoll(data, theform, 1000, false); 
				}
		});
	});
	//if a cookie is set
	$('.poll').each(function() {
		theform = $(this).children('form').attr('id');
		if ($.cookie(theform)==1){
			$.ajax({ 
			type: "POST", 
			url: "http://"+window.location.hostname+"/cascade-poll/process_results.php", 
			data: 'getResults=true&poll_name='+theform, 
			success: function(data){ 
					populatePoll(data, theform, 0, false); 
				}
			});
		}
	});
	
	function populatePoll(data,formName, speed, setCookie) {
		//fadeOut the form and buttons
		$('#'+formName).fadeOut(0);
		$('#'+formName).parent().children('.view-results').fadeOut(0);
		
		//populate fields, create bars
		var datapoints = data.split(','); // split on commas
		len = datapoints.length;
		//get total votes, so we can calculate a percentage
		total = 0;
		for(i=0; i<len; i++) {
			total += parseInt(datapoints[i],10);
		}
		//loop again, this time setting span widths
		for(i=0; i<len; i++) {
			var width = parseInt(datapoints[i],10)/total;
			width *= 218;
			$('#'+formName).parent().children('.poll-results').children('p:nth-child('+(i+1)+')').children(' span').css('width',width).after(' '+datapoints[i]);
		}
		
		//fade in results
		$('#'+formName).parent().children('.poll-results').fadeIn(speed, function(){
			if(setCookie) $.cookie(theform, 1, { expires: 30 });
		});
	}
});
