﻿

pages = Array();
curent_page = Array(0, 0);
dest_page = Array(0, 0);
road = Array();
can_click = true;
reset_flag_pos = false;
stage_container = 'stage_container';
default_motion_speed_per_page = 250;
motion_speed = Array();
past_road = Array();
sstring_corection = 0;
load_to_stage_flag = Array();
loading_html_code = '<div class="loading_mask"><img src="/_images/loaderB64.gif" width="64" height="64" alt="Chargement" /></div>';
var is_loaded_timeout;
mini_map_fade_time = 0;




if (BrowserDetect.browser == "Explorer" & BrowserDetect.version <= 7) {
	sstring_corection = 11;
}




	function init_dw_Scroll() {
	  var wndo = new dw_scrollObj('stage_container', 'stage');
	  trace("INIT SCROLL COMPLETED", 'S');
	}
		
	
	
/*
	--------------------	BUILD ROAD TO DESTINATION	--------------------
*/		
	function road_builder(destination) {
		
		road = Array();
		can_click = false;
		
		//check where is the destinaotion
		for (var i = 0; i < pages.length ; i++) {
			for (var ii = 0; ii < pages[i].length ; ii++) {
				if (pages[i][ii] == destination) {
					dest_page[0] = i;
					dest_page[1] = ii;
				}	
			}
		}
		
		update_browser_url(pages[dest_page[0]][dest_page[1]]);
		highlight_menu(pages[dest_page[0]][dest_page[1]]);
		update_mini_map(pages[dest_page[0]][dest_page[1]], 'dest');
	
		if (curent_page[0] == dest_page[0] && curent_page[1] == dest_page[1]) {
			can_click = true;
		} else {
			// build the road to new page
			if (dest_page[0] != curent_page[0]) {
				
				if (curent_page[1] != 0) {
					for (x=curent_page[1]-1; x >= 0; x--) {
						road.push(Array(curent_page[0], x));
					}
					reset_flag_pos = true;
				}
				
				up_down = dest_page[0] - curent_page[0];
				if (up_down > 0) {
					for (x=curent_page[0]+1; x<=dest_page[0]; x++) {
						road.push(Array(x, 0));
					}
				} else {
					for (x=curent_page[0]-1; x >= dest_page[0]; x--) {
						road.push(Array(x, 0));
					}
				}
				
			} else if (dest_page[1] == 0) {
				for (x=curent_page[1]-1; x >= 0; x--) {
					road.push(Array(dest_page[0], x));
				}
			}
			
			if (dest_page[1] != 0) {
				
				if (reset_flag_pos) {
					curent_page[1] = 0;
					reset_flag_pos = false;
				}
				
				up_down = dest_page[1] - curent_page[1];
				if (up_down > 0) {
					for (x=curent_page[1]+1; x<=dest_page[1]; x++) {
						road.push(Array(dest_page[0], x));
					}
				} else {
					for (x=curent_page[1]-1; x >= dest_page[1]; x--) {
						road.push(Array(dest_page[0], x));
					}
				}
			}
			
			time_motion_speed();
		
			curent_page[0] = dest_page[0];
			curent_page[1] = dest_page[1];
			
			motion();
		}	
	}
	
	
	
	
	
/*
	--------------------	SET MOTION SPEED	--------------------
*/	

	function time_motion_speed() {
		for (var i = 0; i < road.length ; i++) {
			motion_speed.push(default_motion_speed_per_page);
		}
	}
	
	
	
/*
	--------------------	MOTION CALL/CALLBACK	--------------------
*/	
	function motion() {
		// LOAD AND UNLAD PAGE BEFORE WE GO TROUGHT THEM
		if (past_road.length > 1) {
			unload_pages(past_road[0]);
			past_road.shift();
		}
		
		if (pages[road[0][0]][road[0][1]]) {
			load_pages(pages[road[0][0]][road[0][1]]);
		}

		motion_request(pages[road[0][0]][road[0][1]], motion_speed[0]);
		update_mini_map(pages[road[0][0]][road[0][1]], '');

		past_road.push(pages[road[0][0]][road[0][1]]);
		road.shift();
		motion_speed.shift();
	}
	
	function motion_request(where, speed) {
		if (typeof(speed) == 'undefined') {
			dw_scrollObj.defaultSlideDur = dw_scrollObj.prototype.slideDur = default_motion_speed;
		} else {
			dw_scrollObj.defaultSlideDur = dw_scrollObj.prototype.slideDur = speed;
		}
		dw_scrollObj.scrollToId(stage_container, where , '', '');
	}
	
	function motion_end() {
		if (road.length) {
			motion();
		} else {
			can_click = true;
			trace('motion Completed', 'S');
			

			id = pages[curent_page[0]][curent_page[1]];
		
			if ($('#' + id).hasClass('content_loaded')) {
				//content is in place an ready to input	
			} else {
				if(load_to_stage_flag[id]) {
					//waiting content -  but no worries, another callback will place it on the stage
				} else {
					//content is on in place and the loading screen is still showing, start timer to remove it once loaded
					trace('waiting content - Act on it ...', 'fF0');
					is_loaded_timeout = setTimeout(load_on_time,500);
				}
			}
		}
	}

	function motion_on_init(destination) {
		can_click = false;
		//check where is the destination exist
		is_valid_dest = false;
		for (var i = 0; i < pages.length ; i++) {
			for (var ii = 0; ii < pages[i].length ; ii++) {
				if (pages[i][ii] == destination) {
					dest_page[0] = i;
					dest_page[1] = ii;
					is_valid_dest = true;
				}	
			}
		}
	
		if (is_valid_dest) {
			update_mini_map(pages[dest_page[0]][dest_page[1]], 'dest');
			curent_page[0] = dest_page[0];
			curent_page[1] = dest_page[1];
			motion_request(destination, 0);
			trace('MOTION INIT TO: ' + destination,'S')
			load_content(destination, true);
			update_browser_url(pages[dest_page[0]][dest_page[1]]);
			highlight_menu(pages[dest_page[0]][dest_page[1]]);
		} else {
			can_click = true;
			trace('MOTION INIT: ' + destination + ' is not a valid destination','E')	
		}
		$('#menu').css('display','block');
		
	}


	

	
