
// '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_219_page2
// ---------------------------------------------------------------------

// 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_219_page2 = {};

// 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_219_page2 = (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.1.3 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_219_page2 = function() {
		showcontainer_stacks_in_219_page2();
		hidecounter_stacks_in_219_page2();
	}
	showcontainer_stacks_in_219_page2 = function() {
		$('#countdown-container-stacks_in_219_page2').slideDown('slow');
	}
	hidecounter_stacks_in_219_page2 = function() {
		// Can add an option in here to hide the counter or not
		if (true == true) {
		    var hideDelay = 10 * 1000;
    		$('#stacks_in_219_page2 .counter-wrapper').delay(hideDelay).slideUp('slow');
		}
	}

	var dateFuture  = new Date('Feburar 25, 2012 16: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() - (1 *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_219_page2 .counter-wrapper').hide();
		}
		processall_stacks_in_219_page2();
	}
	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_219_page2
			});	
	
			$('#counter').countdown({
				image: 'files/liftoff-images/countdown1.png',
				startTime: timeDiff.days +':'+ timeDiff.hours +':'+ timeDiff.minutes +':'+ timeDiff.seconds,
				timerEnd: hidecounter_stacks_in_219_page2
			});	
		}
		else {
			$('#counter').countdown({
				image: 'files/liftoff-images/countdown1.png',
				startTime: timeDiff.days +':'+ timeDiff.hours +':'+ timeDiff.minutes +':'+ timeDiff.seconds,
				timerEnd: processall_stacks_in_219_page2
			});		
		}
	}
	
	if ('inline-block'  == 'none') { $('div.cntSeparator:eq(2)').remove(); }
	if ('inline-block'  == 'none') { $('div.cntSeparator:eq(1)').remove(); }
	if ('inline-block' == 'none') { $('div.cntSeparator:eq(0)').remove(); }
	if ('inline-block'  == 'none') { $('div.cntSeparator:eq(0)').remove(); }
});
// End Liftoff
	return stack;
})(stacks.stacks_in_219_page2);


// Javascript for stacks_in_4_page2
// ---------------------------------------------------------------------

// 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_4_page2 = {};

// 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_4_page2 = (function(stack) {

	// When jQuery is used it will be available as $ and jQuery but only
	// inside the closure.
	var jQuery = stacks.jQuery;
	var $ = jQuery;
	
//-- Gyro Stack v1.2 by Joe Workman --//

$(document).ready(function() {

	// Create the ribbon HTML
	createPager = function(){	
        $("#stacks_in_4_page2 .gyro_image_reel img").each(function(index) {
            $('<a href="#" rel="' + (index+1) + '">' + (index+1) + '</a>').appendTo('#stacks_in_4_page2 div.gyro_ribbon_middle');
        });
		// Need to swap the bragrounds when the ribbon is on the left
		if ('right' == 'left') {
			$('#stacks_in_4_page2 .gyro_ribbon_start').css("background", "url('rw_common/plugins/stacks/images/ribbon3/gyro_ribbon_right_end_3.png') no-repeat");
			$('#stacks_in_4_page2 .gyro_ribbon_end').css("background", "url('rw_common/plugins/stacks/images/ribbon3/gyro_ribbon_right_start_3.png') no-repeat");			
		}
		// This is to set some CSS styles for ribbons that don't  fit into the defaults set in the CSS file
		/* if ('3' == 2 || '3' == 3 || '3' == 4) {
			$('#stacks_in_4_page2 .gyro_ribbon').css("right", "-13px");
		}
		if ('3' == 5 || '3' == 6) {
			$('#stacks_in_4_page2 .gyro_ribbon_end').css("width", "32px");
		} */
	}; 
	createPager(); //Run function on launch

	//Set Default State of each portfolio piece
	 $("#stacks_in_4_page2 .gyro_ribbon").show();
	$("#stacks_in_4_page2 .gyro_ribbon a:first").addClass("active");

	//Get size of images, how many there are, then determin the size of the image reel.
	var imageHeight = $("#stacks_in_4_page2 .gyro_image_reel img:first").height();
	var imageWidth = $("#stacks_in_4_page2 .gyro_image_reel img:first").width();
	var imageSum = $("#stacks_in_4_page2 .gyro_image_reel img").size();
	var imageReelWidth = imageWidth * imageSum;
	
	//Adjust the image reel to its new size
	$("#stacks_in_4_page2 .gyro_container").css({'width' : imageWidth});
	$("#stacks_in_4_page2 .gyro_window").css({'width' : imageWidth});
	$("#stacks_in_4_page2 .gyro_window").css({'height' : imageHeight});
	$("#stacks_in_4_page2 .gyro_image_reel").css({'width' : imageReelWidth});
			
	//ribbon + Slider Function
	rotate = function(){	
		var triggerID = $active.attr("rel") - 1; //Get number of times to slide
		var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

		$("#stacks_in_4_page2 .gyro_ribbon a").removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)
		
		//Slider Animation
		$("#stacks_in_4_page2 .gyro_image_reel").animate({ 
			left: -image_reelPosition
		}, 995 );		
	}; 
	
	var play;
	//Rotation + Timing Event
	rotateSwitch = function(){		
		play = setInterval(function(){ //Set timer - this will repeat itself every 3 seconds
			$active = $('#stacks_in_4_page2 .gyro_ribbon a.active').next();
			if ( $active.length === 0) { //If ribbon reaches the end...
				$active = $('#stacks_in_4_page2 .gyro_ribbon a:first'); //go back to first
			}
			rotate(); //Trigger the ribbon and slider function
		}, 6000); //Timer speed in milliseconds
	};
	rotateSwitch(); //Run function on launch
	
	//On Hover
	$("#stacks_in_4_page2 .gyro_image_reel a").hover(function() {
		clearInterval(play); //Stop the rotation
	}, function() {
		rotateSwitch(); //Resume rotation
	});	

	//On Click
	$("#stacks_in_4_page2 .gyro_ribbon a").click(function() {	
		$active = $(this); //Activate the clicked ribbon
		//Reset Timer
		clearInterval(play); //Stop the rotation
		rotate(); //Trigger rotation immediately
		if (true) {
			clearInterval(play); //Stop the rotation
		}
		else {
			rotateSwitch(); // Resume rotation
		}
		return false; //Prevent browser jump to link anchor
	});	
});

//-- End Gyro Stack --//
	return stack;
})(stacks.stacks_in_4_page2);



