$(function(){
	$('div.columns a.f span.i').each(function(){
		var cnt = $(this);
		var sr = $(this).find('img').attr('src');
		$(this).find('img').remove();
		var im = $('<img />');
		var color = $(this).css('color');
		im.load(function(){
			var cg = document.createElement('canvas');
			$(cg).appendTo(cnt);
			if (window.G_vmlCanvasManager) G_vmlCanvasManager.initElement(cg);
			cg.width = 250;
			cg.height = 125;
			ctx = cg.getContext('2d');
			ctx.drawImage(this,0,0,250,125);
			ctx.save();
			ctx.transform(1,0,-0.28,1,0,0);
			ctx.fillStyle = color;
			ctx.fillRect(0, 0, 40, 125);
			if (window.G_vmlCanvasManager) {
				ctx.fillStyle = '#fff';
				ctx.fillRect(0, 0, 35, 125);
				ctx.fillRect(250, 0, 35, 125);
			}else{
				ctx.clearRect(0, 0, 35, 125);
				ctx.clearRect(250, 0, 35, 125);
			}
			ctx.restore();
			ctx.beginPath();
			ctx.strokeStyle = color;
			ctx.save();
			ctx.scale(1.5,1);
			ctx.arc(150,93,10,0,Math.PI*180,true);
			ctx.restore();
			ctx.fillStyle = color;
			ctx.fill();
			ctx.moveTo(210,106);
			ctx.lineTo(225,92);
			ctx.lineTo(230,96);
			ctx.lineTo(210,106);
			ctx.fill();			
			ctx.stroke();
		});
		im.attr('src',sr);
	});
	
	$('h1 span.comments').each(function(){
		var color = $(this).css('color');
		$(this).find('a').css({color: '#fff'});
		var cg = document.createElement('canvas');
		$(cg).appendTo(this);
		if (window.G_vmlCanvasManager) G_vmlCanvasManager.initElement(cg);
		cg.width = 32;
		cg.height = 30;
		ctx = cg.getContext('2d');
		ctx.beginPath();
		ctx.strokeStyle = color;
		ctx.save();
		ctx.scale(1.5,1);
		ctx.arc(11,11,10,0,Math.PI*180,true);
		ctx.restore();
		ctx.fillStyle = color;
		ctx.fill();
		ctx.moveTo(2,24);
		ctx.lineTo(17,10);
		ctx.lineTo(22,14);
		ctx.lineTo(2,24);
		ctx.fill();			
		ctx.stroke();
	});
	
	$('div.wt div.img').each(function(){
		var cnt = $(this);
		var sr = $(this).find('img').attr('src');
		$(this).find('img').remove();
		var im = $('<img />');
		var color = $(this).css('color');
		im.load(function(){
			var cg = document.createElement('canvas');
			$(cg).appendTo(cnt);
			if (window.G_vmlCanvasManager) G_vmlCanvasManager.initElement(cg);
			cg.width = 400;
			cg.height = 200;
			ctx = cg.getContext('2d');
			ctx.save();
			ctx.drawImage(this,0,0);
			ctx.transform(1,0,-0.28,1,0,0);
			ctx.fillStyle = color;
			ctx.fillRect(395, 0, 75, 200);
			if (window.G_vmlCanvasManager) {
				ctx.fillStyle = '#fff';
				ctx.fillRect(0, 0, 70, 200);
				ctx.fillRect(400, 0, 70, 200);
				setTimeout(function(){
					$('div.wt div.img').get(0).innerHTML += '';
				}, 1000);
				$(cg).find('>div').css({width: 400, height:200});
			}
			else {
				ctx.clearRect(0, 0, 70, 200);
				ctx.clearRect(400, 0, 70, 200);
			}
			ctx.restore();
		});
		im.attr('src',sr);
	});
	
	$('div.text blockquote').each(function(){
		var cnt = $(this);
		var color = $(this).parents('div.container').find('h1').css('color');
		var cg = document.createElement('canvas');
		$(cg).appendTo(cnt);
		$(cg).css({
			'position': 'absolute',
			'left': 0,
			'top': 0
		});
		if (window.G_vmlCanvasManager) G_vmlCanvasManager.initElement(cg);
		cg.width = 40;
		cg.height = 125;
		ctx = cg.getContext('2d');
		ctx.save();
		//ctx.transform(1,0,-0.28,1,0,0);
		ctx.fillStyle = color;
		ctx.fillRect(0, 0, 40, 125);
		if (window.G_vmlCanvasManager) {
			ctx.fillStyle = '#fff';
			ctx.fillRect(0, 0, 35, 125);
			setTimeout(function(){$('div.text blockquote').get(0).innerHTML+='';},1000);
		}else{
			ctx.clearRect(0, 0, 35, 125);
		}
		ctx.restore();
	});
	
	
	$('form.comment .text').each(function(){
		this.value = (this.value=='')? this.title: this.value;
		$(this).blur(function(){
			this.value = (this.value=='')? this.title: this.value;
		}).focus(function(){
			this.value = (this.value==this.title)? '': this.value;
			$(this).removeClass('error');
		})
	});
	if ($.browser.msie && parseInt($.browser.version)<=6){
		$('form.comment div.bg').each(function(){
			$(this).css({
				'height':$(this).parent().height()
			});
		});
	}
	
	function isvemail(email) {
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(reg.test(email) == false) {
			return false;
		}
		return true;
	}

	$('form.comment').submit(function(){
		var flag = false;
		$('form.comment .text').each(function(){
			if (this.value=='' || this.value==this.title || (this.name=='email' && !isvemail(this.value))){
				flag = true;
				$(this).addClass('error');
			}
		});
		if (flag) return false;
		$('form.comment input[type=submit]').attr({'disabled': true});
	});
	
	$('#comments div.reply a').click(function(){
		$('form.comment p.error').hide();
		$('form.comment div.reply').html('К сообщению <a href="'+$(this).attr('href')+'#comment-'+$(this).attr('rel')+'">#'+$(this).attr('rel')+'</a> <span class="separator">/</span> <a class="cancel" href="#">отменить</a>');
		$('form.comment div.reply a.cancel').click(function(){
			$('form.comment div.reply').html('');
			$('form.comment input[name=parent]').val(0);
			return false;			
		});
		$('form.comment input[name=parent]').val($(this).attr('rel'));
		
		$('body,html').animate({
			scrollTop: $('form.comment').position().top
		},300);
		return false;
	});
	
	$('p.feedback a').click(function(){
		window.open(this.href,'feedback','width=800,height=670,toolbar=0,scrollbars=yes');
		return false;
	});
	
	if ($('body.main div.header').length){
		var cloud_flag = [];
		cloud_flag[0] = false;
		cloud_flag[1] = false;
		cloud_flag[2] = false;
		cloud_flag[3] = false;

		var clouds = $('div.cloud').not('.cloud-news');
		var cloud_active = clouds.eq(0);
		var cloud_total = clouds.length-1;  
		var cloud_time;
		
		function rcloud(){
			var flag = false;
			$.each(cloud_flag,function(){
				if (this === true) {
					flag = true;
				}
			}); 
			if (!flag) {
				var cloud_rand = Math.round(Math.random() * (cloud_total - 1));
				cloud_active.fadeOut(300);
				cloud_active = clouds.not(cloud_active).eq(cloud_rand);
				cloud_active.fadeIn(300);

				cloud_time = setTimeout(rcloud, 6000);
			}
		}
		rcloud();
		
		$('div.cloud-hv').mouseenter(function(){
			var index = $('div.cloud-hv').index(this);
			clearTimeout(cloud_time);
			cloud_flag[index] = true;
			cloud_active.fadeOut(0);
			cloud_active = clouds.eq(index);
			cloud_active.fadeIn(0);
		});

		$('div.cloud-hv').mouseleave(function(){
			var index = $('div.cloud-hv').index(this);
			cloud_flag[index] = false;

			clearTimeout(cloud_time);
			cloud_time = setTimeout(rcloud,6000);
		});
	}
	//add
	var browsername = navigator.appName;
	if (browsername == 'Microsoft Internet Explorer'){
		$('#menu>ul>li').hover(function(){
			$('#menu>ul>li ul').hide();
			$(this).find('>ul').show();
			$('#menu>ul>li ul').stopTime('timer2');
		},function(){
			$('#menu>ul>li ul').everyTime(500, 'timer2', function() {
			$('#menu>ul>li ul').hide();
			$('#menu>ul>li.active>ul').show();
			},0);
		});
	}else{
		
		$('#menu>ul>li').mouseenter(function(){
			$('#menu>ul>li ul').hide();
			$(this).find('>ul').show();
		}).mouseleave(function(){
			$('#menu>ul>li ul').hide();
			$('#menu>ul>li.active>ul').show();
		});
		
	}
	
	/*$('#menu>ul>li').mouseenter(function(){
		$('#menu>ul>li ul').hide();
		$(this).find('>ul').show();
	}).mouseleave(function(){
		$('#menu>ul>li ul').hide();
		$('#menu>ul>li.active>ul').show();
	});*/
	
	

});

jQuery(document).ready(function() {
	$("a[href*=#]:not([href=#])").click(function  () {
       var thisHash = this.hash;
	        var targetOffset = $(thisHash).offset().top;
       $("html:not(:animated),body:not(:animated)").animate({ scrollTop: targetOffset}, 1100 );
	        location.hash = this.hash;
	        return false;
	});
});
