// global variables
var jData;

// map related
var mapDuration = 3000;
var tagCounter = 0;
var mapTimeOut;
var testimonialDuration = 14000;
var hovering = false;



//	Last Modified:	Monday, March 9, 2009 2:14:48 PM
//	added switch for dev/live server directories
var dev = false;
if (!dev){
	var workingDirectory = "testimonials/";
}else{
	var workingDirectory = "";
}


// testimonial related
var defTestimonial = 0;
var testimonialLoaded = false;


//	This function loads when the document is ready to be traversed
$(document).ready(function(){
			//	load JSON data, begin animation
			$.getJSON( workingDirectory + "contents.txt", function(data){
			
			for (var i=0;i<data.testimonials.length;i++) 
			{
			    data.testimonials[i].quoteEN = "&ldquo;" + data.testimonials[i].quoteEN + "&rdquo;"
				if(data.testimonials[i].quoteNL !=""){
					data.testimonials[i].quoteNL = "&ldquo;" + data.testimonials[i].quoteNL + "&rdquo;"
				}
			}
			jData = data;
				$("#resumelink").hover(function(){
					hovering = true;
					$("#resumelink a").animate({ 
					top: "175px"
				      }, 200 );
					 $("#screen").animate({ 
						opacity: 0.2
				      }, 200 );
				},
				function(){
						hovering = false;
						$("#resumelink a").animate({ 
						top: "195px",
						opacity: 1
				      }, 200 );
					   $("#screen").animate({ 
						opacity: 0
				      }, 200 );
					  
					  
				});
				$("#screen").animate({ 
						opacity: 0.0
				      }, 200 );
				
				init();
			});
			
			var browser=navigator.appName;
			var b_version=navigator.appVersion;
			var version=parseFloat(b_version);
			if (!(browser=="Microsoft Internet Explorer")){
				$("#screen").css({backgroundColor: "#000"});
			}
			
			
	});		// end of document ready
	
//	Initial animation
function init(){
	// alert (jData.testimonials.length);
	// load the map
	$.preload([ workingDirectory + "images/map.jpg" ], {
			onFinish:beginMap
	});
}

//	Display map, start animations
function beginMap(){
	$("#testimonials").empty().append("<div id =\"map\"></div>");
	$("#map").hide().empty().css("background-image","url(" + workingDirectory + "images/map.jpg)");
	//displayTestimonial(defTestimonial);
	$("#map").fadeIn("slow",function(){
		
			$("#resumelink a").animate({ 
						top: "175px"
				      }, 200 );
		// delay mapDuration milliseconds, then begin the animations
		
		$("#map").append("<h2 id=\"tagline\"></h2>");
		
		if(tagCounter >= jData.quotes.length - 1){
			tagCounter = 0;
		}
		$("#tagline").hide().append(jData.quotes[tagCounter].text).fadeIn("slow",function(){
		});
		tagCounter++;
		mapTimeOut = setTimeout(function(){
			displayTestimonial(defTestimonial)
						if (hovering == false){
							$("#resumelink a").animate({ 
							top: "195px"
					      }, 200 );
						}
						
		},mapDuration);
	});
}

//	Display a Testimonial
function displayTestimonial(id){
		//	make  sure that nothing can be clicked, because we've already started loading the next testimonial...
		//$(".beacon").click(function(){});
		
		//	preload testimonial image, then on with the show
		$.preload([ jData.testimonials[id].imgURL, jData.testimonials[id].NLimgURL  ], {
				onFinish:function(){
					$("#map img").fadeOut("def");
					$("#map").fadeOut("slow",function(){
						$("#map").empty().css({"background-image":"url(" + workingDirectory +"images/"+ jData.testimonials[id].imgURL + ")"}, $(this).fadeIn("slow", function(){
							$("#map").append("<blockquote id=\"quote\"></blockquote>");
							$("#map").append("<div id=\"name\"></div>");
							
							if( jData.testimonials[id].NLimgURL != "" && jData.testimonials[id].NLimgURL != "Unknown"){
								jData.testimonials[id].quoteNL = "<img src=\"" + workingDirectory + "images/" + jData.testimonials[id].NLimgURL + "\" alt=\"quote\" style=\"display: inline-block;\"/>";
							}
							if((jData.testimonials[id].quoteNL == "")&&(jData.testimonials[id].quoteNL != jData.testimonials[id].quoteEN)){
								jData.testimonials[id].quoteNL = jData.testimonials[id].quoteEN;
							}
							$("#quote").hide().append(jData.testimonials[id].quoteNL).fadeIn("slow",function(){
								$("#name").hide().append("<p>&#8211; "+jData.testimonials[id].name+" "+ jData.testimonials[id].minitial +" " + jData.testimonials[id].surname + " <em>&#124;</em> " + jData.testimonials[id].title + " <em>&#124;</em> " +jData.testimonials[id].city+ " <em>&#124;</em> <b>"+jData.testimonials[id].country+"</b></p>").fadeIn("slow",function(){
									
									$("#name p").append("<span id=\"story\"></span>");
									nlTextTime = setTimeout(function(){
										clearTimeout(nlTextTime);
										if(jData.testimonials[id].quoteNL != jData.testimonials[id].quoteEN){
										$("#quote").fadeOut("slow", function(){
											$("#quote").hide().empty().append(jData.testimonials[id].quoteEN).fadeIn("slow");
										});
										}
									}, testimonialDuration / 2 );
									//$("#story").hide().append("<em>&raquo;</em> <a href=\"#\">" + jData.testimonials[id].name + "'s Success Story</a>").fadeIn(1000,function(){
									$("#story").hide().fadeIn(1,function(){
										// after x seconds, go back to map
										//if(id <= jData.testimonials.length-2){
										//	defTestimonial++;
										//}else{
										//	defTestimonial = 0;
										//}
										setTimeout(function(){
											// ie fix....
											$("#quote").fadeOut("slow");
											$("#name").fadeOut("slow");
											$("#tagline").fadeOut("slow");
											// /ie fix...
											$("#map").fadeOut("slow", function(){
												//	init();
												//	Last Modified:	Monday, March 9, 2009 10:41:50 AM
												/*	addition: 
													modify when taglines are displayed, 
													only display them after the last quote has displayed
													
												*/
												if(id <= jData.testimonials.length - 2){
													displayTestimonial(id + 1);		
												}else{
													defTestimonial = 0;
													beginMap();		
												}
												
												//	Last Modified:	Monday, March 9, 2009 11:31:35 AM
											});
										},testimonialDuration);
									});
								});
							});
						}))
					});
				}
		});
}
