$(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('');
	});
	
	// replace submit buttons
    $('input[type=submit].button').each(function(){
        var as_link = $('<div class="button"><a href="#"></a><span></span></div>');
        var form = $(this).closest('form');
        as_link.find('a').text($(this).attr('value'));
        as_link.find('a').click(function(){
            form.submit();
            return false;
        })
        form.keyup(function(e){
            if(e.which == 13){
                form.submit();
                return false;
            }
        })
        $(this).hide().after(as_link);
        Cufon.refresh('.button');
    });
    
    $('.vert-nav').each(function(){
        $(this).find('li.top').first().css('borderTop','none');
        $(this).find('li.top').last().css('borderBottom','none');
    });
    
    
    $('#sidebar2 h2:has(.toggle)').each(function(){
        var panel = $(this).next().hide();
        $(this).find('.toggle').click(function(){
            panel.slideToggle();
        })
    });
    
    
    
    // Allos the media library to be filterable
    $('.content_filter').each(function(){
        
        var content = $(this).next();
        
        $('.filter', this).click(function(e){
            e.preventDefault();
            
            var filter = $(this).attr('href').substr(1);
            
            $('.filterable', content).each(function(){
                $(this).toggle($(this).hasClass(filter));
            });
            
            $(this).addClass('active').siblings().removeClass('active');
            
        }).first().click();
    })
});

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 
	
});


