(function(jQuery) {
	infograph = (function() {
		var init = function() {
			var canvas, ctx, size, value,
				$pie = jQuery('#pie'),
				$map = jQuery('#map');

			canvas = document.getElementById('pie');
				
			if (typeof G_vmlCanvasManager != 'undefined') {
				canvas = G_vmlCanvasManager.initElement(canvas);
			};
				
			ctx = canvas.getContext('2d');
			canvas.width	= $pie.width();
			canvas.height	= $pie.height();

			size = $pie.width()/2;
			value = parseInt($pie.data('result'));

			ctx.fillStyle 	= '#de5227';
			ctx.beginPath();			

			if (value > 0) {
				ctx.moveTo(size, size);
				ctx.arc(size, size, size, -Math.PI/2, -Math.PI/2 + value*Math.PI/50, false);
				ctx.lineTo(size, size);
			} else {
				ctx.arc(size, size,	2, 0, Math.PI*2, false);
			};
			
			ctx.closePath();
			ctx.fill();
			
			value = parseInt($map.data('result'));
			
			$map.css({'height': Math.round(value*1.63) + 'px'});
			
		};
		return {
			init: init
		};
	})(),
	IEcss3Rendering = (function() {
		var init = function() {
			if (jQuery.browser.msie && ($.browser.version.substring(0,jQuery.browser.version.indexOf('.')) == 7)) {
				jQuery('.menu .vcenter > div > div').each(function() {
						jQuery(this).css({'top': (34 - jQuery(this).height()) / 2 +'px'}).parent().css({'width': jQuery(this).find('a').width()+ 14 +'px'});
				});
				if (jQuery('#search').length > 0) {
					jQuery('#search').css({'width': jQuery('#category').outerWidth(true) + jQuery('#search').find('.input').outerWidth(true) +'px'});
				};
			};
        	jQuery('.header, .menu .hcenter, .article, .more, .comments .item, .blue, .grey, .article .pad.round-1 .radio.checked, .article .pad.round-2 .input, .article .pad.round-2 .radio-list, .article .pad.round-43 .friends .item .userpic').each(function() {
   		    	PIE.attach(this);
        	});
		};
		return {
			init: init
		};
	})();
    jQuery(function() {
		jQuery(window).load(function() {
			if (window.PIE) {
				IEcss3Rendering.init();
			};
			if (jQuery('#infograph').length > 0) {
				infograph.init();
			};
			jQuery(document).on('click', '.radio-list label', function(e) {
				var $this = jQuery(this);
					
				$this.parent().siblings().removeClass('checked').end().addClass('checked');
				if (jQuery('.nextQuestion').length > 0)
					jQuery('.nextQuestion:visible').removeClass('grey');
			});
			jQuery(document).on('click', '.check-list label', function(e) {
				var $this = jQuery(this);
					
				jQuery('label').parent().toggleClass('checked');
				$this.parent().hasClass('checked')?jQuery('.nextQuestion:visible').removeClass('grey'):jQuery('.nextQuestion').addClass('grey');
			});
			jQuery(document).on('click', '.nextQuestion.grey', function(e) {
				e.preventDefault();
				e.stopPropagation();
			});
			jQuery(document).on('click', '#category', function(e) {
				jQuery('#categories').slideToggle(400);
			});
			jQuery(document).on('click', '#categories li', function(e) {
				var $this = jQuery(this);
				
				if (!$this.hasClass('active')) {
					jQuery('#search-cat').val($this.attr('id'));
					jQuery('#categories li').removeClass('active');
					$this.addClass('active');
					jQuery('#category').html($this.html());
					if (jQuery('#search').length > 0) {
						jQuery('#search').css({'width': jQuery('#category').outerWidth(true) + jQuery('#search').find('.input').outerWidth(true) +'px'});
					};
				};

				jQuery('#categories').slideToggle(400);	
			});
			jQuery(document).on('focusin focusout', 'input[type="text"], textarea', function(e){
        	    var $this = jQuery(this); 

				switch (e.type) {
					case 'focusin':
						if ($this.val() == $this.data('default'))
            	        	$this.val('');
						break;
					case 'focusout':
						if ($this.val() == '')
        	            	$this.val($this.data('default'));
						break;
				};
    	    });
			jQuery(document).on('click', '#more', function(e) {
				e.preventDefault();
				e.stopPropagation();

				var $this = jQuery(this);
								
				if ($this.hasClass('collapsed')) {
					jQuery('#short').slideUp(400, function() {
						jQuery('#full').slideDown(400, function() {
							$this.toggleClass('collapsed');
						});
					});
				} else {
					jQuery('#full').slideUp(400, function() {
						jQuery('#short').slideDown(400, function() {
							$this.toggleClass('collapsed');
						});
					});
				};
			});
		});
	});
})(jQuery);
