function Mrr () {
	
	this.post = {};
	this.url = '/';
	this.window = null;
	this.projects = null;
	this.parameters = {};
	this.currImage = 1;
	this.currentIndex = null;
	
	this.reload =  function() {
		try {
			$('mainLoading').show();
			$('mainError').hide();
			$('mainContent').innerHTML = '';
			
			this.spawnImage();
			
			new Ajax.Request(this.url, {
				method: 'post',
				parameters: this.parameters,
	    		onSuccess: this.onSuccessMain,
			    onFailure: this.onFailureMain
	  		});
		} catch (e) {
			console.warn(e);
		}
	}
	
	this.onSuccessMain = function (transport, json) {
		$('mainLoading').hide();
		$('mainContent').innerHTML = transport.responseText;
		mrr.evalAllScripts(transport.responseText);
	}
	
	this.onFailureMain = function () {
		$('mainLoading').hide();
		$('mainError').show();
	}
	
	this.printIt = function (id) {
		try {
			if (typeof(id)=='undefined')
				id = 'mainContent';
			window.toPrint = $(id).innerHTML;
			console.debug(window.toPrint);
			sWindow = window.open('/print_ajax', "printWindow", "width=640, height=600, scrollbars, status=no");
			sWindow.focus();
		} catch (e) {
			console.warn(e);
		}
	}
	
	this.spawnImage = function () {
		/*
		try {
			 window.document.mapaSwf.SetVariable('zmienObrazek' , true);
		} catch (e) {
			console.warn(e);
		}
		*/
		if ($('top').className=='sub_en') return ; // w wersji angielskiej nie rotujemy
		var min = 1;
		var max = 4;
		do {
			var rnd = Math.floor(Math.random()*max)+min;
		} while (rnd==this.currImage)
		this.currImage = rnd;
		$('top').className = 'sub'+rnd;
	}
	
	this.loadWindowContent = function (url, post) {
		try {
			this.window.update('<img src="img/loading_circle.gif" alt=""/> Wczytywanie danych, proszę czekać ');
			//this.window.getContent().update('');
			new Ajax.Request(url, {
				method: 'post',
				parameters: post,
	    		onSuccess: this.onSuccessWindow.bind(this),
			    onFailure: this.onFailureWindow.bind(this)
	  		});
		} catch (e) {
			console.warn(e);
		}
	}
	
	this.onSuccessWindow = function (transport, json) {
		//this.window.getContent().update(transport.responseText);
		this.window.hide();
		this.window.update(transport.responseText);
		this.evalAllScripts(transport.responseText);
		Effect.BlindDown('window'+this.currentIndex, {duration:0.3});
		/*
		if (typeof(projectPwt)!='undefined' && projectPwt!='') {
			this.window.setTitle(projectWjw + '<img src=\'./img/arr.gif\' />' + projectPwt);
		} else {
			this.window.setTitle(projectWjw);
		}
		try {
			sliders.descriptionSlider = new Draggable('descriptionHandler', {constraint: 'vertical', change: sliders.slideDown});
		} catch (e) {}
		try {
			sliders.attachmentsSlider = new Draggable('attachmentsHandler', {constraint: 'vertical', change: sliders.attachmentsSlideDown});
		} catch (e) {}
		
		this.window.setSize(this.window.width, $('boxContentRoar').scrollHeight+20, false);
		*/
		
	}
	
	this.onFailureWindow = function () {
		console.warn('PANIC!');
	}
	
	this.getProjectIdByIndex = function (index) {
		try {
			if (typeof(mrr.projects[index])=='undefined' || mrr.projects[index]==null)
				return false;
			return mrr.projects[index];
		} catch (e) {
			return false;
		}
	}
	
	this.showProject = function (index, thisObject) {
		if (typeof(mrr.projects[index])=='undefined' || mrr.projects[index]==null)
			return false;
		var id = this.getProjectIdByIndex(index);
		var prev = this.getProjectIdByIndex(index-1);
		var next = this.getProjectIdByIndex(index+1);
		
		if (id==false)
			return false;
		
		if (this.currentIndex!=null && this.currentIndex!=index) {
			//Effect.BlindUp('window'+this.currentIndex, {duration:0.1});
			if ($('window'+this.currentIndex)!=null)
				$('window'+this.currentIndex).hide();
		}
			
		var post = new Object();
		this.currentIndex = index;
		post.id = id;
		post.index = index;
		if (prev!=false)
			post.prevIndex = index-1;
		if (next!=false)
			post.nextIndex = index+1;
			
		/*if (this.window==null)
			this.openWindow();
		this.window.setTitle('Wczytywanie danych, proszę czekać...');*/
		if(!$('window'+index)) {
			newRow = Builder.node('tr',{className:'insideWindowTr'}, [
				Builder.node('td',{colSpan:4, className:'insideWindowTd'},[
					Builder.node('div',{className:'insideWindow', id:'window'+index, style:'width: 100%'})
				])
			]);
			if(thisObject.nextSiblings()[0]) {
				newContent = thisObject.parentNode.insertBefore(newRow, thisObject.nextSiblings()[0]);	
			} else {
				newContent = thisObject.parentNode.appendChild(newRow);	
			}
			/*
			newContent.onmouseover = function() {
				$('window'+index).style.background = '#fff';
			}
			*/
			this.window = $('window'+index);
			// this.window.update('Wczytywanie projektu, proszę czekać...');
			// $('window'+index).show();
			// Effect.BlindDown('window'+this.currentIndex, {duration:0});
			this.loadWindowContent('/project_ajax', post);	
			// Effect.BlindDown('window'+index, {duration:0.3});
		} else {
			if($('window'+index).visible()) {
				//$('window'+index).hide();
				Effect.BlindUp('window'+index, {duration:0.3});
			} else {
				//$('window'+index).show();
				Effect.BlindDown('window'+index, {duration:0.3});
			}
		}
	}
	
	this.openWindow = function () {
		onCloseFunc = function(win) {
			delete this.window;
			return true;
		}
		var width = 920;
		var height = 740;
		
		this.window = new Window({className: "dialog", title: '', width:width, height:height, destroyOnClose: true, recenterAuto:false, minimizable:false, maximizable:false, resizable:false, showEffectOptions: {duration: 0.2}, hideEffectOptions: {duration: 0.2}}); 
		this.window.setCloseCallback(onCloseFunc.bind(this));
		this.window.showCenter();
		this.window.toFront();
	}
	
	this.open = function (page) {
		try {
			switch (page) {
				case 'main':
					var url = '/main_ajax';
					break;
				case 'news':
					var url = '/news_ajax';
					break;
				case 'contact':
					var url = '/contact_ajax';
					break;
				case 'sitemap':
					var url = '/sitemap_ajax';
					break;
				case 'recommend':
					var url = '/recommend_ajax';
					break;
				case 'about':
					var url = '/about_ajax';
					break;
				default:
					return false;
			}
			this.url = url;
			this.parameters = {};
			this.reload();
		} catch (e) { 
			console.warn(e);
		}
	}
	
	this.showNews = function (symbol) {
		/*
		if (this.window==null)
			this.openWindow();
		this.window.setTitle('Wczytywanie projektu, proszę czekać...');
		this.loadWindowContent('/showNews,symbol,'+symbol, {});
		*/
		this.parameters = {symbol: symbol};
		this.url = '/showNews';
		this.reload();
	}
	
	this.recommend = function () {
		//this.parameters = $('polecForm').serialize(true);
		try {
			this.parameters.form = document.forms['polecForm'].elements['form'].value;
			this.parameters.name = document.forms['polecForm'].elements['name'].value;
			this.parameters.email = document.forms['polecForm'].elements['email'].value;
			this.parameters.emailTo = document.forms['polecForm'].elements['emailTo'].value;
			this.url = '/recommend_ajax';
			this.reload();
		} catch (e) {console.warn(e);}
	}
	
	this.searchProjects = function (wjw_id, pwt_id, fnd_id, prg_id, dzl_id, prj_promocyjny, prj_nazwa, prj_beneficjent, prs) {
		this.parameters.page = 0;
		if (typeof(this.parameters.limit)=='undefined') {
			try {
				this.parameters.limit = $('limit').value;
			} catch (e) {
				this.parameters.limit = 20;
			}
		}
		this.parameters.wjw_id = wjw_id;
		this.parameters.pwt_id = pwt_id;
		this.parameters.fnd_id = fnd_id;
		this.parameters.prg_id = prg_id;
		this.parameters.dzl_id = dzl_id;
		this.parameters.prj_promocyjny = prj_promocyjny;
		this.parameters.prj_beneficjent = prj_beneficjent;
		this.parameters.prj_nazwa = prj_nazwa;
		this.parameters.page = 0;
		this.parameters.orderColumn = 'PRJ_WKLAD';
		this.parameters.orderDirection = 'desc';
		this.parameters.prs = prs;
		this.url = '/projects_list_ajax';
		this.reload();
	}
	
	this.setLimit = function (limit) {
		this.parameters.limit = limit;
		this.parameters.page = 0;
		this.reload();
	}
	
	this.nextPage = function () {
		try {
			this.parameters.page = this.parameters.page*1 + 1;
			this.reload();
		} catch (e) {console.warn(e);}
	}
	
	this.setPage = function (page) {
		try {
			this.parameters.page = page;
			this.reload();
		} catch (e) {console.warn(e);}
	}
	
	this.prevPage = function () {
		try {
			this.parameters.page = this.parameters.page*1 - 1;
			if (this.parameters.page<0)
				this.parameters.page = 0;
			this.reload();
		} catch (e) {console.warn(e);}
	}
	
	this.compareRegions = function (id_1, id_2, powiaty) {
		try {
			console.debug('compareRegions('+id_1+','+id_2+','+powiaty+')');
			var id_1 = parseInt(id_1);
			var id_2 = parseInt(id_2);
			if (isNaN(id_1) || isNaN(id_2))
				throw "Not int" ;
			if (typeof(powiaty)=='undefined')
				var powiaty = false;
			this.parameters = {};
			if (powiaty) {
				this.parameters.mode = 'powiaty';
				this.parameters.pwt_id_1 = id_1;
				this.parameters.pwt_id_2 = id_2;
			} else {
				this.parameters.mode = 'wojewodztwa';
				this.parameters.wjw_id_1 = id_1;
				this.parameters.wjw_id_2 = id_2;
			}
			this.url = '/compare_ajax';
			this.reload();
		} catch (e) {
			console.warn(e);
		}
	}
	
	/**
	 * Wykonuje wszystkie skrypty w tagach SCRIPT
	 * @access private
	 * @param text Tresc HTML w ktorym sa taki SCRIPT
	 **/
	this.evalAllScripts = function(text) {
		reg = new RegExp ('<script.*?>([\\S\\s]*?)</script>', 'gi');
		while (fnd = reg.exec(text)) {
			try {
				eval(fnd[1]);
			} catch (e) {
				console.warn('Failed to eval ' + fnd[1]);
				console.warn(e);
			}
		} 
	}
	
}

window.mrr = new Mrr();

// Preload images
window.preloadedImages = new Array();
for (i=1; i<=6; i++) {
	preloadedImages[i] = new Image();
	preloadedImages[i].src = "img/top"+i+".jpg";
}

