/*
 * Fluster2 0.1.1
 * Copyright (C) 2009 Fusonic GmbH
 *
 * This file is part of Fluster2.
 *
 * Fluster2 is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * Fluster2 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * A cluster marker which shows a background image and the marker count 
 * of the assigned cluster.
 *
 * @constructor
 * @private
 * @param {Fluster2} the Fluster2 itself
 * @param {Fluster2Cluster} the Fluster2Cluster assigned to this marker
 */
function Fluster2ClusterMarker(_fluster, _cluster)
{
	this.fluster = _fluster;
	this.cluster = _cluster;
	this.position = this.cluster.getPosition();
	this.markerCount = this.cluster.getMarkerCount();
	this.map = this.fluster.getMap();
	this.style = null;
	this.div = null;
	
	this.bookIndex = new Array();
	
	var book0Index;
	var book1Index;
	var book2Index;
	var book3Index;
	
	var spacerCharacter ='';
	
	// Assign style
	var styles = this.fluster.getStyles();
	for(var i in styles)
	{
		if(this.markerCount > i)
		{
			this.style = styles[i];
		}
		else
		{
			break;
		}
	}
	
	// Basics
	google.maps.OverlayView.call(this);
	this.setMap(this.map);
	
	// Draw
	this.draw(_cluster);
};

Fluster2ClusterMarker.prototype = new google.maps.OverlayView();

