if(typeof $ != 'undefined') {
	$(document).ready(function() {
		// Detect and rig carousel
		$("div#carousel").carousel();

		// Detect and attach colorbox
		$(".lightbox_iframe").colorbox({opacity:0.65, transition:'elastic', speed:250, innerWidth:484, innerHeight:337, 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 temp = extractNameExt(href);
												var filename = temp.filename;
												var ext = temp.ext;

												try
												{
													var domain = document.location.protocol + '//' + document.location.hostname;
													// 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)');
															}
														}
													});
												}
												catch(e)
												{
													
												}
												
												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;
											}
		});
	});
	$(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) {
	var lastDotPos = href.lastIndexOf('.');
	var filename = href.substr(0, lastDotPos);
	var ext = href.substr(lastDotPos);
	return {'filename':filename,'ext':ext}
}
