﻿var SELECTED_POSTID = null;
var IS_LOCALIZED = null;

function page_Initialize() {
	var pathLength = timeLine_CalculatePathLength();

	var defaultPostInfo = null;
	for (var i = 0; i < POSTS_DATA.length; i++) {
		var postInfo = POSTS_DATA[i];

		timeLine_InitPost(postInfo, pathLength);

		if (i == POSTS_DATA.length - 1)
			defaultPostInfo = postInfo;
	}
	
	if (IS_LOCALIZED) {
		relatedContent_LoadWeather(SELECTED_POSTID, true);
		relatedContent_LoadPhotos(SELECTED_POSTID, true);
		relatedContent_LoadWiki(SELECTED_POSTID, true);
		relatedContent_LoadPartnerPoi(SELECTED_POSTID, true);
		
		$('a.-sel-post-control-related').bind('click', function(event) {
			relatedContent_Load(null);
			event.preventDefault();
		});
	}
	
	if (defaultPostInfo != null) {
		timeLine_InitPaths(pathLength, defaultPostInfo);
		timeLine_InitControls();
		timeline_SetPostFocus(SELECTED_POSTID);
	}
}

// ************ TIMELINE ************
function timeLine_CalculatePathLength() {
	return $('#timeline_path').innerWidth() - 10;
}

function timeLine_Rescale() {
	var pathLength = timeLine_CalculatePathLength();

	var defaultPostInfo = null;
	for (var i = 0; i < POSTS_DATA.length; i++) {
		var postInfo = POSTS_DATA[i];

		timeLine_InitPost(postInfo, pathLength, false);

		if (i == POSTS_DATA.length - 1)
			defaultPostInfo = postInfo;
	}

	if (defaultPostInfo != null) {
		timeLine_InitPaths(pathLength, defaultPostInfo);
		timeline_SetPostFocus(SELECTED_POSTID);
	}
}

function timeLine_InitPost(postInfo, pathLength) {
	var offsetUnit = pathLength / 100;
	var markerOffset = offsetUnit * postInfo.offset;

	var stop = $('#timeline_post_' + postInfo.id)
				.css('left', markerOffset + 'px')
				.show();
}

function timeLine_InitPaths(pathLength, activePost) {
	var offsetUnit = pathLength / 100;
	var completedPathLength;

	if (activePost != null) {
		completedPathLength = offsetUnit * activePost.offset;
	} else {
		completedPathLength = pathLength;
	}

	$('#timeline_path_line')
				.css({
					'left': '5px',
					'width': completedPathLength + 'px'
				})
				.show();

	$('#timeline_path_linePlanned')
				.css({
					'left': (completedPathLength) + 'px',
					'width': (pathLength - completedPathLength) + 'px'
				})
				.show();

	$('#timeline_labels').width(pathLength + 10).show();
}

function timeLine_InitControls() {
	$('#timeline_control_previous')
		.bind('click', function(event) {
			var prevPost = _getPost(SELECTED_POSTID, -1);
			
			if (prevPost != null)
				window.location = $('#timeline_post_' + prevPost.id + ' a').attr('href');
			
			event.preventDefault();
		});

	$('#timeline_control_next')
		.bind('click', function(event) {
			var nextPost = _getPost(SELECTED_POSTID, 1);
			
			if (nextPost != null)
				window.location = $('#timeline_post_' + nextPost.id + ' a').attr('href');
		
			event.preventDefault();
		});
}

function timeline_SetPostFocus(postID) {
	var pinPosition = $('#timeline_post_' + postID).position();

	$('#timeline_slider')
		.css({ 'left': pinPosition.left - 5 + 'px' })
		.show();
}

// ************ TRIPMAP ************
function tripMap_PostFocusChanged(postID) {
	window.location = $('#timeline_post_' + postID + ' a').attr('href');
}

// ************ UTILITIES ************
function _getPost(postID, shift) {
	for (var i = 0; i < POSTS_DATA.length; i++) {
		if (POSTS_DATA[i].id == postID) {
			if (shift == 0)
				return POSTS_DATA[i];
			else if (shift > 0 && shift + i <= POSTS_DATA.length)
				return POSTS_DATA[i + shift];
			else if (shift < 0 && shift + i >= 0)
				return POSTS_DATA[i + shift];
			else
				return null;
		}
	}
	return null;
}