Fluster2ClusterMarker.prototype.draw = function(_cluster)
{
	if(this.div == null)
	{
		var me = this;
		
		// Create div
		this.div = document.createElement('div');
		
		// Set styles
		this.div.style.position = 'absolute';
		this.div.style.width = this.style.width + 'px';
		this.div.style.height = this.style.height + 'px';
		this.div.style.lineHeight = '100%';
		this.div.style.background = 'transparent url("' + this.style.image + '") 50% 50% no-repeat';
		this.div.style.color = this.style.textColor;
		this.div.style.zIndex = 10;	// PNJ
		this.div.style.textAlign = 'left';
		this.div.style.verticalAlign = '50%';
		this.div.style.fontFamily = 'Verdana, Arial, Helvetica';
		this.div.style.fontSize = '20px';
		spacerCharacter = '&nbsp;';	// added 6/1/11 - spacerCharacter adjusts position of count numeral on cluster marker
		if (this.markerCount > 9)
		{
			this.div.style.fontSize = '18px';
			spacerCharacter = '&nbsp;';
		}
		if (this.markerCount > 99)
		{
			this.div.style.fontSize = '14px';
			spacerCharacter = '<SPAN style="display: block;margin-top: -1.5em;">&nbsp;</SPAN>&nbsp;&nbsp;&nbsp;&nbsp;';
		}
		if (this.markerCount > 999)
		{
			this.div.style.fontSize = '12px';
			spacerCharacter = '';
		}
		this.div.style.fontWeight = 'bold';
		this.div.innerHTML = this.markerCount;
		
		// need to compare mapbookids order of ids with existing - run through array and and if (element), if bookIds[i] == mapbookidValueArray[0], set book0Index to i
		
		var mapbookidValueArray = document.getElementById('mapbookid').value.split(',');

		for (var i =0;i<4;i++)
		{
		if (mapbookidValueArray[i])
			for (var j=0;j<mapbookidValueArray.length;j++)
			{
					if (mapbookidValueArray[i]==this.fluster.bookIds[j])
						this.bookIndex[i] = mapbookidValueArray[j];
					
			}
		}
		
		// also get date in italics
		var strBookName0;
		strBookName0 =	(this.fluster.bookIds[0]>0) ? document.getElementById('invisiblebook'+this.fluster.bookIds[0]).text.split('),(')[0] + ')' +' <I>(' + document.getElementById('invisiblebook'+this.fluster.bookIds[0]).text.split('),(')[1].substring(0,4) + ')</I>' : '';
		var strBookMarkerNum0;		
		strBookMarkerNum0 = (this.fluster.bookIds[0] > 0) ? this.cluster.bookNums[0] : '';
		var strBookName1;
		strBookName1 =	(this.fluster.bookIds[1]>0) ? document.getElementById('invisiblebook'+this.fluster.bookIds[1]).text.split('),(')[0] + ')' +' <I>(' + document.getElementById('invisiblebook'+this.fluster.bookIds[1]).text.split('),(')[1].substring(0,4) + ')</I>'  : '';
		var strBookMarkerNum1;		
		strBookMarkerNum1 = (this.fluster.bookIds[1] > 0) ? this.cluster.bookNums[1] : '';		
		var strBookName2;
		strBookName2 =	(this.fluster.bookIds[2]>0) ? document.getElementById('invisiblebook'+this.fluster.bookIds[2]).text.split('),(')[0] + ')' +' <I>(' + document.getElementById('invisiblebook'+this.fluster.bookIds[2]).text.split('),(')[1].substring(0,4) + ')</I>'  : '';
		var strBookMarkerNum2;		
		strBookMarkerNum2 = (this.fluster.bookIds[2] > 0) ? this.cluster.bookNums[2] : '';
		var strBookName3;
		strBookName3 =	(this.fluster.bookIds[3]>0) ? document.getElementById('invisiblebook'+this.fluster.bookIds[3]).text.split('),(')[0] + ')' +' <I>(' + document.getElementById('invisiblebook'+this.fluster.bookIds[3]).text.split('),(')[1].substring(0,4) + ')</I>'  : '';
		var strBookMarkerNum3;		
		strBookMarkerNum3 = (this.fluster.bookIds[3] > 0) ? this.cluster.bookNums[3] : '';
		
		// SCOPE!!! to get correct cluster numbers, this should be done in a separate function, with the values passed as args

		var bookName = new Array();
		var bookNum = new Array();
		
		bookName[0] = strBookName0;
		bookNum[0] = strBookMarkerNum0;
		bookName[1] = strBookName1;
		bookNum[1] = strBookMarkerNum1;
		bookName[2] = strBookName2;
		bookNum[2] = strBookMarkerNum2;
		bookName[3] = strBookName3;
		bookNum[3] = strBookMarkerNum3;
		
		
		

		
		
		this.div.innerHTML = '<P>' + ((navigator.appName == 'Microsoft Internet Explorer') ? '<BR/>' : '');		// added by PNJ for users' benefit
		this.div.innerHTML += '<a href="#viewatlas" class="tt">' + '&nbsp;&nbsp;&nbsp;' +  spacerCharacter + this.markerCount + '<span class="tooltip"><span class="top"></span><span class="middle"><TABLE class="content"><TR><TD>Locations :</TD><TD></TD></TR><TR><TD>'+ strBookName0 + '</TD><TD>'+strBookMarkerNum0+'</TD></TR><TR><TD>' + strBookName1 + '</TD><TD>'+strBookMarkerNum1+ '</TD></TR><TR><TD>' + strBookName2 + '</TD><TD>'+strBookMarkerNum2+ '</TD></TR><TR><TD>' + strBookName3+'</TD><TD>'+strBookMarkerNum3 + '</TD></TR></TABLE></span><span class="bottom"></span></span></a><P>';
		
		
		// Cursor and onlick
		this.div.style.cursor = 'pointer';
		google.maps.event.addDomListener(this.div, 'click', function() {
			me.map.fitBounds(me.cluster.getMarkerBounds());
		});	// might alter this to show enclosed markers' page numbers and by colour (infowindow?)
		
		this.getPanes().overlayLayer.appendChild(this.div);
	}
	
	// Position
	var position = this.getProjection().fromLatLngToDivPixel(this.position);
	this.div.style.left = (position.x - parseInt(this.style.width / 2)) + 'px';
	this.div.style.top = (position.y - parseInt(this.style.height / 2)) + 'px';
};

Fluster2ClusterMarker.prototype.hide = function()
{
	// Hide div
	this.div.style.display = 'none';
};

Fluster2ClusterMarker.prototype.show = function()
{
	// Show div
	this.div.style.display = 'block';
};
