var progress = 0;

$(document).ready(
	function() {
		
		//Homepage Features
		$('#feature div.featurecontent').removeClass('hidden');
		$("#progressbar").progressbar({value:progress});
		var features = $('#feature').tabs();  	// enable tabs
		var selected = features.tabs('option', 'selected');
		var length = features.tabs('length');
		features.tabs( "rotate", 8000, false );  	// rotation
		features.tabs( "option", "fx", { opacity:'toggle', duration:200 } );  // transition effect
		features.tabs( {show: function(event, ui) { progress=0;} });
		$("#featuretools li a").click(function(){ stopRotation(); });
		
		$('#featuretools .next').click(function() {
			selected = features.tabs('option', 'selected');
			var next = selected+1;   		// next tab
			if(next >= length) { next=0; }  // check range
			features.tabs('select', next); // switch to next tab
			stopRotation();
			return false;
		});
		$('#featuretools .prev').click(function() {
			selected = features.tabs('option', 'selected');
			var next = selected-1;   		// prev tab
			if(next < 0) { next=length-1; }  // check range
			features.tabs('select', next); // switch to next tab
			stopRotation();
			return false;
		});
		var t = setInterval(updateProgressBar, 150);
		function updateProgressBar(){
			progress = progress+2.03;
			$("#progressbar").progressbar({value:progress});
		}
		function stopRotation(){
			clearInterval(t); //stop timer
			features.tabs( "rotate", 0 );  //stop rotation
			$("#progressbar").progressbar({value:100});  //fill progressbar
		}
		
		
		//Homepage Tabs
		$('#hometabs div.tabcontent').removeClass('hidden');
		$('#hometabs').tabs({
			fx: { opacity:'toggle', duration:100 }
		});
		
		//Homepage Rotation
		$('#homerotations div.tabcontent').removeClass('hidden');
		$('#homerotations').tabs( {
			fx: { opacity:'toggle', duration:100 }
		});
		if($('#homerotations').hasClass('scroll')) {
			$('#homerotations').tabs("rotate", 7000, false);
		}
		
		
		//Video Launcher
		$("a.videobox").colorbox({inline:true, width:"555", height:"460", href:function(){
			var url = $(this).attr('href');
			return url;
		}});
		
		
		//Product Menu
		if(!$('body').hasClass('products')) {
			$('#headerwrapper').removeClass('open');
			$('.productnav').hide();
			$('.mainnav .productsservices').hoverIntent({ over:openProducts, timeout:0, out:doNothing });
			$('.productnav').hover(doNothing, closeProducts);
		}
		$('.productnav li:nth-child(4n)').addClass('noborder'); //no border every four li
		$('.productnav li a:gt(7)').addClass('noborder');		//no border last four a
		
		
		// Textbox Hints
		$('input.txbhint').focus(function() {
			if ($(this).val() == "" || $(this).val() == $(this).attr('title')){
				$(this).val("").removeClass("hinted");
			}
		}).blur(function() {
			if ($(this).val() == "") {
				$(this).val($(this).attr('title')).addClass("hinted");
			}
		}).filter(function() {
			if ($(this).val() == "" || $(this).val() == $(this).attr('title')){
				$(this).val($(this).attr('title')).addClass("hinted");
			}
		});

		
		// Careers Features
		$('.videoinfo').removeClass('hidden');
		var careersfeatures = $('#videos').tabs();  	// enable tabs
		var careersselected = careersfeatures.tabs('option', 'selected');
		var careerslength = careersfeatures.tabs('length');
		//careersfeatures.tabs( "rotate", 8000 );  		// rotation
		careersfeatures.tabs( "option", "fx", { opacity:'toggle', duration:500 } );  // transition effect
		
		$('body.careers #nav .next').click(function() {
			careersselected = careersfeatures.tabs('option', 'selected');
			var careersnext = careersselected+1;   		// next tab
			if(careersnext >= careerslength) { careersnext=0; }  // check range
			careersfeatures.tabs('select', careersnext); // switch to next tab
			return false;
		});
		$('body.careers #nav .prev').click(function() {
			careersselected = careersfeatures.tabs('option', 'selected');
			var careersnext = careersselected-1;   		// prev tab
			if(careersnext < 0) { careersnext=careerslength-1; }  // check range
			careersfeatures.tabs('select', careersnext); // switch to next tab
			return false;
		});
		

		
		
		// Wrap internal-page images with a caption block if they start with "caption:"
		  $("#content img").each(function() {
			var imgalt = $(this).attr('alt');
			var imgwidth = $(this).width();
			var captionsplit = imgalt.split(":");
			if (jQuery.trim(captionsplit[0].toLowerCase()) == "caption") {
			  $(this).wrap('<div class="captionbox" style="width:' + $(this).attr('width') + 'px" />');
			  $(this).after('<p>' + jQuery.trim(captionsplit[1]) + '</p>');
			}
		  });


		// Open links with rel="external" in new window - like ye ole' target="_blank"
		$('a[rel="external"]').click(function() {window.open( $(this).attr('href') ); return false;});


		// Zebra-stripe data tables
		$(".data tr:even").addClass("even");
				
		$(".data tr").mouseover(function() {
		$(this).addClass("over");
		}).mouseout(
		function() {$(this).removeClass("over");});

		$("td:last-child").css({border:"none"});
		
		// PNG Fix IE6
		DD_belatedPNG.fix('.featurecontent');
		DD_belatedPNG.fix('.pbox span');
		
		//Announcement Toggle
		$('body.productcategory .announcement a.actionButton').click(function() {
			var aText = $(this).text();
			$("body.productcategory .announcement .announcementContent").slideToggle("slow");
			if(aText == "Read More") {
				$(this).text("Close");
			}
			else {
				$(this).text("Read More");
			}			
			return false;
		});
		
		// Announcement rearranging for product detail pages 
		if(elementExists('announcement')) {
			$('body.productdetail .main2col').attr('style','top:0;');
		}
				  
	}
);

function openProducts(){ $('#headerwrapper').addClass('open'); $('.productnav').slideDown('normal'); }
function closeProducts(){ $('.productnav').slideUp('fast', function(){ $('#headerwrapper').removeClass('open'); }); }
function doNothing(){}

function elementExists(id) {
	return document.getElementById(id);
}
