/*
 * Interаctive Mazaika Viewer JS functions - (c) Boris A. Glazer 2014
 */


function fitMosaic() {
	map.fitWorld({reset: true}).zoomIn();
}

function submitRowCol() {
	
	var UICol=document.getElementById('inputCol').value;
	var UIRow=document.getElementById('inputRow').value;
	
	var pt1 = map.unproject([cellWidth*UICol-cellWidth, cellHeight*UIRow-cellHeight], maxMapZoom);
	var pt2 = map.unproject([cellWidth*UICol, cellHeight*UIRow-cellHeight], maxMapZoom);
	var pt3 = map.unproject([cellWidth*UICol, cellHeight*UIRow], maxMapZoom);
	var pt4 = map.unproject([cellWidth*UICol-cellWidth, cellHeight*UIRow], maxMapZoom);
	if( polygon ) {
		map.removeLayer(polygon);
	}
	
	
	polygon = L.polygon([pt1,pt2,pt3,pt4]).addTo(map);
	polygon.setStyle({fillColor: 'transparent'});
	
	if (document.getElementById('keepZoom').checked) {
		var coordinate = map.unproject([cellWidth*UICol-cellWidth/2, cellHeight*UIRow-cellHeight/2], maxMapZoom);
		map.panTo(coordinate);
	} else {
		map.fitBounds([pt1,pt2,pt3,pt4]) ;
	}
	
	info.update([UICol, UIRow]) ;
	
	var invertedRow = numRows - UIRow + 1 ;
    var srchType = "1" ;
	
    ajax({
		 url:phpUrl,
		 statbox:"status",
		 method:"POST",
		 data:
		 {
		 type:srchType,
		 col:UICol,
		 row:invertedRow
		 },
		 success:function(data){info.updateMeta(data);}
		 }) ;
	
}

function searchByTitle() {
	
	var titleText = document.getElementById('inputTitle').value;
	
    var srchType = "2" ;
	
    ajax({
		 url:phpUrl,
		 statbox:"status",
		 method:"POST",
		 data: 
		 {
		 type:srchType,
		 text:titleText,
		 },
		 success:function(data){showSerachResult(data);}
		 }) ;
	
}

function showSerachResult(txt){
	document.getElementById('searchResult').innerHTML = (txt ? txt : 'Nothing was found. Please query another text.') ;
}

function zoomToSelectedCell() {
	if( polygon ) {
		map.fitBounds(polygon.getBounds()) ;
	}
}

var info = L.control();

info.onAdd = function (map) {
	this._div = L.DomUtil.create('div', 'info');
	this.update();
	return this._div;
};

info.update = function (e) {
	this._div.innerHTML = '<h4>' +  (e ? 'Cell: ' + e.toString() : 'Select cell') + '</h4>' ;
};

info.updateMeta = function (e) {
	this._div.innerHTML = this._div.innerHTML + '<h4>Title: ' +  (e ? e.toString() : 'N/A') + '</h4>' ;
};

info.addTo(map);

function positionToRowCol( clc_col , clc_row ) {
	
	var clc_row = numRows - clc_row + 1 ;
	
	var pt1 = map.unproject([cellWidth*clc_col-cellWidth, cellHeight*clc_row-cellHeight], maxMapZoom);
	var pt2 = map.unproject([cellWidth*clc_col, cellHeight*clc_row-cellHeight], maxMapZoom);
	var pt3 = map.unproject([cellWidth*clc_col, cellHeight*clc_row], maxMapZoom);
	var pt4 = map.unproject([cellWidth*clc_col-cellWidth, cellHeight*clc_row], maxMapZoom);
	
	if( polygon ) {
		map.removeLayer(polygon);
	}
	polygon = L.polygon([pt1,pt2,pt3,pt4]).addTo(map);
	polygon.setStyle({fillColor: 'transparent'});
	
	if (document.getElementById('keepZoom').checked) {
		var coordinate = map.unproject([cellWidth*clc_col-cellWidth/2, cellHeight*clc_row-cellHeight/2], maxMapZoom);
		map.panTo(coordinate);
	} else {
		map.fitBounds([pt1,pt2,pt3,pt4]) ;
	}
	
	info.update([clc_col, clc_row]) ;
	
}

function onMapMouseClick(e) {
	
	var coord2 = map.project( e.latlng, maxMapZoom );
	var thisCol = Math.ceil( coord2.x / cellWidth ) ;
	var thisRow = Math.ceil( coord2.y / cellHeight ) ;
	
	if ((thisCol>numCols)||(thisCol<1)||(thisRow>numRows)||(thisRow<1)) {
		if( polygon ) { map.removeLayer(polygon); }
		info.update(false) ;
		return;	
	}
	
	var invertedRow = numRows - thisRow + 1 ;
    var srchType = '1' ;
	
    ajax({
		 url:phpUrl,
		 statbox:"status",
		 method:"POST",
		 data:
		 {
		 type:srchType,
		 col:thisCol,
		 row:invertedRow
		 },
		 success:function(data){info.updateMeta(data);}
		 }) ;
	
	var coordlng = thisCol * cellWidth ;
	var coordlat = thisRow * cellHeight ;
	document.getElementById('inputCol').value = thisCol;
	document.getElementById('inputRow').value = thisRow;
	var coord = e.latlng ;
	var pt1 = map.unproject([coordlng-cellWidth , coordlat-cellHeight], maxMapZoom);
	var pt2 = map.unproject([coordlng			, coordlat-cellHeight], maxMapZoom);
	var pt3 = map.unproject([coordlng			, coordlat], maxMapZoom);
	var pt4 = map.unproject([coordlng-cellWidth , coordlat], maxMapZoom);
	
	if( polygon ) {
		map.removeLayer(polygon);
	}
	
	polygon = L.polygon([pt1,pt2,pt3,pt4]).addTo(map);
	polygon.setStyle({fillColor: 'transparent'});
	info.update([thisCol, thisRow]) ;
	
}

map.on('click', onMapMouseClick);

function XmlHttp()
{
	var xmlhttp;
	try{xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");}
	catch(e)
	{
		try {xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");} 
		catch (E) {xmlhttp = false;}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined')
	{
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function ajax(param)
{
	if (window.XMLHttpRequest) req = new XmlHttp();
	method=(!param.method ? "POST" : param.method.toUpperCase());
	
	if(method=="GET")
	{
		send=null;
		param.url=param.url+"&ajax=true";
	}
	else
	{
		send="";
		for (var i in param.data) send+= i+"="+param.data[i]+"&";
		send=send+"ajax=true";
	}
	
	req.open(method, param.url, true);
	//        if(param.statbox)document.getElementById(param.statbox).innerHTML = '<img src="loader.gif">';
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	req.send(send);
	req.onreadystatechange = function()
	{
		if (req.readyState == 4 && req.status == 200) //если ответ положительный
		{
			if(param.success)param.success(req.responseText);
		}
	}
}
