$(function() {
	$('.nonjavascriptpollsubmit').hide();
	$('.javascriptpollsubmit').show();
	innitLogin();
}
);

$(document).ready(function(){
	//$('#debugcontainer').empty().append("<pre></pre>");
	//$('#debugcontainer pre').append("jQuery ready\n");
	$("#inpPercentageRevenueFromServices").bind('focus',function(){
		var servicesValue = $(this).attr('value');
		
		var productsValue = $('#inpPercentageRevenueFromProducts').attr('value');
		//$('#debugcontainer pre').append("focus gained\n");	
		if(servicesValue == 0){
			//$('#debugcontainer pre').append("servicesValue is zero\n");
			if(productsValue > 0){
				var newValue = 100 - productsValue;
				//$('#debugcontainer pre').append("productsValue is not zero\n");
				//$('#debugcontainer pre').append("newvalue: " + newValue + "\n");
				$(this).attr('value',newValue);
			}
		}
	});
});

$(document).ready(function(){
	$('#channelprev').click(function(){
		$('#go_previous').val('prev');
	});
	
	
	// Strip single spaces from forms
	$('textarea').each( function() {
		if (' '==$(this).text())
		$(this).text('');
	});
});

function getID(strID)
{
	return strID.replace(/[^0-9]/gi, '');
}

// sets up the login form so that the labels appear on top of the inputs 
// (keeps it accessible)
function innitLogin()
{
	if ($('#login'))
	{
		
		// on focus on an input, remove the label
		$('#login .inputline input').focus(function()
			{
				$("#login .inputline label[for='"+$(this).attr('id')+"']").hide(); // TODO upgrade: remove @
			}
		).blur(function()
			{
				// on blur, if there is no value in the input, replace the label
				if (jQuery.trim($(this).val()) == '')
				{
					$("#login .inputline label[for='"+$(this).attr('id')+"']").show(); // TODO upgrade: remove @
				}
			}
		).change(function(){
					// on blur, if there is no value in the input, replace the label
				if (jQuery.trim($(this).val()) == '')
				{
					$("#login .inputline label[for='"+$(this).attr('id')+"']").show(); // TODO upgrade: remove @
				}
		});
	}
}

function hideLabels(){

	$('#login .inputline label').addClass('ontop').click(function()
			{
				// when clicked, find the input element, and focus it
				$('#'+$(this).attr('for')).focus();
				// now hide the label
				$(this).hide();
			}
		).each(function()
			{
				// if the input has a value to start with, hide the lable
				if (jQuery.trim($('#'+$(this).attr('for')).val()) != '')
				{
					$(this).hide();
				}
			}
		);
}

/*** Media Library ****/
$(document).ready(function(){
	var medialibrary = $('#medialibrary');
	
	// firstly, hide off the videos
	$('div.mediavideo', medialibrary).css('display','none');
	
	// listen for button click
	$('#toimages', medialibrary).click(function(){
		if($(this).is('.medialibactive')){
			return false;
		} else {
			$(this).addClass('medialibactive');
			$('#tovideos').removeClass('medialibactive');
			$('div.mediaimage', medialibrary).css('display','block');
			$('div.mediavideo', medialibrary).css('display','none');
			return false;
		}
	});
	$('#tovideos', medialibrary).click(function(){
		if($(this).is('.medialibactive')){
			return false;
		} else {
			$(this).addClass('medialibactive');
			$('#toimages').removeClass('medialibactive');
			$('div.mediavideo', medialibrary).css('display','block');
			$('div.mediaimage', medialibrary).css('display','none');
			return false;
		}
	});
	  // if 
		// hide current images and show video images
		// remove active state from images button
		// add active state to videos button 
	
});

