
// 'stacks' is the Stacks global object.
// All of the other Stacks related Javascript will 
// be attatched to it.
var stacks = {};


// this call to jQuery gives us access to the globaal
// jQuery object. 
// 'noConflict' removes the '$' variable.
// 'true' removes the 'jQuery' variable.
// removing these globals reduces conflicts with other 
// jQuery versions that might be running on this page.
stacks.jQuery = jQuery.noConflict(true);

// Javascript for stacks_in_254_page0
// ---------------------------------------------------------------------

// Each stack has its own object with its own namespace.  The name of
// that object is the same as the stack's id.
stacks.stacks_in_254_page0 = {};

// A closure is defined and assigned to the stack's object.  The object
// is also passed in as 'stack' which gives you a shorthand for referring
// to this object from elsewhere.
stacks.stacks_in_254_page0 = (function(stack) {

	// When jQuery is used it will be available as $ and jQuery but only
	// inside the closure.
	var jQuery = stacks.jQuery;
	var $ = jQuery;
	
/*
	Background Stretcher jQuery Plugin
	� 2009 ajaxBlender.com
	For any questions please visit www.ajaxblender.com 
	or email us at support@ajaxblender.com
	
	Version: 1.3 (adapted to Stacks by Mauricio Sabene at RWExtras.com)
*/

	/*  Variables  */
	var container = null;
	var allImgs = '', allLIs = '', containerStr = '';	
	var element = this;
	var _bgStretcherPause = false;
	var _bgStretcherTm = null;
	
	jQuery.fn.bgStretcher = function(settings){
		settings = jQuery.extend({}, jQuery.fn.bgStretcher.defaults, settings);
		jQuery.fn.bgStretcher.settings = settings;
		
		function _build(){
			if(!settings.images.length){ return; }
			
			_genHtml();
			
			containerStr = '#' + settings.imageContainer;
			container = jQuery(containerStr);
			allImgs = '#' + settings.imageContainer + ' IMG';
			allLIs = '#' + settings.imageContainer + ' LI';

			jQuery(allLIs).hide();
			var bgrandom = Math.round(Math.random() * jQuery(allLIs).length);
			if(settings.randomFirst) {
			jQuery(allLIs + ':eq(' + bgrandom + ')').fadeIn(1000).addClass('bgs-current');
			} else {
			jQuery(allLIs + ':first').fadeIn(1000).addClass('bgs-current');}
			
			if(!container.length){ return; }
			jQuery(window).resize(_resize);
			
			if(settings.slideShow && jQuery(allImgs).length > 1){
				_bgStretcherTm = setTimeout( bgSlideshow , settings.nextSlideDelay);
			}
			_resize();
		};
		
		function _resize(){
			var winW = jQuery(window).width();
			var winH = jQuery(window).height();
			var imgW = 0, imgH = 0;

			//	Update container's height
			container.width(winW);
			container.height(winH);
			
			//	Non-proportional resize
			if(!settings.resizeProportionally){
				imgW = winW;
				imgH = winH;
			} else {
				var initW = settings.imageWidth, initH = settings.imageHeight;
				var ratio = initH / initW;
				
				imgW = winW;
				imgH = winW * ratio;
				
				if(imgH < winH){
				imgH = winH;
				imgW = imgH / ratio;
				}
			}
			
			//	Apply new size for images
			if(!settings.resizeAnimate){
				jQuery(allImgs).width(imgW).height(imgH);
			} else {
				jQuery(allImgs).animate({width: imgW, height: imgH}, 'normal');
			}
		};
		
		function _genHtml(){
			var code = '<div id="' + settings.imageContainer + '" class="bgstretcher"><ul>';
			for(i = 0; i < settings.images.length; i++){
				code += '<li><img src="' + settings.images[i] + '" alt="" /></li>';
			}
			code += '</ul></div>';
			jQuery(settings.codeLocation).prepend(code);
		};

		function bgSlideshow (){
			var current = jQuery(containerStr + ' LI.bgs-current');
			var next = current.next();
			if(!next.length){
			if(settings.stopEnd)	
			{ return false; }
			else { next = jQuery(containerStr + ' LI:first'); }
			}

			jQuery(containerStr + ' LI').removeClass('bgs-current');
			next.addClass('bgs-current');

			next.fadeIn( jQuery.fn.bgStretcher.settings.slideShowSpeed );
			current.fadeOut( jQuery.fn.bgStretcher.settings.slideShowSpeed );
			_bgStretcherTm = setTimeout( bgSlideshow, jQuery.fn.bgStretcher.settings.nextSlideDelay);
		};
		
		/*  Start bgStretcher  */
		_build();
	};

	/*  Default Settings  */
	jQuery.fn.bgStretcher.defaults = {
		imageContainer:             'bgstretcher',
		resizeProportionally:       true,
		resizeAnimate:              false,
		images:                     [],
		imageWidth:                 1024,
		imageHeight:                768,
		nextSlideDelay:             3000,
		slideShowSpeed:             'slow',
		codeLocation: 				'body',
		randomFirst: 				false,
		slideShow:                  true
	};
	jQuery.fn.bgStretcher.settings = {};

jQuery(document).ready(function(){
	var bg_images = [];
	jQuery('div#inputimages img').each(function() {
		bg_images.push(jQuery(this).attr('src'))
	});

		jQuery(document).bgStretcher({
			images: bg_images,
			imageWidth: 1280,
			imageHeight: 720,
			imageContainer: 'stretcher',
			nextSlideDelay: 3000,
			slideShowSpeed: 'slow',
			resizeAnimate: false,
			codeLocation: 'body',
			randomFirst: false,
			slideShow: false,
			stopEnd: false
		});
});
	return stack;
})(stacks.stacks_in_254_page0);


