// JavaScript Document
var mouseX, mouseY;

var div_mouseX, div_mouseY;

function TrimString(sInString){
	//sInString = sInString.replace(/ /g,' ');
	return sInString.replace(/(^\s+)|(\s+$)/g, "");
}

function getCanvasContex() {
	
	var canvas = document.getElementById("map_canvas");
	
	if (canvas){
	if (canvas.getContext) {
		
		return canvas.getContext("2d");

	}}
	
	return null;
}

function clearCanvas()
{
	var canvas = document.getElementById("map_canvas");
	if (canvas.getContext) {
		
		ctx = canvas.getContext("2d");
	
		ctx.clearRect(0, 0, canvas.width, canvas.height);	
	}
}

function reDrawBackground()
{
	clearCanvas();
	$("area").each(function (i) {
		
		_coords = $(this).attr('coords');
		
		bg_color = $(this).attr('bgcolor');
		
		coords = _coords.split(",");
		
		ctx = getCanvasContex();
		ctx.strokeStyle = "#FF0000";
		ctx.beginPath();
		for (i = 0; i < coords.length; i=i+2){
			if (!i) {
				ctx.moveTo(TrimString(coords[i]),TrimString(coords[i+1]));
			} else {
				ctx.lineTo(TrimString(coords[i]),TrimString(coords[i+1]));
			}

		}
		ctx.closePath();
		ctx.stroke();
		
		ctx.fillStyle = bg_color;//"#FF0000";

		ctx.fill();
		

    });

}

function reDrawBackgroundIE()
{
	//clearCanvas();
	
	var areasCount = $("area").length;
	var areasInd = 0;
	
	canvas_ie = "";
	$("area").each(function (ind) {
		
		region_id = $(this).attr('alt');
		
		bg_color = $(this).attr('bgcolor');
		
		_coords = $(this).attr('coords');
		
		coords = _coords.split(",");
		
		//canvas_ie = $('#map_canvas_ie').html();

		vShape = '<v:shape';
		vShape += ' name="shape"';
		vShape += ' coordorigin="0,0"';
		vShape += ' coordsize="620,600"';
		vShape += ' style="zoom:1; margin:0 ;padding:0; display:block; position:absolute; top:0px; left:0px; width:620px; height:600px;"';
		vShape += ' fillcolor="'+bg_color+'"';
		vShape += ' regionid="'+region_id+'"';
		vShape += '';
		vShape += ' path = "m ';
		
		//'1,1 l 1,200, 200,200, 200,1 x e"></v:shape>'
		
		for (i = 0; i < coords.length; i=i+2) {
			if (i==2) { vShape += " l "; } else { if (i!=0) { vShape += ", "; } }
			vShape += TrimString(coords[i])+","+TrimString(coords[i+1]);	
		}
		
		vShape += ' x e">';
		vShape += '<v:fill opacity="50%">';
		vShape += '</v:shape>'+"\n";
		
		canvas_ie += vShape;
		
		//$('#map_canvas_ie').html(canvas_ie);
		
		//alert(vShape);
		
		
		//alert($('#map_canvas_ie').html());
		areasInd++;
		
		message = "Dynamic map download("+areasInd+"/"+areasCount+")";

		window.status = message;
    });
	$('#map_canvas_ie').append(canvas_ie);
	//alert(canvas_ie);
	//$('#map_canvas_ie').html(canvas_ie);
}

function postRegionRequest(regionid, masterplanid)
{
	
	$.post("/masterplan/get_region_info/", { 'region_id' : regionid , 'masterplan_id' : masterplanid },

	function(data)
	{
		
		if (data.result=='ok')
		{
			//alert("X:"+mouseX+"; Y:"+mouseY);
			hintObject = 'div[hint]';
			
			$(hintObject).css("left", div_mouseX-2);
			$(hintObject).css("top", div_mouseY-2);
			
			$(hintObject).html(data.content);
			
			$(hintObject).show("slow");
			
			$('a:has(img).mg').lightbox();
			
		} 
	}

	,"json");	
	
}

function masterplan_init()
{
    $("#wrap2").mousemove(function(e){
								   
	   var divOffset = $("#wrap2").offset();
	   
	   mouseX = e.pageX-divOffset.left;
	   mouseY = e.pageY-divOffset.top;
    }); 
	
	if (getCanvasContex() != null) {
		reDrawBackground();
		
		$("area").mouseover(function(){
			region_id = $(this).attr('alt');
			
			_coords = $(this).attr('coords');
			
			coords = _coords.split(",");
			
			mes = "";
			ctx = getCanvasContex();
			
			ctx.beginPath();
			for (i = 0; i < coords.length; i=i+2){
				if (i == 0) {
					ctx.moveTo(TrimString(coords[i]),TrimString(coords[i+1]));
				} else {
					ctx.lineTo(TrimString(coords[i]),TrimString(coords[i+1]));
				}
	
			}
			ctx.closePath();
			
			ctx.strokeStyle = "#FFFFFF";
			ctx.stroke();
			
			ctx.fillStyle = "#FFFFFF";
			ctx.fill();
		});
		
		$("area").mouseleave(function(){
			reDrawBackground();
		});
		
		$("area").click(function() {
			
			div_mouseX = mouseX;
			div_mouseY = mouseY;
			
			region_id = $(this).attr('alt');
			
			masterplan_id = $("input[name=masterplan_id]").attr("value");
			
			postRegionRequest(region_id, masterplan_id);
			
		});
	} else {
		
		$('#map_image').remove();
		
		//showWaitMessage();
		$('html').attr('xmlns:v','urn:schemas-microsoft-com:vml');
		$('html').attr('xmlns:o','urn:schemas-microsoft-com:office:office');
		
		reDrawBackgroundIE();
		
		$("[name='shape']").mouseover(function(){
			$(this).attr('fillcolor','#FFFFFF');
		});

		$("[name='shape']").mouseleave(function(){
			//$(this).attr('fillcolor','green');
			regionid = $(this).attr('regionid');
			bg_color = $("area[alt="+regionid+"]").attr('bgcolor');
			$(this).attr('fillcolor',bg_color);
		});
		
		$("[name='shape']").click(function() {

			div_mouseX = mouseX;
			div_mouseY = mouseY;
			
			regionid = $(this).attr('regionid');
			
			masterplanid = $("input[name=masterplan_id]").attr("value");
			
			postRegionRequest(regionid, masterplanid);
			
		});
		
		//hideWaitMessage();
	}
	
	$('div[hint]').mouseleave(function(){
		$(this).hide('slow');
	});
	
	//alert("Script work done");
}

function HexToR(h) {return parseInt((cutHex(h)).substring(0,2),16)}
function HexToG(h) {return parseInt((cutHex(h)).substring(2,4),16)}
function HexToB(h) {return parseInt((cutHex(h)).substring(4,6),16)}
function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}

