
function TElementProperties( containerId ) {
    if ( typeof containerId == "string" )
	this.containerDIV = document.getElementById( containerId );
}

TElementProperties.prototype = new Object();
TElementProperties.prototype.marker = null;
TElementProperties.prototype.containerDIV = null;
TElementProperties.prototype.eventListener = null;
TElementProperties.prototype.eventHandler = null;
TElementProperties.prototype.url = null;

function TObjectTypeSettings( url, process, parameters, urlCreator ) {
    if ( typeof url == "string" )
		this.dataUrl = url;
    if ( typeof process == "function" )
		this.process = process;
    if ( typeof parameters == "object" )
		this.parameters = parameters;
    if ( typeof urlCreator == "function" )
		this.getUrl = urlCreator;
}

TObjectTypeSettings.prototype = new Object();
TObjectTypeSettings.prototype.dataUrl = null;
TObjectTypeSettings.prototype.process = function() {};
TObjectTypeSettings.prototype.getUrl = function() { return null; };
TObjectTypeSettings.prototype.parameters = new Object();

OT_NEWS = new TObjectTypeSettings( 
	    "/digitalcity/news/list.xml", 
	    processNews,  
	    { ordering: "-creationDate", icount: 7 },
	    function ( element ) { return "/digitalcity/news/redirect.jsp?hir=" + element.getAttribute("key"); }
    );
    
OT_EVENT = new TObjectTypeSettings(
	    "/digitalcity/events/list.xml", 
	    processEvent, 
	    { ordering: "-creationDate", icount: 8 },
	    function ( element ) { return "/digitalcity/entity/entityEvents.jsp?dom=AAAABIOO&prt=AAAAUPPJ&egd=" + element.getAttribute("entity-key") + "&ese=" + element.getAttribute("key"); }
    ); 
    
OT_DOCUMENT = new TObjectTypeSettings(
	    "/digitalcity/documents/list.xml", 
	    processDocument, 
	    { ordering: "-creationDate", icount: 8 },
	    function ( element ) { return "/digitalcity/documents/all.jsp?dom=AAAABIOO&prt=AAAAUPPJ&fmn=AAAAUPVS&men=AAAABFLD&dok=" + element.getAttribute("key"); }
	);
    
OT_OFFER = new TObjectTypeSettings(
	    "/digitalcity/business/list.xml", 
	    processOffer, 
	    { ordering: "-creationDate", icount: 8 },
	    function ( element ) { return "/digitalcity/entity/entityBusinessOffers.jsp?dom=AAAABIOO&prt=AAAAUPPJ&egd=" + element.getAttribute("entity-key") + "&uza=" + element.getAttribute("key"); }
     );
    
OT_ENTITY = new TObjectTypeSettings(
	    "/digitalcity/entities/list.xml", 
	    processEntity, 
	    { ordering: "-creationDate", icount: 10 },
	    function ( element ) { return "/digitalcity/entity.jsp?dom=AAAABIOO&prt=AAAAUPPJ&egd=" + element.getAttribute("key"); }
    
     );
     
function getObjectTypeSettings( resourceUrl ) {
	var objtypes = new Array( OT_NEWS, OT_EVENT, OT_DOCUMENT, OT_OFFER, OT_ENTITY );
	for ( i=0; i<objtypes.length; i++ )
		if ( objtypes[i].dataUrl == ('/digitalcity'+resourceUrl) )
			return objtypes[i];
	return null;
}

var ICON = new GIcon();
ICON.iconSize = new GSize(34,30);
ICON.iconAnchor = new GPoint(0,30);
ICON.infoWindowAnchor = new GPoint(24,0);

pictures = new Array();
pictures[0] = new GIcon( ICON, '/kepek/map/pushpin1.png' );
pictures[1] = new GIcon( ICON, '/kepek/map/pushpin2.png' );
pictures[2] = new GIcon( ICON, '/kepek/map/pushpin3.png' );
pictures[3] = new GIcon( ICON, '/kepek/map/pushpin4.png' );
pictures[4] = new GIcon( ICON, '/kepek/map/pushpin5.png' );
pictures[5] = new GIcon( ICON, '/kepek/map/pushpin6.png' );
pictures[6] = new GIcon( ICON, '/kepek/map/pushpin7.png' );
pictures[7] = new GIcon( ICON, '/kepek/map/pushpin8.png' );
pictures[8] = new GIcon( ICON, '/kepek/map/pushpin9.png' );
pictures[9] = new GIcon( ICON, '/kepek/map/pushpin10.png' );



