/*
* Author:      Marco Kuiper (http://www.marcofolio.net/)
*/
jQuery.extend( jQuery.easing, // from the jquery.easing plugin
{
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	}	
});

(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

// scrolling
var scrollWin = function(toDiv){
	$('html,body').animate({
		scrollTop: $(toDiv).offset().top
	}, 1000, "easeInCubic");
}

// preloader
jQuery.preLoadImages("images/generators.jpg", "images/miele.jpg", "images/tomtom.jpg", "images/airhogs.jpg", "images/intuit.jpg");

// Speed of the automatic slideshow
var slideshowSpeed = 10000;

// Variable to store the images we need to set as background
var photos = [ {
		"image" : "intuit.jpg",
		"url" 	: "http://cache.vendaria.com/vpop/VpopX.html?purl=vnd_intuit_quickbooks_selector&iid=bpfzvvmluKqKvuJlKLLIfwqJquJlLf&bg=FFFFFF&nm=BZOpener&err=0&title=&pf=t&fr=t",
		"title" : "Intuit Quickbooks Selector",
		"width" : 810,
		"height": 610
	}, {
		"image" : "airhogs.jpg", 
		"url" : "http://www.vendaria.com/gallery/spinmaster/air_hogs/UI/UI_gallery.html?ap=vnd_spin_master_sst715_v7&am=MAIN_spin_master_sst715_v7",
		"title" : "Spinmaster Airhogs Hawkeye",
		"width" : 730,
		"height": 520
	}, {
		"image" : "tomtom.jpg",
		"url" : "http://www.vendaria.com/gallery/tomtom/2011/InteractiveKiosk_v4_cs4/loader.htm",
		"title" : "TomTom In-store Kiosk",
		"width" : 1034,
		"height": 650
	}, {
		"image" : "miele.jpg",
		"url" : "http://www.vendaria.com/gallery/miele/src/container/UI/UI_callisto.html",
		"title" : "Miele Vacuums",
		"width" : 710,
		"height": 530
	}, {
		"image" : "generators.jpg",
		"url" : "http://cache.vendaria.com/vpop/VpopX.html?purl=vnd_homedepot_portable_generators&iid=AddpfzJuvllqqqKlmLuJfwmvluKKJI&bg=FFFFFF&nm=BZOpener&err=0&title=&pf=t&fr=t",
		"title" : "Generator Selector",
		"width" : 795,
		"height": 595
	}
];



$(document).ready(function() {
	
	var interval;
	var activeContainer = 1;	
	var currentImg = 0;
	var lastImg = 0;
	var animating = false;
	var currentZindex = 889;
	var shownImage;	
	
	 //safe mailto's
	 $('a[href*="[at]"][href*="[dot]"]').each(function() {
	  var email = $(this).attr('href').split('[at]').join('@').split('[dot]').join('.');
	  $(this).attr('href', 'mailto:' + email.toLowerCase());
	  //if ($(this).text().length == 0) $(this).text(email);
	 });
	
	// button navigation
	$("#nav1").click(function() {
		stopAnimation();
		navigate("previous");
	});		
	
	// button navigation
	$("#nav2").click(function() {
		stopAnimation();
		navigate("next");
	});			
	
	// dynamic title rollovers
	$("#demotiles img").hover(
	 function()
	 {
	  this.src = this.src.replace("_off","_on");
	 },
	 function()
	 {
	  this.src = this.src.replace("_on","_off");
	 }
	);	
	
	// dynamic title rollovers
	$("#nav_btns img").hover(
	 function()
	 {
	  this.src = this.src.replace("_off","_on");
	 },
	 function()
	 {
	  this.src = this.src.replace("_on","_off");
	 }
	);	

	// dynamic fancybox function	
	 $('a.fb_dynamic').each(function(){  
	        var dWidth  = parseInt($(this).attr('href').match(/width=[0-9]+/i)[0].replace('width=',''));  
	        var dHeight     =  parseInt($(this).attr('href').match(/height=[0-9]+/i)[0].replace('height=',''));  
			$(this).fancybox({  
			    'width':dWidth,  
			    'height':dHeight,  
			    'autoScale'     : false,  
			    'type'          : 'iframe',
				'autosize'		: false  
			});  
			$(this).click(function() {
				stopAnimation();
			});					
	   });
    	
	
	var 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 == photos.length + 1) {
				currentImg = 1;
			}
		} else if (direction == "previous") {
			currentImg--;
			if(currentImg == 0) {
				currentImg = photos.length;
			}		
		
		} else {
			currentImg = direction;
		}
		
		// Check which container we need to use
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}
		
		if (shownImage != photos[currentImg - 1].image)
		{
			showImage(photos[currentImg - 1], currentContainer, activeContainer);
		}
		
	};

	var showImage = function(photoObject, currentContainer, activeContainer) {
		animating = true;
		
		// Make sure the new container is always on the background
		currentZindex--;
			
		// set gallery photo URL			
		$("#photoURL").attr("href", photoObject.url)				
		$("#photoURL").attr("title", photoObject.title)	
		$("a#photoURL").fancybox({
			'opacity'		: true,
			'overlayShow'	: false,	
			'width'         : photoObject.width,
			'height'        : photoObject.height,
			'type'			: 'iframe',
			'autosize'		: false,
			'autoScale'     : false 
		});	
		$("a#photoURL").click(function() {
			stopAnimation();
		});				
		
		// Set the background image of the new active container
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(images/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		shownImage = photoObject.image;
		
		// Fade out the current container and display the header text when animation is complete
		$("#headerimg" + currentContainer).animate({"left": -1024}, 600, "easeInCubic", function() { 
			setTimeout(function() {
				$("#headerimg" + currentContainer).css({"z-index" : currentZindex - 1});
				$("#headerimg" + currentContainer).animate({"left": 0}, 250, "easeInOutExpo");
				animating = false;
			}, 500);
		});
		lastImg = currentImg;
	};
	
	var stopAnimation = function() {
		// Clear the interval
		clearInterval(interval);
	};
	
	// We should statically set the first image
	navigate("3");
	
	// Start playing the animation
	interval = setInterval(function() {
		navigate("next");
	}, slideshowSpeed);
	
});
