//globals
var map;
var lat, lng;


//init
$(window).load(function() { 
	
	ieCheck();
	getMyPosition();
	initSwipeContact();

});

$(document).ready(function() { 

	addListeners();
	
});

function addListeners() {
	$(window).resize(function() { setMapPosition(); });
}

//map functions
function initMap() {
	var mapOptions = {
			zoom: 12,
          	center: new google.maps.LatLng(lat, lng),
          	mapTypeId: google.maps.MapTypeId.ROADMAP,
          	scrollwheel: false,
          	disableDoubleClickZoom: true,
          	disableDefaultUI: true,
          	draggable: false,
          	keyboardShortcuts: false
    	};
    	
   	var mapStyles = [
  			{
    			featureType: 'all',
    			stylers: [
      				{ visibility: 'off' },
    			]
  			},{
    			featureType: 'road',
    			stylers: [
      				{ visibility: 'simplified' },
      				{ lightness: 100 },
       			]
  			},{
    			featureType: 'road',
    			elementType: 'labels',
    			stylers: [
      				{ visibility: 'off' }
    			]
  			},{
    			featureType: 'landscape',
    			stylers: [
      				{ hue: '#000000' },
    				{ saturation: -100 },
   				 	{ lightness: 0 }
       			]
  			}];
		
	var styledMapType = new google.maps.StyledMapType(mapStyles, {name: 'Tim Wuyts'});
    
    map = new google.maps.Map(document.getElementById('map'), mapOptions);
    map.mapTypes.set('tim_wuyts', styledMapType);
  	map.setMapTypeId('tim_wuyts');
  	
  	setMapPosition();
} 

function setMapPosition() {
	var w = $(window).width();
	var h = $(window).height();
	var pos = $('#content').offset();
	
	var toX =  (w/2 - pos.left);
	var toY =  - ((pos.top + 285) - (h/2));
	
	if(w <= 480) toX -= 50;  
	
	map.panTo(new google.maps.LatLng(lat, lng));
	map.panBy(toX, toY);
}

// mobile functions

function initSwipeContact() {
	if (screen.width <= 480) {
		var wBtn = $('#contact ul li').width();
	
		$('#contact ul').wrap('<div id="swipebox" />');
	
		$('#swipebox').css({
			'overflow' 	: 'hidden',
			'width'		: $('body').width(),
			'height'	: $('#contact ul').height()
		});	
		$('#contact ul').css({
			'overflow' 	: 'auto',
			'left'		: '0',
			'width' 	: wBtn * 7
		});	
		$('#contact ul li').css({
			'float'		: 'left',
			'marginRight': '3px',
			'width' 	: wBtn
		});	
		
		$('#swipebox').touchwipe({
    	 	wipeLeft: function() { swipeContact('left'); },
    	 	wipeRight: function() { swipeContact('right'); },
    	    preventDefaultEvents: true
		});
	}
}

function swipeContact(direction) {
	var elem = $('#contact ul');
	var speed = 200;
	var wContact = $('#contact').width();
	var curPos = parseInt($('#contact ul').css('left').replace('px', ''));
	
	if(curPos == 0 && direction == 'left') {
		elem.animate({
			'left'		: -wContact
		}, { duration: speed, easing: 'easeInOutSine' });
	} else if (curPos < 0 && direction == 'right') {
		elem.animate({
			'left'		: '0'
		}, { duration: speed, easing: 'easeInOutSine' });
	} else if (curPos < 0 && direction == 'left') {
		elem.animate({
			'left'		: '-=20' 
		}, 
		{ duration: (speed/2), easing: 'easeInOutSine', complete: function() {
			$(elem).animate({
				'left'		: '+=20' 
			}, { duration: (speed/2), easing: 'easeInOutSine' });
		} 
		});
	} else if (curPos == 0 && direction == 'right') {
		elem.animate({
			'left'		: '+=20' 
		}, 
		{ duration: (speed/2), easing: 'easeInOutSine', complete: function() {
			$(elem).animate({
				'left'		: '-=20' 
			}, { duration: (speed/2), easing: 'easeInOutSine' });
		} 
		});
	}
}

// foursquare functions

function getMyPosition() {
    var clientId = 'ZXTOZCKVEAGSKNO3SE3AQ0DKKXZBGKPY5FARKBKEOWDTG2YR';
    var clientSecret = 'PYT2GXPKIUMKHLQB20HGAZOFAY3CMW43GKBSKEUXNRUP1O5O';
    var redirectUri = location.href;
    
    try {
    	var fsclient = new FourSquareClient(clientId, clientSecret, redirectUri, true);
    
   		fsclient.usersClient.users('8515977', { 
    		onSuccess: function(data) { 
    			var spot = data.response.user.checkins.items[0].venue;
    		
    			lat = spot.location.lat;
    			lng = spot.location.lng;
    		
    			initMap();
    			setTagline(spot.name);
       		},
    		onFailure: function(data) {
    			doItTheHardWay();
    		}
    	 });	 
    } catch(e) {
    	doItTheHardWay();
    }
}

function doItTheHardWay() {
	lat = 51.164461; //hardcoded location + name for spot
	lng = 4.748354;
    
   	initMap();
 	setTagline('Familie Wuyts');
}

function setTagline(spot) {
	var tagline;
	
	switch(spot) {
		case 'Familie Wuyts': tagline = "Family time!";
		break;
		case 'Digiti': tagline = "Working my ass off at the office.";
		break;
		case 'Café Engels Huis': tagline = "Drinking a pint (or five).";
		break;
		case 'Familie De Meyer': tagline = "Spending some quality time with my girlfriend.";
		break;
		case 'Chiro Bouwel': tagline = "Giving a good example to some youngsters.";
		break;
		default:
			var phrases = new Array(
				"Cheating on my girlfriend... Don't tell!",
				"Saving the world, cause I'm Captain Obvious!",
				"I've lost my way! HELP.",
				"Don't know what I'm doing here...",
				"Not at home. Please don't rob my house!"
			);
			
			tagline = phrases[Math.floor(Math.random()*(phrases.length + 1))];
	}
	
	$('#content p').text(tagline).css('visibility', 'visible');
}

function ieCheck() {
	if($.browser.msie && parseInt($.browser.version.split('.')[0]) < 9) {
		window.location = 'http://windows.microsoft.com/en-US/internet-explorer/downloads/ie-9/worldwide-languages';
	}
}

