var Map = Class.create({
  initialize: function(name, array, title) {
  	if (name) this.name = name;
  	if (array) this.src = array;
  	if (title) this.title = title;
  },
  
  index: 0,
  
  select: function(id){
	  	var target = $(id);
	  	this.deselect();
	  	//new Effect.Appear(target, { duration: 0.3 });
	  	target.setStyle({ 'display': 'block' });
	  	
	  	target = $(this.name+"-content").update("");
	  	
	  	target.setStyle( { 'backgroundImage': 'url(images/global/assets/loading.gif)' } );
	  	
	  	this.index = this.getID(id);
  		this.apiCall(id);
  },
  
  apiCall: function(action, params){
    var options = { method : 'post', parameters : 'r=feed/map&section='+action, onSuccess: this.onComplete.bindAsEventListener(this) };
    new Ajax.Request(Loader.AJAX_PATH + 'index.php', options);
  },
  
  onComplete: function(transport){
  	var json = transport.responseJSON;
  	
  	this.generateHTML( this.index, json );
  	
  	sIFR.replace(dbozone, {
	  selector: '#fold-1 h3',
	  wmode: 'transparent',
	  css: [
	    '.sIFR-root { text-align: left;  font-size: 22px; leading:0; letter-spacing: -0.2; color: #336600; padding-right: 10px; line-height:-4px;  }'
	    ,'a { text-decoration: none; }'
	    ,'a:link { color: #336600; }'
	    ,'a:hover { color: #003300; text-decoration: none; }'
	  ]
	});
	
	sIFR.replace(dbozone, {
      selector: '#fold-1 h4',
      wmode: 'transparent',
      css: [
        '.sIFR-root { text-align: left;  font-size: 19px; leading:0; letter-spacing: -0.2; color: #336600; padding-right: 10px; line-height:-4px;  }'
        ,'a { text-decoration: none; }'
        ,'a:link { color: #336600; }'
        ,'a:hover { color: #003300; text-decoration: none; }'
      ]
    });
	
	sIFR.replace(dbozone, {
      selector: '#fold-1 h2',
      wmode: 'transparent',
      css: [
        '.sIFR-root { text-align: left;  font-size: 28px; leading:0; letter-spacing: 0.05; color: #336600; padding-right: 10px; line-height:-4px;  }'
        ,'a { text-decoration: none; }'
        ,'a:link { color: #336600; }'
        ,'a:hover { color: #003300; text-decoration: none; }'
      ]
    });
 
  },
  
  deselect: function(){
  	var target;
  	for (i = 0; i < this.src.length; i++){
  		target = $( this.src[i] );
  		if (target.getStyle('display') != "none") {
  			//new Effect.Fade(target, { duration: 0.5 })
  			target.setStyle({ 'display': 'none' });
  		};
  	}
  },
  
  getID: function(id){
  	for (i = 0; i < this.src.length; i++){
  		if (id == this.src[i]) return i;
  	}
  },
  
  generateHTML: function(index, json){
  	this.data = json.items;
  	this.length = json.length;
  	
  	var target = $(this.name+"-content");
  	target.setStyle( { 'backgroundImage': 'none' } );
  	var header = new Element('h2').update(this.title[index]);
  	target.appendChild(header);
  	
  	var box;
  	
  	for (i=0; i < 6; i++){
  		if (i == 0){
  			box = new Element('div').addClassName('box');
  			header = new Element('h3').update('<a href="' + this.data[i].link + '">' + this.data[i].title + '</a>');
  			
  			var image = new Element('img', { 'class': 'post-thumbnail', width: 120, height: 68 });
  			image.src = this.data[i].image;
  			
  			var description = new Element('p').update( this.data[i].description );
  			
  			box.appendChild(header);
  			box.appendChild(image);
  			box.appendChild(description);
  		} else if ( i > 0 && i < 3 ){
  			box = new Element('div').addClassName('box');
  			header = new Element('h3').update('<a href="' + this.data[i].link + '">' + this.data[i].title + '</a>');
  			
  			var description = new Element('p').update( this.data[i].description );
  			
  			box.appendChild(header);
  			box.appendChild(description);
  		} else if ( i >= 3){
  			box = new Element('div').addClassName('box');
  			header = new Element('h4').update('<a href="' + this.data[i].link + '">' + this.data[i].title + '</a>');
  			box.appendChild(header);
  		}
  		target.appendChild(box);
  	}
  }
  
});