basicContentVideo = {
	init: function(){
		/* optional SWFObject param objects */
		var flashvars = {};
		var params = {};
		var attributes = {
			wmode: 						"opaque"
		};

		swfobject.embedSWF(invjs.jspath+"flash/videoTest.swf", "contentVideo", "514", "267", "9.0.0",invjs.jspath+"flash/expressInstall.swf", flashvars, params, attributes);
	}
}

officeLoc = {
	// xmlPath: getXmlConfigurationPath(), // use same XML file for both flash and ajax
	init: function(){
		$("form#searchLocations").submit(function(){
			return false;
		});

		$("div.result").hide();

		$("#searchAreaButton").click(function(){
			var selReg = $("#area").val();
			if(selReg != "none"){
				officeLoc.regionFromHTML(selReg);
			}
		});

		$("#searchOfficeButton").click(function(){
			var selOff = $("#office").val();
			if(selOff != "none"){
				officeLoc.writeOfficeHTML(selOff);
				officeLoc.officeFromHTML(selOff);
			}
		});
	
		officeLoc.popAreas();

		$("#area").change(function(){
			var selReg = $(this).val();

			if(selReg != "none"){
				officeLoc.popOffices(selReg);
			} else {
				$("select#office").empty();
				$("select#office").append('<option value="none">-- Select an Office --</option>');
				$("select#office").attr("disabled", "disabled");
			}
		});

		
		// swfobject 
		var flashvars = {
			assetPath: 					getAssetPath(), 
			xmlPath: 					getXmlConfigurationPath(),
			apiKeyPath:					getApiKeyPath()
		};
		var params = {
			allowScriptAccess: 			"sameDomain",
			allowFullScreen: 			"false",
			quality: 					"high",
			wmode: 						"opaque"
		};
		var attributes = {};

		swfobject.embedSWF(getShellPath(), "mapHolder", "512", "364", "9.0.0",getExpressInstallPath(), flashvars, params, attributes);
		
	},
// END: this part is moved to "/apps/investec/components/officeLocator/officeLocator.jsp"
	popAreas: function(){
		$.ajax({
			type: 		"GET",
			url: 		getXmlConfigurationPath(),
			dataType: 	"xml",
			success: 	function(xml){
				$("select#area").empty();
				$("select#area").append('<option value="none">-- Select a Region --</option>');
				$(xml).find('regions region').each(function(){
					var region_id = $(this).attr("id");
					var region_text = $(this).children("name").text();

					$('<option></option>').html(region_text).attr("value", region_id).appendTo("select#area");
				});
			}
		});
	},
	popOffices: function(selReg){
		$.ajax({
			type: 		"GET",
			url: 		getXmlConfigurationPath(),
			dataType: 	"xml",
			success: 	function(xml){
				$("select#office").empty();
				$("select#office").append('<option value="none">-- Select an Office --</option>');
				$(xml).find('region#'+selReg+' offices office').each(function(){
					var office_id = $(this).attr("id");
					var office_text = $(this).find("city").text();
					$('<option></option>').html(office_text).attr("value", office_id).appendTo("select#office");
				});
				$("select#office").attr("disabled", null);
			}
		});
	},
	writeOfficeHTML: function(officeId){
		$.ajax({
			type: 		"GET",
			url: 		getXmlConfigurationPath(),
			dataType: 	"xml",
			success: 	function(xml){
				var wOff = $(xml).find('office#'+officeId);

				var officeName = $(wOff).find("city").text();
				var businessUnit = $(wOff).find("address").find("businessUnit").text();
				if (businessUnit){
					officeName += '<br /><span class="businessUnit">' + businessUnit + '</span>';
				}

				$(".officeDetails h4").html( officeName );
				$(".officeDetails p.address").html( $(wOff).find("address").find("street").text() + "<br />" + $(wOff).find("address").find("postcode").text() + "<br />" + $(wOff).find("address").find("country").text() );
				$(".officeDetails p.contact").html( $(wOff).find("contact").find("phone").text() + "<br /><br />" + $(wOff).find("contact").find("fax").text()/*  + "<br /><br />"+ $(wOff).find("contact").find("email").text() */);

				var postalAddress = $(wOff).find("postalAddress").text();
				$(".officeDetails p.postalAddress").remove();
				if(postalAddress){
					$(".officeDetails").append('<p class="postalAddress">Postal Address:<br />'+postalAddress+'</p>');
				}
				
				var pdfFileLoc = $(wOff).find("pdfFile").text();
				$(".officeDetails p.pdf").remove();
				if(pdfFileLoc){
					$(".officeDetails").append('<p class="pdf"><a href="'+pdfFileLoc+'">'+ getDownloadMessage() +'</a></p>');
				}

				var emailMeAddress = $(wOff).find("mainEmail").text();

				$("a.emailMeOffice").remove();
				var emailMeButton =  $(wOff).find("emailMeButton");				
				if(emailMeButton){
					// id = toName
					$(".officeDetails").append('<a class="emailMeLink emailMeOffice" rel="Office='+officeId+'" href="#" emaildata="'+ emailMeAddress +'" data="office:'+wOff.attr('handle')+'" ><img src="'+ getEmailMeButton() +'" alt="Email me" /></a>');
				}
				
				$("a.callBackOffice").remove();
				var callBackButton =  $(wOff).find("callBackButton");
				if(callBackButton){
					// id = toName
					$(".officeDetails").append('<a class="requestCallbackLink callBackOffice" rel="Office='+officeId+'" href="#" emaildata="'+ emailMeAddress +'" data="office:'+wOff.attr('handle')+'" ><img src="'+ getRequestCallbackButton() +'" alt="Request a call back" /></a>');
				}

				// INVJS path here was used - should be?
				$("img#officeImage").attr("src", "" + $(wOff).find("image").text() ).attr("alt", officeName );

				$(".result").show();

				invjs.setupPopupForms();
			}
		});
	},
	printFromFlash: function(){
		window.print();
	},
	thisMovie: function(movieName) {
		if (navigator.appName.indexOf("Microsoft") != -1) {
	        return window[movieName];
	    } else {
	        return document[movieName];
	    }
	},
	regionFromHTML: function(value){
		officeLoc.thisMovie("mapHolder").regionFromHTML(value);
	},
	officeFromHTML: function(value){
		officeLoc.thisMovie("mapHolder").officeFromHTML(value);
	}
}

