(function () {
	var paper = false;
	var mapka = false;
	var xCorrect = false;
	var yCorrect = false;
	var infoHoverCien = false;
	var infoHover = false;
	var cien = false;
	var plachta = false;
	var last = false;
	var infoHoverCienAttr = {
		'fill': 'rgb(0,0,0)',
		'fill-opacity': 0.8,
		'stroke-opacity': 0,
		'display': 'inline',
		'cursor' : 'pointer'
	};
	var infoHoverAttr = {
		'fill' : 'rgb(255,255,255)',
		'cursor' : 'pointer'
	};
		var cien, plachta, info, infoCien;
			var cienAttr = {
			'fill': 'rgb(54, 112, 25)',
			'fill-opacity': 1,
			'stroke-opacity': 0,
			'display': 'inline',
			'cursor' : 'auto'
		};
		var plachtaAttr = {
			'fill': 'rgb(77, 160, 36)', 
			'fill-opacity': 1,
			'stroke-opacity': 0, 
			'display': 'inline',
			'cursor' : 'auto'
		};
	
	function pokaz (region) {
		var temp = paper.path(region.path);
		temp.attr(wojewodztwaAttr);
		temp.nazwa = region.nazwa;
		temp.tytul = region.tytul;
		//console.log(set);
		mapka.push(temp);
	} 
	function pokazEnd () {
		//console.log(mapka[0]);
		//console.log(mapka);
		//alert(mapka);
		//mapka.scale(0.48, 0.48, 0, 0);
	}
	
	function setHovers () {
		mapka.hover(hoverIn);
		mapka.click(mapkaClick);
		(jQuery.proxy(mapkaClick, mapka[5]))(); // wybierz wojewodztwo-region automatycznie
	}	
	function unsetHovers () {
		mapka.unbind('click');
		mapka.unbind('hover');
	}
	function init (x, y) {
		if (paper) {
			paper.clear();
		} else {
			paper = Raphael("map_woj", 330, 310);
		}
		mapka = paper.set();
		
		xCorrect = (typeof(x) != 'undefined') ? x : 15;
		yCorrect = (typeof(y) != 'undefined') ? y : 15;
		
		infoHoverCien = false;
		infoHover = false;
		cien = false;
		plachta = false;
	}
	function obrys (path) {
			var calosc = paper.path(path);
				calosc.attr(caloscAttr);
				//calosc.scale(0.48, 0.48, 0, 0);
	}
	

	function hoverIn () { 
		if (infoHover) { infoHover.remove(); }
		if (infoHoverCien) { infoHoverCien.remove(); }
				if (info) {
			info.remove();
		}
		if (infoCien) {
			infoCien.remove();
		}
		if (last) {
			
			last.attr({ 'fill' : 'rgb(224, 236, 219)' });
			
		}
		var offset = this.getBBox();
		var textX = offset.x + (offset.width/2) - xCorrect;
		var textY = offset.y + (offset.height/2) - yCorrect;
		this.attr({'fill' : 'rgb(174,208,159)'}); 
		var hoverCienWidth = (this.tytul.length*9);
		infoHoverCien = paper.rect(textX-(hoverCienWidth/2), textY-10, hoverCienWidth, 20);
		infoHoverCien.attr(infoHoverCienAttr);
		infoHover = paper.text(textX, textY, this.tytul);
		infoHover.attr(infoHoverAttr);
		infoHoverCien.click(jQuery.proxy(mapkaClick, this));
		infoHover.click(jQuery.proxy(mapkaClick, this));
		last = this;
	}
	

	
	function mapkaClick () {
		//console.log(this); // nazwa wojewodztwa
		if (cien) {
			cien.remove();
		}
		if (plachta) {
			plachta.remove();
		}
		if (info) {
			info.remove();
		}
		if (infoCien) {
			infoCien.remove();
		}
		cien = this.clone();
		cien.attr(cienAttr);
		plachta = this.clone();
		plachta.attr(plachtaAttr);
		plachta.insertAfter(mapka);
		cien.insertAfter(mapka);
		plachta.hover(jQuery.proxy(hoverIn, this));
		plachta.animate({'scale': '1.15, 1.15 '}, 200);
		var offset = this.getBBox();
		cien.animate({ 'scale': '1.15, 1.15, ' + offset.x + ', ' + offset.y  }, 200);
		
		$("#map_woj").trigger('kbk', [this.nazwa]);			
	}
	var caloscAttr = {
		'fill':'none',
		'fill-opacity':1,
		'stroke':"#000000",
		'stroke-width':"1px",
		'stroke-linecap':"butt",
		'stroke-linejoin':"miter",
		'stroke-opacity':1
	};
	
	// wojewodztwa
	var wojewodztwaAttr = {
		"fill": "rgb(224, 236, 219)",
		"fill-opacity": "1",
		"stroke": "rgb(0, 0, 0)",
		"stroke-width": "1px",
		"stroke-linecap": "butt",
		"stroke-linejoin": "miter",
		"stroke-opacity": "1",
		"display": "inline",
		"cursor": "pointer"
	};
	this.caloscAttr = caloscAttr;
	this.wojewodztwaAttr = wojewodztwaAttr;
	this.mapkaClick = mapkaClick;
	this.dodaj = pokaz;
	this.hoverIn = hoverIn;

	this.pokaz = pokaz;
	this.setHovers = setHovers;
	this.init = init;
	this.obrys = obrys;
	this.pokazEnd = pokazEnd;

	window.gminyMapa = this;
	//console.log(this);
	//console.log(this);
})();

