var totalTestimonials = 0;
var testimonials_itemList = [];
var bannerInterval;
var bannerRotateIndex=0;
$(document).ready(function() {
	
	// top navigation flyouts
	RenderNavigation("Navigation");
	
	// preload active state images
	var preloadImages = ["/images/redesign2009/bannerButton_active.jpg","/images/redesign2009/facebook_active.jpg", "/images/redesign2009/logoFooter_active.png", "/images/redesign2009/navigationBg_active.jpg", "/images/redesign2009/productButton_active.jpg", "/images/redesign2009/sideboxNavButtonNext_active.jpg", "/images/redesign2009/sideboxNavButtonPrev_active.jpg", "/images/redesign2009/techtalkFooter_active.jpg", "/images/redesign2009/twitter_active.jpg", "/images/redesign2009/youtube_active.jpg", "/images/redesign2009/rssFooter_active.jpg"];
	var loadedImagesCount = 0;
	for (var i = 0; i < preloadImages.length; i++) {
		$("body").append("<img class='hide' id='PreloadImage_" + i + "' src='" + preloadImages[i] + "'/>");
	}
	
	// handle language choose
	$("#LanguageChooser").change(function() {
		
		switch ($(this).val()) {
			case 'de':
				window.location.href = "/de/pcmatic.asp";
				break;
			case 'es':
				window.location.href = "/es/pcmatic.asp";
				break;
			case 'fr':
				window.location.href = "/fr/pcmatic.asp";
				break;
			case 'it':
				window.location.href = "/it/pcmatic.asp";
				break;
			case 'ptb':
				window.location.href = "/br/pcmatic.asp";
				break;
			case 'jp':
				window.location.href = "http://pcmatic.jp";
				break;
			
		}
		
	});
	
	// switch banner content areas
	if (($.browser.msie && $.browser.version >= 7) || !$.browser.msie) {
			
		$("#BannerImages").jcarousel({
	      	scroll: 1,
			visible: 1,
			animation: 1500,
		    buttonNextHTML: null,
		    buttonPrevHTML: null,
			initCallback: banners_initCallback
	    });
		
		
	} else {
		
		// ie6 doesnt get the filmstrip effect for the banners because it can't deal with z-indexes properly and the top nav goes behind the banners
		// noCarousel class allows us to hide/show banners in a different manner than the carousel
		$("#MainWrapper #Content #Column1 #Banner ul#BannerImages li").addClass("noCarousel");
		// make first banner active
		$("#MainWrapper #Content #Column1 #Banner ul#BannerImages li:first").addClass("active");
		
	}
	
	// make clicked banner button active
	$("#MainWrapper #Content #Column1 #Banner #BannerBottom #BannerButtons .bannerButton .buttonContainer a").click(function(e) {
		
		if (!e.isAutoClicked) {
			clearInterval(bannerInterval);	
		}		
		
		$("#MainWrapper #Content #Column1 #Banner #BannerBottom #BannerButtons .bannerButton").removeClass("active");
		$(this).parent().parent().addClass("active");
		
		// ie6 doesnt get the filmstrip effect for the banners because it can't deal with z-indexes properly and the top nav goes behind the banners
		if ($.browser.msie && $.browser.version < 7) {
			
			// make all banners inactive
			$("#MainWrapper #Content #Column1 #Banner ul#BannerImages li").removeClass("active");
			// make clicked banner active
			$("#Banner" + $(this).attr("class")).addClass("active");
			
		}
		
	});
	
	
	// auto-rotate banners every 15 seconds, if banner button is manually clicked, then auto-rotate is turned off
	bannerInterval = setInterval(rotateBanner,15000);
	
	// library tabs
	$("#MainWrapper #Content #Column1 #Libraries #LibraryWrapper #LibraryInnerWrapper ul.tabs li a").click(function() {
		
		// get content id
		var id = $(this).attr("id").split("_")[1];
		
		// remove all active tabs
		$("#MainWrapper #Content #Column1 #Libraries #LibraryWrapper #LibraryInnerWrapper ul.tabs li").removeClass("active");
		
		// make clicked tab active
		$(this).parent("li").addClass("active");
		
		// remove active content
		$("#MainWrapper #Content #Column1 #Libraries #LibraryWrapper #LibraryInnerWrapper .content .libraryContent").removeClass("active");
		
		// make related content active
		$("#LibraryContent_"+id).addClass("active");
		
		return false;
		
	});
	
	// wireup testimonial navigation
	totalTestimonials = $("#TestimonialList .sideboxInnerTop ul li").size();
	$("#TestimonialTotal").html(totalTestimonials);
	$("#TestimonialList .sideboxInnerTop ul li").each(function() {
		// push testimonial objects into testimonials_itemList array
		testimonials_itemList.push({
			content: $(this).html()
		});
		
	});

	$("#TestimonialList .sideboxInnerTop ul").jcarousel({
		scroll: 1,
		visible: 1,
		initCallback: testimonials_initCallback,
		buttonNextHTML: null,
		buttonPrevHTML: null,
		wrap: "circular",
		itemVisibleInCallback: {
			onBeforeAnimation: testimonials_itemVisibleInCallback
		},
		itemVisibleOutCallback: {
			onAfterAnimation: testimonials_itemVisibleOutCallback
		},
		size: totalTestimonials
	});
		
	// make columns equal height
	$(".column").equalHeights().css("overflow","hidden");
	
});



function banners_initCallback(carousel) {
			
	$(".bannerButton a").click(function() {
        carousel.scroll($.jcarousel.intval($(this).attr("class")));
        return false;
    });
}

function testimonials_initCallback(carousel) {
    $('#Testimonials .pagination .next').click(function() {
        carousel.startAuto(0);
		carousel.next();
        return false;
    });

    $('#Testimonials .pagination .previous').click(function() {
        carousel.startAuto(0);
		carousel.prev();
        return false;
    });
}

function testimonials_itemVisibleInCallback(carousel, item, i, state, evt)
{ 
    var idx = carousel.index(i, testimonials_itemList.length);
    carousel.add(i, testimonials_getItemHTML(testimonials_itemList[idx - 1]));
	$("#TestimonialCurrent").html(idx);
}

function testimonials_itemVisibleOutCallback(carousel, item, i, state, evt)
{
    carousel.remove(i);
}

function testimonials_getItemHTML(item)
{
    return item.content;
}

function rotateBanner() {
		
	bannerRotateIndex++;
	if (bannerRotateIndex>$("ul#BannerImages li").size() - 1) bannerRotateIndex=0;
	
	e = $.Event('click');
    e.isAutoClicked = true;
	
	$("#MainWrapper #Content #Column1 #Banner #BannerBottom #BannerButtons .bannerButton .buttonContainer a:eq(" + bannerRotateIndex + ")").trigger(e);
	
}