challengeNewsList = {
	init: function(){
		$("div#tombstoneAccordion").accordion({
			header: 		"div.tsHeader",
			active: 		false,
			alwaysOpen: 	false,
			clearStyle: 	true,
			autoHeight: 	false,
			selectedClass: 	"opened"
		});
		
		$(".tsClose span").click(function(){
			$("div#tombstoneAccordion").accordion("activate", -1);
			return false;
		});
		
		/* -> INVESTECAFTERCARE-59 */
		$(".tsCopy a.readMore").each(function() {
			var prev = $(this).prev();
			
			/* find first not empty tag/paragraph with text where we inject "read more" */
			var body = $.trim($(prev).text());
			// dont ask me why '1', ie returns text length as '1' even was empty after trimming 
			if (body.length <= 1) {
				prev = $(prev).prev();
			}
			
			/* append ... to last paragraph */
			var elem = $(prev).get(0);
		
			if (elem) {
				elem.innerHTML = elem.innerHTML + '...&nbsp;';
			}
			$(this).remove().appendTo($(prev));

			// we've unbound ajaxify with this remove(),
			// so we need to strike with ajaxify again
			if (invjs.elegance) {
				$(this).unbind("click").click(function(){
					invjs.urlHash(this.href);
					this.blur();
					return false;	
				});
			}
		});

	}
}

