﻿Ext.ns('Ico');

/*
Ico.scroll = function(e){
	var x = e.getPageX();

	Ico.frameWidth = Ico.frame.getWidth();
	Ico.frameX = Ico.frame.getX();
	Ico.ribbonWidth = Ico.ribbon.getWidth();
	Ico.ribbonX = Ico.ribbon.getX();

	if(x < Ico.frameX + Ico.frameWidth/2){
		Ico.endX = Ico.frame.getWidth() - Ico.ribbon.getWidth() + Ico.ribbon.getX();
		if(Ico.endX < (Ico.frame.getWidth() - Ico.ribbon.getWidth() + Ico.ribbonXstart))
			Ico.endX = Ico.frame.getWidth() - Ico.ribbon.getWidth() + Ico.ribbonXstart;
	} else {
		Ico.endX = Ico.ribbonXstart;
	}

	Ico.stop();
	Ico.ribbon.shift({
		 x: Ico.endX
		,easing: 'easeNone'
		,duration: 2
	})
}

Ico.stop = function(){
	if(Ico.ribbon.hasActiveFx()){
		var x = Ico.ribbon.getX();
		Ico.ribbon.stopFx();
		Ico.ribbon.setX(x);
	}
}
*/

Ico.addIcon = function(item, noanim){
	if(noanim == false) Ico.ribbon.setX(Ico.ribbon.getX() - Ico.iconLength);
	var containerEl = Ico.iconTpl.insertFirst(Ico.ribbon, {
		 src: item.src
		,href: item.href
		,alt: item.alt
	}, true);
	if(Ext.isIE6){
		containerEl.on('mouseenter', function(e, t, o){
			Ext.get(e.target).child('.icon').addClass('hover');
		});
		containerEl.on('mouseleave', function(e, t, o){
			Ext.get(e.target).child('.icon').removeClass('hover');
		});
		containerEl.on('click', function(e, t, o){
			window.location.href = item.href;
		});
	}
	//console.log(Ico.ribbon.query('div.container').length);
	if(Ico.ribbon.query('div.container').length > Ico.iconsMax){
		//console.log('max: '+Ico.iconsMax+' len: '+Ico.ribbon.query('div.container').length);

		for(var i = Ico.ribbon.query('div.container').length; i > Ico.iconsMax; i--){
			Ico.ribbon.last('div.container').remove();
		}
	}
	if(noanim == false){
		Ico.ribbon.shift({
			 x: Number(Ico.ribbon.getX() + Ico.iconLength)
			,easing: 'easeBoth'
			,duration: 1.5
			,callback: function(){
			}
		});
	}
}

Ico.resizeIconMask = function(){
	Ico.iconMaskEl.setWidth(Ico.frame.getWidth());
	Ico.iconMaskEl.setHeight(Ico.frame.getHeight());
};

Ico.showMask = function(){
	Ico.iconMaskEl = Ext.DomHelper.insertFirst(
		 Ico.frame
		,{
			 id: 'iconMask'
			,tag: 'div'
		}
		,true
	);
	Ico.resizeIconMask();
	Ico.iconMaskEl.setOpacity(.5);
	Ico.iconMaskEl.on('click', function(e, t, o){
		Ico.startMaskInterval();
		Ext.get(e.target).remove();
	});

	Ico.iconMaskEl.on('mouseenter', function(e, t, o){
		Ext.get(e.target).setOpacity(.3);
	});

	Ico.iconMaskEl.on('mouseleave', function(e, t, o){
		Ext.get(e.target).setOpacity(.5);
	});

	window.onresize = Ico.resizeIconMask;
};

Ico.getIconsMax = function(){
	return Math.ceil(Ico.frame.getWidth() / Ico.iconLength) + 1
}

Ico.shiftFn = function(noanim){
	if(Ext.isEmpty(noanim)) noanim = false;
	Ico.iconsMax = Ico.getIconsMax();
	Ico.ribbon.setWidth(Ico.frame.getWidth() + Ico.iconLength * 2);
	//Ico.addIcon(Server.iconostasis[Ico.iconNext++], noanim);
	Ico.addIcon(Server.iconostasis[Ico.iconNext], noanim);
	Ico.iconPrev.push(Ico.iconNext);
	if(Ico.iconPrev.length == Server.iconostasis.length)
		Ico.iconPrev.remove(Ico.iconPrev[0]);
	do{
		Ico.iconNext = Ico.getRandomInt(0, Server.iconostasis.length - 1);
	} while(Ico.iconPrev.indexOf(Number(Ico.iconNext)) != -1)
	/*
	if(Ico.iconNext == Server.iconostasis.length)
		Ico.iconNext = 0;
	*/
};