window.map = null;
window.mapMarkers = new Array();
window.mapMarkersOption = new Array();





function processNews( newsItem, container ) {
    var title  = newsItem.getElementsByTagName("title").item(0).firstChild.data;
    var lead   = newsItem.getElementsByTagName("lead").item(0).firstChild.data;
    var source = newsItem.getElementsByTagName("source").item(0).firstChild.data;
    var creationDate = newsItem.getElementsByTagName("creationDate").item(0).firstChild.data;
    var thumbnailUrl = null;
    if ( newsItem.getElementsByTagName("thumbnailUrl").length > 0 )
		thumbnailUrl = newsItem.getElementsByTagName("thumbnailUrl").item(0).firstChild.data;

    var divTitle = document.createElement("div");
    divTitle.className = "mapNewsTitle";
    divTitle.appendChild( document.createTextNode( title ) );

    var divSource = document.createElement("div");
    divSource.className = "mapNewsSource";
    divSource.appendChild( document.createTextNode( source ) );

    var divCreationDate = document.createElement("div");
    divCreationDate.className = "mapNewsCreationDate";
    divCreationDate.appendChild( document.createTextNode( creationDate ) );
    
    container.appendChild( divTitle );
    container.appendChild( divCreationDate );
    container.appendChild( divSource );
    
    return function( element ) {
		iwDiv = document.createElement("div");
		iwDiv.className = "iw";
		
		iwTitle = document.createElement("div");
		iwTitle.className = "iwTitle";
		iwTitle.appendChild( document.createTextNode( title ) );
		
		iwLead = document.createElement("div");
		if ( thumbnailUrl != null ) {
		    iwNewsThumbnail = document.createElement("img");   
		    image = new Image();
		    image.src = thumbnailUrl;
		    image.className = "iwThumbnail";
		    iwLead.appendChild( image );
		}
		iwLead.appendChild( document.createTextNode( lead ) );
		
		iwDiv.appendChild( iwTitle );
		iwDiv.appendChild( iwLead );
	
		if ( element.url != null ) {
		    iwDetails = document.createElement("a");
		    iwDetails.className = "iwDetails";
		    iwDetails.href = element.url;
		    iwDetails.target = "mapreader"
		    iwDetails.appendChild( document.createTextNode( window.linkText ) );
		    iwDiv.appendChild( iwDetails );
		}
		
		element.marker.openInfoWindow( iwDiv );
    }
}

function processEvent( eventItem, container ) {
    var title = eventItem.getElementsByTagName("title").item(0).firstChild.data;
    var organizer = eventItem.getElementsByTagName("organizer").item(0).firstChild.data;
    var address = eventItem.getElementsByTagName("address").item(0).firstChild.data;
    var startDate = eventItem.getElementsByTagName("startDate").item(0).firstChild.data;
    var finishDate = null;
    if ( eventItem.getElementsByTagName("finishDate").length > 0 )
		finishDate = eventItem.getElementsByTagName("finishDate").item(0).firstChild.data;

    var divTitle = document.createElement("div");
    divTitle.className = "mapEventTitle";
    divTitle.appendChild( document.createTextNode( title ) );

    var divInterval1 = document.createElement("div");
    divInterval1.className = "mapEventInterval";
    divInterval1.appendChild( document.createTextNode( startDate ) );

    var divInterval2 = null;
    if ( finishDate != null ) {
		divInterval2 = document.createElement("div");
		divInterval2.className = "mapEventInterval";
		divInterval2.appendChild( document.createTextNode( finishDate ) );
    }
    
    container.appendChild( divTitle );
    container.appendChild( divInterval1 );
    if ( divInterval2 != null )
		container.appendChild( divInterval2 );
	
    return function( element ) {
		var iwDiv = document.createElement("div");
		iwDiv.className = "iw";
		
		var iwTitle = document.createElement("div");
		iwTitle.className = "iwTitle";
		iwTitle.appendChild( document.createTextNode( title ) );
		
		var iwOrganizer = document.createElement("div");
		iwOrganizer.appendChild( document.createTextNode( organizer ) );
	
		var iwAddress = document.createElement("div");
		iwAddress.appendChild( document.createTextNode( address ) );
	
		var iwStartDate = document.createElement("div");
		iwStartDate.appendChild( document.createTextNode( startDate ) );
	       
		var iwFinishDate = null;
		if ( finishDate != null ) {
		    iwFinishDate = document.createElement("div");
		    iwFinishDate.appendChild( document.createTextNode( finishDate ) );
		}
	
		iwDiv.appendChild( iwTitle );
		iwDiv.appendChild( iwOrganizer );
		iwDiv.appendChild( iwAddress );
		iwDiv.appendChild( iwStartDate );
		if ( iwFinishDate != null )
		    iwDiv.appendChild( iwFinishDate );   
		    
		if ( element.url != null ) {
		    iwDetails = document.createElement("a");
		    iwDetails.className = "iwDetails";
		    iwDetails.href = element.url;
		    iwDetails.target = "mapreader"
		    iwDetails.appendChild( document.createTextNode( window.linkText ) );
		    iwDiv.appendChild( iwDetails );
		}
	
		element.marker.openInfoWindow( iwDiv );
    }
	
}

