


/*
point[0] = region name
point[1] = region X
point[2] = region Y
point[3] = local X
point[4] = local Y
point[5] = local Z
point[6] = parcel name
*/
function loadMapWithMarkers(points, mapOptions, options) {
	if (typeof(SLMap)=='undefined') return;
    	
    if (!mapOptions) mapOptions =  {disableVoiceInfo: true, hasOverviewMapControl:false};
    if (!options) options =  {};

    var map = new SLMap(document.getElementById('map-container'), mapOptions);	

	var validPoints = $.grep(points, function(n, i){
      return (n);
    });
        
    if (!validPoints || !validPoints.length) {
    	map.centerAndZoomAtSLCoord(new XYPoint(997,1002),4);
    	return;	
	}
	
	function globalCoords(regionX,regionY,localX,localY) {
    	return [parseFloat(1*regionX + 1*localX/256), parseFloat(1*regionY + 1*localY/256)];
	}
	
	function htmlentities(str) {
        str = str.replace('>','&gt;');
    	return str.replace('<','&lt;')	
	}
	
	var n = validPoints.length;
	var zoom = (n > 1)?6:1;
    var coords = globalCoords(validPoints[0][1],validPoints[0][2],validPoints[0][3],validPoints[0][4]);
	map.centerAndZoomAtSLCoord(new XYPoint(coords[0],coords[1]),zoom);

	var marker = options.marker || '/css/iconmap.gif';
	var markerWidth = options.markerWidth || 30;
	var markerHeight = options.markerHeight || 30;
    var icon = new Icon(new Img(marker,markerWidth,markerHeight));
    var all_images = [icon, icon, icon, icon, icon, icon];
	    
    // creates the icons
    for (var i=0; i<n; i++) {      
      // creates the marker
      coords =  globalCoords(validPoints[i][1],validPoints[i][2],validPoints[i][3],validPoints[i][4]);
      var marker = new Marker(all_images, new XYPoint(coords[0],coords[1]), {centerOnClick: true, verticalAlign: "bottom"});
      
      // creates a window
      var txt = 'Teleport to <a href="secondlife://' + validPoints[i][0] + '/' + Math.round(validPoints[i][3]) + '/' + Math.round(validPoints[i][4]) + '/' + Math.round(validPoints[i][5])+ '">' + htmlentities(validPoints[i][6]) + '</a><br/>';
      var mapWindow = new MapWindow(txt, {width:"200", height:"200"});      
      // adds the marker to the map
      map.addMarker(marker, mapWindow);            
    }
    
    //guide: selector to a list of dom elements which on mouseover will recenter the map
    //(list of elements must be the same length as points) 
    if (options.guide) {
        //add event handlers for recentering the map
        $(options.guide).each( function(i) {
            if (points[i]) {
                $(this).mouseover(function() {           
                    var coords = globalCoords(points[i][1],points[i][2],points[i][3],points[i][4]);
                    map.panOrRecenterToSLCoord(new XYPoint(coords[0],coords[1]), true);   
                });
            }
        });
    }
}

function checkNewMessages(url) {    
    function hasNewMessages() {
        $.getJSON(url,
        function(data){
          if (parseInt(data,10) > 0) {
            $('.new_messages').html(' ('+data+')');   
          } else {
            $('.new_messages').html('');   
          }
        });    
    }
    hasNewMessages();
    setInterval(hasNewMessages,30000);
}

function processComments(selector, options) {
    var comments = $(selector);
    options = options || {};
    var w = options.width || 420;
    var h = options.height || 344;
    if (options.size) {
        w = (options.size=='large')?420:225;    
        h = (options.size=='large')?344:185;
    }
    var a = comments.get()
    var re = /\[youtube=http:\/\/(www\.)?youtube\.com\/([^\]]+)\]/gi;
    $.each(comments, function(i, comment) {					       
        comment.innerHTML = comment.innerHTML.replace(re, '<a href="http://www.youtube.com/$2">YouTube</a>');
	});

	comments.find('a[@href^="http://www.youtube.com"]').flash(
        { height: h, width: w },
        { version: 8 },
        function(htmlOptions) {
            $this = $(this);
            htmlOptions.src = $this.attr('href');
            $this.replaceWith($.fn.flash.transform(htmlOptions));						
        }
    );
}


jQuery.fn.sortCloud = function(mode) {
	var container = this;
	var sortMode = (mode=='size')?'size':'alpha';
	var tags = container.find('li').get();
	if (sortMode=='alpha') {
		$.each(tags, function(i, tag) {			
    		tag.sortKey = tag.firstChild.nextSibling.innerHTML.toUpperCase();			
		});
	} else {
		$.each(tags, function(i, tag) {    		
			tag.sortKey = -1*parseInt(tag.firstChild.innerHTML,10);
		});
	}
	
	tags.sort(function (a, b) {
		if (a.sortKey < b.sortKey) return -1;
		if (a.sortKey > b.sortKey) return 1;
		return 0;
	});
	
	$.each(tags, function(i, tag) {
		container.append(tag).append("\n");
		tag.sortKey = null;		
	});
	

}




