/*
* Author:      Marco Kuiper (http://www.marcofolio.net/)
*              Sylvain Papet (http://www.com-ocean.com/)
*              Toxic Web (http://www.toxic-web.co.uk/) - v1.0.1
*/
    
(function($) {
  $.bgimgSlideshow = {version: '1.0.1'};
  
  $.fn.bgimgSlideshow = function(options) {
    options = jQuery.extend({
      slideshowSpeed: 7000,
			method : "fade", /* slide or fade, default is fade */
      fadeSpeed :1000, /* how quickly one image fades/slides into the next */
			contDiv : "#homewrapper", /* id or class of div you want the images to appear in. */
      photos : [],
      markup1: '<div id="headerimgs">\
		             <div id="headerimg1" class="headerimg"></div>\
		             <div id="headerimg2" class="headerimg"></div>\
	              </div>',
	   markup2:'<div id="headernav-outer">\
		           <div id="headernav">\
			          <div id="back" class="btn"></div>\
			          <div id="control" class="btn"></div>\
			          <div id="next" class="btn"></div>\
		           </div>\
	            </div>\
	            <div id="headertxt">\
		           <p class="caption">\
			          <span id="firstline"></span>\
			           <a href="#" id="secondline"></a>\
		           </p>\
		           <p class="pictured">\
			          Read More:\
			          <a href="#" id="pictureduri"></a>\
		           </p>\
	            </div>'
      },options);

    var interval;
    var activeContainer = 1;	
    var currentImg = 0;
    var started = false;
    var animating = false;
    var currentZindex = -1;
    
    var image_cache = [];

    $.bgimgSlideshow.preLoadImage = function() {
      var args_len = arguments.length;
      for (var i = args_len; i--;) {
        var cacheImage = document.createElement('img');
        cacheImage.src = arguments[i];
        image_cache.push(cacheImage);
      }
    }

    $.bgimgSlideshow.preLoadPhotoObjects = function(photoObjects) {
      for(i in photoObjects)
      {
        $.bgimgSlideshow.preLoadImage(photoObjects[i].image, photoObjects[i].image);
      }
    }

    $.bgimgSlideshow.initialize = function() {
      $("" + options.contDiv + "").prepend(options.markup1);
			$("" + options.contDiv + "").append(options.markup2);
      $.bgimgSlideshow.preLoadPhotoObjects(options.photos);
  
      // Backwards navigation
      $("#back").click(function() {
      	$.bgimgSlideshow.stopSlideshow();
      	$.bgimgSlideshow.navigate("back");
      });
      
      // Forward navigation
      $("#next").click(function() {
        $.bgimgSlideshow.stopSlideshow();
      	$.bgimgSlideshow.navigate("next");
      });
      
      $("#control").click(function(){ 
        if(started)
        {
          $.bgimgSlideshow.stopSlideshow();
        }
        else
        {
          $.bgimgSlideshow.startSlideshow();
        }
      });
      $.bgimgSlideshow.startSlideshow();
    };
    
    $.bgimgSlideshow.navigate = function(direction) {
    	// Check if no animation is running. If it is, prevent the action
    	if(animating) {
    		return;
    	}
    	
    	// Check which current image we need to show
    	if(direction == "next") {
    		currentImg++;
    		if(currentImg == options.photos.length + 1) {
    			currentImg = 1;
    		}
    	} else {
    		currentImg--;
    		if(currentImg == 0) {
    			currentImg = options.photos.length;
    		}
    	}
    	
    	// Check which container we need to use
    	var currentContainer = activeContainer;
    	if(activeContainer == 1) {
    		activeContainer = 2;
    	} else {
    		activeContainer = 1;
    	}
    	
    	$.bgimgSlideshow.showImage((currentImg - 1), currentContainer, activeContainer);
    };
    
    $.bgimgSlideshow.showImage = function(numImg, currentContainer, activeContainer) {
      var photoObject = options.photos[numImg];
    	animating = true;
    	
    	// Make sure the new container is always on the background
    	currentZindex--;
    	
    	// Set the background image of the new active container
    	$("#headerimg" + activeContainer).css({
    		"background-image" : "url(" + photoObject.image + ")",
    		"display" : "block",
    		"z-index" : currentZindex
    	});
    	
		// Hide the header text
		$("#headertxt").css({"display" : "none"});
		
		// Set the new header text
		$("#firstline").html(photoObject.firstline);
		$("#secondline")
			.attr("href", photoObject.url)
			.html(photoObject.secondline);
		$("#pictureduri")
			.attr("href", photoObject.url)
			.html(photoObject.title);

    	// Fade out the current container
    	// and display the header text when animation is complete
		
		if(options.method == "slide"){
		$("#headerimg" + currentContainer).animate({width: 'toggle'},options.fadeSpeed,function() {
			setTimeout(function() {
				$("#headertxt").css({"display" : "block"});
				animating = false;
			}, 10);
    	});
		}
		else{
		$("#headerimg" + currentContainer).fadeOut(options.fadeSpeed,function() {
			setTimeout(function() {
				$("#headertxt").css({"display" : "block"});
				animating = false;
			}, 10);
    	});
			}
    };

    $.bgimgSlideshow.stopSlideshow = function() {
    	// Change the background image to "play"
      $("#control").css({ "background-image" : "url(http://www.gocompletely.com/wp-content/uploads/2011/10/newhome/images/btn_play_sml.png)" });
    	// Clear the interval
    	clearInterval(interval);
      started = false;
    };
    
    $.bgimgSlideshow.startSlideshow = function() {
      $("#control").css({ "background-image" : "url(http://www.gocompletely.com/wp-content/uploads/2011/10/newhome/images/btn_pause_sml.png)" });
    	$.bgimgSlideshow.navigate("next");
    	interval = setInterval(function() { $.bgimgSlideshow.navigate("next"); }, options.slideshowSpeed);
      started = true;
    };    
        
    $.bgimgSlideshow.initialize();
    return this;
  }
 
})(jQuery);