// ************ FLASH MAP ************
var FlashMap = function(containerID, apiKey, showPath, detailStopID) {
	this.containerID = containerID;
	this.apiKey = apiKey;
	this.showPath = showPath;
	this.detailStopID = detailStopID;
	
	this.getFlashObject = function() {
		if (navigator.appName.indexOf('Microsoft') != -1) {
			return document.forms[0]['DetailMap'];
		} else {
			return document['DetailMap'];
		}
	};
}
FlashMap.prototype.loadMap = function(callback) {
	var params = {
		wmode: 'transparent',
		bgcolor: '#ffffff'
	};

	var attributes = {  
		id: 'DetailMap',  
		name: 'DetailMap'
	};

	var flashvars = {
		mapkey: this.apiKey,
		mapMode: 'detail',
		showPath: this.showPath,
		detailStopID: this.detailStopID
	};

	swfobject.embedSWF('/res/skins/TripMap.swf?v=1.8', this.containerID, '100%', '100%', '9.0.0',
		'/res/editarticle/expressInstall.swf', flashvars, params, attributes
	);
}
FlashMap.prototype.loadPosts = function(posts) {
	this.getFlashObject().loadPosts(posts);
}
FlashMap.prototype.loadPostDetails = function(postDetails) {
	this.getFlashObject().loadPostDetails(postDetails);
}

// ************ JAVASCRIPT MAP ************
var JSMap = function(containerID, showPath, detailStopID) {
	this.containerID = containerID;
	this.showPath = showPath;
	this.detailStopID = detailStopID;
	
	this.map = null;
	this.bounds = new google.maps.LatLngBounds();
	this.detailStop = _getPost(this.detailStopID, 0);
}
JSMap.prototype.loadMap = function(callback) {
	var that = this;
	$(window).load(function() {
		var mapHolder = document.getElementById(that.containerID);
		
		var mapOptions = {
			zoom: 8,
			center: new google.maps.LatLng(-34.397, 150.644),
			navigationControl: true,
			navigationControlOptions: { 
				style: google.maps.NavigationControlStyle.DEFAULT
			}, 
			scaleControl: true,
			mapTypeControl: false,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};
		
		that.map = new google.maps.Map(mapHolder, mapOptions);
		
		if (typeof callback == 'function')
			callback();
	});
}
JSMap.prototype.loadPosts = function(posts) {
	var pathPoints = new Array();
	
	var image = new google.maps.MarkerImage(
		'/res/skins/default/stop.png',
		new google.maps.Size(22, 22),
		new google.maps.Point(0, 0),
		new google.maps.Point(11, 11)
	);
	
	for (var i = 0; i < posts.length; i++) {
		var post = posts[i];
		
		if (post.lat == null)
			continue;
			
		var latLng = new google.maps.LatLng(post.lat, post.lng);

		pathPoints.push(latLng);
		this.bounds.extend(latLng);
		
		var marker = new google.maps.Marker({
			position: latLng,
			map: this.map,
			icon: image
		});
	}

	if (this.showPath) {
		var pathOutline = new google.maps.Polyline({
			path: pathPoints,
			strokeColor: "#FFFFFF",
			strokeOpacity: 1.0,
			strokeWeight: 4
		});
		pathOutline.setMap(this.map);
		var path = new google.maps.Polyline({
			path: pathPoints,
			strokeColor: "#1282C0",
			strokeOpacity: 1.0,
			strokeWeight: 2
		});
		path.setMap(this.map);
	}

	if (this.detailStop != null && this.detailStop.lat != null) {
		this.map.setCenter(new google.maps.LatLng(this.detailStop.lat, this.detailStop.lng));
		this.map.setZoom(15);
	} else {
		this.map.fitBounds(this.bounds);

	}
}
JSMap.prototype.loadPostDetails = function(postDetails) {
	var image = new google.maps.MarkerImage(
		'/res/skins/default/stopPlanned.png',
		new google.maps.Size(22, 22),
		new google.maps.Point(0, 0),
		new google.maps.Point(11, 11)
	);
	
	for (var i = 0; i < postDetails.length; i++) {
		var post = postDetails[i];
		
		if (post.lat == null || post.lng == null)
			continue;
			
		var latLng = new google.maps.LatLng(post.lat, post.lng);

		this.bounds.extend(latLng);
		
		var marker = new google.maps.Marker({
			position: latLng,
			map: this.map,
			icon: image
		});
	}
	
	if (this.detailStop == null || this.detailStop.lat == null)	
		this.map.fitBounds(this.bounds);
}