var website_statebox_open_bydialog = false;
var website_dialog_left = 0;
var website_settimeout = null;
var website_dialog_lowered = false;
var website_infront_timer = null;
var website_last_what = null;
var trajet_cap_current = '';
var website_trip_graph = null;
var website_trip_graph_highlight = null;
var website_trip_graph_previous = null;
var website_trip_graph_last_view = false;
var trackRelatedPage = 0;
var trackRelatedLastTrack = 0;
var trackRelatedCategory = 'related';
var trackRelatedLoad = false;
$(document).ready(function() {
	website_set_css(true);
	website_add_listener();
	//player_creer();
	setTimeout('player_check_create_success()',_website_timeout_create_player);
	// Make content-left resizable
	$('#content-left').resizable({
		handles: 'e',
		minWidth: 320,
		maxWidth: 800,
		resize: function(event, ui) {
			// With of the map
			$('#gmap').width($(window).width() - $(this).outerWidth()-1);
			// Shadow on the left of the map
			$('#content-shadow-left').css('left',$('#gmap').offset().left);
			// New player height
			var new_height = $(this).width() * 0.80625;
			$('#player-space').height(new_height);
			// Ads on the top left of the map
			if($('#ads').is(':visible'))
				$('#ads').css('left', $(this).width() + 5);
			// Default info (news)
			if($('#default-info').is(':visible'))
				$('#default-info').height($('BODY').innerHeight()-$('#default-info').offset().top);
			// Trip info
			if($('#trip-info').is(':visible'))
			{
				$('#trip-info').height($('BODY').innerHeight()-$('#trip-info').offset().top);
				$('#trip-html').height($('BODY').innerHeight()-$('#trip-html').offset().top-$('#track-related').innerHeight());
			}
		},
		stop: function(event, ui) {
			gmap_check_size();
			website_trip_graph_check_draw();
			// Ads on the top left of the map
			if($('#ads').is(':hidden'))
				$('#ads').css('left', $(this).width() + 5);
			// Default info (news)
			if($('#default-info').is(':hidden'))
				$('#default-info').height($('BODY').innerHeight()-$('#default-info').offset().top);
			// Trip info
			if($('#trip-info').is(':hidden'))
			{
				$('#trip-info').height($('BODY').innerHeight()-$('#trip-info').offset().top);
				$('#trip-html').height($('BODY').innerHeight()-$('#trip-html').offset().top-$('#track-related').innerHeight());
			}
		}
	});
});
$(window).unload(function() {
	GUnload();
});
jQuery().zoom(function() {
	if($.browser.msie)
		setTimeout('website_set_css(false);',500);
});
$(window).resize(function() {
	website_set_css(false);
	// Center the dialog div if she is view
	if($('#dialog').css('display') != 'none')
	{
		if($('#dialog TABLE:first').width() < $(window).width())
			$('#dialog').css('left',($(window).width()-$('#dialog TABLE:first').width())/2);
		else
			$('#dialog').css('left',0);
		// Dialog are lowered
		if(website_dialog_lowered)
		{
			website_dialog_left = $('#dialog').css('left');
			$('#dialog').css('left',$(window).width() - 80 - $('#dialog').width());
		}
	}
	// Center the statebox div if she is view
	if($('#statebox').css('display') != 'none')
		$('#statebox').css('left',($('BODY').innerWidth()-$('#statebox TABLE:first').width())/2);
	// Show it
	if($('#trip-info').css('display') != 'none')
		$('#trip-html').height($('BODY').innerHeight()-$('#trip-html').offset().top-$('#track-related').innerHeight());
	// If graph view
	if($('#trip-graph').css('display') != 'none')
		website_trip_graph_check_draw();
	// Default-info
	$('#default-info').height($('BODY').innerHeight()-$('#default-info').offset().top);
	// Shadow on left and right
	$('#content-shadow-left').height($('#content-left').height());
	$('#content-shadow-right').height($('#content-left').height());
});
function website_load(what) {
	// If website_last_what is null
	if(!website_last_what)
		website_last_what = what;
	else
	{
		trip.init(gmap,player,150,website_icon_path);
		trip.set_function_call('website_loading_show','website_loading_hide','website_dashboard_show','website_dashboard_hide','website_dashboard_update','website_enable_categoryChoice','website_disable_categoryChoice','websiteManageAds');
		trip.set_url('dialog-track-startpoint-load.html','dialog-track-infowindow.html','dialog-track-info-load.html','dialog-track-play-load.html','dialog-track-moreinfo.html');
		trip.set_config(startpoint_zoommin_details,startpoint_zoom_step,player_video_default,player_image_default);
		// There is not autoplay track
		if(_track_autoplay == 0)
		{
			if(dialog_autoopen != '')
				website_open_dialog(dialog_autoopen,dialog_autoopen_data);
			else
				website_open_dialog('dialog-top');
			// Load startpoint
			trip.sp_load();
			// If first coming
			if(website_first_coming == 1)
				setTimeout('player_play();',website_timeout_default);
		}
		else
			trip.play(_track_autoplay);
	}
}
function website_set_css(first) {
	if($(window).width() < 900)
	{
		$('#content-left').width(320);
		$('#player-space').height(258);
		$('#ads').css('left',325);
	}
	else
	{
		if($(window).width() > 1500)
		{
			$('#content-left').width(640);
			$('#player-space').height(498);
			$('#ads').css('left',645);
		}
		else
		{
			$('#content-left').width(460);
			$('#player-space').height(366);
			$('#ads').css('left',405);
		}
	}
	// Set height of div content
	$('#content').height($('BODY').innerHeight()-$('#content').offset().top);
	// Set with of gmap
	$('#gmap').width($(window).width() - $('#content-left').outerWidth()-1);
	// Set height of gmap
	if($('#trip-graph').css('display') != 'none')
		$('#gmap').height($('#content').height()-150);
	else
		$('#gmap').height($('#content').height());
	// Set left of loading
	$('#loading').css('left',($('BODY').innerWidth()-$('#loading').innerWidth())/2);
	if(first)
	{
		// Add style 'tabs-selected' for the first tab
		$('#top-left LI:first').addClass('tabs-selected');
		// *** Shadow top content ***
		$('#content-shadow-top').css('top',$('#gmap').offset().top);	// Top
		// *** Shadow left content ***
		$('#content-shadow-left').css('height',$('#gmap').height());	// Height
		$('#content-shadow-left').css('top',$('#gmap').offset().top);	// Top
		// *** Shadow right content ***
		$('#content-shadow-right').css('height',$('#gmap').height());	// Height
		$('#content-shadow-right').css('top',$('#gmap').offset().top);	// Top
	}
	// *** Shadow left content ***
	$('#content-shadow-left').css('left',$('#gmap').offset().left);	// Left
	// Dialog
	if($('#dialog').css('display') != 'none')
	{
		// Set the dialog in center of window if the dialog-content width aren't more than the BODY
		if($('#dialog TABLE:first').width() < $('BODY').innerWidth())
			$('#dialog').css('left',($('BODY').innerWidth()-$('#dialog TABLE:first').width())/2);
		else
			$('#dialog').css('left',0);
	}
	// If ie browser, less the width of result search
	if($('#trip-html').css('display') != 'none' && $.browser.msie)
	{
		//$('#trip-html').width($('#content-left').width());
		$('#trip-html TABLE:first').width($('#content-left').width() - 18);
	}
	// Default-info
	$('#default-info').height($('BODY').innerHeight()-$('#default-info').offset().top);
	// Tooltip Addtrack
	/*if($('#tooltip-addtrack').css('display') != 'none')
	{
		$('#tooltip-addtrack').css('left',$('#uploader-sendtrack').offset().left);
		$('#tooltip-addtrack').css('top',$('#top-right').height());
	}*/
	// Check the size of gmap
	gmap_check_size();
}
function website_add_listener() {
	$('.link_ext').live('click',function() {
		window.open(this.href);
		//website_open_dialog_iframe(this.href);
		return false;
	});
	$('.dialog').live('click',function() {
		if($(this).attr('id') != '')
			website_open_dialog('dialog-'+$(this).attr('id'));
		else
			website_open_dialog('dialog-'+$(this).attr('title'));
	});
	$('#close-dialog').click(function () {
		website_close_dialog();
	});
	$('#close-statebox').click(function () {
		website_close_statebox();
	});
	$('.tooltip').live('mouseover',function(e) {
		if($(this).attr('alt') != undefined)
			website_open_tooltip($(this).attr('alt'),e.pageX,e.pageY);
		else
			website_open_tooltip($(this).attr('title'),e.pageX,e.pageY);
	});
	$('.tooltip').live('mouseout',function() {
		website_close_tooltip();
	});
	$('#dialog FORM INPUT').live('mousedown',function() {
		website_close_statebox();
	});
	$('#dialog FORM SELECT').live('mousedown',function() {
		website_close_statebox();
	});
	$('#dialog FORM TEXTAREA').live('mousedown',function() {
		website_close_statebox();
	});
	/*$('#lang').change(function() {
		website_lang_change($('#lang option:selected').val());
	});*/
	$('#dialog_lowered').live('click',function() {
		website_dialog_upper();
	});
	$('.dialog-view-track').live('click',function() {
		// Make the style of the dialog lowered
		website_dialog_lower_style(this);
		// View the track
		trip.show($(this).attr('id'),false,true);
	});
	$('.dialog-play-track').live('click',function() {
		// If it's from a window on map
		/*if($(this).parent().parent().parent().parent().hasClass('window-list-track'))
			trajet_lancer_lecture($(this).attr('id'));
		else
		{*/
			// Make the style of the dialog lowered
			website_dialog_lower_style(this);
			// Play the track
			trip.play($(this).attr('id'));
			//trajet_lancer_lecture($(this).attr('id'),true);
		//}
	});
	$('.for-play-track').live('mouseover',function() {
		$(this).children('SPAN').css('background-position','0 -28px');
	});
	$('.for-play-track').live('mouseout',function() {
		$(this).children('SPAN').css('background-position','0 -1px');
	});
	$('.dialog-info-track').live('click',function() {
		if($(this).parent().parent().next().css('display') == 'none')
		{
			if($(this).parent().parent().next().children().html() == '')
			{
				$(this).parent().parent().next().children().html(website_html_loading);
				trip.more_info($(this).attr('id'),$(this).parent().parent().next().children());
			}
			$(this).parent().parent().next().show();
		}
		else
			$(this).parent().parent().next().hide();
	});
	$('INPUT[class="permalink"]').live('click',function() {
		this.focus();
		this.select();
	});
	$('.website-share').live('click',function() {
		pop_share = window.open(website_homepage+'page-track-share-websitepopup-'+$(this).attr('id')+'.html','website_share','toolbar=0,status=0,width=430,height=630');
		pop_share.focus();
		//pop_share.moveTo(($(document).width()-400)/2,($(document).height()-250)/2);	// Error with Chrome !
	});
	$('#show-graph').click(function() {
		if($('#trip-graph').css('display') != 'none')
			website_trip_graph_hide();
		else
		{
			// If playing
			if(trip.playing)
				website_trip_graph_draw();
			else
			{
				website_open_statebox(_trip_graph_playing,'info',false);
				// Automaticaly close the statebox
				website_settimeout = setTimeout('website_close_statebox()',website_timeout_viewbox);
			}
		}
	});
	$('#car-follow').click(function() {
		if(trip.gpsmarker_follow_read())
			$(this).css('background-position','0 -30px');
		else
			$(this).css('background-position','0 -1px');
		trip.gpsmarker_follow_toggle();
	});
	$('#car-follow').hover(
		function() {
			if(trip.gpsmarker_follow_read())
				$(this).css('background-position','0 -30px');
			else
				$(this).css('background-position','0 -1px');
		},
		function() {
			if(trip.gpsmarker_follow_read())
				$(this).css('background-position','0 -1px');
			else
				$(this).css('background-position','0 -30px');
		}
	);
	$('#track-center').click(function() {
		trip.trip_center();
	});
	$('#track-center').hover(
		function() {
			$(this).css('background-position','0 -30px');
		},
		function() {
			$(this).css('background-position','0 -1px');
		}
	);
	$('#trip-close').click(function() {
		trip.stop(true);
	});
	$('#marker-near-track').click(function() {
		if(trip.searchmarker == null)
		{
			trip.searchmarker_toggle();
			$(this).css('background-position','0 -62px');
		}
		else
		{
			trip.searchmarker_toggle();
			$(this).css('background-position','0 0');
			trip.sp_load();
		}
	});
	$('#marker-near-track').hover(
		function() {
			if(trip.searchmarker == null)
				$(this).css('background-position','0 -31px');
		},
		function() {
			if(trip.searchmarker == null)
				$(this).css('background-position','0 0');
		}
	);
	/*$(window).keydown(function(event){
		if(event.keyCode == 27)
		{
			if(trip.searchmarker != null)
			{
				trip.searchmarker_toggle();
				$(this).css('background-position','0 0');
				trip.sp_load();
			}
		}
	});*/
	// Add event click trip-graph
	$('#trip-graph').bind('plotclick',function(event,pos,item) {
		if(item) {
			/*player.sendEvent('PLAY',true);
			player.sendEvent('SEEK',item.dataIndex);*/
			player_play_and_seek(item.dataIndex);
		}
	});
	$('#trip-graph').bind('plothover',function(event,pos,item) {
		if(item)
		{
			if(website_trip_graph_previous != item.datapoint)
			{
				var txt = _unit_elevation;
				if(item.seriesIndex == 1)
					txt = _unit_speed;
				website_trip_graph_previous = item.datapoint;
		    	// Convert second to time
		    	var time = second_to_minute(item.dataIndex);
				website_open_tooltip(item.series.label+' '+item.datapoint[1]+txt+_trip_graph_sep+time,item.pageX,item.pageY);
			}
		}
		else
		{
			website_close_tooltip();
			website_trip_graph_previous = null;
		}
	});
	/*$('.dialog-iframe-close').click(function() {
		website_close_dialog_iframe();
	});
	$(window).keydown(function(event){
		if(event.keyCode == 27)
			website_close_dialog_iframe();
	});*/
	$('#open-category').click(function() {
		if($('#open-category').css('opacity') == 1)
			$('#category-choice').toggle();
		else
		{
			website_open_statebox(_website_changeCategory_info,'info');
			// Automaticaly close the statebox
			website_settimeout = setTimeout('website_close_statebox()',website_timeout_viewbox);
		}
	});
	$('.vehicle').click(function() {
		$('#category-choice').hide();
		website_loading_show();
		var veh_id = $(this).attr('class').split(' ');
		veh_id = veh_id[0];
		$('#open-category').children('IMG').attr('src',$(this).children('TD:first').children('IMG').attr('src'));
		trip.changeCategory(veh_id,'website_categoryChoice_callback');
	});
	$('#ads').hover(
		function() {
			$(this).css('opacity',1);
		},
		function() {
			$(this).css('opacity',0.5);
		}
	);
	$('.edit-track').live('click',function() {
		website_open_dialog('dialog-track-edit',{trk_id:$(this).attr('id')});
	});
	$('.edit-snapshot').live('click',function() {
		website_open_dialog('dialog-track-snapshot',{poi_id:$(this).attr('id')});
	});
	$('.change-snapshot').live('click',function() {
		trip.snapshot_infowindow_change($(this).attr('id'));
	});
	$('.play-snapshot').live('click',function() {
		if(playerPlaying() || playerPaused())
			player_play_and_seek($(this).attr('id'));
		else
		{
			_track_autoplay_start = $(this).attr('id');
			player_play();
		}
	});
	$('.track-related-page').click(function(e) {
		e.preventDefault();
		if($(this).hasClass('next'))
			trackRelatedPage++;
		else
		{
			if(trackRelatedPage > 0)
				trackRelatedPage--;
		}
		trackRelatedLastTrack = 0;
		websiteLoadTrackRelated();
	});
	$('.track-related-category').click(function(e) {
		e.preventDefault();
		if($(this).hasClass('member'))
		{
			trackRelatedCategory = 'member';
			$(this).addClass('textbold');
			$('.track-related-category.related').removeClass('textbold');
		}
		else
		{
			trackRelatedCategory = 'related';
			$(this).addClass('textbold');
			$('.track-related-category.member').removeClass('textbold');
		}
		trackRelatedPage = 0;
		trackRelatedLastTrack = 0;
		websiteLoadTrackRelated();
	});
	$('A.link-dialog').live('click',function(e) {
		e.preventDefault();
		website_open_dialog('dialog-'+$(this).attr('href'), {trk_filename:$(this).attr('id')});
	});
	$('#add_track_scheme').live('mouseover',function(e) {
		$(this).stop(true,true);
	});
	$('#add_track_scheme').live('mouseout',function() {
		effect_add_track();
	});
	$('#3d_mod').live('click',function() {
		load_3d();
	});
}
function website_enable_categoryChoice() {
	$('#open-category').css('opacity',1);
	$('#open-category').css('cursor','pointer');
}
function website_disable_categoryChoice() {
	$('#open-category').css('opacity',0.5);
	$('#open-category').css('cursor','default');
}
function website_categoryChoice_callback(veh_id) {
	// #### Stats ####
	try {
		//pageTracker._trackEvent('Track','Category',veh_id);
		_gaq.push(['_trackEvent', 'Track', 'Category', veh_id]);
	}
	catch(e) {
		// Do nothing
	}
	switch($('#dialog-content DIV:first').attr('id'))
	{
		case 'dialog-top'	:	top_track(null,1);
								break;
		case 'dialog-search':	search_track();
								break;
	}
}
/*function website_open_dialog_iframe(url) {
	// #### Stats ####
	try {
		pageTracker._trackEvent('Website','Open Iframe',url);
	}
	catch(e) {
		// Do nothing
	}
	// #### Stats ####
	$('#overlay').show();
	$('#dialog-iframe').show();
	$('#dialog-iframe').width($('BODY').innerWidth()-30);
	$('#dialog-iframe').height($('BODY').innerHeight()-30);
	$('#dialog-iframe IFRAME:first').height($('#dialog-iframe').height()-$('SPAN.dialog-iframe-close').height());
	$('#dialog-iframe IFRAME:first').attr('src',url);
}
function website_close_dialog_iframe() {
	if($('#dialog-iframe').css('display') != 'none')
	{
		$('#dialog-iframe IFRAME:first').hide();
		$('#dialog-iframe').hide();
		$('#dialog-iframe IMG:first').show();
		$('#overlay').hide();
	}
}
function website_hide_loading_iframe() {
	if($('#dialog-iframe').css('display') != 'none')
	{
		$('#dialog-iframe IMG:first').hide();
		$('#dialog-iframe IFRAME:first').show();
	}
}*/
function website_enable_searchmarker() {
	trip.searchmarker_toggle();
	$('#marker-near-track').css('background-position','0 0');
}
/*function website_lang_change(new_lang) {
	website_loading_show();
	// get the script for change the language
	$.ajax({
		type: 'POST',
		data: ({lang:new_lang}),
		url: 'dialog-lang-change.html',
		success: function() {
			// #### Stats ####
			try {
				//pageTracker._trackEvent('Website','Change Lang',new_lang);
				_gaq.push(['_trackEvent', 'Website', 'Change Lang', new_lang]);
			}
			catch(e) {
				// Do nothing
			}
			// #### Stats ####
			//window.location.replace(website_homepage);
			window.location.reload();
		}
	});
}*/
/*function website_open_CURRENT_PHP_FILE(request,bt) {
	website_loading_show();
	// Test if the div are not already loaded (she isn't indexed)
	if($('div').index($('#'+request+'-content')) != -1)
	{
		// Hide the current open tabs and update the style of bt of the tabs
		website_change_tabs(bt);
		// Toggle the visibility of the player
		website_toogle_visibility_player(request);
		// Show the request page
		$('#'+request+'-content').show();
		website_loading_hide();
	}
	else
	{
		// Load the html of the request page
		$.ajax({
			type: 'POST',
			url: 'page-'+request+'.html',
			success: function(html) {
				// Hide the current open tabs and update the style of bt of the tabs
				website_change_tabs(bt);
				// Toggle the visibility of the player
				website_toogle_visibility_player(request);
				// Show the request page
				$('#content').append(html);
				website_loading_hide();
			}
		});
	}
}*/
/*function website_change_tabs(tab) {
	$('#content .content-tabs').hide();
	$('#tabs li').removeClass('tabs-selected');
	tab.addClass('tabs-selected');
}*/
/*function website_toogle_visibility_player(request) {
	if(request == 'map')
		$('#player-overlay').css('visibility','visible');
	else
	{
		$('#player-overlay').css('visibility','hidden');
		player_pause();
	}
}*/
function website_open_dialog(request,post_data) {
	// If the dialog aren't already opened
	if($('#dialog-content DIV:first').attr('id') != request)
	{
		website_dialog_lowered = false;
		// Clear the timeout of close dialog
		clearTimeout(website_settimeout);
		// Show the loading
		website_loading_show();
		// If data not isset
		if(!post_data)
			post_data = {};
		// Receive the html
		$.ajax({
			type: 'POST',
			dataType: 'json',
			data: (post_data),
			url: request+'.html',
			success: function(data) {
				// If a statebox open by a dialog
				if(website_statebox_open_bydialog)
					website_close_statebox();
				// data[0] : html
				// data[1] : true or false : load script or not
				// data[2] : variable of the script (for test if load or not)
				// data[3] : url of script
				// if need to load javascript file
				if(data[1])
				{
					// #### Stats ####
					try {
						//pageTracker._trackEvent('Website','Open Dialog',request);
						_gaq.push(['_trackEvent', 'Website', 'Open Dialog', request]);
					}
					catch(e) {
						// Do nothing
					}
					// #### Stats ####
					var script_loaded = 'typeof(filejs_'+data[2]+')';
					if(eval(script_loaded) == 'undefined')
					{
						$.getScript(data[3],
							function() {
								website_show_dialog(data[0]);
								// Exception for onchange event
								if(request == 'dialog-top')
								{
									$('#top_sort').change(function() {
										_gaq.push(['_trackEvent', 'Top', 'Sort By', $('#top_sort').val()]);
										top_track(null,1);
									});
								}
								if(request == 'dialog-uploader-sendtrack')
									effect_add_track();
							});
					}
					else
					{
						website_show_dialog(data[0]);
						// Exception for onchange event
						if(request == 'dialog-top')
						{
							$('#top_sort').change(function() {
								_gaq.push(['_trackEvent', 'Top', 'Sort By', $('#top_sort').val()]);
								top_track(null,1);
							});
						}
						if(request == 'dialog-uploader-sendtrack')
							effect_add_track();
					}
				}
				else
				{
					if(data[0] != '')
						website_show_dialog(data[0]);
					else
						website_close_dialog();
				}
			}
		});
	}
	else
	{
		// If the dialog are lowered => upper it
		if(website_dialog_lowered)
			website_dialog_upper();
	}
}
function effect_add_track() {
	// If ie browser, less the width of result search
	if($.browser.msie)
	{
		if($('#add_track_scheme').css('background-position-y') == '0px')
		{
			$('#add_track_scheme').delay(3000).animate({
					opacity:0.2
				},
				1000,
				function() {
					$(this).css('background-position','0px -400px');
					$(this).css('opacity',1);
					if($('#add_track_scheme').is(':visible'))
						effect_add_track();
				}
			);
		}
		else
		{
			$('#add_track_scheme').delay(3000).animate({
					opacity:0.2
				},
				1000,
				function() {
					$(this).css('background-position','0px 0px');
					$(this).css('opacity',1);
					if($('#add_track_scheme').is(':visible'))
						effect_add_track();
				}
			);
		}
	}
	else
	{
		if($('#add_track_scheme').css('background-position') == '0px 0px')
		{
			$('#add_track_scheme').delay(3000).animate({
					opacity:0.2
				},
				1000,
				function() {
					$(this).css('background-position','0px -400px');
					$(this).css('opacity',1);
					if($('#add_track_scheme').is(':visible'))
						effect_add_track();
				}
			);
		}
		else
		{
			$('#add_track_scheme').delay(3000).animate({
					opacity:0.2
				},
				1000,
				function() {
					$(this).css('background-position','0px 0px');
					$(this).css('opacity',1);
					if($('#add_track_scheme').is(':visible'))
						effect_add_track();
				}
			);
		}
	}
}
function website_show_dialog(data) {
	// Add the content in the dialog-content
	$('#dialog-content').html(data);
	// Show the dialog
	$('#dialog').show();
	// Set the dialog in center of window if the dialog-content width aren't more than the BODY
	if($('#dialog TABLE:first').width() < $('BODY').innerWidth())
		$('#dialog').css('left',($('BODY').innerWidth()-$('#dialog TABLE:first').width())/2);
	else
		$('#dialog').css('left',0);
	// tab_list_track
	if($('#list_track').css('display') == 'block')
	{
		if($('#list_track').height() + $('#list_track').offset().top > $('BODY').innerHeight())
			$('#list_track').height($('BODY').innerHeight() - $('#list_track').offset().top - 50);
		else
			$('#list_track').height($('#list_track').height());
		// If ie browser, less the width of result search
		if($.browser.msie)
			$('#list_track TABLE:first').width($('#list_track').width() - 20);
	}
	// Dialog of uploader
	if($('#dialog-uploader-sendtrack').css('display') == 'block')
		uploader_sendtrack_check_height();
	// Place the focus on the first input type text in the form
	$('#dialog INPUT:visible:enabled:first').focus();
	website_loading_hide();
}
function website_close_dialog() {
	// If a dialog are open
	if($('#dialog').css('display') != 'none')
	{
		// If a statebox open by a dialog
		if(website_statebox_open_bydialog)
			website_close_statebox();
		// Close the tooltip
		website_close_tooltip();
		if($.browser.msie)
		{
			$('#dialog').hide();
			$('#dialog-content').html('');
			website_dialog_lowered = false;
		}
		else
		{
			$('#dialog').fadeOut(
				'fast',
				function () {
					$('#dialog-content').html('');
					website_dialog_lowered = false;
				}
			);
		}
	}
	website_loading_hide();
}
function website_open_tooltip(text,mouse_left,mouse_top) {
	$('#tooltip').html(text);
	if(mouse_left+40 > $('BODY').innerWidth())
		$('#tooltip').css('left',mouse_left-20);
	else
		$('#tooltip').css('left',mouse_left+10);
	if(mouse_top+40 > $('BODY').innerHeight())
		$('#tooltip').css('top',mouse_top-20);
	else
		$('#tooltip').css('top',mouse_top+10);
	$('#tooltip').show().delay(5000).fadeOut('fast');
}
function website_close_tooltip() {
	if($('#tooltip').css('display') != 'none')
	{
		/*if($.browser.msie)*/
			$('#tooltip').hide();
		/*else
			$('#tooltip').fadeOut('fast');*/
	}
}
function website_open_statebox(data,type,bydialog) {
	// if bydialog is set
	if(bydialog)
		website_statebox_open_bydialog = true;
	else
		website_statebox_open_bydialog = false;
	// Add the content in the dialog-content
	$('#statebox-content').html(data);
	$('#statebox-content').removeClass();
	$('#statebox-content').addClass('statebox-'+type);
	// Show the dialog
	$('#statebox').show();
	// If the statebox have a width > 300
	if($('#statebox TABLE:first').width() > 300)
		$('#statebox-content').width(300);
	// Set the statebox in center of window if the statebox-content width aren't more than the BODY
	$('#statebox').css('left',($('BODY').innerWidth()-$('#statebox TABLE:first').width())/2);
}
function website_close_statebox() {
	if($('#statebox').css('display') != 'none')
	{
		if($.browser.msie)
			$('#statebox').hide();
		else
			$('#statebox').fadeOut('fast');
	}
}
function website_loading_show() {
	$('#loading').show();
}
function website_loading_hide() {
	$('#loading').hide();
}
function website_toggle_state_submit(bt) {
	if(bt)
	{
		if($(bt).attr('disabled'))
			$(bt).removeAttr('disabled');
		else
			$(bt).attr('disabled','disabled');
	}
}
function website_processing_error_form(error,bt,processing) {
	// If there is a processing, do that
	if(processing != '');
		eval(processing);
	// Add the error
	website_open_statebox(error,'error',true);
	// enable submit button
	website_toggle_state_submit(bt);
	website_loading_hide();
}
function website_dashboard_show(html) {
	//track_check_map_type();
	// Set html
	$('#trip-html').html(html);
	// Init the dashboard
	$('#speed').text('-');
	$('#elevation').text('-');
	$('#cap').css('background-position','0 -124px');
	$('#cap-text').text('-');
	// Show it
	if($('#trip-info').css('display') == 'none' || _track_autoplay != 0)
	{
		$('#default-info').hide();
		//$('#content-left').toggleClass('background-logo');
		$('#trip-info').show();
		if($.browser.msie && $.browser.version <= 7)
		{
			// Set height of trip-html
			$('#trip-html').height($('BODY').innerHeight()-$('#trip-html').offset().top-$('#track-related').innerHeight()-14);
		}
		else
		{
			// Set height of trip-html
			$('#trip-html').height($('BODY').innerHeight()-$('#trip-html').offset().top-$('#track-related').innerHeight());
		}
	}
	// If ie browser, less the width of result search
	if($.browser.msie)
	{
		if($.browser.version <= 6)
			$('#trip-html TABLE:first').width($('#content-left').width() - 36);
		else
			$('#trip-html TABLE:first').width($('#content-left').width() - 18);
	}
	// Load related track
	websiteLoadTrackRelated();
}
function website_dashboard_hide() {
	if($('#trip-info').css('display') != 'none')
	{
		$('#trip-info').hide();
		$('#trip-html').html('');
		website_trip_graph_hide(true);
		//$('#content-left').toggleClass('background-logo');
		$('#default-info').show();
	}
}
function website_dashboard_update(altitude,speed,cap) {
	$('#speed').text(speed);
	$('#elevation').text(altitude);
	if(speed >= trajet_vitessemin_cap)
	{
		var cap_css = '0 -124px';
		if(cap >= 22 && cap < 67)
			cap_css = '0 -83px';
		else
		{
			if(cap >= 67 && cap < 112)
				cap_css = '0 -1px';
			else
			{
				if(cap >= 112 && cap < 157)
					cap_css = '0 -247px';
				else
				{
					if(cap >= 157 && cap < 202)
						cap_css = '0 -206px';
					else
					{
						if(cap >= 202 && cap < 247)
							cap_css = '0 -288px';
						else
						{
							if(cap >= 247 && cap < 292)
								cap_css = '0 -329px';
							else
							{
								if(cap >= 292 && cap < 337)
									cap_css = '0 -165px';
								else
									cap_css = '0 -42px';
							}
						}
					}
				}
			}
		}
		if(trajet_cap_current != cap_css)
		{
			$('#cap').css('background-position',cap_css);
			trajet_cap_current = cap_css;
		}
		$('#cap-text').text(cap);
	}
}
//this function lower the dialog
function website_dialog_lower(text) {
	if(text.length >= 30)
		text = text.substr(0,30) + '...';
	website_dialog_left = $('#dialog').css('left');
	$('#dialog_lower').hide();
	$('#dialog_lowered_text').text(text);
	$('#dialog_lowered').show();
	var new_left = $(window).width() - 150 - $('#dialog').width();
	$('#dialog').animate({left:new_left},'fast');
	website_dialog_lowered = true;
}
function website_dialog_lower_style(clicked) {
	if($(clicked).hasClass('track-thumb-size'))
	{
		// Add style to the clicked line
		$(clicked).parent().addClass('selectedline');
		website_dialog_lower($(clicked).next().text());
	}
	else
	{
		// Add style to the clicked line
		$(clicked).parent().parent().prev().addClass('selectedline');
		website_dialog_lower($(clicked).parent().parent().prev().children('TD:eq(1)').text());
	}
}
//this function upper the dialog
function website_dialog_upper() {
	$('#dialog_lowered').hide();
	$('#dialog_lower').show();
	$('#dialog').animate({left:website_dialog_left},'fast');
	website_dialog_lowered = false;
}
/*function website_close_tooltip_addtrack() {
	$('#tooltip-addtrack').fadeOut();
}*/
function website_trip_graph_check_show() {
	// If graph last view if true, show graph
	if(website_trip_graph_last_view)
	{
		website_trip_graph_show();
		website_trip_graph_check_draw();
		trip.trip_center();
	}
}
function website_trip_graph_hide() {
	if($('#trip-graph').css('display') != 'none')
	{
		website_trip_graph_last_view = true;
		$('#trip-graph').hide();
		$('#gmap').height($('#gmap').height() + 150);
		gmap_check_size();
		gmap_overview_show();
	}
	else
		website_trip_graph_last_view = false;
}
function website_trip_graph_show() {
	if($('#trip-graph').css('display') == 'none')
	{
		$('#gmap').height($('#gmap').height() - 150);
		$('#trip-graph').show();
		gmap_check_size();
		gmap_overview_hide();
	}
}
function website_trip_graph_check_draw() {
	if($('#trip-graph').css('display') != 'none')
		website_trip_graph_draw();
}
function website_trip_graph_draw() {
	$('#trip-graph').width($('#gmap').width() - 80);
	website_trip_graph_show();
	if(website_trip_graph)
	{
		website_trip_graph.unhighlight(0,website_trip_graph_highlight);
		website_trip_graph.unhighlight(1,website_trip_graph_highlight);
		website_trip_graph_highlight = null;
		website_trip_graph_previous = null;
	}
	var info = trip.return_info_graph();
	var data = [{
		data:info[0],
		label:_trip_graph_altitude,
		color:'#AFD8F8',
		yaxis:1,
		lines:{fill: true}
	},
	{
		data:info[1],
		label:_trip_graph_speed,
		color:'#EDC240',
		yaxis:2
	}];
	website_trip_graph_create(data);
}
function website_trip_graph_create(data) {
	// Make plot graph
	var options = {
		xaxis: { mode:null,tickDecimals: 0 },
		yaxis: { tickDecimals:0 },
		grid: { hoverable: true, clickable: true,color:'#FFFFFF',labelMargin:5 },
		legend: {backgroundColor:'#363636',noColumns:2},
		series: {lines:{lineWidth:1}}
	};
	website_trip_graph = $.plot($('#trip-graph'),data,options);
}
function website_trip_graph_update(sec) {
	// If graph view
	if($('#trip-graph').css('display') != 'none')
	{
		website_trip_graph.unhighlight(0,website_trip_graph_highlight);
		website_trip_graph.unhighlight(1,website_trip_graph_highlight);
		website_trip_graph_highlight = sec;
		website_trip_graph.highlight(0,website_trip_graph_highlight);
		website_trip_graph.highlight(1,website_trip_graph_highlight);
	}
}
function websiteManageAds(ads) {
	// If a ads are set
	if(ads[0] != 0)
	{
		$('#ads A:first').attr('href',ads[2]);
		$('#ads IMG:first').attr('src',ads[1]);
		if($('#ads').css('display') == 'none')
			$('#ads').show();
	}
	else
	{
		if($('#ads').css('display') != 'none')
			$('#ads').hide();
	}
}
function websiteTrackModify(bt) {
	// Show a confirm dialog
	if(confirm(website_areyousure+website_track_edit))
	{
		// disable submit bt
		website_toggle_state_submit(bt);
		website_loading_show();
		// Envoi du formulaire en post
		$.ajax({
			type: 'POST',
			dataType: 'json',
			data: ({trk_id: $('#trk_id').val(),
					trk_name: $('#trk_name').val(),
					trk_label: $('#trk_label').val(),
					trk_veh_id: $('#trk_veh_id').val(),
					trk_map_type: $('#trk_map_type').val(),
					trk_statut: $('#trk_statut').val()}),
			url: 'dialog-track-edit-edit.html',
			success: function(data) {
				// data[0] : [0] error boolean, [1] error message if error is true or next page or info, [2] error type or info for success
				// ---------
				// data[0][1] : number of result
				// data[0][2] : text
				if(data[0][0] == 0)
				{
					website_open_statebox(data[0][1],'success',false);
					setTimeout('website_close_statebox()',website_timeout_viewbox);
					website_close_dialog();
					//website_toggle_state_submit(bt);
					//profile_open_tab('profile-track',$('#profile-track'));
					//website_loading_hide();
				}
				else
				{
					var processing = '';
					if(data[0][2] == 'trk_name')
					{	
						processing += '$("#trk_name").focus();';
						//Traitement += '$("#mdp").css("border","solid 1px red");';
					}
					//var processing = track_edit_processing_error(data[0][2]);
					website_processing_error_form(data[0][1],bt,processing);
				}
			}
		});
	}
}
function websiteLoadTrackRelated() {
	// not in loading
	if(!trackRelatedLoad)
	{
		if(trip.view_current != null && trip.view_current != trackRelatedLastTrack)
		{
			trackRelatedLoad = true;
			if(trackRelatedLastTrack != 0 && trip.view_current != trackRelatedLastTrack)
				trackRelatedPage = 0;
			trackRelatedLastTrack = trip.view_current;
			// Show load
			$('#track-related-loading').show();
			$.ajax({
				type: 'POST',
				dataType: 'json',
				data: ({id: trip.view_current,
						page: trackRelatedPage,
						category: trackRelatedCategory}),
				url: 'dialog-track-related-load.html',
				success: function(data) {
					// State of bt previous next
					if(trackRelatedPage == 0)
						$('.track-related-page.previous').hide();	// Hide previous
					else
						$('.track-related-page.previous').show();	// Show previous
					// View next bt page
					if(data[1] == 1)
						$('.track-related-page.next').show();
					else
						$('.track-related-page.next').hide();
					$('#track-related-content').html(data[0]);
					// Hide load
					$('#track-related-loading').hide();
				},
				complete: function() {
					trackRelatedLoad = false;
				}
			});
		}
	}
}
function playTrackFromRelated(id) {
	// #### Stats ####
	try {
		_gaq.push(['_trackEvent', 'Track', 'PlayRelated', id]);
	}
	catch(e) {
		// Do nothing
	}
	// #### Stats ####
	trip.play(id);
}
function websiteSetSetting(bt) {
	// disable submit bt
	website_toggle_state_submit(bt);
	website_loading_show();
	// get the script for change the setting
	$.ajax({
		type: 'POST',
		data: ({unit_speed:$('input[type=radio][name=speed]:checked').val(),
			unit_length:$('input[type=radio][name=length]:checked').val(),
			unit_elevation:$('input[type=radio][name=elevation]:checked').val(),
			date_format:$('input[type=radio][name=date]:checked').val(),
			lang:$('input[type=radio][name=lang]:checked').val()}),
		url: 'dialog-setting-change.html',
		success: function() {
			// #### Stats ####
			try {
				//pageTracker._trackEvent('Website','Change Lang',new_lang);
				_gaq.push(['_trackEvent', 'Website', 'Change Setting']);
			}
			catch(e) {
				// Do nothing
			}
			// #### Stats ####
			//window.location.replace(website_homepage);
			window.location.reload();
		}
	});
}