$(function(){
	// handle all livestream links
	$('a[class*="livestream"][class*="thickbox"]').each(function(){
		var url = $(this).attr('href');
		var clip = url.split('?clipId=');
		try{
			clip = clip[1];
			
			//$(this).attr({href: './tb/livestream.php?KeepThis=true&amp;TB_iframe=true&amp;clip=' + clip + '&amp;width=560&amp;height=350'});
			$(this).attr({href: './tb/livestream.php?clip=' + clip + '&KeepThis=true&TB_iframe=true&width=560&height=350'});
		}catch(e){}
	});
	
	// handle poll voting
	$('form.poll').submit(function(){
		if($(this).find('input:radio:checked').length == 0){
			alert('Oops! Please select an option before voting :)');
			return false;
		}
	});

	// handle q&a
	$('form.qa').submit(function(){
		if($(this).find('input[name="question"]').val() == '' || $(this).find('input[name="submitted_by"]').val() == ''){
			alert('Oops! Please provide a question and your name :)');
			return false;
		}
	});
	
	// convert thickbox to colorbox
	$('a.thickbox:not([href*="TB_iframe=true"])').colorbox({current:'{current} of {total}'});
	$('a.thickbox[href*="TB_iframe=true"]').each(function(){
		var width = 700;
		var height = 500;
		var href = $(this).attr('href');
		try{
			width = href.split('&width=');
			width = width[1].split('&');
			width = Number(width[0]);
		}catch(err){alert(err)}
		try{
			height = href.split('&height=');
			height = height[1].split('&');
			height = Number(height[0]);
		}catch(err){alert(err)}
		$(this).colorbox({iframe:true, innerWidth:width+20, innerHeight:height+20});
	});
	
	// prevent multiple q_a submissions
	$('#frm_qa').submit(function(){
		var submitBtn = $(this).find('[type="submit"]:input');
		submitBtn.hide();
		submitBtn.after('<h3 style="text-align:center;">Submitting...</h3>');
	});
});