function processDocument( documentItem, container ) {
    var title = documentItem.getElementsByTagName("title").item(0).firstChild.data;
	var author = documentItem.getElementsByTagName("author").item(0).firstChild.data;
    var issue = documentItem.getElementsByTagName("issue").item(0).firstChild.data;

    var divTitle = document.createElement("div");
    divTitle.className = "mapDocumentTitle";
    divTitle.appendChild( document.createTextNode( title ) );

    var divAuthor = document.createElement("div");
    divAuthor.className = "mapDocumentAuthor";
    divAuthor.appendChild( document.createTextNode( author ) );

    var divIssue = document.createElement("div");
    divIssue.className = "mapDocumentIssue";
    divIssue.appendChild( document.createTextNode( issue ) );

    container.appendChild( divTitle );
    container.appendChild( divAuthor );
    container.appendChild( divIssue );

    return function( element ) {
		iwDiv = document.createElement("div");
		iwDiv.className = "iw";
		
		iwTitle = document.createElement("div");
		iwTitle.className = "iwTitle";
		iwTitle.appendChild( document.createTextNode( title ) );

		iwAuthor = document.createElement("div");
		iwAuthor.appendChild( document.createTextNode( author ) );

		iwIssue = document.createElement("div");
		iwIssue.appendChild( document.createTextNode( issue ) );
		
		iwDiv.appendChild( iwTitle );
		iwDiv.appendChild( iwAuthor );
		iwDiv.appendChild( iwIssue );
	
		if ( element.url != null ) {
		    iwDetails = document.createElement("a");
		    iwDetails.className = "iwDetails";
		    iwDetails.href = element.url;
		    iwDetails.target = ""
		    iwDetails.appendChild( document.createTextNode( window.linkText ) );
		    iwDiv.appendChild( iwDetails );
		}
	
		element.marker.openInfoWindow( iwDiv );
    }
}

function processOffer( offerItem, container ) {
    var title = offerItem.getElementsByTagName("shortDescription").item(0).firstChild.data;

    var divShortDescription = document.createElement("div");
    divShortDescription.className = "mapOfferShortDescription";
    divShortDescription.appendChild( document.createTextNode( title ) );

    container.appendChild( divShortDescription );
    return function( element ) {
		iwDiv = document.createElement("div");
		iwDiv.className = "iw";
		
		iwTitle = document.createElement("div");
		iwTitle.className = "iwTitle";
		iwTitle.appendChild( document.createTextNode( title ) );
		
		iwDiv.appendChild( iwTitle );
	
		if ( element.url != null ) {
		    iwDetails = document.createElement("a");
		    iwDetails.className = "iwDetails";
		    iwDetails.href = element.url;
		    iwDetails.target = "mapreader"
		    iwDetails.appendChild( document.createTextNode( window.linkText ) );
		    iwDiv.appendChild( iwDetails );
		}
	
		element.marker.openInfoWindow( iwDiv );
    }
}

