$('document').ready(function(){
	preloadImages();
	createUserLinks('body');
	createPollInputs();
	
	$('#content ul li').each(function(){
		$(this).html('<span class="li_content">'+$(this).html()+'</span>');
	})
	
	$('#search').focus(function(){
		if ($('#search').val() == 'search..') {
			$('#search').val('');
		}
	});
	
	$('#search').blur(function(){
		if ($('#search').val() == '') {
			$('#search').val('search..');
		}
	});
	
	$('#search').keyup(function(e){
		if(e.keyCode == 13){
			window.location.replace("http://www.how2skate.com/search/"+$('#search').val()+".htm");
		}
	});
});


function preloadImages(){
	pic1= new Image(1,35); 
	pic1.src="assets/images/layout/button_hover.png";
}


function createUserLinks(parentNode){
	active = false;
	
	$(parentNode+' a.userlink').mouseover(function(){
		active = true;
		userId = $(this).attr('id').replace('user','');
		
		link = $(this);
		
		$.post('ajax_functions.php', {action:'getUserInfo',userId:userId}, function(data){
			if (active) {
				var topIE = 0;
				var leftIE = 0;
				if ( $.browser.msie ) {
					if ($.browser.version.substring(0,2) == '7.'){
						topIE = -30;
						leftIE = -100;
					}
				}
				link.after('<div class="hoverbox">' + data + '</div>');
				link.parent().children('.hoverbox').each(function(){
					$(this).css('marginTop', '-' + ($(this).height() + 30 + topIE) + 'px');
					$(this).css('marginLeft', ((link.width() / 2) - ($(this).width() / 2) + 50 + leftIE) + 'px');
					$(this).fadeIn('fast');
				});
			}
		});
		
	});
	
	$('a.userlink').mouseout(function(){
		active = false;
		$('.hoverbox').fadeOut('fast');
	});
}


function createPollInputs(){
	$('div.poll input[type="checkbox"]').each(function(){
		$(this).addClass('display_none');
		
		$(this).parents('label').unbind("click").click(function(){
			var thisCheck = $(this);
			if ($(this).find('input').is(':checked')){
				$(this).find('input').attr('checked', false);
				makeUnchecked($(this).find('input'));
			} else {
				$(this).find('input').attr('checked', true);
				makeChecked($(this).find('input'));
			}
		});
	
	});
	
	$('div.poll input[type="radio"]').each(function(){
		$(this).addClass('display_none');
		
		$(this).parents('label').unbind("click").click(function(){
			$(this).parents('form').find("input").each(function(){
				$(this).attr('checked', false);
				makeUnchecked($(this));
			});
			$(this).find('input').attr('checked', true);
			makeChecked($(this).find('input'));
		});
	
	});
	
	$('div.poll label').prepend('<img src="assets/images/layout/poll/unchecked.png" />');
}

function makeChecked(element){
	element.parents('label').children('img:first').attr('src','assets/images/layout/poll/checked.png');
	element.parents('label').addClass('inputChecked');
}

function makeUnchecked(element){
	element.parents('label').children('img:first').attr('src','assets/images/layout/poll/unchecked.png');
	element.parents('label').removeClass('inputChecked');
}

