(function($, R, win, doc, M){
	
	$.fbSvgMap = function(svg, options){
		this.svg = svg;
		this.options = $.extend({
			
		}, options);
		this.holder = $('<div class="fb-svg-map"></div>');
		this.isLoad = false;
		this.isRender = false;
		this.isInit = false;
		this.dataSplitter1 = '-';
		this.vector = {};
				
	};	
	
	$.fbSvgMap.prototype = {
		
		$doc: $(doc),
		$proxy: $.proxy,
		ceil: M.ceil,
		
		// IE<9
		oldIE: (function(){ return $.browser.msie && $.browser.version < 9 })(),
		
		load: function(){
			
			if(this.isLoad || !this.svg || this.svg.constructor != String )		
			{	
				this.log('selhalo při loadu, nejsou dodány potřebná data nebo byl load již proveden');
				return;
			}
			
			// načtení svg
			$.get(this.svg, this.$proxy(function(svg, textStatus){  
				this.isLoad = true;
				this.render(svg)
				this.$doc.trigger('load.fbSvgMap');
			}, this), 'xml');
			
			return this;
		},
		render: function(svg){
			// vytvoří plátno
			var $svg = $('svg', svg),
				width = this.ceil(parseFloat($svg.attr('width'))),
				height = this.ceil(parseFloat($svg.attr('height')));
			this.canvas = R(this.holder.get(0), width, height);
			// vytvoří cesty
			$('polygon', svg).each(this.$proxy(function(i, el){
				this.createPath($(el));   
			}, this));
			
			this.isRender = true;
			this.$doc.trigger('render.fbSvgMap');	
		},
		init: function(element){
			
			// normalizece objektů - rozlišná implementace
			this.pathElement =  !this.oldIE ? 'path' : 'shape';  
			this.element = $(element);
			
			this.element
				.delegate(this.pathElement, 'mouseover.fbSvgMap mouseout.fbSvgMap', this.$proxy(this.handlePathOverOut, this))
				.delegate(this.pathElement +', '+ this.textElement, 'click.fbSvgMap',  this.$proxy(this.handleClick, this))
				.bind('highlight.fbSvgMap', this.$proxy(this.handleHighlight, this))
				
			// vykreslení mapy
			this.element.append(this.holder);	
		},
		destroy: function(){
			this.element
				.undelegate(this.pathElement, 'mouseover.fbSvgMap mouseout.fbSvgMap', this.handlePathOverOut)
				.undelegate(this.pathElement +', '+ this.textElement, 'click.fbSvgMap', this.handleClick)
				.unbind('highlight.fbSvgMap', this.handleHighlight)	
		},
		handlePathOverOut: function(e){
			// pokud najedu na vrstvu stejne bunky tak se nic nestane
			// todo kontrola na čísla
			var data = $(e.currentTarget).data('svg');
			var rData = $(e.relatedTarget).data('svg')
			
			if(data && data.number !== this.activeNumber)
			{
				if(e.type == 'mouseover')
				{
					if(!this.hoverNumber || data.number != this.hoverNumber){
						this.hoverNumber = data.number;
						this.handlePathHover(e, data);
					}
				}
				else
				{
					if(!rData || rData.number != this.hoverNumber){
						this.hoverNumber = null;
						this.handlePathHover(e, data);
					}
					
				}
			}
			return false;	
		},
		handlePathHover: function(e, data){
			
			var groupArrVector = this.vector['box'+data.number];
				
			for(var j = 0, k = groupArrVector.length; j < k; j++)
			{
				var layer = groupArrVector[j];
				layer.stop().animate({'opacity': e.type=="mouseover" ? '0.45' : '0'}, 500, ">");
			}
				
			
			/*this.changeTimer = clearTimeout(this.changeTimer)
			this.changeTimer = setTimeout(this.$proxy(function(){
				this.element.trigger('changed.fbSvgMap', [box, e.type]);
			}, this), 50)
                           */
			return false;	
		},
		handleClick: function(e){
			var data= $(e.currentTarget).data('svg');;
					
			if(data)
			{
				var num = data.number;
			
				this.element
					.trigger('clicked.fbSvgMap', [num])	
					.trigger('highlight.fbSvgMap', [num]);
			}
			
			return false;		
		},
		handleHighlight: function(e, number)
		{
			
			if(number)
			{		
				var highlight = function(number, opacity, cursor)
				{
					var groupArrVector = this.vector['box'+number];
						
					for(var j = 0, k = groupArrVector.length; j < k; j++)
					{
						var layer = groupArrVector[j];
						
						layer
							.stop()
							.attr({
								 
								'cursor': cursor
							})
							.animate({'opacity': opacity}, 500, ">");
					}
				}
			
				if(typeof this.activeNumber !== 'undefined')
				{
					highlight.call(this, this.activeNumber, '0', 'pointer'); 	
				}   
				
				highlight.call(this, number, '0.85', 'default');
				this.activeNumber = number;
			}
			
			return false;
		},
		createPath: function($polygon){	
			var data = this.getData($polygon.attr('id')),       	
				path = $polygon.attr('points');
			
			var attr = {
				 'fill': $polygon.attr('fill'), 
				 'stroke': 0,
				 'opacity': 0,
				 'cursor': 'pointer'   
			};
				
			var layer = this.canvas.path('M '+ path +' z').attr(attr);
			$(layer.node).data({'svg': data});
			
			if(typeof this.vector['box'+data.number] == 'undefined')
			{
				this.vector['box'+data.number] = [];
			}	
			this.vector['box'+data.number].push(layer);	
		},
		// připraví data z řetězce
		getData: function(str){ 
			try
			{
				var arr = str.split(this.dataSplitter1);
				
				return {
					number: parseInt(arr[1]),
					side: arr[2]
				};
			}
			catch(err)
			{
				this.log('chyba při získání barvy');
				this.log(err);
			}
		},
		log: function(text){
			typeof console != 'undefined' && console.log(text);	
		}
	};
	
})(jQuery, Raphael, window, document, Math);