pressReleases = {
	init: function(){
		$("div#tombstoneAccordion").accordion({
			header: 		"div.tsHeader",
			active: 		false,
			alwaysOpen: 	false,
			clearStyle: 	true,
			autoHeight: 	false,
			selectedClass: 	"opened"
		});
		
		$(".tsClose span").click(function(){
			$("div#tombstoneAccordion").accordion("activate", -1);
			return false;
		});
		
		/* -> INVESTECAFTERCARE-59 */
		$(".tsCopy a.readMore").each(function() {
			var prev = $(this).prev();
			
			/* find first not empty tag/paragraph with text where we inject "read more" */
			var body = $.trim($(prev).text());
			// dont ask me why '1', ie returns text length as '1' even was empty after trimming 
			if (body.length <= 1) {
				prev = $(prev).prev();
			}
			
			/* append ... to last paragraph */
			var elem = $(prev).get(0);
			if (elem) {
				// dreaded IE cannot mess with span's innerHTML, don't ask...
				var hasSpan = Boolean($(elem).children('span:first').length);
				if (hasSpan) {
					elem = $(elem).children('span:first');
				}
				elem.innerHTML = elem.innerHTML + '...&nbsp;';
			}
			$(this).remove().appendTo($(prev));

			// we've unbound ajaxify with this remove(),
			// so we need to strike with ajaxify again
			if (invjs.elegance) {
				$(this).unbind("click").click(function(){
					invjs.urlHash(this.href);
					this.blur();
					return false;	
				});
			}
		});

	}
}

singleVideo = {
	init: function(){
		/* optional SWFObject param objects */
		var flashvars = {};
		var params = {};
		var attributes = {
			wmode: 		"opaque"
		};

		swfobject.embedSWF("flash/videoTest.swf", "singleVideo", "514", "267", "9.0.0","swf/expressInstall.swf", flashvars, params, attributes);
	}
}

sitemap = {
	init: function(){
		$("div#sitemapAccordion").accordion({
			header: 		"div.smHeader",
			active: 		false,
			alwaysOpen: 	false,
			clearStyle: 	true,
			autoHeight: 	false,
			selectedClass: 	"opened"
		});
		
		$(".smClose span").click(function(){
			$("div#sitemapAccordion").accordion("activate", -1);
			return false;
		});
		
		
		$("ul.sitemapTree").treeview({
			animated: "fast",
			collapsed: true
		});
	}
}

tombstone = {
	init: function(){
		$("div#tombstoneAccordion").accordion({
			header: 		"div.tsHeader",
			active: 		false,
			alwaysOpen: 	false,
			clearStyle: 	true,
			autoHeight: 	false,
			selectedClass: 	"opened"
		});
		
		$(".tsClose span").click(function(){
			$("div#tombstoneAccordion").accordion("activate", -1);
			return false;
		});
	
		/* -> INVESTECAFTERCARE-59 */
		$(".tsCopy a.readMore").each(function() {
			
			var prev = $(this).prev();
			var body = $.trim($(prev).text());
			// dont ask me why '1', ie returns text length as '1' even was empty after trimming 
			if (body.length <= 1) {
				prev = $(prev).prev();
			}
			
			/*var prev = $(this);
			do {
				var body = $.trim($(prev).text());
				prev = $(prev).prev();
			} while (body.length <= 1)*/
			
			/* append ... to last paragraph */
			var elem = $(prev).get(0);
			if (elem) {
				elem.innerHTML = elem.innerHTML + '...&nbsp;';
			}
			$(this).remove().appendTo($(prev));

			// we've unbound ajaxify with this remove(),
			// so we need to strike with ajaxify again

			if (invjs.elegance) {
				$(this).unbind("click").click(function(){
					invjs.urlHash(this.href);
					this.blur();
					return false;	
				});
			}

		});

	}
}

challengeSquadProfileList = {
		init: function(){
			
			$("div#tombstoneAccordion").accordion({
				header: 		"div.tsHeader",
				active: 		false,
				alwaysOpen: 	false,
				clearStyle: 	true,
				autoHeight: 	false,
				selectedClass: 	"opened"
			});
			
			$(".tsClose span").click(function(){
				$("div#tombstoneAccordion").accordion("activate", -1);
				return false;
			});

		
			/* -> INVESTECAFTERCARE-59 */
			$(".tsCopy a.readMore").each(function() {
				
				var prev = $(this).prev();
				var body = $.trim($(prev).text());
				// dont ask me why '1', ie returns text length as '1' even was empty after trimming 
				if (body.length <= 1) {
					prev = $(prev).prev();
				}
				
				/*var prev = $(this);
				do {
					var body = $.trim($(prev).text());
					prev = $(prev).prev();
				} while (body.length <= 1)*/
				
				/* append ... to last paragraph */
				var elem = $(prev).get(0);
				if (elem) {
					elem.innerHTML = elem.innerHTML + '...&nbsp;';
				}
				/* -> INVESTECAFTERCARE-1000 */
				//$(this).remove().appendTo($(prev));

				// we've unbound ajaxify with this remove(),
				// so we need to strike with ajaxify again
				
				if (invjs.elegance) {
					$(this).unbind("click").click(function(){
						invjs.urlHash(this.href);
						this.blur();
						return false;	
					});
				}

			});

		}
	}

