var CCManager=new Class({
	config:{
		cp:'HP',
		lng:'it',
		'swf-container': 'flash_catalogues',
		'min-fpv':8,
		'swf-width':'100%',
		'swf-height':'100%',
		links:{
			maniago_design: 'http://www.maniagodesign.it',
			qualita_maniago: 'http://www.qualitamaniago.it'
		},
		fpv:null,
		top_menu_y: 10,
		effMenu:null,
		effToobar:null,
		effLogin:null,
		e1:null
	},
	login:'closed',
	initialize:function(cp,lng){
		this.config.fpv=Browser.Plugins.Flash.version;
		this.config.cp=(cp === '') ? 'HP':cp;
		this.config.lng=(lng === '') ? this.config.lng : lng;
		this.addListeners().addEffects().updateMainMenu().initPageLayout();
		//this.showMenu();
	}
});
CCManager.prototype.initPageLayout=function()
{
	switch(this.config.cp)
	{
		case 'HP':
			this.addCatalogues();
		break;
		default:
			$(this.config['swf-container']).show();
			this.showMenu();
		break;
	}
    
	return this;
}
CCManager.prototype.updateMainMenu=function()
{
	var active='.mm-';
	switch(this.config.cp){

		case 'soci':
			active+='associates';
		break;
		case 'progetti':
			active+='store';
		break;
		case 'prodotti':
			active+='store';
		break;
		case 'contatti':
			active+='contacts';
		break;
		default:
			active+=this.config.cp;
		break;
	}
	$$(active).addClass('selected');
	return this;
}
CCManager.prototype.navigateToURL=function(type){
	switch(type)
	{
		case 'maniago_design':
			window.open(this.config.links[type]);
		break;
		case 'qualita_maniago':
			window.open(this.config.links[type]+'?lang='+this.config.lng);
		break;
	}
}
CCManager.prototype.addListeners=function()
{
	window.addEvent('FlashComplete',this.showMenu.bind(this));
	/* window.addEvent('ccm:toggle-members-docs',this.toggleMembersDocs.bind(this)); */
	return this;
}
CCManager.prototype.addEffects=function()
{
	this.config.effMenu=new Fx.Morph($('topmenu'),{
		duration: 3000,
		transition: Fx.Transitions.Elastic.easeOut
	});
	this.config.effToobar=new Fx.Morph($$('.toolbar_1')[0],{
		duration: 1000,
		transition: Fx.Transitions.Sine.easeOut
		
			
		
	});
	this.config.effLogin=new Fx.Morph($('login-box'),{
		duration: 1000,
		transition: Fx.Transitions.Sine.easeOut
	});
	return this;
}
CCManager.prototype.showMenu=function()
{
	this.showLayout();
}
CCManager.prototype.showLayout=function()
{
	this.config.effMenu.cancel();
	//var sY=$('topmenu').getPosition().y;
	$('topmenu').show();
	this.config.effMenu.start({
		'top': this.config.top_menu_y
	});
	
	$$('.ccmfooter')[0].setStyle('opacity',0);
	try {
		if (this.config.cp != 'HP') {
			switch(this.config.cp){
				case "progetti":
					this.prepareElements($$('.productObject'));
				break;
				case 'news':
					this.prepareElements($$('.news_row'));
					//this.prepareNews();
				break;
			}
			if (!$$('.bottom-left-content')[0] || !$$('.top-right-content')[0]) {
			 throw({
			 'name' : 'Error on display content layout',
			 'type' : 'one or more layout objects appear to be null or undefined',
			 'fn': '@CCManager Class: method showLayout'
			 })
			}
			$$('.bottom-left-content')[0].setStyle('opacity', 0);
			$$('.top-right-content')[0].setStyle('opacity', 0);
			new Fx.Morph($$('.top-right-content')[0], {
				fps: 50
			}).start({
				'opacity': [0, 1],
				'top': [-15, 0]
			});
			new Fx.Morph($$('.bottom-left-content')[0], {
				fps: 25,
				onComplete: function(){
                    // Call any in-page after-page-drawing initialization code
                    // we might have
                    if ( window.inpage_init ) {
                        inpage_init();
                    }
					switch(this.config.cp){
						case "progetti":
							$each($$('.productObject'),function(item,index,arr){
								this.showElement.delay((index+1)*500,this,item);
							}.bind(this));
						break;
						case 'news':
							$each($$('.news_row'),function(item,index,arr){
								this.showElement.delay((index+1)*500,this,item);
							}.bind(this));
						break;
					}
					new Fx.Morph($$('.ccmfooter')[0], {
						duration: 1000,
						transition: Fx.Transitions.Elastic.easeOut,
						onComplete:function(){
						}.bind(this)
					}).start({
						'opacity': [0, 1],
						'top': [-50, 100]
					});
				}.bind(this)
			}).start({
				'opacity': [0, 1],
				'margin-top': [-30, 0]
			});
		}
		else {
			new Fx.Morph($$('.ccmfooter')[0], {
				duration: 1000,
				transition: Fx.Transitions.Elastic.easeOut
			}).start({
				'opacity': [0, 1],
				'top': [-50, 100]
			});
		}
	} 
	catch (e) {
		try {
			//console.log(e);
		}catch(e){}
	}
	$('wrap').show();
	this.showToolbar.delay(1500,this);
	this.showLogin.delay(2000,this);
}
CCManager.prototype.showElement=function(ele)
{
	var fn=ele.retrieve('eff').anim.start({'opacity':[0,1]})
}
CCManager.prototype.prepareElements=function(ele)
{
	$each(ele,function(item,index,arr){
		item.store('eff',{'anim': new Fx.Morph(item, { duration: 600})});
		item.setStyle('opacity',0);
		var ele=item.getElement('.img-loader');
		new Swiff('swf/Loader/MacLoader.swf', {
		    id: 'swf_loader',
		    container:ele,
		    width: 30,
		    height: 30,
		    params: {
		        wmode: 'transparent',
		        bgcolor: '#ff3300'
		    },
		    vars: {
		        loader_color:"0x000000"
		    }
		});
	}.bind(this));
}
CCManager.prototype.showToolbar=function()
{
	this.config.effToobar.cancel();
	this.config.effToobar.start({
		'top': -8
	});
}
CCManager.prototype.showLogin=function(){
	this.config.effLogin.cancel();
	this.config.effLogin.start({
		'top': -100
	});
	$$('.open-login')[0].addEvent('click',function(){
		this.toggleLogin();
	}.bind(this))
}
CCManager.prototype.toggleLogin=function()
{
	var aY=$('login-box').getPosition().y;
	this.config.effLogin.options.duration=500;
	//console.log(this.config.effLogin);
	this.login=(this.login=='closed') ? 'opened' : 'closed';
	var nY=(this.login=='opened') ? 0 : -100;
	this.config.effLogin.cancel();
	this.config.effLogin.start({
		'top': [aY,nY]
	});
	
}
CCManager.prototype.addCatalogues=function()
{
	if (this.config.fpv >= this.config['min-fpv']) {
		$(this.config['swf-container']).show();
		
		var swf=new Swiff('swf/ccm.swf', {
		    id: 'swf_catalogues',
		    container:this.config['swf-container'],
		    width: this.config['swf-width'],
		    height: this.config['swf-height'],
		    params: {
		        wmode: 'transparent',
		        bgcolor: '#ff3300'
		    },
		    vars: {
		        lng: this.config.lng
		    }
		});
		
	}
	else
	{
		$(this.config['swf-container']).show();
		try {
			$('flash-alert').show();
		}catch(e){}
	}
}
CCManager.prototype.addBackground=function()
{
   if (this.config.fpv >= this.config['min-fpv']) {
   	var swf=new Swiff('swf/ccm.swf', {
   		id: 'swf_catalogues',
   		container: this.config['swf-container'],
   		width: this.config['swf-width'],
   		height: this.config['swf-height'],
   		params: {
   			wmode: 'transparent',
   			bgcolor: '#ff3300'
   		},
   		vars: {
   			lng: this.config.lng,
   			onlyBackground: true
   		}
   	});
   }
   else
   {
   	//console.log('No flash player!');
   }
}
CCManager.prototype.toggleMembersDocs=function(ele)
{
	var mc=$(ele).getElement('.member_catalogue');
	$each(mc,function(item,index,arr){
			if(!item.hasClass('closed')){
				item.addClass('closed');
			}else{
				item.removeClass('closed');
			}
		
		}.bind(this)
	);
}
