google.setOnLoadCallback(initialize, 0);

$(document).ready(function() {
	var service = $('#services .service:not(.not-available)');
	if(service.length > 0) {
		service.click(function() {
		  if($(this).hasClass('mobypicture') && $(this).hasClass('not-connected')) {
		    $.ajax({url: '/mobypicture.php', success: function(data){
		      $('#overlay .inner').html(data);
		      $('.close').click(function() {
		        $('#overlay').hide();
		      });
		      $("#mobypicture-add-form").submit(function(){
            $.post('/mobypicture.php?action=submit', $("#mobypicture-add-form").serialize(), function(data){
              if(data) {
                $('.close').click();
                // Easy fix, no AJAX
                window.location.reload();
              }
              else {
                alert('An error occured. Please try again.');
              }
            });
            return false;
          });
		      $('#overlay').show();
		    }});
      }
		  else if($(this).hasClass('heatmap')) {
		    if($(this).hasClass('on')) {
		      hideHeatmap();
		      $(this).removeClass('on');
	        $(this).addClass('off');
		    }
		    else {
		      showHeatmap();
          $(this).removeClass('off');
          $(this).addClass('on');
		    }
      }
		  else if($(this).hasClass('not-connected')) {
		    window.location = $(this).attr('href');
		  } else if($(this).hasClass('on')) {
				$(this).removeClass('on');
				$(this).addClass('off');
				switchMarkers($(this).attr('rel'));
			} else {
				$(this).removeClass('off');
				$(this).addClass('on');
				switchMarkers($(this).attr('rel'));
			}
		})
	}
  
	// go to step
  $('#howbigisyourworld').click(function() {
    $(this).hide();
    $('.step1').show();
  });
  // Step navigation
  currentstep = 1;  
  $('.step-next').click(function() {
    // Hide all steps
    $('.step').hide();
    // Get next step
    currentstep++;
    // Show current step
    $('.step'+currentstep).show();
    $('#step'+currentstep).show();
  });
  
  $('.step1-button').click(function() {
    // Hide all steps
    $('.step').hide();
    $('.step1').show();
    currentstep = 1;
  });

  $('.step2-button').click(function() {
    // Hide all steps
    $('.step').hide();
    $('.step2').show();
    currentstep = 2;
  });
	
	$('#step-go').click(function() {
		$('#overlay').hide();
		createCookie('introShown', 1);
	});
	
	$('.close').click(function() {
		$('#overlay').hide();
	});
	
	// check if overlay have to be shown
	if(!parseInt(readCookie('introShown')) > 0 || (typeof(current_step) != 'undefined' && parseInt(current_step) > 0)) {
		$('#overlay').show();
		if(typeof(current_step) != 'undefined' && parseInt(current_step) > 0) {
		  $('.step, #howbigisyourworld').hide();
	    $('.step'+current_step).show();
		}
	}
	
	var readmore = $('.readmore');
	if(readmore.length > 0) {
		readmore.click(function() {
			var hidden = $(this).siblings('.hiddentext');
			if(hidden.hasClass('hidden')) {
				hidden.removeClass('hidden');
				$(this).text('Read less');
			} else {
				hidden.addClass('hidden');
				$(this).text('Read more');
			}
			hidden.slideToggle();
		});
	}
});

function switchMarkers(type) {
	var i = 0;
	switch(type) {
		case 'twitter':
			for(i = 0; i < tweetMarkers.length; i++) {
				switchState(tweetMarkers[i]);
			}
			break;
		case 'foursquare':
			for(i = 0; i < fsMarkers.length; i++) {
				switchState(fsMarkers[i]);
			}
			break;
		case 'mobypicture':
			for(i = 0; i < mobyMarkers.length; i++) {
				switchState(mobyMarkers[i]);
			}
			break;
	}
}

function switchState(el) {
	if(el.isHidden()) {
		el.show();
	} else {
		el.hide();
	}
}

// Cookie functions
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}

