var t = '';

$(document).ready(function() {
	t = setTimeout ( "rotateNormal()", 5000 );

	//Activate PNG Fix
	if (jQuery.fn.pngFix) $(document).pngFix();
});

//Carousel
$('#slides a').hide();
$('#slides a:first').show();
$disable = false;
$disable2 = true;

function rotate($next) {
	$disable = true;	
	$('#slides a:visible').fadeOut(2000,
		function() {
			t = setTimeout ( "rotateNormal()", 5000 );
			$disable = false;
		});
	
	$next.fadeIn(2000);
	
	$('.car_nav li.current').removeClass('current');
	
	$count = $next.prevAll('a').size();
	$('.car_nav li:eq('+$count+')').addClass('current');
	
}

function rotateNormal() {
	
	if($('#slides a:visible').next().length > 0) {
		$next = $('#slides a:visible').next();
	
	}else{
		$next = $('#slides a:first');
	}
	
	rotate($next);
}

$('.car_nav a').click(function(){
		if($disable == false) {
			clearTimeout (t);
			
			$count = $(this).parent().prevAll('li').size();
			$next = $('#slides a:eq('+$count+')');
			
			rotate($next);
		}
		return false;
	});

$('#slides a').hover(
	function(){
		if($disable == false) {
			clearTimeout(t);
			$disable2 = false;
		}
	},
	
	function(){
		if($disable2 == false) {		
			t = setTimeout ( "rotateNormal()", 2000 );
			$disable2 = true;
		
		}
	});

$(document).ready(function() {	
		$("a.lightbox, area.lightbox").fancybox({
			'overlayOpacity'	:	0.7,
			'overlayColor'		:	'#FFF',
			'zoomSpeedIn'		:	400,
			'zoomSpeedOut'		:	400,
			'centerOnScroll'	:   false
		});
	});

$("a.lightbox").hover(
  function () {
	$offset = $(this).children('img').offset();
	$(this + ' img').remove();
	$('body').append('<img class="plus" src="/images/icons/plus.png" alt="enlarge" \/>');
	$('.plus').css({
        'top' 	: $offset.top-10 + 'px',
        'left' 	: $offset.left-10 + 'px'
      });
	if (jQuery.fn.pngFix) $(document).pngFix();
  }, 
  function () {
	$('.plus').remove();

  }
);


Cufon.replace('#carousel h1, .product_header h1', {
		color: '-linear-gradient(#fff, #e1e1e1, #adadad)'
	});

Cufon.replace('.post_title h1, .post_head h1', {
		color: '-linear-gradient(#e84044, #b92527)'
	});
		
Cufon.now();	

$('#sn_switch').click(function(){
		$('#tweet_box div:visible').slideUp();
		$('#tweet_box div:hidden').slideDown('slow');
		$image = $('#sn_switch img').attr('src');
		$image2 = $('#tweet_box .banner img').attr('src');
		$('#tweet_box .banner img').attr({src: $image});
		$('#sn_switch img').attr({src: $image2});
		return false;
	});

function scrollAd($element, $adHeight, $padding, $topOffset, $footerHeight){
		
	var aOffset = $($element).offset();
	
	if($(document).height() - $footerHeight - $padding < $(window).scrollTop() + $adHeight) {
		var $top = $(document).height() - $adHeight - $footerHeight - $padding - 20;
		$($element).attr('style', 'position:absolute; top:'+$top+'px;');
	
	}else if($(window).scrollTop() + $padding > $topOffset) {
		$($element).attr('style', 'position:fixed; top:'+$padding+'px;');
	
	}else{
		$($element).attr('style', 'position:relative;');
	}
	
}

$(window).scroll(function(){
	scrollAd('#nav', 100, 5, 185, 247);
	scrollAd('#side_ads', 576, 10, 231, 247);
});

//Tipsy
(function($) {
    $.fn.tipsy = function(opts) {

        opts = $.extend({fade: false, gravity: 'n'}, opts || {});
        var tip = null, cancelHide = false;

        this.hover(function() {
            
            $.data(this, 'cancel.tipsy', true);

            var tip = $.data(this, 'active.tipsy');
            if (!tip) {
            	if ($(this).attr('href') != '' && $(this).attr('href') != '#' && $(this).attr('href') != undefined) {
            		$image = '<img src="' + $(this).attr('href') + '" /><br />';
            	}else{
            		$image = '';
            	}
                tip = $('<div class="tipsy"><div class="tipsy-inner">' + $image + $(this).attr('title') + '</div></div>');
                tip.css({position: 'absolute', zIndex: 100000});
                $(this).attr('title', '');
                $.data(this, 'active.tipsy', tip);
            }
            
            var pos = $.extend({}, $(this).offset(), {width: this.offsetWidth, height: this.offsetHeight});
            tip.remove().css({top: 0, left: 0, visibility: 'hidden', display: 'block'}).appendTo(document.body);
            var actualWidth = tip[0].offsetWidth, actualHeight = tip[0].offsetHeight;
            
            switch (opts.gravity.charAt(0)) {
                case 'n':
                    tip.css({top: pos.top + pos.height, left: pos.left + pos.width / 2 - actualWidth / 2}).addClass('tipsy-north');
                    break;
                case 's':
                    tip.css({top: pos.top - actualHeight, left: pos.left + pos.width / 2 - actualWidth / 2}).addClass('tipsy-south');
                    break;
            }

            if (opts.fade) {
                tip.css({opacity: 0, display: 'block', visibility: 'visible'}).animate({opacity: 1});
            } else {
                tip.css({visibility: 'visible'});
            }

        }, function() {
            $.data(this, 'cancel.tipsy', false);
            var self = this;
            setTimeout(function() {
                if ($.data(this, 'cancel.tipsy')) return;
                var tip = $.data(self, 'active.tipsy');
                if (opts.fade) {
                    tip.stop().fadeOut(function() { $(this).remove(); });
                } else {
                    tip.remove();
                }
            }, 100);
            
        });

    };
})(jQuery);

$(".tip").tipsy({gravity: 's'});

$(".tip").click(function(){
	return false;
});

google.load("search", "1");

function OnLoad() {


	// Attach branding
	google.search.Search.getBranding(document.getElementById("branding"));
		
	// Create a search control
	var searchControl = new google.search.SearchControl();
		
	//open in expand mode
	options = new google.search.SearcherOptions();
	options.setExpandMode(google.search.SearchControl.EXPAND_MODE_OPEN);
	options.setRoot(document.getElementById("returnResults"));
		
	//set up websearch Limit to GNCC Racing
	var siteSearch = new google.search.WebSearch();
	siteSearch.setUserDefinedLabel("Racer X VT");
	siteSearch.setUserDefinedClassSuffix("siteSearch");
	siteSearch.setSiteRestriction("racerxvt.com");
	searchControl.addSearcher(siteSearch, options);
	
	searchControl.setSearchStartingCallback(this, OnLoad.prototype.OnSearchStarting);

	// Tell the searcher to draw itself and tell it where to attach
	searchControl.draw(document.getElementById("searchcontrol"));
	
	$("div.gsc-clear-button").click(
	  function () {
	  	 $("#branding").css({display:"none"});
		
	  }
	 );


}
OnLoad.prototype.OnSearchStarting = function(sc, searcher, query) {
  $("#branding").css({display:"inline"});
}
google.setOnLoadCallback(OnLoad);	
	

