// Fixes the transparent PNGs in IE6
var DD_belatedPNG;
if (DD_belatedPNG) { // Only execute for IE6
	// Selectors need to match what's in the CSS exactly
	DD_belatedPNG.fix('#Content ul.documents li a.pdf, #Content ul.documents li a.video');
	DD_belatedPNG.fix('#Content .productInfographic .largeBox a, #Content .productInfographic .largeBox a span,#Content .productInfographic .smallBox a, #Content .productInfographic .smallBox a span');
	DD_belatedPNG.fix('#Sidebar .faithQuote');
}

// Control images that are displayed for a product
var ProductShots = {
	selectedIndex: 0,
	productShots: [],
	init: function() {

		var PS = ProductShots;
		//PS.productShots = $('.productShots');
		PS.productShots = $('div.standardContainer').siblings('div.productShots');
		
		if (PS.productShots.length == 0) return;

		var $lis = PS.productShots.find('ul.thumbnails li');
		if ($lis.length == 0) return;
		
		// Need to call prettyPhoto to setup the open function if it's not already avail
		if ($.fn.prettyPhoto && ! $.prettyPhoto.open)
			$('a.prettyPhoto').prettyPhoto({theme: 'facebook', allowresize: false });

		
		if ($lis.length == 1) {
			$('ul.thumbnails').attr('style','display: none'); // Hide thumbnail if there's only 1
		} 
		else {
			PS.setupEvents($lis);
			var $selected = $lis.filter('.selected');
			if ($selected.length == 0) $selected = $lis;
			PS.select($selected[0]);
		}
	},
	setupEvents: function($lis) {
		$lis.hover(ProductShots.thumbOver, ProductShots.thumbOut).click(ProductShots.click);
		$('#ProductImage').click(ProductShots.largeClick);
	},
	thumbOver: function(event) {
	},
	thumbOut: function(event) {
	},
	click: function(event) {
		ProductShots.select(this);
	},
	largeClick: function(event) {
		// Convert src from thumb to product image
		var src = $(this).attr('src');
		src = src.replace('-med.png','-large.png');
		if ($.prettyPhoto) {
			$.prettyPhoto.open(src, '', $(this).attr('title'));
		}
	},
	select: function(elm) {
		
		var $selected_lis = $(elm).siblings('.selected') // li.selected
															.removeClass('selected');
		
		$(elm).addClass('selected');

		// Convert src from thumb to product image
		var $img = $(elm).find('> img');
		var src = $img.attr('src');
		src = src.replace('-thumb.png','-med.png');
		$('#ProductImage').attr( { src: src, title: $img.attr('title') } );
	}
};
$(function() { ProductShots.init(); });

// Convert on-tabbed display into tabbed display and activate tabs
var Tabs = {
	init: function() {
		if (! $.fn.tabs) {
			return;
		}
		var $tabs = $('div.tabs');
		var $tabnav = $tabs.find('ul.tabNav');
		var liClass = "first";
		if ($tabnav.length == 0) {
			$tabnav = $tabs.prepend('<ul class="tabNav selfClear"></li>').find('> ul');
			$tabs	.find('> div')
					.each( function(i) {
						var id = this.id;
						var $h2 = $(this).find('h2');
						var title = '';
						if ($.fn.metadata && $(this).metadata().tabTitle)
							title = $(this).metadata().tabTitle;
						else
						 	title = $h2.html();
						
						$tabnav.append('<li class="'+ liClass +'"><a href="#' + id + '" class="noPopup"><span>' + title + '</span></a></li>');
						$h2.remove();
						liClass = "";
					} );
			// Need delay for browsers to get their DOM in order
			setTimeout(function() { $tabs.tabs(); }, 500);
		}
	}
};
$(document).ready(Tabs.init);

// Convert on-tabbed display into tabbed display and activate event and webcasts tabs on homepage
var EventTabs = {
	init: function() {
		if (! $.fn.tabs) {
			return;
		}
		var $tabs = $('div.eventTabs');
		var $tabnav = $tabs.find('ul.tabNav');
		var liClass = "first";
		if ($tabnav.length == 0) {
			$tabnav = $tabs.prepend('<ul class="tabNav selfClear"></li>').find('> ul');
			$tabs	.find('> div')
					.each( function(i) {
						var id = this.id;
						var $h2 = $(this).find('h2');
						var title = '';
						if ($.fn.metadata && $(this).metadata().tabTitle)
							title = $(this).metadata().tabTitle;
						else
						 	title = $h2.html();
						
						$tabnav.append('<li class="'+ liClass +'"><a href="#' + id + '" class="noPopup"><span>' + title + '</span></a></li>');
						$h2.remove();
						liClass = "";
					} );
			// Need delay for browsers to get their DOM in order
			setTimeout(function() { $tabs.tabs(); }, 500);
		}
	}
};
$(document).ready(EventTabs.init);

var Todo = {
	init: function() {
		$('a[href*=#todo]').addClass('todo');
	}
};
$(document).ready(Todo.init);

var viewVideoModal = function(e) {
	e.preventDefault();
	// Need to call prettyPhoto to setup the open function if it's not already avail
	// if (! $.fn.prettyPhoto)
	// 	return;
		
	// Reset prettyPhoto to use the standard flash markup code
	var options = {
		theme: 'facebook',
		flash_markup: '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="{width}" height="{height}"><param name="wmode" value="{wmode}" /><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="movie" value="{path}" /><embed src="{path}" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="{width}" height="{height}" wmode="{wmode}"></embed></object>'
	};
	$.prettyPhoto.settings(options);
	$.prettyPhoto.open('/images/homepage/flv_wrapper.swf?width=862&height=485', '', '');
	pageTracker._trackPageview("/homepage/coverity5BugVideo");
}

$(document).ready(function(){
	$('a.bugVideo').click(viewVideoModal);
});

// Benefits Modal window
var $dialog;

$(document).ready(function() {
	$('#Benefits').dialog({
		autoOpen: false,
		draggable: false,
		modal: true,
		bgiframe: false,
		width: 896,
		buttons: {}
	});
	

	$('#Sidebar a.whatWeOffer').click(function(e) {
		e.preventDefault();
		$('#Benefits').dialog('open');
	});
	$('#Benefits a.close').click(function(e) {
		e.preventDefault();
		$('#Benefits').dialog('close');
	});
});