// -- INTI FUCNTION TO  SETP UP ALL IN IT"S PLACE	
	
	function build_map() {
		// -- collect all page on the stage into 2D Array; Section=>Page
		$("#stage .stage_row").each(function(i){
			s = Array();
			sid = this.id;
			$('.stage_frame', this).each(function(ii){
				s.push(this.id);
			});
			pages.push(s);	
		 });
		build_mini_map();
	} 



// -- BUILD MINI MAPS AND LOAD SITE CONTENT INTO IT

	function build_mini_map() {
		result = '';
		for(x=0; x < pages.length; x++) { 
			result += '<div id="mini_map_r' + x + '" class="mini_map_row" >';
			display_name = $('#' + pages[x][0]).parent().attr('title');
			result += '<div class="mini_map_frame_title" >' + display_name + '</div><div class="clear_all">&nbsp;</div>';
			for(y=0; y < pages[x].length; y++) {
				
				
				display_name = $('#' + pages[x][y]).attr('title');
				result += '<div id="mini_map_' + pages[x][y] + '" class="mini_map_frame" title="' + display_name + '" >';
				result += display_name;
				result += '<div id="mini_map_' + pages[x][y] + '_c" class="mini_map_frame_content" >' + loading_html_code + '</div></div>';
				
				
			}
			result += '<div class="clear_all">&nbsp;</div></div>';
		}

		$('#mini_map').html(result);
		
		$('#mini_map div.mini_map_frame').each(function(iii){	
			// -- LOAD CONTENT OF THE SITE IN HIDDEN DIV INSIDE THE MINIMAP
			id_to_get = '' + $(this).attr('id').substring(9);
			load_content(id_to_get, false);
		});

		$('#mini_map div.mini_map_frame').each(function(iii){
			$(this).click(function () {
				if (can_click) {
					id_to_get = '' + $(this).attr('id').substring(9);
					$('#mini_map_box').fadeOut(mini_map_fade_time, function() {
						road_builder(id_to_get);													
					});
				} else {
					trace('No Click yet', 'E');
				}
			});
		 });
		
		place_mini_map();
	}
	
	first_mini_map_replace = true;
	
	function place_mini_map() {
		l = ($(window).width() - $('#mini_map').width())/2;
		t = ($(window).height() - $('#mini_map').height())/2;
		$('#mini_map_box').css('width', $(window).width() + 'px');
		$('#mini_map_box').css('height', $(window).height() + 'px');
		$('#mini_map').css('left', l + 'px');
		$('#mini_map').css('top', t + 'px');
		
		if (BrowserDetect.browser == "Explorer" & BrowserDetect.version <= 6) {
			//alert('is ie 6 or lower !!' + $('#mini_map_box').height())
			//$('#mini_map_box, #container').fixPNG();
		}
		
		if (first_mini_map_replace) {
			$('#mini_map_box').fadeOut(0);
			$('#mini_map_box').css('visibility', 'visible');
			first_mini_map_replace = false;
		}
	}

	$(document).ready( function() {
								
		$('#site_map_btn').click( function(){
			$('#mini_map_box').fadeIn(mini_map_fade_time);
			return false;
		});
		
		$('#mini_map_box').click( function(){
			$('#mini_map_box').fadeOut(mini_map_fade_time);
		});

		$('#header_logo_btn').click( function(){
			road_builder(176);
			return false;
		});

		$(window).bind('resize', function() {
			place_mini_map();
		});

				
	});
	

// -- UPDATE VARIOUS STATE FOR THE MINIMAP
	
	function update_mini_map(what, how) {
		
		switch(how) {
			case 'dest':
				$('.destination').removeClass('destination');
				$('#mini_map_' +  what).addClass('destination');
			break;
			case 'loaded':
				$('#mini_map_' +  what).addClass('loaded');
			break;
			default:
				$('.selected').removeClass('selected');
				$('#mini_map_' +  what).addClass('selected');			
			break;
		}
	}
	
	


