//------------------------------------
//
//	HOME.JS
//	Requires:	jquery 1.6.x
//
//------------------------------------

////////////////////////////
// BEGIN JQUERY

$(function(){

	//////////////////////////
	// SLIDER
	
	$('#promotions').slideThat({
		'setSize':	false
	});
	

	//////////////////////////
	// BUBBLES
	// Use 'name': false to create a new bubble with dev mode
	
	$.bubbles({
		'home_latest_news':	[554,456.5,162,190],
		'all_about':		[171,-607.5,120,120],
		'peek':				[768,-612,144,217]
	});


	//////////////////////////
	// WE LOVE THIS PANEL
	
	function lovingThis(){
	
		// Objects
		var $this = $('#loving_this');

		// Variables
		var t = $this.find('.slide').length;
		var c = 1;
		var pause = 4;
		var fancy = supports3d() && $('html').hasClass('webkit');
		
		// Show first one
		$this.find('.slide:first').addClass('active');
		
		if( t > 1 ){
		
			setTimeout(function(){
				next();
			},pause*1000);
		
		}
		
		//////////////////////////
		// GO TO SLIDE
		
		function next(){
			
			c++;
			
			if( c > t ){
				c = 1;
			}else if( c < 1 ){
				c = t;
			}
		
			var $new = $this.find('.slide:eq(' + ( c - 1 ) + ')');
			var $old = $this.find('.slide.active');
			
			var cleanUp = function(){
			
				$new.addClass('active').removeClass('new').unbind('webkitAnimationEnd');
				$old.removeClass('active old');
				
				setTimeout(function(){
					next();
				},pause*1000);

			}
			
			if( fancy ){
				
				$old.addClass('old');
				
				$new.addClass('new').bind({
					'webkitAnimationEnd':	cleanUp
				});
			
			}else{
			
				$new.addClass('new').css({
					'opacity':	0
				}).animate({
					'opacity':	1
				},400,function(){
					cleanUp();
				});
				
				$old.addClass('old').css({
					'opacity':	1
				}).animate({
					'opacity':	0
				},400);
			
			}
			
		}

	}
	
	lovingThis();
	
	
	//////////////////////////
	// NEWS PAGE TURNER
	
	function news(){
	
		// Objects
		var $this = $('#news');
		var $a = $this.find('.arrow');
		var $bind = $this.find('.binder');
		
		// Variables
		var t = $this.find('aside.left').length;
		var p = 1;
		var fancy = supports3d() && $('html').hasClass('webkit');
		var pause = 6;
		var mooseBan = false;
		
		// NEXT AND PREV CLICKS
		$a.click(function(e){
			
			if( !$(this).hasClass('disabled') && !mooseBan ){
			
				mooseBan = true;
			
				var dir = $(this).hasClass('next') ? 'rtl' : 'ltr';
				
				dir == 'rtl' ? p++ : p--;
				
				if( p > t ){
					p = 1;
				}else if( p < 1 ){
					p = t;
				}
				
				if( p == t ){
					$this.find('.next').addClass('disabled');
				}else if( p == 1 ){
					$this.find('.prev').addClass('disabled');
				}else{
					$this.find('.disabled').removeClass('disabled');
				}
				
				goto(dir);
			
			}
			
			e.preventDefault();
			
		});
		
		// ANIMATE THIS
		function goto(dir){
		
			// The new pages
			var $newLeft = $this.find('aside.left:eq(' + ( p - 1 ) + ')');
			var $newRight = $this.find('aside.right:eq(' + ( p - 1 ) + ')');
			
			// The old pages
			var $oldLeft = $('.active.left');
			var $oldRight = $('.active.right');
			
			// After animations
			var cleanUp = function(){
			
				$oldLeft.removeClass('active');
				$oldRight.removeClass('active');
				
				$this.removeClass('rtl ltr').find('.turn').removeClass('turn').unbind('webkitAnimationEnd');
				
				mooseBan = false;

			}
			
			// 3D support?
			if( fancy ){
			
				$this.addClass(dir);
				
				if( dir == 'rtl' ){
					
					$oldRight.addClass('turn');
				
					$newLeft.addClass('turn active').bind({
						'webkitAnimationEnd':	cleanUp
					});
					
					$newRight.addClass('active');
					
				}else{
				
					$oldLeft.addClass('turn');
				
					$newRight.addClass('turn active').bind({
						'webkitAnimationEnd':	cleanUp
					});
					
					$newLeft.addClass('active');
				
				}
				
				$bind.animate({
					'opacity': .2
				},300,'easeInExpo').animate({
					'opacity': 1
				},300,'easeOutExpo');
		
			}else{
			
				$newLeft.addClass('active');
				$newRight.addClass('active');
				
				$oldLeft.removeClass('active');
				$oldRight.removeClass('active');
				
				cleanUp();
			
			}
			
		}
		
		
		// LAST PAGE
		$('#last_page a').hover(function(){
			$(this).siblings('a').toggleClass('fade');
		});
	
	}
	
	news();
	
	
	//////////////////////////
	// CHECK FOR 3D
	// Thanks to http://www.modernizr.com/
	
	function supports3d() {
		// borrowed from modernizr
		var div = document.createElement('div'),
			ret = false,
			properties = ['perspectiveProperty', 'WebkitPerspective'];
		for (var i = properties.length - 1; i >= 0; i--){
			ret = ret ? ret : div.style[properties[i]] != undefined;
		};
	    
	    // webkit has 3d transforms disabled for chrome, though
	    //   it works fine in safari on leopard and snow leopard
	    // as a result, it 'recognizes' the syntax and throws a false positive
	    // thus we must do a more thorough check:
	    if (ret){
	        var st = document.createElement('style');
	        // webkit allows this media query to succeed only if the feature is enabled.    
	        // "@media (transform-3d),(-o-transform-3d),(-moz-transform-3d),(-ms-transform-3d),(-webkit-transform-3d),(modernizr){#modernizr{height:3px}}"
	        st.textContent = '@media (-webkit-transform-3d){#test3d{height:3px}}';
	        document.getElementsByTagName('head')[0].appendChild(st);
	        div.id = 'test3d';
	        document.body.appendChild(div);
	        
	        ret = div.offsetHeight === 3;
	        
	        st.parentNode.removeChild(st);
	        div.parentNode.removeChild(div);
	    }
	    return ret;
	}
	
});