function processEntity( entityItem, container ) {
    var title = entityItem.getElementsByTagName("title").item(0).firstChild.data;
    var creationDate = entityItem.getElementsByTagName("creationDate").item(0).firstChild.data;
    var thumbnailUrl = null;
    if ( entityItem.getElementsByTagName("thumbnailUrl").length > 0 )
		thumbnailUrl = entityItem.getElementsByTagName("thumbnailUrl").item(0).firstChild.data;

    var divTitle = document.createElement("div");
    divTitle.className = "mapEntityTitle";
    divTitle.appendChild( document.createTextNode( title ) );

    var divCreationDate = document.createElement("div");
    divCreationDate.className = "mapEntityCreationDate";
    divCreationDate.appendChild( document.createTextNode( creationDate ) );

    container.appendChild( divTitle );
    container.appendChild( divCreationDate );
    return function( element ) {
		iwDiv = document.createElement("div");
		iwDiv.className = "iw";
		
		iwTitle = document.createElement("div");
		iwTitle.className = "iwTitle";
		iwTitle.appendChild( document.createTextNode( title ) );
		
		iwDiv.appendChild( iwTitle );

		if ( thumbnailUrl != null ) {
			iwImage = document.createElement("div");
		    image = new Image();
		    image.src = thumbnailUrl;
		    image.className = "iwThumbnail";
		    iwImage.appendChild( image );
			iwDiv.appendChild( image );
		}

	
		if ( element.url != null ) {
		    iwDetails = document.createElement("a");
		    iwDetails.className = "iwDetails";
		    iwDetails.href = element.url;
		    iwDetails.target = "mapreader"
		    iwDetails.appendChild( document.createTextNode( window.linkText ) );
		    iwDiv.appendChild( iwDetails );
		}
	
		element.marker.openInfoWindow( iwDiv );
    }
}


function getInfoWindowCreator( element, funct, manager ) {
    return function() {
    	manager.__opening = true;
		funct( element );
    	manager.__opening = false;
    }
}

function processXML( bounds ) {
    window.mapManagement.objectType = tabs[actualTab];
    window.mapManagement.processXML( bounds );
}

// Map management

function createEventHandler( element ) {
	return function () {
		element.eventHandler( element );
	}
}

function TMapManagement( mapId, conatinerIds, emptyId, objectType, onchange, latlng, zoom ) {
	this.__objectType = objectType;    
    this.elements = new Array();
    this.onchange = onchange;
    for ( i = 0; i < containerIds.length && i < 10; i++ ) {
		var div = document.getElementById(containerIds[i]);
		var ep = new TElementProperties();
		ep.containerDIV = div;
		div.onclick = createEventHandler( ep );
		this.elements[i] = ep;
    }
    this.emptyDiv = document.getElementById(emptyId);
    this.managedMap = new GMap2(document.getElementById(mapId));
    
    if ( (latlng instanceof GLatLng) && ( typeof zoom == "number") )
	    this.managedMap.setCenter( latlng, zoom );
	else
	    this.managedMap.setCenter( (new GLatLngBounds( 
	    	new GLatLng(35.17380831799959, -15.99609375), 
	    	new GLatLng(62.915233039476135, 45.52734375) )).getCenter(), 4);
    var manager = this;
    GEvent.addListener( manager.managedMap, "movestart", function() {
    	manager.__moveing = true;
    } );
    GEvent.addListener( manager.managedMap, "moveend", function() {
    	manager.__moveing = false;
    	manager.page = 1;
		manager.processXML( manager.managedMap.getBounds() );
    } );
    this.processXML( manager.managedMap.getBounds(), true );
}


TMapManagement.prototype.elements = new Array();
TMapManagement.prototype.emptyDiv = null;
TMapManagement.prototype.managedMap = null;
TMapManagement.prototype.onchange = null;
TMapManagement.prototype.__moveing = false;
TMapManagement.prototype.__opening = false;
TMapManagement.prototype.__objectType = null;
TMapManagement.prototype.page = 1;
TMapManagement.prototype.__nextPage = null;
TMapManagement.prototype.__previousPage = null;

TMapManagement.prototype.setObjectType = function( objectType ) {
	if ( ! ( objectType instanceof TObjectTypeSettings ) )
		return;
	this.__objectType = objectType;
	this.page = 1;
	this.processXML( this.managedMap.getBounds(), true );
}

