infoController=function(){
	this.msgFrame=new Element('div',{'id':'infoBox'})
	$$('body')[0].appendChild(this.msgFrame);
	this.msgFrame.update("<div id='infoBoxBg'></div><div id='infoBoxCont'><img id='infoBoxContBg'/>" +
						 "<div id='infoBoxContainer'>" +
						 "<input id='infoClose' type='button'/>" +
						 "<div id='infoBrandLayer'><div id='projectorInfoView'></div><br class='clear'/><div class='infoContainer'> " +
						 "<div class='layerListCenter'><div class='sub'><div class='layerListCenterTitle'>Marki</div><div id='projectorInfoBrandList'>" +
						 "</div></div><div class='sub'><div class='layerListCenterTitle'>Kategorie</div><div id='projectorInfoCategoryList'>" +
						 "</div></div></div><br class='clear'/><div class='layerListBottom'>" +
						 "<div id='layerListView' style='float:left'><h2><span class='gray'>Podgląd dla : </span><span id='infoPageDetailTitle'>Kategorii</span></h2></div>" +
						 "<div id='layerListSort' style='float:right'><span class='gray'>Sortuj wg:</span>" +
						 "<span class='infoSBtn infoSortBtnActive' id='btn_infoCategoryContainer'>Kategorii</span>," +
						 "<span class='infoSBtn infoSortBtn' id='btn_infoBrandContainer'>Marek</span>,<span class='infoSBtn infoSortBtn' id='btn_infoPageContainer'>Stron</span>" +
						 "</div><br class='clear'/><div class='infoSortContainer' id='infoCategoryContainer' style='display:block;'></div>" +
						 "<div class='infoSortContainer' id='infoBrandContainer' style='display:none;'></div>" +
						 "<div class='infoSortContainer' id='infoPageContainer' style='display:none;'></div>" +
						 "<br class='clear'/></div></div></div><div id='infoFirmLayer'><div id='projectorInfoFirmView'></div><br class='clear'/>" +
						 "<div class='infoContainer'><div class='layerListCenter'><div class='sub'><div class='layerListCenterTitle'>Marki</div>" +
						 "<div id='projectorInfoFirmBrandList'></div></div><div class='sub'><div class='layerListCenterTitle'>Kategorie</div>" +
						 "<div id='projectorInfoFirmCategoryList'></div></div></div><br class='clear'/><div class='layerListBottom'>" +
						 "<div id='layerFirmListView' style='float:left'><h2><span class='gray'>Podgląd dla : </span><span id='infoCatalogDetailTitle'>Kategorii</span></h2></div>" +
						 "<div id='layerFirmListSort' style='float:right'><span class='gray'>Sortuj wg:</span>" +
						 "<span class='infoSBtn infoSortBtnActive' id='btn_infoFirmCategoryContainer'>Kategorii</span>," +
						 "<span class='infoSBtn infoSortBtn' id='btn_infoFirmBrandContainer'>Marek</span>," +
						 "<span class='infoSBtn infoSortBtn' id='btn_infoFirmPageContainer'>Katalogów</span>" +
						 "</div><br class='clear'/><div class='infoSortContainer' id='infoFirmCategoryContainer' style='display:block;'></div>"+
						 "<div class='infoSortContainer' id='infoFirmBrandContainer' style='display:none;'></div>" +
						 "<div class='infoSortContainer' id='infoFirmPageContainer' style='display:none;'></div>" +
						 "<br class='clear'/></div></div></div></div></div><div>");
	$('infoBoxContBg').setStyle({'behavior':'url(public/js/pngbehavior.htc)'});
	$('infoBoxContBg').setAttribute('src','/public/img/common/infoBoxBg.png');
	this.msgFrame=$('infoBox');
	var i=$$('.singleItem .info');
	for(var a=0;a<i.length;a++){ i[a].observe('click',this.openFromList.bind(this)); }
	if($('projector_info')){ $('projector_info').observe('click',this.openFromProjector.bind(this)); }
	$('infoClose').observe('click',this.close.bind(this));
}
infoController.prototype={
	observe:function(e){
		switch(e){
			case 'projector': if($('projector_info')){ $('projector_info').observe('click',this.openFromProjector.bind(this)); } break; 
		}
	},
	openFromList:function(ev){
		Event.stop(ev);
		this.cid=Event.element(ev).up('.singleItem').select('input[name="rid"],[type="hidden"]')[0].value;
		this.fB=new floatBox('infoBoxCont','fromTop');
		var arr=pageSize();
		this.msgFrame.setStyle({'width':arr[0]+'px','height':arr[1]+'px','display':'block'});
		this.msgFrame.setStyle({'visibility':'visible'});
		this.getThumbs();
	},
	openFromProjector:function(ev){
		this.cid=cid;
		this.fB=new floatBox('infoBoxCont','fromTop');
		var arr=pageSize();
		this.msgFrame.setStyle({'width':arr[0]+'px','height':arr[1]+'px','display':'block'});
		this.msgFrame.setStyle({'visibility':'visible'});
		this.getThumbs();
	},
	close:function(ev){
		this.fB.stop();
		this.msgFrame.setStyle({'visibility':'hidden'});
	},
	getThumbs:function(ev){
		if(this.infoCurrentCatalog!=this.cid){
			$('infoFirmLayer').style.display = 'none';
			$('infoBrandLayer').style.display = 'block';
			$('projectorInfoView').update('');
			$('projectorInfoView').setStyle({'height':'80px'});
			$('projectorInfoFirmView').update('');
			$('projectorInfoFirmView').setStyle({'height':'80px'});
			$('projectorInfoBrandList').update('');
			$('projectorInfoBrandList').setStyle({'height':'80px'});
			$('projectorInfoFirmBrandList').update('');
			$('projectorInfoFirmBrandList').setStyle({'height':'80px'});
			$('projectorInfoCategoryList').update('');
			$('projectorInfoCategoryList').setStyle({'height':'80px'});
			$('projectorInfoFirmCategoryList').update('');
			$('projectorInfoFirmCategoryList').setStyle({'height':'80px'});
			$('infoCategoryContainer').update('');
			$('infoFirmCategoryContainer').update('');
			$('infoBrandContainer').update('');
			$('infoPageContainer').update('');
			setLoaderBackground('projectorInfoView',1);
			setLoaderBackground('projectorInfoBrandList',1);
			setLoaderBackground('projectorInfoCategoryList',1);
			setLoaderBackground('infoCategoryContainer',1);
			setLoaderBackground('infoBrandContainer',1);
			setLoaderBackground('infoPageContainer',1);
			var href = 'projector/thumbs/catalog/'+this.cid+'/';
			new Ajax.Request(href, {		
				method:'get',
				onSuccess:this.onGetThumbs.bind(this)
			});
		}
		else{ $('infoBrandLayer').style.display = 'block'; $('infoFirmLayer').style.display = 'none'; this.create(''); }
	},
	onGetThumbs:function(ev){
		this.pages=ev.responseText.evalJSON(true);
		for(var a=0;a<this.pages['thumbs'].length;a++){
			this.pages['thumbs'][a]['href'] = this.pages['href_prefix'] + this.pages['thumbs'][a]['href'] + this.pages['href_postfix'];
			this.pages['thumbs'][a]['path'] = this.pages['path_prefix'] + this.pages['thumbs'][a]['path'] + this.pages['path_postfix'];
		}
		this.create('');
	},
	create:function(ev){
		try{Event.stop(ev);}catch(e){}
		if(this.section==1){
			$('infoFirmLayer').toggle();
			$('infoBrandLayer').toggle();
		}
		else{
			if(this.infoCurrentCatalog!=this.cid){
				href ='catalog/info/cid/'+this.cid+'/';
				new Ajax.Request(href, {		
					method: 'get',
					onSuccess:this.onCreate.bind(this)
				});
			}
			this.section=0;
		}
	},
	onCreate:function(ev){
		clearLoaderBackground('projectorInfoView');
		$('projectorInfoView').setStyle({'height':'auto'});
		clearLoaderBackground('projectorInfoBrandList');
		$('projectorInfoBrandList').setStyle({'height':'auto'});
		clearLoaderBackground('projectorInfoCategoryList');
		$('projectorInfoCategoryList').setStyle({'height':'auto'});
		clearLoaderBackground('infoCategoryContainer');
		clearLoaderBackground('infoBrandContainer');
		clearLoaderBackground('infoPageContainer');
		this.infoData=ev.responseText.evalJSON(true);
		$('projectorInfoView').update(this.infoData.html);
		this.createThumbsList(this.infoData.object[0],'Category',this.pages,0);
		this.createThumbsList(this.infoData.object[1],'Brand',this.pages,0); 
		this.createPagesList('Page',this.pages,0);
		this.observeNewElements();
		this.infoCurrentCatalog=this.cid;
	},
	createThumbsList:function(List,postfix,tPC,type){
		var a=0;
		var names=new Array();
		Object.keys(List).each( function(s){ names[a++]=s; });
		a=0;
		var html=new Array();
		var box=new Array();
		var obId=null;
		for(var xx=0;xx<names.length;xx++){
			html[a]="<span id='info"+postfix+"Container_info"+postfix+"Child_"+a+"' class='infoL infoList'>"+names[xx]+"</span>";
			var span=Element.extend(document.createElement('div'));
			var secId=null;
			if(postfix=='Category'){secId='a';}
			if(postfix=='Brand'){secId='b' }
			if(postfix=='FirmCategory'){secId='a'; }
			if(postfix=='FirmBrand'){secId='b'; }
			var div = Element.extend(document.createElement('div'));
			div.setAttribute('id', 'info'+postfix+'Child_' + a);
			div.addClassName('infoSectionChild');
			div.addClassName('clear');
			if(a>0) div.style.display = 'none';
			$('info'+postfix+'Container').appendChild(div);
			obId=List[names[xx]][1]['id'];
			for(var x=0;x<List[names[xx]][0].length;x++){
				$('info'+postfix+'Child_'+a).appendChild(this.createThumb(parseInt((List[names[xx]][0][x]-1)),tPC,type,obId,secId));
			}
			a++;
		}
		
		var s='';
		for(var a=0; a<html.length; a++)s += html[a] + ', ';
		$('projectorInfo'+postfix+'List').update(s.substring(0,s.length-2));
		for(var a=0; a<4; a++){ $('info'+postfix+'Container').appendChild(Element.extend(document.createElement('br'))) ; }
	},
	createPagesList:function(postfix,tPC,type){
		for(var a=0; a<tPC['thumbs'].length; a++){ 
			$('info'+postfix+'Container').appendChild(this.createThumb(parseInt((a)),tPC,type,null,null)); 
		}
		if(type!=1){ 
			for(var a=0; a<4; a++){ 
				$('info'+postfix+'Container').appendChild(new Element('br')) ; 
			} 
		}
	},
	createThumb:function(a,tPC,type,obId,secId){
		if(type==0){
			obj=Element.extend(document.createElement('a'));
			obj.setAttribute('rel','nofollow');
			obj.addClassName('infoThumbs'); 
			obj.addClassName('projectorThumb');
			obj.href = tPC['thumbs'][a].href; 
		}
		else if(type==1){ 
			obj=Element.extend(document.createElement('a')); obj.setAttribute('rel','nofollow'); 
			if(obId!=null&&secId!=null) obj.href = tPC['thumbs'][a].href/*+','+secId+','+obId*/;
			else obj.href = tPC['thumbs'][a].href; 
			obj.addClassName('imageBox');
		}
		else return null;
		if(obId!=null&&secId!=null) var postf=secId+'_'+obId+'_'; else var postf=''; 
		obj.setAttribute('id', 'infoPage_'+this.cid+'_'+postf+(a+1));
		obj.addClassName('floatLeft');
		obj.setStyle({'background':'#ededed url('+tPC['thumbs'][a].path+') 50% 50% no-repeat'});
		return obj;
	},
	observeNewElements:function(){
		if($('infoShowAllCatalog')){ $('infoShowAllCatalog').observe('click',this.showAllCatalogs.bind(this)); }
		var col=$$('.infoSBtn');
		for(var a=0;a<col.length;a++){ col[a].stopObserving('click'); col[a].observe('click',this.switchFiltrEvent.bind(this)); }
		var col=$$('.infoL');
		for(var a=0;a<col.length;a++){ col[a].observe('click',this.switchSectionFromListEvent.bind(this)); }
		this.allCatalogsLoaded=0;
	},
	switchFiltrEvent:function(ev){
		var element=Event.element(ev); 
		var id=element.id.split("_")[1];
		this.switchFiltr(id,element.id);
	},
	switchFiltr:function(id,btnId){
		$$('.infoSortContainer').each( function(s){s.style.display = 'none'});
		$$('.infoSortBtnActive').each( function(s){s.removeClassName('infoSortBtnActive'); s.addClassName('infoSortBtn');});
		$$('.infoListActive').each( function(s){s.removeClassName('infoListActive'); s.addClassName('infoList');});
		if($('infoShowCatalog')){ $('infoShowCatalog').removeClassName('infoShowCatalogActive'); }
		if($('infoShowCatalog')){ $('infoShowCatalog').addClassName('infoShowCatalog'); }
		$(btnId).removeClassName('infoSortBtn');
		$(btnId).addClassName('infoSortBtnActive');
		$(id).style.display = 'block';
		if(id=='infoBrandContainer'){ this.switchSectionFromList($('projectorInfoBrandList').select('span')[0]); }
		else if(id=='infoCategoryContainer'){ this.switchSectionFromList($('projectorInfoCategoryList').select('span')[0]); }
		else if(id=='infoFirmCategoryContainer'){ this.switchSectionFromList($('projectorInfoFirmCategoryList').select('span')[0]); }
		else if(id=='infoFirmBrandContainer'){ this.switchSectionFromList($('projectorInfoFirmBrandList').select('span')[0]); }
		else if(id=='infoPageContainer'){ $('infoPageDetailTitle').update($(btnId).innerHTML); }
		else if(id='infoFirmPageContainer'){ $('infoCatalogDetailTitle').update($(btnId).innerHTML); }
	},
	switchSectionFromListEvent:function(ev){
		Event.stop(ev);
		var e=Event.element(ev);
		this.switchSectionFromList(e);
	},
	switchSectionFromList:function(element){
		var id=element.id;
		var arr=id.split("_");
		$$('.infoSortContainer').each(function(s){s.style.display='none'});
		$(arr[0]).style.display = 'block';
		$$('.infoSectionChild').each(function(s){s.style.display='none'});
		if(this.section==0) $('infoPageDetailTitle').update($(id).innerHTML);
		else if(this.ection==1) $('infoCatalogDetailTitle').update(($(id).innerHTML));
		$$('.infoSortBtnActive').each(function(s){s.removeClassName('infoSortBtnActive'); s.addClassName('infoSortBtn');});
		$$('.infoListActive').each(function(s){s.removeClassName('infoListActive'); s.addClassName('infoList');});
		$$('infoSBtn').each(function(s){s.removeClassName('infoSortBtnActive');});
		$('btn_'+arr[0]).addClassName('infoSortBtnActive');
		$(id).removeClassName('infoList');
		$(id).addClassName('infoListActive');
		$(arr[1]+'_'+arr[2]).style.display = 'block';
	},
	showAllCatalogs:function(ev){
		Event.stop(ev);
		if(!this.allCatalogsLoaded){
			$('infoBrandLayer').style.display='none'; 
			$('infoFirmLayer').style.display='block';
			setLoaderBackground('projectorInfoFirmView',1);
			$('projectorInfoFirmView').setStyle({'height':'80px'});
			setLoaderBackground('projectorInfoFirmBrandList',1);
			$('projectorInfoFirmBrandList').setStyle({'height':'80px'});
			setLoaderBackground('projectorInfoFirmCategoryList',1);
			$('projectorInfoFirmCategoryList').setStyle({'height':'80px'});
			$('infoFirmPageContainer').update('');
			setLoaderBackground('infoFirmCategoryContainer',1);
			$('infoFirmBrandContainer').update('');
			setLoaderBackground('infoFirmBrandContainer',1);
			$('infoFirmPageContainer').update('');
			setLoaderBackground('infoFirmPageContainer',1);
			if(this.allCatalogsLoaded==0){
				new Ajax.Request('catalog/infoall/cid/'+this.cid+'/', {		
					method:'get',
					onSuccess:this.onShowAllCatalogs.bind(this)
				});
			}
		}
		else{ 
			$('infoBrandLayer').style.display='none'; 
			$('infoFirmLayer').style.display='block'; 
		}
		this.section = 1;
		this.switchFiltr('infoFirmPageContainer','btn_infoFirmPageContainer');
	},
	onShowAllCatalogs:function(ev){
		clearLoaderBackground('projectorInfoFirmView');
		$('projectorInfoFirmView').setStyle({'height':'auto'});
		clearLoaderBackground('projectorInfoFirmBrandList');
		$('projectorInfoFirmBrandList').setStyle({'height':'auto'});
		clearLoaderBackground('projectorInfoFirmCategoryList');
		$('projectorInfoFirmCategoryList').setStyle({'height':'auto'});
		clearLoaderBackground('infoFirmCategoryContainer');
		clearLoaderBackground('infoFirmBrandContainer'); 
		clearLoaderBackground('infoFirmPageContainer');	
		var j=ev.responseText.evalJSON(true);
		$('projectorInfoFirmView').update(j.html);
		this.createThumbsList(j.object[1],'FirmCategory',j.object[0],1);
		this.createThumbsList(j.object[2],'FirmBrand',j.object[0],1);
		this.createPagesList('FirmPage',j.object[0], 1);
		this.observeNewElements();
		$('infoShowCatalog').observe('click',this.create.bind(this));
		this.allCatalogsLoaded=true;
	}
}