function initialize() {
  var zoomed = false;
  
  // map
  var latlng = new google.maps.LatLng(22.593726,9.140625);
  var myOptions = {
    backgroundColor: '#99B3CC',
    zoom: 2,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    scrollwheel: false,
    mapTypeControl: false
//    scaleControl: false,
//    disableDoubleClickZoom: true,
  };
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  
  // country markers
  var directions = true;
  var current = 0;
  var markers = new Array();
  var selected = new Array();
    
  $('#country-info').find('.country').each(function(i){
    var c_title = $(this).find('.title').text();
	var c_lat = $(this).find('.lat').text();
	var c_long = $(this).find('.long').text();
	var c_tid = $(this).find('.tid').text();
	
	var c_latlng = new google.maps.LatLng(c_lat,c_long);
	
	// create marker
	markers[i] = new google.maps.Marker({
      position: c_latlng,
      map: map,
      title: c_title,
      icon:'sites/all/themes/wwc/styles/images/markerOff.png',
      tid: c_tid
	});
	
	selected[i] = false;
	
	google.maps.event.addListener(markers[i], 'click', function() {
	  if (!selected[i]) {
	    loadCountry(i);
	    zoomed = true;
      } else {
        unloadCountry(i);
      }
	});
  });
  
  google.maps.event.addListener(map, 'tilesloaded', function() {
    var country = location.hash.substring(1);
    
    for (var i = 0; i < markers.length; i++) {
      if (country.toLowerCase() == markers[i].title.toLowerCase()) {
        loadCountry(i);
        zoomed = true;
      }
    }
  });
  
  function loadCountry(i) {
    markers[i].setIcon('sites/all/themes/wwc/styles/images/markerOn.png');
    
    if (!zoomed) {
	    map.setZoom(3);
    	map.setCenter(markers[i].getPosition());
    }
    
    $('span#country-name').html(markers[i].title);
    
    if (directions) {
      $('#map-directions').hide();
      directions = false;
    }
    $('#mapplication').show();
    
    if (current != i) {
      selected[current] = false;
      markers[current].setIcon('sites/all/themes/wwc/styles/images/markerOff.png');
    }
      
    var dataString = 'tid='+markers[i].tid;
    
    $.ajax({
      type: 'POST',
      url: 'sites/all/themes/wwc/scripts/php/map-stories.php', 
	  data: dataString,
      success: function(msg) {
	    $('#stories-list-wrapper').html(msg);
	    $('#stories-list-wrapper').trigger('change');
	  
	    if ( $('#stories-list-inner').children('ul').size() > 3 ) {
	      $('#lightNextSlide').hide();
	      $('#nextSlide').show();
	    } else {
	      $('#prevSlide').hide();
	      $('#nextSlide').hide();
	      $('#lightNextSlide').show();
	      $('#lightPrevSlide').show();
	    }
      }
    });
	
    current = i;
    selected[i] = true;
    
    location.hash = '#'+markers[i].title.toLowerCase();
  }
  
  function unloadCountry(i) {
    markers[i].setIcon('sites/all/themes/wwc/styles/images/markerOff.png');
    
    
    $('#mapplication').hide();
    if (!directions) {
      $('#map-directions').show();
      directions = true;
    }
    
    if ( $('#stories-list-inner').children('ul').size() > 3 ) {
      $('#prevSlide').hide();
      $('#nextSlide').hide();
      $('#lightNextSlide').show();
      $('#lightPrevSlide').show();
    }
    
    $('span#country-name').html('');
    $('#map-stories-list').html('');
    
    selected[i] = false;
    
    location.hash = '#select-a-country';
  }
}

$(document).ready(function() {
  var NUM_OF_COL = 3;
  
  var total;
  var ctr = NUM_OF_COL;
  
  var location;
  var xLoc;
  var duration = 500;
  
  
  $('#stories-list-wrapper').change( function() {
    ctr = NUM_OF_COL;
    location = 0;
    total = $('#stories-list-inner').children('ul').size();
  });
  
  // handle button clicks
  $('#prevSlide').click(function( event ) {
    event.preventDefault();
    handleClick('prev');
  });
  
  $('#nextSlide').click(function( event ) {
    event.preventDefault();
    handleClick('next');
  });
  
  function handleClick(btn) {
    if(btn == 'next') {
      ctr++;
      moveSlider(320);
      reloadButtons();
      updateCounter('+');
    } else if(btn == 'prev') {
      ctr--;
      moveSlider(-(320));
      reloadButtons();
      updateCounter('-');
    }
  }
  
  // moves the slider
  function moveSlider(distance) {
    location += distance;
    xLoc = "-"+(location)+"px";
    $('#stories-list-inner').animate({left:xLoc}, duration);
  }
  
  function reloadButtons() {
    $('#lightPrevSlide').hide();
    $('#lightNextSlide').hide();
    $('#prevSlide').show();
    $('#nextSlide').show();
  
    if (ctr == NUM_OF_COL) {
      $('#prevSlide').hide();
      $('#lightPrevSlide').show();
    } else if (ctr == total) {
      $('#nextSlide').hide();
      $('#lightNextSlide').show();
    }
  }
  
  function updateCounter(direction) {
    var from = parseInt($('span#count-from').html());
    var to = parseInt($('span#count-to').html());
    var of = parseInt($('span#count-of').html());
    
    if(direction == '+') {
      from += 3;
      to += 3;
      
      if (to > of) { to = of; }
    } else if(direction == '-') {
      if (to == of && to % 3 != 0) { to += 3 - (to % 3); }
      
      from -= 3;
      to -= 3;
    }
    
    $('span#count-from').html(from);
    $('span#count-to').html(to);
  }

});