gallery = {
	currPic: 0,
	numPics: null,
	init: function(){
		gallery.numPics = $("div#gallery ul a.pic").length;
		$("div#gallery ul a.pic").click(function(){
			gallery.showPic( $("div#gallery ul a.pic").index(this) );
			this.blur();
			return false;
		});
		
		$("div#gallery ul li.next").hover(function(){
			$(this).toggleClass("over");
		}, function(){
			$(this).toggleClass("over");
		}).click(function(){
			gallery.nudgePic(1);
		});
		$("div#gallery ul li.previous").hover(function(){
			$(this).toggleClass("over");
		}, function(){
			$(this).toggleClass("over");
		}).click(function(){
			gallery.nudgePic(-1);
		});
		
		// get all images and hide all above 10 count
		var arr = $("div#gallery ul a.pic");
		for (var i= 10; i< arr.length; i++){
			$(arr.get(i)).parent().hide();
		}
		
	},
	showPic: function(wPic){
		var wSrc = $("div#gallery ul a.pic:eq("+wPic+")").attr("href");
		var wText = $("div#gallery ul a.pic:eq("+wPic+")").attr("title");
		var wAlt = $("div#gallery ul a.pic:eq("+wPic+")").attr("alt");
		
		
		if(wPic != gallery.currPic){
			gallery.currPic = wPic;
			$("div#gallery img.galleryImg").attr("src", wSrc );
			$("div#gallery img.galleryImg").attr("alt", wAlt );
			$("p#imageTitle").text(wText);
			$("div#gallery ul a").removeClass("selected");
			$("div#gallery ul a.pic:eq("+wPic+")").addClass("selected");
		}
		if (gallery.currPic > 9) {
			if (gallery.currPic % 10 == 9 ) {
				gallery.updateVisible(gallery.currPic,false);
			}
		} else {
			if (gallery.currPic % 9 == 0 ) {
				gallery.updateVisible(gallery.currPic,false);
			}
		}
	},
	nudgePic: function(wAdj){
		if(((gallery.currPic + wAdj) < gallery.numPics) && ((gallery.currPic + wAdj) >= 0)){
			gallery.showPic(gallery.currPic + wAdj);
		}
		if (wAdj == -1) {
			//previous was clicked and we are at the range of visible elements
			if (gallery.currPic > 9) {
				if (gallery.currPic % 9 == 0 ) {
					gallery.updateVisible(gallery.currPic, true);
				}
			}
			else {
				if (gallery.currPic % 8 == 0 ) {
					gallery.updateVisible(gallery.currPic, true);
				}
			}

		}
	},
	updateVisible: function(index, isPrev){
		var arr = $("div#gallery ul a.pic");
		if (index == 0 || index == arr.length-1) { return; }
		
		//setting loop bounds
		if (!isPrev){
			//triggers when next or x10 item is clicked
			var begin = index;
			// either next 10 items or till end of array
			var end = index+11 > arr.length ? arr.length : index+11;
		} else {
			//triggers when prev is clicked
			if (index > 9) {
				var begin = index-9;
				var end = index+2;
			}
			else {
				var begin = index-8;
				var end = index+2;
			}
		}
	
		arr.each(function(){$(this).parent().hide();});
		for(var i = begin; i < end; i++) {
			$(arr.get(i)).parent().show();
		}
	}
}




// unfortunately, the office locator swf is calling old JS functions that have now been changed into methods of the officeLoc object.
// so I need to put in functions that call those methods for compatibility.
function writeOfficeHTML(officeId){
	officeLoc.writeOfficeHTML(officeId);
}
function printFromFlash(){
	officeLoc.printFromFlash();
}