Ico.shiftTask = {
	 run: Ico.shiftFn
	,interval: 3000
	,args: [false]
};

Ico.firstRunTask = {
	run: function(){
		Ico.shiftFn(true);
		Ico.i++;
		if(Ico.i == Ico.getIconsMax() - 1){
			Ext.TaskMgr.stop(Ico.firstRunTask);
			Ico.startMaskInterval();
		}
	}
	,interval: 1
};

Ico.startMaskInterval = function(){
	Ext.TaskMgr.start(Ico.shiftTask);

	(function(){
		Ext.TaskMgr.stop(Ico.shiftTask);
		Ico.showMask();
	}).defer(1000 * 60 * 3); // остановить ленту через x минут
};

Ico.getRandomInt = function(min, max){
	return Math.floor(Math.random() * (max - min + 1)) + min;
};

Ext.onReady(function(){
	/*
	if(Ext.get('master-video')){ // not available

		var playMaskShow = function(e, el, o){
			Ext.get('show-play')
				.setWidth(Ext.get('master-image').getWidth())
				.setHeight(Ext.get('master-image').getHeight())
				.setOpacity(.5)
				.show();
		};
		var playMaskHide = function(e, el, o){
			Ext.get('show-play')
				.hide();
		};
		var playMaskPlay = function(e, el, o){
			Ext.get('master-video')
				.show()
			;
			Ext.get('master-image')
				.hide()
			;

			Ext.get('show-play')
				.un('mouseleave', playMaskHide)
				.un('click', playMaskPlay)
			;
			Ext.get('master-image')
				.un('mouseenter', playMaskShow)
			;
			Ext.get('camera')
				.un('mouseleave', playMaskHide)
				.un('mouseenter', playMaskShow)
				.un('click', playMaskPlay)
			;
		}

		Ext.get('show-play')
			.hide()
			.on('mouseleave', playMaskHide, this)
			.on('click', playMaskPlay, this)
		;

		Ext.get('master-image')
			.addClass('video-available')
			.on('mouseenter', playMaskShow, this)
			.on('click', playMaskPlay, this)
		;

		Ext.get('camera')
			.on('mouseleave', playMaskHide, this)
			.on('mouseenter', playMaskShow, this)
			.on('click', playMaskPlay, this)
		;


		flowplayer(
			 "player"
			,"/js/flowplayer/flowplayer-3.1.5.swf"
			,{
				clip: {
					 autoPlay: true
					,autoBuffering: true
					,scaling: 'fit'
				}
			}
		);
	}
	*/


	Ico.ribbon = Ext.get('iconostasis');
	Ico.frame = Ico.ribbon.parent();

	Ico.iconLength = 100 + 6 + 6;
	Ico.iconPrev = [];
	Ico.iconNext = Ico.getRandomInt(0, Ico.getIconsMax());

	if(Ext.isIE6)
		Ico.pngStyle = "background: none; filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='{src}', sizingMethod='scale');";
	else
		Ico.pngStyle = "background-image: url({src});";

	Ico.iconTpl = new Ext.Template(
		'<div class="container" title="{alt}" style="'+Ico.pngStyle+'">'
			,'<div class="icon">'
				,'<a href="{href}">'
					,'<img src="'+Server.px+'" width="100" height="75" alt="{alt}" />'
				,'</a>'
			,'</div>'
		,'</div>'
	);

	if(!Ext.isEmpty(Server.iconostasis) && Server.iconostasis.length){
		Ico.i = 0;
		Ext.TaskMgr.start(Ico.firstRunTask);

	}

	var collapsible = Ext.query('div.collapsible');
	if(!Ext.isEmpty(collapsible) && Ext.isArray(collapsible) && collapsible.length){
		Ext.each(collapsible, function(item, index){
			var el = Ext.get(item);
			var h2 = el.first();
			//el.hide(true);
			var newElTpl = new Ext.Template(
				'<p>'
					,'<a class="local" title="Развернуть">{text}</a>'
				,'</p>'
			);
			var expandEl = newElTpl.insertBefore(el, {text: 'Биография'}, true);
			el.setVisibilityMode(Ext.Element.DISPLAY);
			expandEl.setVisibilityMode(Ext.Element.DISPLAY);
			el.on('click', function(){
				expandEl.show();
				el.slideOut('t', {
					 easing: 'easeOut'
					,duration: 1
					,callback: function(){
					}
				});
			});
			expandEl.on('click', function(){
				el.slideIn('t', {
					 easing: 'easeOut'
					,duration: 1
					,callback: function(){
						//expandEl.hide();
					}
				});
				expandEl.hide(true);
				/*
				expandEl.slideOut('t', {
					 easing: 'easeOut'
					,duration: 1
				});
				*/
			});
		});
	}



});

