if(typeof $ != 'undefined') {
	$(document).ready(function() {
		// Detect and rig carousel
		if($.fn.carousel){$("div#carousel").carousel();}

		// Detect and attach colorbox
		$(".lightbox_kpi").colorbox({opacity:0.65, transition:'elastic', speed:250, innerWidth:600, innerHeight:500, iframe:true});
		$(".lightbox_iframe").colorbox({opacity:0.65, transition:'elastic', speed:250, innerWidth:490, innerHeight:340, iframe:true});
		$(".lightbox_noframe").colorbox({opacity:0.65, transition:'elastic', speed:250, width:500, height:320, iframe:false});
		
		$(".lightbox_preview").colorbox({	opacity:0.65,
											transition:'elastic',
											speed:250,
											iframe:false,
											initialWidth:0,
											initialHeight:0,
											scrolling:false,
											html:function(){
												// Assumes there is a <filename>_preview.<ext> version of the file you are previewing.
												var href = $(this).attr('href');
												var domain = document.location.protocol + '//' + document.location.hostname;
												var temp = extractNameExt(href);
												var filename = temp.filename;
												var ext = temp.ext;

												// Request the header of the img file on the server - this tells us the file size
												var request;
												request = $.ajax({
													type: "HEAD",
													url: domain + filename + ext,
													success: function () {
														var filesize = -1;
														filesize =  request.getResponseHeader("Content-Length");
														if(filesize > 0) {
															var filesizeKB = filesize/1024;
															$('#_preview_download_link').text('Download File (' + filesizeKB + 'kB)');
														}
													}
												});

												var htmlStr = '<div id="preview_container"><a target="_blank" href="' + filename + ext + '"><img id="_preview_img" src="' + filename + '_preview' + ext + '" /></a><p><a id="_preview_download_link" target="_blank" href="' + filename + ext + '">Download File</a></p></div>';
												return htmlStr;
											}
		});
		
		//	ON CLICK EVENTS FOR INSIDE VIEW
		
			//	Remove on-state and set H1
			$("#insideview_ri").click(function(){		$("#horiz-nav li").removeClass("on");	$("h1").html("Rentokil Initial");	});
			$("#insideview_rentokil").click(function(){	$("#horiz-nav li").removeClass("on");	$("h1").html("Rentokil");			});
			$("#insideview_initial").click(function(){	$("#horiz-nav li").removeClass("on");	$("h1").html("Initial");			});
			$("#insideview_citylink").click(function(){	$("#horiz-nav li").removeClass("on");	$("h1").html("City Link");			});
			$("#insideview_ambius").click(function(){	$("#horiz-nav li").removeClass("on");	$("h1").html("Ambius");				});
			
			//	Remove on-state and set H1 to be select value
			$("#insideview_select").click(function(){	
				$("#horiz-nav li").removeClass("on");	
				$("h1").html("Videos from "+$("#insideview_select_criteria").val());	
			});
			//	Remove on-state and set H1 to be submitted value
			$("#insideview_search").click(function(){	
				$("#horiz-nav li").removeClass("on");	
				$("h1").html("Results for '"+$("#insideview_search_criteria").val()+"'");	
			});
	});
	$(window).load(function(){

		if (false === isIE6 && $('#timeline') && typeof init_timeline === 'function') {
			init_timeline();
		}

		// start precaching images when the document has fully loaded.
		var $lp_cache = $(".lightbox_preview");
		if($lp_cache.length > 0) {
			// Cache preview images.
			$.fn.imgCache = [];
			$('body').append('<div id="cache_report" style="position: absolute; bottom: 0; left:0; background:#000; color:#fff;">Precaching...</div>');
			$('#cache_report').fadeIn();
			var index = 0;
			var temp = '';
			$lp_cache.each(function(){
				//alert($.fn.imgCache.length);
				index = $.fn.imgCache.length;
				$.fn.imgCache[index] = new Image();
				
				temp = extractNameExt($(this).attr('href'));
				
				($.fn.imgCache[index]).src = temp.filename + '_preview' + temp.ext;
				$('#cache_report').html('Precaching ' + $.fn.imgCache.length + ' of ' + $lp_cache.length);
			});
			$('#cache_report').fadeOut().remove();
		}
	});
}

function extractNameExt(href)
{
	var lastDotPos = href.lastIndexOf('.');
	var filename = href.substr(0, lastDotPos);
	var ext = href.substr(lastDotPos);
	return {'filename':filename,'ext':ext}
}

function insideViewSearch(src)
{
	// Turn off nav and add new text to H1
	$('#horiz-nav li.on').removeClass('on');
	var h1 = $('#content h1').html('Search');
	var breadcrumb = $('#breadcrumb li');
	var breadcrumb_text = breadcrumb.html();
	
	var delimiter = '&gt; ';
	
	var pos = breadcrumb_text.lastIndexOf(delimiter);
	
	var breadcrumb_text_left = '';
	
	// If pos is negative then  the delimiter wasn't found.
	if( -1 !== pos ) {
		// found it! add the length of the delimiter to the position since we want the position of the stuff after the delimiter and not the delimiter itself...
		pos += delimiter.length;
	
		breadcrumb_text_left = breadcrumb_text.substring(0, pos);
	}
	
	// check if there's already a search comment under the H1.
	if( 0 === $('p#searchComment').length ) {
		h1.after('<p id="searchComment"></p>');
	}
	
	var str = 'your search';
	// detect where the search term is coming from (is it a <select>, an <input>, a javascript string or something else?
	if( src.options )
	{
		str = src.options[src.selectedIndex].text;
	}
	else if (src.value)
	{
		str = src.value;
	}
	else if('string' === typeof src)
	{
		str = src;
	}
	
	$('p#searchComment').html('Results for "<em>' + str + '</em>"');
	breadcrumb.html(breadcrumb_text_left + 'Search results for ' +str);
	document.title = 'Search Results - Rentokil Initial';
}
