$(document).ready(function() {
		if (arr_images.length > 0) {
			var str_image_container = '.background-image';
			var str_text_container = '#background-content';
			var obj_image = null;
			var int_image_width = 0;
			var int_image_height = 0;
			var timer;
			var int_next_image = 0;
			var int_transition_time = 1000;
			var int_pause_time = 7000;
			var start_index = 999999;
			var bln_first_image = true;
			
			$(window).resize(function(event) {	
				if (bln_first_image) {
					initFirstImageText();
				} else { resizeImage(); }
			});
	
			function resizeImage() {
				var int_future_image = getNextImage(int_next_image);
				var obj = arr_images[int_future_image];
				var arr_pos = calculateContentPosition(obj);
				if (typeof arr_pos != 'undefined') {
					$(str_text_container).css({ 'left' : arr_pos[0]+'px', 'top' :  arr_pos[1]+'px', 'width': obj.content_width+'px'});	
				}
			}
			
			function initFirstImageText() { 
				var obj = arr_images[int_next_image];
				var arr_pos = calculateContentPosition(obj);
				if (typeof arr_pos != 'undefined') {
					$(str_text_container).css({ 'left' : arr_pos[0]+'px', 'top' :  arr_pos[1]+'px', 'width': obj.content_width+'px' });
				}
			}
			
			function calculateContentPosition(obj) {
				if (obj != null) {
					var availableWidth = $(window).width(); // 2 x 10px left/right padding
					var availableHeight = $(window).height(); // minus header, minus footer
					if (availableWidth > 980) {
						var scale_w = availableWidth / int_image_width;
						var scale_h = availableHeight / int_image_height; 
						var scale = Math.max(scale_w,scale_h);
						var new_w = int_image_width*scale
						var new_h = int_image_height*scale
						$(str_image_container).find('img').width(new_w);
						$(str_image_container).find('img').height(new_h);
						
						var int_top_pos = Math.max(106 + Math.round(obj.posy * scale));  // Number(Math.ceil(obj_image.posy * scale_h)) + 122;
						var int_left_pos = Math.max(Math.round(obj.posx * scale)); //1Math.ceil(obj_image.posx * scale_w);
						
						return new Array(int_left_pos, int_top_pos);
					}
				} 	
			}
			
			function setActiveImageProperties(obj) {
				obj_image = obj;	
				int_image_width = obj_image.image_width;
				int_image_height = obj_image.image_height;
			}
			
			function setImageProperties(obj, bln_first) {
				setActiveImageProperties(obj);
				resizeImage();
				
				fixAlphaProblem($(str_text_container + ' img'))
				$(str_text_container).fadeIn();
				
				clearTimeout(timer);
				timer = setTimeout(function() { goToNextImage() }, int_pause_time);	
			}
			
			function getNextImage(next_image) {
				if (next_image < arr_images.length-1) {
					next_image++;	
				} else {
					next_image = 0;
				}
				return next_image;
			}
			
			function getPreviousImage(count) {
				if (count <= 0) {
					count = arr_images.length-1;	
				} else {
					count--;
				}
				return count;
			}
			
			function goToNextImage() {
				int_next_image = getNextImage(int_next_image);
				changeImage( arr_images[int_next_image], false );
			}
			
			function placeNewContent(obj) {
				fixAlphaProblem($(str_text_container + ' img'))
				$(str_text_container).fadeOut(int_transition_time, function() {
					$(str_text_container).html( obj.content_html );	
					setImageProperties(obj, false);
				});	
			}
			
			function fixAlphaProblem(objimg) {
				if (objimg.attr('src')) {
					
					var imgSrc = objimg.attr('src')
					if (objimg.attr('src').substr(objimg.attr('src').length-4) === '.png' || objimg.attr('src').substr(objimg.attr('src').length-4) === '.PNG') {
						objimg.css({'filter' : "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='crop',src='" + objimg.attr('src') + "')"  });
					}
				}
			}

			function changeImage( obj, bln_first ) {
				var int_future_image = getNextImage(int_next_image);
				//Check to see if the next image is loaded, if not stay on the current image
				if ($('#fimage-'+int_future_image).attr('class') != 'loaded') {
					clearTimeout(timer);
					timer = setTimeout(function() { changeImage(obj, false); }, 1000);
					return;
				}
				//Show the image below so that it will transition
				var obj_n_image = arr_images[int_future_image];
				$('#fimage-'+int_future_image).css({ 'z-index' : start_index-- });
				$('#fimage-'+int_future_image).show();
				$('#fimage-'+obj["id"]).fadeOut(int_transition_time, 
					function() {  
						bln_first_image = false;
						placeNewContent(obj_n_image);
					}
				);
			}
			
			function initFader() {  
				//shuffle
				//arr_images = shuffle(arr_images);
				for (var i in arr_images) {
					$(str_image_container).append(
						'<img src="'+arr_images[i]["image_src"]+'" width="'+arr_images[i]["image_width"]+'" height="'+arr_images[i]["image_height"]+'" alt="" id="fimage-'+i+'" style="position: absolute; top: 0px; left: 0px;display: none; z-index: '+start_index--+'" />'
					);
					arr_images[i]["id"] = i;
				}
				//turn on the first image
				$(str_image_container).find('img').eq(0).show();
				$(str_text_container).html( arr_images[0]["content_html"]).show();
				setActiveImageProperties(arr_images[0]);
				initFirstImageText();
				if (arr_images.length > 1) {
					setTimeout(function() { changeImage(arr_images[0], true); }, int_pause_time);
				}
			}
			
			initFader();
			
			
			var imageLoadingComplete = function() 
			{	
				var curitem = obj_items.eq(obj_current_img);
				curitem.addClass('loaded');
				
				obj_current_img++;
				var str_arg = obj_items.eq(obj_current_img);
				$(str_arg, this ).imagesLoaded(imageLoadingComplete);
			}
		
			//Find the total images to cache
			obj_items = $(str_image_container + ' img');
			int_total_images = obj_items.size();
			var obj_current_img = 0;
			var str_arg = obj_items.eq(0);
			$(str_arg, this).imagesLoaded(imageLoadingComplete);
		}
	});
