// JavaScript Document


(function(){
  var id = 0, head = $$('head')[0], global = this;
  global.getJSON = function(url, callback) {
    var script = document.createElement('script'), token = '__jsonp' + id;
    
    // callback should be a global function
    global[token] = callback;
    
    // url should have "?" parameter which is to be replaced with a global callback name
    script.src = url.replace(/\?(&|$)/, '__jsonp' + id + '$1');
	
    // clean up on load: remove script tag, null script variable and delete global callback function
    script.onload = function() {
      script.remove();
      script = null;
      delete global[token];
    };
    head.appendChild(script);
    
    // callback name should be unique
    id++;
  }
})();


var WorkApi = Class.create({
 
 initialize: function() {
   
    this.Key 			  = null;
    
	this.DataReservation  = null;
   	this.HandshowLoad 	  = this.showLoad.bindAsEventListener(this);
	this.HandshowResponse = this.showResponse.bindAsEventListener(this);
	this.Handfalla 		  = this.falla.bindAsEventListener(this);
	this.title			  = '';
	this.caption		  = '';
	
	this.iter 			  = 0;
	
	this.url    		  = ('http://search.twitter.com/search.json');
	
  },
  
  trace : function( str ){
		
		if( console ){
			
			console.debug( str );
		
		}else{
		
			alert( str );
		
		}
	  
  },
  
  msg : function( msg ){
		
		alert( msg );
  },
  
  
  initEvent : function(){
	  
	  Event.observe(arguments[0],arguments[1],arguments[2]); 
	  
  },
  
  
  /////////////////////////////////////////////////////////////////////////// 
  
  /* Ajax Section */
  	showLoad : function(){
		
		
	},
		
	 showResponse : function(t){
	
		
	},
			
	falla : function (){
	
		this.title = 'Alerta';
		this.caption = '¡Ha ocurrido un error por favor intente nuevamente!';
		
			
	},
  
  request : function(){
	  
		new Ajax.Request(this.url,this.objt);
			
	},
	
	updater : function(){
	  
		new Ajax.Updater(this.target,this.url,this.objt);
			
	},
	
	
	updaterTimmer : function(){
	  
		new Ajax.PeriodicalUpdater(this.target,this.url,this.objt);
		
		
	
	},
	
	
///////////////////////////////////////////////////////////////////////////////////////////////
	
	initializeGoogleMap : function (url){
		
		var purl      = "http://maps.google.com/maps/ms?ie=UTF8&hl=en&t=p&msa=0&output=georss&msid=106484775090296685271.0004681a37b713f6b5950";		
		var uservice  = typeof(url) != 'undefined' ? url : purl;
		
		if (GBrowserIsCompatible()) {
			geoXml = new GGeoXml(uservice);
			map    = new GMap2(document.getElementById("map_canvas"));
			map.setCenter(new GLatLng(19.562378, -99.244995), 6); 
			map.addControl(new GLargeMapControl());
			map.addControl(new GLargeMapControl());
			map.addControl(new GOverviewMapControl());
			map.addOverlay(geoXml);
			map.enableDoubleClickZoom();
			
			var mapControl = new GMapTypeControl();
     	    map.addControl(mapControl);

  	  }
		
	},
	
	
	toggleMyKml : function (){
  	 
	  if (toggleState == 1) {
    	
		map.removeOverlay(geoXml);
    	toggleState = 0;
		
  	  } else {
   		
		map.addOverlay(geoXml);
    	toggleState = 1;
		
  	  }
	  
	  
	},
		
	loadGoogleMaps : function(){
		
	 var map;
	 var geoXml;
	 var toggleState = 1;

	 this.initializeGoogleMap();

	},
	
	loadTwitterEs : function( ){
		
		var parameters = {lang:'es',ors:'Influenza', rpp:50, page:1, count:3, callback:'WApi.parserJsonEs'};		
		var url 	= this.url+'?'+Object.toQueryString(parameters);
		
		getJSON(url);
		
		
	},
	
	
	loadTwitterEn : function( ){
		
		var  parameters = {lang:'en',q:'swineflu', rpp:50, page:1, count:3, callback:'WApi.parserJsonEn'}		
		var url 	= this.url+'?'+Object.toQueryString(parameters);
		
		getJSON(url);
		
	},
	
	
	getPublishTime : function (date){
		
		var published = new Date(date);		  
			published = published.getDay() +'-'+ published.getMonth() +'-'+published.getFullYear() +'  '+ published.getHours() +':'+ published.getMinutes();	
			
			return published;
	},
	
	parserJsonEs : function(request){
		
		this.target = 'feedTwitterEs';
		$(this.target).update('');
		this.parserJson( request );
		
	},
	
	parserJsonEn : function( request ){
		
		this.target = 'feedTwitterEn';
		$(this.target).update('');
		this.parserJson( request );
	},
	
	parserJson : function( request ){
		
		request.results.each(function( rowset, id){
									  
			var published = this.getPublishTime(rowset.created_at);
			var thumb	  = rowset.profile_image_url;
			var author    = rowset.from_user;
			var twitter    = rowset.source;
			var content    = rowset.text;
		
		
		var LiRow = document.createElement('li');
			Element.extend(LiRow);
			$(this.target).appendChild(LiRow);
			
			
			var LiRowDiv = document.createElement('div');					
			Element.extend(LiRowDiv);
			LiRowDiv.className = 'zoneContent';				
			LiRow.appendChild(LiRowDiv);
			
			var LiRowSpan = document.createElement('span');					
			Element.extend(LiRowSpan);
			LiRowSpan.innerHTML = published;				
			LiRowDiv.appendChild(LiRowSpan);
			
			var LiRowImg = document.createElement('img');					
			Element.extend(LiRowImg);
			LiRowImg.src = thumb;				
			LiRowDiv.appendChild(LiRowImg);
			
			var LiRowh3 = document.createElement('h3');					
			Element.extend(LiRowh3);			
			LiRowDiv.appendChild(LiRowh3);
			
			var LiRowh3A = document.createElement('a');					
			Element.extend(LiRowh3A);	
			LiRowh3A.href = twitter;
			LiRowh3A.target = '_blank';
			LiRowh3A.innerHTML = author;
			LiRowh3.appendChild(LiRowh3A);
			
			var LiRowDiv = document.createElement('div');					
			Element.extend(LiRowDiv);
			LiRowDiv.innerHTML = content;
			LiRow.appendChild(LiRowDiv);
			
			var LiRowDiv = document.createElement('div');					
			Element.extend(LiRowDiv);
			LiRowDiv.style.clear = 'both';
			LiRow.appendChild(LiRowDiv);
			
		  }.bind(this));
		
		
	
	},
		
	createPeriodicalExecuter : function(){
		
		if( this.iter < 1 ) {
	
			this.iter++;
			
			this.HandLoadEs   = this.loadTwitterEs.bindAsEventListener(this);
			this.HandLoadEn   = this.loadTwitterEn.bindAsEventListener(this);
			
			new PeriodicalExecuter( this.HandLoadEs, 60 );
			new PeriodicalExecuter( this.HandLoadEn, 60 );
			
		}
		
	},
	
	loadApi : function(){
		
		this.loadTwitterEs();		
		this.loadTwitterEn();	
		this.loadGoogleMaps();
		this.createPeriodicalExecuter();
		new Control.Tabs('NavContainer');
		
		
	}
		 
});


	
	var WApi = new WorkApi();