// -- FETCH CONTENT FROM THE HIDDEN DIV IN THE MINIMAD AN PLACE IT ON THE STAGE WHEN NEEDED (OR REMOVE IT)

function load_pages(what) {
	aa = $('#mini_map_' +  what).children('.mini_map_frame_content').html();
	// (?<=<!--\|TITLE\|).*?(?=\|TITLE\|-->)
 	regexMatch = aa.match(/<!--\|TITLE\|.*?\|TITLE\|-->/);
 	var title = '';
	if(regexMatch)
	{
		for(i=0;i<regexMatch.length;++i)
		{
			title = regexMatch[i].replace("<!--|TITLE|","")
			title = title.replace("|TITLE|-->","")
		}
	}
	document.title = title;
	$('#'+what).html(aa);
	replace_a_click()
	replace_arrow_click();	
}

function unload_pages(what) {
	$('#'+what).html("");
}

function load_content(what, to_stage) {
	if (to_stage) {
		load_to_stage_flag[what] = true;
	}

	$('#mini_map_' +  what).children('.mini_map_frame_content').load('/client/get_page.asp?page=' + what + '&r='+ make_random(), '' , function() {
		if (to_stage) {
			load_pages(what);
		}
		$('#' + what).addClass('content_loaded');
		update_mini_map($(this).parent().attr('id').substring(9), 'loaded');																								  
	});
}

function make_random() {
	return Math.random()*1000;
}
function load_on_time() {
	id = pages[curent_page[0]][curent_page[1]];
	
	if ($('#' + id).hasClass('content_loaded')) {
		load_pages(id);
	} else {
		is_loaded_timeout = setTimeout(load_on_time,100);	
	}
}



// -- REPLACE CLICK OF THE MENU BY CUSTUM JQUERY EVENT
function replace_menu_click() {
	
// - MENU LINKS
	$('.menu a').each(function(i) {
		$(this).click(function() {
			if (can_click) {
				road_builder($(this).attr('rel'));
			} else {
				trace('No Click yet', 'E');
			}
			return false;					
		});	
		
		
		
	});
	
// - SOUS MENU LINKS
	$('.sous_menu a').each(function(i) {
		$(this).click(function() {
			if (can_click) {
				road_builder($(this).attr('rel'));
			} else {
				trace('No Click yet', 'E');
			}
			return false;					
		});								 
	});
}

// - OTHER MARKED LINKS <a class="link"></a>
function replace_a_click() {
	// - OTHER MARKED LINKS
	$('a.link').each(function(i) {
		$(this).click(function() {
			if (can_click) {
				
				id = $(this).attr('href').substring($(this).attr('href').lastIndexOf('page=')+5).substring(0, $(this).attr('href').substring($(this).attr('href').lastIndexOf('page=')+5).indexOf('&'));
				// - NO LINK FOUND IN URL, TRY TO AJAX IT FROM THE BD
				if (id == "") {

					url_link = $(this).attr('href').substring($(this).attr('href').lastIndexOf('.com/')+sstring_corection);
					trace('No ID Found; Chencking in logical URL DB FOR: ' + url_link , 'W');
				
					$.post("/client/get_url.asp?rand=" + Math.random(), { url: url_link }, function(data){
						trace("Data Loaded: " + data, 'S');
						if(data == -1 ) {
							//window.open($(this).attr('href'), '_blank'); 
						} else {
							// - WE GOT ID, MOVE TO IT
							road_builder(data);
						}
					 });
				} else {
					road_builder(id);
				}
			} else {
				trace('No Click yet', 'E');
			}
			return false;					
		});								 
	});
	

	
	$('a.popup').each(function(i) {
							  
		/* TODO
			Metre function pour faire ouvrir les popup sur les a qui on la classe popup
		*/						  
							  
	});
	
	
	
}

function replace_arrow_click() {
	id = pages[curent_page[0]][curent_page[1]];
	
	next_id = $('#'+id).next().attr('id');
	prev_id = $('#'+id).prev().attr('id');
	show_next_nav = $('.sous_menu [rel=' + next_id + '] ').hasClass('hide_me');
	show_prev_nav = $('.sous_menu [rel=' + prev_id + '] ').hasClass('hide_me');
	
	if (show_next_nav) {
		$('#'+id+' .real_arrow_right_box a').css('display','block');
		$('#'+id+' .real_arrow_right_box a').click(function() {
			road_builder(next_id);
			return false;
		});
	} else {		
		$('#'+id+' .real_arrow_right_box a').remove();
	}

	if (show_prev_nav) {
		$('#'+id+' .real_arrow_left_box a').css('display','block');
		$('#'+id+' .real_arrow_left_box a').click(function() {
			road_builder(prev_id);
			return false;
		});	
	} else {
		$('#'+id+' .real_arrow_left_box a').remove();
	}
}





function update_browser_url(what) {
	location.hash = 'p' + what;
}

ss_menu_open = '';