// Javascript for stacks_in_260_page0
// ---------------------------------------------------------------------

// Each stack has its own object with its own namespace.  The name of
// that object is the same as the stack's id.
stacks.stacks_in_260_page0 = {};

// A closure is defined and assigned to the stack's object.  The object
// is also passed in as 'stack' which gives you a shorthand for referring
// to this object from elsewhere.
stacks.stacks_in_260_page0 = (function(stack) {

	// When jQuery is used it will be available as $ and jQuery but only
	// inside the closure.
	var jQuery = stacks.jQuery;
	var $ = jQuery;
	
// Liftoff v1.0.0 by Joe Workman
/* jquery-counter plugin
 * Copyright (c) 2009 Martin Conte Mac Donell <Reflejo@gmail.com>
 * Dual licensed under the MIT and GPL licenses.
 * http://docs.jquery.com/License
 */
jQuery.fn.countdown = function(userOptions)
{
  // Default options
  var options = {
    stepTime: 60,
    // startTime and format MUST follow the same format.
    // also you cannot specify a format unordered (e.g. hh:ss:mm is wrong)
    format: "dd:hh:mm:ss",
    startTime: "01:12:32:55",
    digitImages: 6,
    digitWidth: 53,
    digitHeight: 77,
    timerEnd: function(){},
    image: "digits.png"
  };
  var digits = [], interval;

  // Draw digits in given container
  var createDigits = function(where) 
  {
    var c = 0;
    var tempStartTime = options.startTime;
    // Iterate each startTime digit, if it is not a digit
    // we'll asume that it's a separator
    for (var i = 0; i < options.startTime.length; i++)
    {
      if (parseInt(tempStartTime.charAt(i)) >= 0) 
      {
        elem = jQuery('<div id="cnt_' + i + '" class="cntDigit" />').css({
          height: options.digitHeight * options.digitImages * 10, 
          float: 'left', background: 'url(\'' + options.image + '\')',
          width: options.digitWidth});
        digits.push(elem);
        margin(c, -((parseInt(tempStartTime.charAt(i)) * options.digitHeight *
                              options.digitImages)));
        digits[c].__max = 9;
        // Add max digits, for example, first digit of minutes (mm) has 
        // a max of 5. Conditional max is used when the left digit has reach
        // the max. For example second "hours" digit has a conditional max of 4 
        switch (options.format.charAt(i)) {
          case 'h':
            digits[c].__max = (c % 2 == 0) ? 2: 9;
            if (c % 2 != 0)
              digits[c].__condmax = 3;
            break;
          case 'd': 
            digits[c].__max = 9;
            break;
          case 'm':
          case 's':
            digits[c].__max = (c % 2 == 0) ? 5: 9;
        }
        ++c;
      }
      else 
        elem = jQuery('<div class="cntSeparator"/>').css({float: 'left'})
                .text(tempStartTime.charAt(i));

          where.append('<div>');
      where.append(elem);
          where.append('</div>');
    }
  };
  
  // Set or get element margin
  var margin = function(elem, val) 
  {
    if (val !== undefined)
      return digits[elem].css({'marginTop': val + 'px'});

    return parseInt(digits[elem].css('marginTop').replace('px', ''));
  };

  // Makes the movement. This is done by "digitImages" steps.
  var moveStep = function(elem) 
  {
    digits[elem]._digitInitial = -(digits[elem].__max * options.digitHeight * options.digitImages);
    return function _move() {
      mtop = margin(elem) + options.digitHeight;
      if (mtop == options.digitHeight) {
        margin(elem, digits[elem]._digitInitial);
        if (elem > 0) moveStep(elem - 1)();
        else 
        {
          clearInterval(interval);
          for (var i=0; i < digits.length; i++) margin(i, 0);
          options.timerEnd();
          return;
        }
        if ((elem > 0) && (digits[elem].__condmax !== undefined) && 
            (digits[elem - 1]._digitInitial == margin(elem - 1)))
          margin(elem, -(digits[elem].__condmax * options.digitHeight * options.digitImages));
        return;
      }

      margin(elem, mtop);
      if (margin(elem) / options.digitHeight % options.digitImages != 0)
        setTimeout(_move, options.stepTime);

      if (mtop == 0) digits[elem].__ismax = true;
    }
  };

  jQuery.extend(options, userOptions);
  this.css({height: options.digitHeight, overflow: 'hidden'});
  createDigits(this);
  interval = setInterval(moveStep(digits.length - 1), 1000);
};

$(document).ready(function() {
	function get_time_difference(dateOne,dateTwo,futureDateTwo) {	    
		// if future date is used find that diff else find the diff for the date in the past.
		var nTotalDiff = futureDateTwo ? dateTwo.getTime() - dateOne.getTime() : dateOne.getTime() - dateTwo.getTime();
		var oDiff = new Object();
		// if the number happens to be negative, just return 0
		if (nTotalDiff < 0) {
			oDiff.days = oDiff.hours = oDiff.minutes = oDiff.seconds = '00';
			return oDiff;
		}
		// Days		
		oDiff.days = Math.floor(nTotalDiff/1000/60/60/24);
		nTotalDiff -= oDiff.days*1000*60*60*24;
		if (oDiff.days < 10) { oDiff.days = '0' + oDiff.days; }
		// Hours
		oDiff.hours = Math.floor(nTotalDiff/1000/60/60);
		nTotalDiff -= oDiff.hours*1000*60*60;
		if (oDiff.hours < 10) { oDiff.hours = '0' + oDiff.hours; }
		// Minutes
		oDiff.minutes = Math.floor(nTotalDiff/1000/60);
		nTotalDiff -= oDiff.minutes*1000*60;
		if (oDiff.minutes < 10) { oDiff.minutes = '0' + oDiff.minutes; }
		// Seconds
		oDiff.seconds = Math.floor(nTotalDiff/1000);
		if (oDiff.seconds < 10) { oDiff.seconds = '0' + oDiff.seconds; }
		
		return oDiff;	 
	}
	processall_stacks_in_260_page0 = function() {
		showcontainer_stacks_in_260_page0();
		hidecounter_stacks_in_260_page0();
	}
	showcontainer_stacks_in_260_page0 = function() {
		$('#countdown-container-stacks_in_260_page0').slideDown('slow');
	}
	hidecounter_stacks_in_260_page0 = function() {
		// Can add an option in here to hide the counter or not
		if (true == true) {
		    var hideDelay = 10 * 1000;
    		$('#stacks_in_260_page0 .counter-wrapper').delay(hideDelay).slideUp('slow');
		}
	}

	var dateFuture  = new Date('Dec 22, 2011 00:00:00');
	var dateCurrent = new Date();

    // console.log('Future Date: '+ dateFuture.toString());
    // console.log('Current Date: '+ dateCurrent.toString());
    // console.log('Future GMT Date: '+ dateFuture.toGMTString());
    // console.log('Current GMT Date: '+ dateCurrent.toGMTString());
	
    // Set Both Dates to GMT so its consistent across the board
    dateFuture.setTime(dateFuture.getTime() - (2 *60*60*1000));
    dateCurrent.setTime(dateCurrent.getTime() + (dateCurrent.getTimezoneOffset() *60*1000));
    
    // console.log('Future My GMT Date: '+ dateFuture.toString());
    // console.log('Current My GMT Date: '+ dateCurrent.toString());
    
	var earlyFinish = 0;
	var timeDiff = get_time_difference(dateCurrent,dateFuture,true);
	
	if (timeDiff.days == '00' && timeDiff.hours == '00' && timeDiff.minutes == '00' && timeDiff.seconds == '00') {
		if ('true' == 'true') {
		    $('#stacks_in_260_page0 .counter-wrapper').hide();
		}
		processall_stacks_in_260_page0();
	}
	else {	
		if (earlyFinish) {	
			var earlyDateFuture = new Date(dateFuture.getTime() - (earlyFinish*60*1000));
			var earlyTimeDiff = get_time_difference(dateCurrent,earlyDateFuture,true);
	
			$('#counter-hide').countdown({
				image: 'files/liftoff-images/countdown1.png',
				startTime: earlyTimeDiff.days +':'+ earlyTimeDiff.hours +':'+ earlyTimeDiff.minutes +':'+ earlyTimeDiff.seconds,
				timerEnd: showcontainer_stacks_in_260_page0
			});	
	
			$('#counter').countdown({
				image: 'files/liftoff-images/countdown1.png',
				startTime: timeDiff.days +':'+ timeDiff.hours +':'+ timeDiff.minutes +':'+ timeDiff.seconds,
				timerEnd: hidecounter_stacks_in_260_page0
			});	
		}
		else {
			$('#counter').countdown({
				image: 'files/liftoff-images/countdown1.png',
				startTime: timeDiff.days +':'+ timeDiff.hours +':'+ timeDiff.minutes +':'+ timeDiff.seconds,
				timerEnd: processall_stacks_in_260_page0
			});		
		}
	}
	
	
});
// End Liftoff
	return stack;
})(stacks.stacks_in_260_page0);



