$(document).ready(function() {
	$('body').addClass('js-on');
	
	if($('#step1').length > 0 && !$.browser.msie) {

		var what = $('#what').attr('title');
		var options = $.trim($('#options').attr('title'));
		var who = $.trim($('#who').attr('title'));
		var emailaddress = $('#emailaddress').attr('title');
		var yourname = $('#yourname').attr('title');
		var invite = $.trim($('#invite').attr('title'));
		if($.trim($('#what').val())=='' || $.trim($('#what').val())==what) {
			$('#what').addClass('placeholder').val(what).focus(function() {
				if (this.value == what) {
					$(this).removeClass('placeholder').val('');
				};
			}).blur(function() {
				if (this.value == '') {
					$(this).addClass('placeholder').val(what);
				};
			});
		}
		if($.trim($('#options').val())=='' || $.trim($('#options').val())==options) {
			$('#options').addClass('placeholder').val(options).focus(function() {
				if (this.value == options) {
					$(this).removeClass('placeholder').val('');
				};
			}).blur(function() {
					if (this.value == '') {
						$(this).addClass('placeholder').val(options);
					};
			});
		}
		if($.trim($('#who').val())=='' || $.trim($('#who').val())==who) {
			var who = $('#who').attr('title');
			$('#who').addClass('placeholder').val(who).focus(function() {
				if (this.value == who) {
					$(this).removeClass('placeholder').val('');
				};
			}).blur(function() {
					if (this.value == '') {
						$(this).addClass('placeholder').val(who);
					};
			});
		}
		if($.trim($('#emailaddress').val())=='' || $.trim($('#emailaddress').val())==emailaddress) {		
			$('#emailaddress').addClass('placeholder').val(emailaddress).focus(function() {
				if (this.value == emailaddress) {
					$(this).removeClass('placeholder').val('');
				};
			}).blur(function() {
				if(($('#yourname').val() == '' || $('#yourname').val() == yourname) && this.value != emailaddress) {
					var email = $('#emailaddress').val();
					$('#yourname').removeClass('placeholder').val(email.substring(0,email.indexOf('@')));
				}
				if (this.value == '') {
					$(this).addClass('placeholder').val(emailaddress);
				};
			});
		}
		
		if($.trim($('#yourname').val())=='' || $.trim($('#yourname').val())==yourname) {				
			$('#yourname').addClass('placeholder').val(yourname).focus(function() {
				if (this.value == yourname) {
					$(this).removeClass('placeholder').val('');
				};
			}).blur(function() {
					if (this.value == '') {
						$(this).addClass('placeholder').val(yourname);
					};
			});
		}
		
		if($.trim($('#invite').val())=='' || $.trim($('#invite').val())==invite) {				
			$('#invite').addClass('placeholder').val(invite).focus(function() {
				if (this.value == invite) {
					$(this).removeClass('placeholder').val('');
				};
			}).blur(function() {
					if (this.value == '') {
						$(this).addClass('placeholder').val(invite);
					};
			});
		}
		
		$('#step1').submit(function() {
			if ($('#what').val() == what) {
				$('#what').val('');
			}
			if ($('#options').val() == options) {
				$('#options').val('');
			}
			if ($('#who').val() == who) {
				$('#who').val('');
			}
			if ($('#emailaddress').val() == emailaddress) {
				$('#emailaddress').val('');
			}
			if ($('#yourname').val() == yourname) {
				$('#yourname').val('');
			}
			if ($('#invite').val() == invite) {
				$('#invite').val('');
			}

		});

	}
	
 	if($('table.answers'))
	{
		$('table.answers').each(function(){
			var table = $(this);
			table.before('<div class="wrapanswers"><h3>'+$(this).find('th').html()+'</h3><ul class="answers"></ul></div>');
			var ul = table.prev().find('ul.answers');
			//ul.before('<h3>'+$(this).find('th').html()+'</h3>');
			
			$(this).find('.avatar_y div.popke').parent().each(function(){
				email=$(this).find('.popke').attr('title');
				username = email.substring(0,email.indexOf('@'));
				balloon = '<div class="popke-id"><span><em>'+username+'</em></span></div>';
				$('<li>'+balloon+$(this).html()+'</li>').appendTo(ul);
			});
			$(this).find('.avatar_n div.popke').parent().each(function(){
				email=$(this).find('.popke').attr('title');
				username = email.substring(0,email.indexOf('@'));
				balloon = '<div class="popke-id"><span><em>'+username+'</em></span></div>';
				$('<li>'+balloon+$(this).html()+'</li>').appendTo(ul);
			});
			$(this).find('.avatar_u div.popke').parent().each(function(){
				email=$(this).find('.popke').attr('title');
				username = email.substring(0,email.indexOf('@'));
				balloon = '<div class="popke-id"><span><em>'+username+'</em></span></div>';
				$('<li>'+balloon+$(this).html()+'</li>').appendTo(ul);
			});
			
			table.remove();
		});
		$('.answers .popke-id').hide();
		$('.answers .popke').hover(
	      function () {
	       //if($.support.cssFloat) 
			$(this).prev().show();
	 		 if(!$.support.objectAll) $(this).parent().parent().prev().children().css('visibility','hidden'); //IE7 fix
	      }, 
	      function () {
			$(this).prev().hide();
	        if(!$.support.objectAll) $(this).parent().parent().prev().children().css('visibility','visible'); //IE7 fix
	      }
	    );
	 
		
		
	}
	
	$("#popkes li input").hide();
	$("#popkes li input").parent().parent().removeClass('checked');
	$("#popkes li input:checked").parent().parent().addClass('checked');
	
	$('#popkes li label img').click(function(){
		if($(this).next().attr('checked')!='checked') {
			$(this).next().attr('checked','checked');
		} else {
			$(this).next().attr('checked','');
		}
	});
	
	$('#popkes li').click(function(){
		$("#popkes li input").parent().parent().removeClass('checked');
		$("#popkes li input:checked").each(function(){
			$(this).parent().parent().addClass('checked');
			if($(this).attr('name')=='pupke[skin]') $('#live-popke div.popke img').next().attr('class',$(this).attr('id'));

			if($(this).attr('name')=='pupke[haircolour]') {
				var hair = $('#live-popke div.popke img').next().next();
				var hairstyle = hair.attr('class').substring(0,hair.attr('class').length-1);
				var haircolour = $(this).attr('id').substring($(this).attr('id').length-1);
				if(haircolour==0) hairstyle = 'kapsel0';
				hair.attr('class',hairstyle+haircolour);
			}		
			
			if($(this).attr('name')=='pupke[hairstyle]') {
				var hair = $('#live-popke div.popke img').next().next();				
				var hairstyle = $(this).attr('id').substring(0,$(this).attr('id').length-1);
				var haircolour = hair.attr('class').substring(hair.attr('class').length-1);
				if(hairstyle=="kapsel6" || hairstyle=="kapsel0") {
					hair.attr('class',$(this).attr('id'));					
				} else {
					if(haircolour=='0') haircolour = 'a';
					hair.attr('class',hairstyle+haircolour);										
				}
			}
			
		    if($(this).attr('name')=='pupke[glasses]') $('#live-popke div.popke img').next().next().next().attr('class',$(this).attr('id'));
			if($(this).attr('name')=='pupke[accessories]')  $('#live-popke div.popke img').next().next().next().next().attr('class',$(this).attr('id'));
		});
	});
	
});


