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 singleFilename = extractNameExt(href,1);
												var singleFilename = singleFilename.singlefilename;
												var htmlStr = '<div id="preview_container"><a href="/static/p/downloadimage.php?file=' + singleFilename + '"><img id="_preview_img" src="' + filename + '_preview' + ext + '" /></a><p><a id="_preview_download_link" href="/static/p/downloadimage.php?file=' + singleFilename + '">Download File</a> - (this may take a few seconds)</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(){

		// 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,mode)
{
	if(mode==1)
	{	var lastSlashPos = href.lastIndexOf('/');
		href = href.substr(lastSlashPos+1);
	}
	var lastDotPos = href.lastIndexOf('.');
	var filename = href.substr(0, lastDotPos);
	var ext = href.substr(lastDotPos);
	if(mode==1)
	{	return {'singlefilename':filename+ext}	}
	else
	{	return {'filename':filename,'ext':ext}	}
}
