/**
 * Mifgaim homepage javascript
 * 
 * @since 09/11/11
 * @author Imri
 */
 
$(function(){

	loadMapScript();
	
	$('#tabTips').vTicker({
	
		speed: 500,
		pause: 4000,
		animation: 'fade',
		mousePause: false,
		showItems: 5,
		direction: 'down'
	});
	
	$( "#tabs" ).tabs({ });
	
	getRecentItems();
	
	getTraficReports();
	
	
	
});

//-------------------------------------
// Map
//-------------------------------------

 /**
 * Home Map
 * Js functionality
 *
 * @since 03/10/11
 * @author Imri
 */

var initialLocation;
var browserSupportFlag =  new Boolean();

/**
 * Initialize map
 * @todo change obj id from 'map_canvas' to a unique one
 * @todo add 'settings' array for stuff like 'zoom' and so
 */
function initialize()
{
	/*---------------------------*/
	// Init map
	/*---------------------------*/
	
	var loc = new google.maps.LatLng(32.068611,34.777222);
	loadMap(loc);
	
	/*---------------------------*/
	// Geolocation
	/*---------------------------*/
  
	// Try W3C Geolocation (Preferred)
	
	if (navigator.geolocation)
	{
		browserSupportFlag = true;
		
		navigator.geolocation.getCurrentPosition(function(position) {
		
			initialLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
			loadMap(initialLocation);
		}, function() {
			handleNoGeolocation(browserSupportFlag);
		});
  
	}
	
	// Try Google Gears Geolocation
	
	else if (google.gears)
	{
	
		browserSupportFlag = true;
		
		var geo = google.gears.factory.create('beta.geolocation');
		geo.getCurrentPosition(function(position) {
		
			initialLocation = new google.maps.LatLng(position.latitude,position.longitude);
			loadMap(initialLocation);
		}, function() {
			handleNoGeoLocation(browserSupportFlag);
		});
  
	}
	
	// Browser doesn't support Geolocation
	
	else
	{
	
		browserSupportFlag = false;
		handleNoGeolocation(browserSupportFlag);
	}
  
  
  
	function handleNoGeolocation(errorFlag)
	{
		if (errorFlag == true) {
		  //alert("Geolocation service failed.");
		  initialLocation = new google.maps.LatLng(32.068611,34.777222); // Newyork
		} else {
		  //alert("Your browser doesn't support geolocation. We've placed you in Siberia."); // Siberia
		  initialLocation = new google.maps.LatLng(32.068611,34.777222);
		}
		loadMap(initialLocation);
	}
  
}
  
function loadMapScript()
{
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src = "http://maps.googleapis.com/maps/api/js?sensor=true&language=he&callback=initialize";
	document.body.appendChild(script);
	  
	script.type = "text/javascript";
	script.src = "http://code.google.com/apis/gears/gears_init.js";
	document.body.appendChild(script);
}

/**
 * Load Map
 */

function loadMap( location )
{

    map = new google.maps.Map(document.getElementById('map_canvas'), {
      center: location,
      zoom: 12,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
 
    var layer = new google.maps.FusionTablesLayer({
      query: {
        select: 'location',
        from: '1973358',
        where: 'status = 1'
      }
    });
    
    layer.setMap(map);

}

//-------------------------------------
// Get recent items
//-------------------------------------

var start_date = null, end_date = null;

function getRecentItems()
{
	var ticker_pause = 4000;
	var ticker_speed = 500;
	
	$.ajax({
	  url: recentItemsFeedUrl,
	  data: { start_date: start_date, end_date: end_date },
	  cache: false,
	  success: function(msg)
	  {

		var results = $.parseJSON(msg);
		
		/* Get results */
		
		if ( results.length == 0 )
		{
			return false;
		}
		
		var new_startDate = results[0];
		var new_endDate = results[1];
		
		var html = results[2];
		
		//$(".homeDynamic .right").prepend(html);
		
		/* Append html */
		
		$('#container_recentReports ul').append(html);
		
		if ( end_date == null )
		{
			$('#container_recentReports').removeClass("loading");
			
			$('#container_recentReports').vTicker({
	
				speed: ticker_speed,
				pause: ticker_pause,
				animation: 'fade',
				mousePause: false,
				showItems: 9,
				direction: 'down'
			});
		}
		
		/* Set new ids */
		
		start_date = new_startDate;
		end_date = new_endDate;
	  },
	  
	  complete: function()
	  {
		window.setTimeout(getRecentItems, 2800);
	  }
	});
}

//-------------------------------------
// Get Traffic Reports
//-------------------------------------

function getTraficReports()
{
	$.ajax({
	  url: trafficReportsFeedUrl,
	  cache: false,
	  success: function(msg)
	  {
		/* Put marquee */
		
		$('#tabTrafficReports').html('<marquee behavior="scroll" direction="up" scrollamount="1"></marquee>');
		$('#tabTrafficReports marquee').html('<ul></ul>');
		$('#tabTrafficReports marquee ul').html(msg);
		
		$('#tabTrafficReports marquee').marquee().mouseover(function () {
		    $(this).trigger('stop');
		}).mouseout(function () {
		    $(this).trigger('start');
		});
		
		/* Remove loader */
		
		$('#tabTrafficReports').removeClass("loading");
		
		
		/*$('#tabTrafficReports').vTicker({
			speed: 500,
			pause: 4000,
			animation: 'fade',
			mousePause: false,
			direction: 'down'
		});*/
	  }
	});
}