TMapManagement.prototype.loadNextPage = function() {
	if ( this.__nextPage == null )
		return;
	this.page = this.__nextPage;
	this.processXML( this.managedMap.getBounds(), true );
}
TMapManagement.prototype.loadPreviousPage = function() {
	if ( this.__previousPage == null )
		return;
	this.page = this.__previousPage;
	this.processXML( this.managedMap.getBounds(), true );
}

TMapManagement.prototype.processXML = function ( bounds, closeInfoWindow ) {
    if ( ( ! this.managedMap.getInfoWindow().isHidden() ) && ( closeInfoWindow != true ) )
    	return;
   	else if ( ! this.managedMap.getInfoWindow().isHidden() )
   		this.managedMap.closeInfoWindow();
    if ( ! (this.__objectType instanceof TObjectTypeSettings) ) {
		alert("Internal error! Object type not valid!");
		return;
	}

    var parameters = 
		'lat1=' + bounds.getNorthEast().lat() +
		'&lat2=' + bounds.getSouthWest().lat() +
		'&lon1=' + bounds.getNorthEast().lng() +
		'&lon2=' + bounds.getSouthWest().lng() +
		'&page=' + this.page 
    for ( i in this.__objectType.parameters )
		parameters += "&" + i + "=" + this.__objectType.parameters[i];
    
    var xmlhttp = (!document.all) ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    var response = null;
	
    //prompt( "", this.__objectType.dataUrl + "?" + parameters );
	try {
	    xmlhttp.open('POST', this.__objectType.dataUrl, false);
    	xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded; charset=utf-8');
	    xmlhttp.send( parameters );
    } catch ( e ) {
    	return;
    }
    response = xmlhttp.responseXML;
   
	var nextPage = null, previousPage = null;
	if ( ( nextPage = response.getElementsByTagName("next") ).length > 0 )
		this.__nextPage = nextPage.item(0).getAttribute("page");
	else
		this.__nextPage = null;
	if ( ( previousPage = response.getElementsByTagName("previous") ).length > 0 )
		this.__previousPage = previousPage.item(0).getAttribute("page");
	else
		this.__previousPage = null;
   
    var results  = response.getElementsByTagName("record");
    var manager = this;
    for ( i = 0; i < this.elements.length; i++ ) {
		var element = this.elements[i];
		if ( i < results.length ) {
		    var actualItem = results.item(i);
		    var title = actualItem.getElementsByTagName("title").item(0).firstChild.data;
		    var loc = actualItem.getElementsByTagName("location").item(0);
		    var gLatLng = new GLatLng( loc.getAttribute("lat"), loc.getAttribute("long") );
		    var infoWindowCreator = null;
		    
		    element.url = this.__objectType.getUrl( actualItem );
		    try {
				itemDiv = element.containerDIV;
				itemDiv.innerHTML = "";
				infoWindowCreator = this.__objectType.process( actualItem, itemDiv );
				itemDiv.style.display = "block";
		    } catch( except ) {
				alert( except );
		    }
		    
		    if ( element.eventListener != null )
				GEvent.removeListener( element.eventListener )
		    if ( element.marker != null )
				this.managedMap.removeOverlay( element.marker );
			
		    element.marker = new GMarker( gLatLng, { icon: pictures[i], title: title } );
		    if ( infoWindowCreator != null && typeof infoWindowCreator == "function" )
				element.eventHandler = getInfoWindowCreator( element, infoWindowCreator, manager );
		    else
				element.eventHandler = function() { alert("NotImplemented"); }
		    element.eventListener = GEvent.addListener( element.marker, "click", element.eventHandler );
		    this.managedMap.addOverlay( element.marker );
		} else {
		    if ( element.eventListener != null )
				GEvent.removeListener( element.eventListener )
		    if ( element.marker != null )
				this.managedMap.removeOverlay( element.marker );
		    element.marker = null;
		    element.eventListener = null;
		    element.containerDIV.style.display = "none";
		}
    }
    this.emptyDiv.style.display = (results.length == 0) ? "block" : "none";
    
    if ( typeof this.onchange == "function" ) {
    	this.onchange( this.__previousPage != null, this.__nextPage != null );
    }
}




