var newPanel;
YUI().use('node', 'yui', '_utils', '_ag', function(Y) {
    
    Y.on
	(
		'domready', 
		function()
		{
			Y.one('#crazy').setY(Y.one('#crazy-start').getY()+2);
			Y.one('#crazy').setX(Y.one('#crazy-start').getX()+2);
			
			newPanel = new Y.my.utils.loadingPanel({id:'xxx-image', delay:2000, styles:{padding:'5px 15px', width : 'auto', background:'#c00',fontSize:'14px', fontStyle:'italic'}});
			if(Y.one('#message-form')) {initMessageForm();}
			if(Y.one('#brief-web-form')) {initBriefWebForm();}
			if(Y.one('#brief-fstyle-form')) {initBriefFstyleForm();}
			if(Y.one('#c-form')){initCommentsForm();}
			
			if(Y.UA.ie) Y.on('click', dropIcon, Y.one('#crazy-start'));
			else Y.on('click', dropIcon, Y.one('#crazy'));
			//Y.on('click', function(){iCloner.cloneElement()}, Y.one('#add-ing'));
			if(Y.one('#gallery-widget'))
			{
				var arr = [{src:'/content/1.jpg', alt:'image #1', href:'/content/1.jpg'}, {src:'/content/2.jpg', alt:'image #2', href:'/content/2.jpg'}, {src:'/content/3.jpg', alt:'image #3', href:'/content/3.jpg'}, {src:'/content/4.jpg', alt:'image #4', href:'/content/4.jpg'}, {src:'/content/5.jpg', alt:'image #5', href:'/content/5.jpg'}, {src:'/content/6.jpg', alt:'image #6', href:'/content/6.jpg'}, {src:'/content/7.jpg', alt:'image #7', href:'/content/7.jpg'}, {src:'/content/8.jpg', alt:'image #8', href:'/content/8.jpg'}, {src:'/content/9.jpg', alt:'image #9', href:'/content/9.jpg'}, {src:'/content/10.jpg', alt:'image #10', href:'/content/10.jpg'}],
					temp = new Y.my.ag.gallery('gallery-widget',{ height:400, imageArray:arr, imageWidth:300, imageHeight:200, currentIndex:2, delay : .5 });
				temp.init();
			}
		}
	);
	
	function initMessageForm()
	{
		var msgForm = new Y.my.utils.callForm({form:'message-form'}),
			s = { setup : {query:'/ajax/send_mail.ajax.php', formId:'message-form', successHandler : sendMessageCallback} };
		
		Y.on('submit', messageSend, Y.one('#message-form'),s);
		
		function messageSend(e)
		{
			Y.one('#message').setStyle('display','none');
			Y.one('#message-form-info').set('innerHTML','<img src="/i/ajax-loader.gif" />');
			Y.one('#message-form-info').setStyle('display','');
			msgForm.sendForm(this.setup,e);
		}
		
		function sendMessageCallback()
		{
			Y.one('#message-form-info').set('innerHTML','Сообщение отправлено<br /><span id="resend">Написать еще</span>');
			Y.on
			(
				'click',
				function()
				{ 
					Y.one('#message-form-info').set('innerHTML','');
					Y.one('#message-form-info').setStyle('display','none');
					Y.one('#message').setStyle('display','');
				},
				Y.one('#resend')
			)
		}
	}
	
	function initBriefWebForm()
	{
		var form = new Y.my.utils.callForm({form:'brief-web-form'}),
			s = { setup : {query:'/ajax/send_brief_web.ajax.php', formId:'brief-web-form', successHandler : sendMessageCallback} };
		
		Y.on('submit', sendBrief, Y.one('#brief-web-form'),s);
		
		function sendBrief(e)
		{
			newPanel.show('Отправка...');
			Y.one('#b-load').setStyle('display','');
			form.sendForm(this.setup,e);
		}
		
		function sendMessageCallback()
		{
			newPanel.hide('Готово');
			Y.one('#b-load').setStyle('display','none');
		}
	}
	function initBriefFstyleForm()
	{
		var form = new Y.my.utils.callForm({form:'brief-fstyle-form'}),
			s = { setup : {query:'/ajax/send_brief_fstyle.ajax.php', formId:'brief-fstyle-form', successHandler : sendMessageCallback} };
		
		Y.on('submit', sendBrief, Y.one('#brief-fstyle-form'),s);
		
		function sendBrief(e)
		{
			newPanel.show('Отправка...');
			Y.one('#b-load').setStyle('display','');
			form.sendForm(this.setup,e);
		}
		
		function sendMessageCallback()
		{
			newPanel.hide('Готово');
			Y.one('#b-load').setStyle('display','none');
		}
	}
	
	function initCommentsForm()
	{
		Y.on('click', toggleCommentForm, Y.one('#comments-message'));
		var commentForm = new Y.my.utils.callForm({form:'c-form'}),
			s = { setup : {query:'/ajax/comments.ajax.php?url='+document.location+'&mode=add', formId:'c-form', successHandler : addComment} };
		Y.on('submit', commentSend, Y.one('#c-form'), s);
		
		function commentSend(e)
		{
			newPanel.show('5 сек...');
			commentForm.sendForm(this.setup,e);
		}
		
		function addComment(result)
		{
			newPanel.hide('Готово');
			Y.one('#comments-frame').setStyle('display', 'none');
			Y.DOM.addHTML(Y.one('#comments-container'),result['result']);
		}
		
		function toggleCommentForm()
		{
			var d = (Y.one('#comments-frame').get('offsetHeight') == 0) ? '' : 'none';
			Y.one('#comments-frame').setStyle('display',d); 
		}
	}
	
	function dropIcon(e)
	{
		e.halt();
		
		if(Y.UA.ie) Y.detach('click', dropIcon, Y.one('#crazy-start'));
		else Y.detach('click', dropIcon, Y.one('#crazy'));
		
		var max = Y.all('#crazy img.crazy').size()-1,
			index = Y.my.utils.getRandom(0,max),
			icon = Y.all('#crazy img.crazy').item(index);
		if(icon)
		{
			icon.setStyle('visibility','visible');
			var dTime = Y.my.utils.getRandom(2,4);
			var anim = new Y.Anim
			({
				node: icon,
				duration: dTime,
				easing: Y.Easing.bounceOut
			});
			
			var y = (Y.DOM.winHeight() - icon.get('offsetHeight') - icon.getY()) + 'px';
			
			anim.set
			(
				'to', { top: y }
			);
			
			anim.on('end', function() 
			{  
				setTimeout(function(){ sniff(icon); }, 2000);
			});
			anim.run();	
		}
	}
	
	function sniff(el)
	{
		var dTime = Y.my.utils.getRandom(3,6);
			x = Y.one('#crazy-start').getX()+2,
			y = Y.one('#crazy-start').getY()+2,
			anim = new Y.Anim
			({
				node: el,
				duration: dTime,
				easing: Y.Easing.easeIn
			});

		anim.set
		(
			'to', { curve: randomCurve([x, y]) }
		);
		
		anim.on('end', function() 
		{  
			anim.get('node').setStyle('visibility', 'hidden');
			if(Y.UA.ie) Y.on('click', dropIcon, Y.one('#crazy-start'));
			else Y.on('click', dropIcon, Y.one('#crazy'));
		});
		
		anim.run();
		
		function randomCurve(end) 
		{
			var points = [], n = 6,	winWidth = el.get('winWidth'), winHeight = el.get('winHeight');
	 
			for (var i = 0; i < n; ++i)
			{
				points.push([Math.floor(Math.random() * winWidth), Math.floor(Math.random() * winHeight)]);
			}
	 
			if (end) { points.push(end); }
			return points;
		};
	}
	var animatedGallery = function(id,setup)
	{
		this.containerId = /\#/.test(id) ? id : '#'+id;
		this.imageWidth = setup.imageWidth;
		this.imageHeight = setup.imageHeight;
		this.height = setup.height;
		this.currentIndex = setup.currentIndex ? setup.currentIndex : 0;
		this.nextIndex = 0;
		this.imageArray = setup.imageArray;
		this.animRun = false;
		this.delay = setup.delay;
	}

});

