// rpm jQuery file

// fix stupid chrome background image bug for Chrome 17 + 19 & OSX 10.7.3  grrrrrrrrr
function refreshBackgrounds(selector) {
  if (/chrome/.test(navigator.userAgent.toLowerCase())) {
    $(selector).each(function() {
      var $this = $(this);
      if ($this.css("background-image")) {
        var oldBackgroundImage = $this.css("background-image");
        setTimeout(function() {
          $this.css("background-image", oldBackgroundImage);
        }, 1);
      }
    });
  }
}

var winHeight;
function windHeight(winHeight){
	$('#wrapper').css('min-height', winHeight + 'px');
};


$(document).ready(function() {
	
	// <><><> detect window height <><><>
	
	windHeight(2400);
	

	// <><><> globe sprite animation <><><>
	// Stack animation frames in a vertical strip and then fill in the vars below
	/////////////////////////////////////////////////////////////////////////////////
	var imagePathName = 'images/45-full.jpg' // set image name and path inside quotes
	var framesPerSecond = 8 // set frames per second (decimel is fine)
	var framesNumber = 45 // set number of frames
	var animationHeight = 242 // set height of a single frame (without px)
	var animationContainer = '#animatedGlobeLoop' // id, class or element container
	/////////////////////////////////////////////////////////////////////////////////
	

	
	// calculate fps
	var fps = 1000 / framesPerSecond;
	framesNumber -= 1;
	
	// preload image
	var imageObj = new Image(); 
	$(imageObj).attr('src', imagePathName).load(function(){
		
		// swap out image, set vars
		$(animationContainer).css('background-image', 'url(' + imagePathName + ')');
		var globeHeight = 0;
		var i = 0;
		
		// animation function, set position
		function globeAnimate() {
			globeHeight -= animationHeight;
			setGlobeHeight = globeHeight + 'px';
			$(animationContainer).css('backgroundPosition', '0px ' + setGlobeHeight);
		}
		
		// interval and reset counter
		setTimeout(function(){
			$(animationContainer).show();
			setInterval(function() {
				i++;
				globeAnimate();
				if (i == framesNumber) {
					i = 0;
					globeHeight = 0;
				};
			}, fps);
		},500);
		
	});
	
	
	// <><><> gallery item rollover <><><>
	
	function GalleryRoll() {
		var tmbTip;
		$('.galleryItem .info').hide();
		$('.galleryItem').hover(function(){
			tmbTip = $(this).find('.thumbnail a').attr('title');
			$(this).find('.thumbnail a').attr('title', '');
			$(this).find('.info').show().animate({bottom:'0px'}, 300 ).delay(1700).animate({bottom:'-51px'}, 200);
			$(this).click(function(){
				$(this).find('.thumbnail a').attr('title', tmbTip);
			});
		}, function(){
			$(this).find('.thumbnail a').attr('title', tmbTip);
			$(this).find('.info').animate({bottom:'-51px'},{queue:false,duration:200});
		});
	};
	
	

	// execute galleryRoll
	GalleryRoll();
	
	
	// <><><> gallery randomizer <><><>
	
	// copy gallery
	$('#galleryNew').empty();
	$('#galleryTempl .galleryItem').clone().appendTo('#galleryNew');
	
	// random function
	$.fn.randomize = function(childElem) {
		return this.each(function() {
			var $this = $(this);
			var elems = $this.children(childElem);

			elems.sort(function() { return (Math.round(Math.random())-0.5); });  

			$this.empty();  

			for(var i=0; i < elems.length; i++)
			$this.append(elems[i]);
		});    
	};
	
	// execute randomize
	$("#gallery #galleryNew").randomize(".galleryItem");
	
	
	// <><><> gallery fade in <><><>
	
	function fadeThumbs(fadeItem) {
		var thumbNailCount = 0;
		$('#galleryNew .' + fadeItem).each(function(){
			thumbNailCount = thumbNailCount + 1;
			$(this).addClass('tNc' + thumbNailCount);
			delayTime = thumbNailCount * 300;
			$(this).delay(delayTime).fadeIn(1000);
		});
		GalleryRoll();
	};
	
	// execute fadeThumbs
	fadeThumbs('galleryItem');
	
	
	// <><><> gallery sort <><><>
	
	$('#sortAll a').click(function(){
		$('#wrapper').css('min-height',"1250px");
		$('#galleryNew').empty();
		$('#galleryTempl .galleryItem a').attr('class','viewAllGroup');
		$('#galleryTempl .galleryItem').clone().appendTo('#galleryNew');
		$('#gallery #galleryNew').randomize('.galleryItem');
		fadeThumbs('galleryItem');
		refreshBackgrounds(".thumbnail");
		windHeight(2400);
	});
	$('#sortWeb a').click(function(){
		$('#wrapper').css('min-height',"1250px");
		$('#galleryNew').empty();
		$('#galleryTempl .galleryItem.web a').attr('class','webGroup');
		$('#galleryTempl .galleryItem').clone().appendTo('#galleryNew');
		$('#gallery #galleryNew').randomize('.galleryItem');
		fadeThumbs('web');
		refreshBackgrounds(".thumbnail");
		windHeight(1300);
	});
	$('#sortFlyer a').click(function(){
		$('#wrapper').css('min-height',"1250px");
		$('#galleryNew').empty();
		$('#galleryTempl .galleryItem.flyer a').attr('class','flyerGroup');
		$('#galleryTempl .galleryItem').clone().appendTo('#galleryNew');
		$('#gallery #galleryNew').randomize('.galleryItem');
		fadeThumbs('flyer');
		refreshBackgrounds(".thumbnail");
		windHeight(1300);
	});
	$('#sortPhoto a').click(function(){
		$('#wrapper').css('min-height',"1250px");
		$('#galleryNew').empty();
		$('#galleryTempl .galleryItem.photo a').attr('class','photoGroup');
		$('#galleryTempl .galleryItem').clone().appendTo('#galleryNew');
		$('#gallery #galleryNew').randomize('.galleryItem');
		fadeThumbs('photo');
		refreshBackgrounds(".thumbnail");
		windHeight(1300);
	});
	
	
	// <><><> Neon Flicker <><><>
	// Two images absolutely positioned, this will flicker the top layer
	/////////////////////////////////////////////////////////////////////////////////
	var neonFlickerContainer = '.neonFlicker' // flicker top layer container
	var initialFlickerDelay = 1000 // delay for initial flicker in milliseconds
	var timeRange = 10000 // how often should it flicker (random within range)
	var flickerRange = 2 // how many times should it flicker each loop (random range)
	/////////////////////////////////////////////////////////////////////////////////
		
	function neonFlicker(){
		// initial flicker
			$(neonFlickerContainer).delay(initialFlickerDelay).fadeIn(20).fadeOut(10).delay(50).fadeIn(30).fadeOut(10).delay(50).fadeIn(30);
		
		// flicker loop
		setInterval(function(){
			var randomNum = Math.ceil(Math.random()*flickerRange);
			for(i=0; i<randomNum; i++){
				$(neonFlickerContainer).fadeOut(10).delay(50).fadeIn(10);
			};
			randomTime = Math.ceil(Math.random()*timeRange);
			$(neonFlickerContainer).delay(randomTime);
		}, 1000);
		
	};
	
	// execute flicker
	neonFlicker();
	
	//<><><> Google Analytics <><><>
	
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', 'UA-27767195-1']);
	_gaq.push(['_trackPageview']);

	(function() {
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();
	
}); // end document ready

$(window).load(function(){
	
	// Stupid chrome bug!!!
	refreshBackgrounds(".thumbnail");
	
	// <><><> Preload background images <><><>
	var imageObjDown = new Image(); 
	$(imageObjDown).attr('src', 'images/down-bg.jpg');
	var imageObjFlicker = new Image(); 
	$(imageObjFlicker).attr('src', 'images/flicker.jpg');
	var imageObjInfo = new Image(); 
	$(imageObjInfo).attr('src', 'images/info-bg.jpg');
	var imageObjContact = new Image(); 
	$(imageObjContact).attr('src', 'images/contact-bg.jpg');
	
});

