// Fix background image flickering in IE
try {
  document.execCommand('BackgroundImageCache', false, true);
} catch(e) { }

/* begin - Navigation Primary */
var NavigationPrimary = {
  init: function() {    
		var c = this;
		var n = $('#NavigationPrimary');
		
		// Use bgiframe to fix dropdowns over selects if it's available.
		$.fn.bgiframe && $('li', n).bgiframe();

		// Mark parent elements so that we can style them with CSS
		$('> li', n).each(function() { c.markParents(this); });
  	
  	// Use the hoverintent plugin if it's available
  	if ($.fn.hoverIntent)
    {
  	  $('li', n).hoverIntent( {
  	    sensitivity: 1,
  	    over: this.mouseenter,
  	    out: this.mouseleave,
  	    timeout: 100
  	  });
    }
  	else
  	  $('li', n).hover(this.mouseenter, this.mouseleave);
  },
  markParents: function(li) {
    var c = this;
    if ($('> ul', li).size() > 0)
	    $(li).addClass('parent').find('> a').addClass('parent').end().find('> ul li').each( function() { c.markParents(this); });
	},
	mouseenter: function() { $(this).addClass('over').children('a').addClass('over'); },
	mouseleave: function() { $(this).removeClass('over').children('a').removeClass('over'); }
	
};
$(function() { NavigationPrimary.init(); });
/* end - Primary Navigation */

var DD_belatedPNG;
if (DD_belatedPNG) { // Only execute for IE6
	// Selectors need to match what's in the CSS exactly
	DD_belatedPNG.fix('#InstallationMap, .mainContent ul.deployments ul li.location, a.pp_close');
	DD_belatedPNG.fix('.mainContent ul.deployments .carousel .jcarousel-next, .mainContent ul.deployments .carousel .jcarousel-prev');
	DD_belatedPNG.fix('#ContentChannel ul.biographies .bio .photo a');
}

$(document).ready(function() {
	$('#VideoSustainability').remove().appendTo('body');	
	$("a[rel^='prettyPhoto']").prettyPhoto({show_title: false});
});

var InstallationMap = {
  init: function() {
		var $installations = $('#Installations');
		if ($installations.length == 0)
			return;
		
		var showInstallation = "";
		if(window.location.hash != null && window.location.hash != "") {
			showInstallation = window.location.hash;	
		}
		
		$.metadata.setType("attr", "data");
		var $map = $('#InstallationMap');
		if ($map.length == 0) {
			$map = $installations.prepend('<ul id="InstallationMap"></ul>').find('> ul#InstallationMap');
			var z = 0;
			var id;
			$('ul.deployments li.deployment').each(function() {
				var title = $(this).find('h2').val();
				var x = $(this).metadata().xCoord;
				var y = $(this).metadata().yCoord;
				//var li_size = $(this).find('li.location').size();
				//if (li_size <= 1) {
				//	id = $(this).find('li.location').attr('id');
				//	$map.append('<li style="left: '+ x +'px; top: '+ y +'px;"><a href="#" data={point:"#'+ id +'"} class="dot showDeployment noPopup" title="' + title + '">' + title + '</a></li>');
				//}
				//else {
					var flyout_direction = $(this).metadata().direction;
					dot_and_flyout = '<li style="left: '+ x +'px; top: '+ y +'px;"><a href="#" class="dot showLocations noPopup" title="' + title + '">' + title + '</a>';
					dot_and_flyout += '<div class="allLocations '+ flyout_direction +'"><div class="content"><a href="#" class="close">Close</a><ul>';
					$(this).find('li.location').each(function() {
						title = $(this).find('div.levelTwoLocation').html();
						id = $(this).attr('id');
						dot_and_flyout += '<li><a class="showDeploymentFromGroup" data={point:"#'+ id +'"} href="#"><span>'+ title +'</span></a></li>';
					});
					dot_and_flyout += '</ul></div></div></li>';
					$map.append(dot_and_flyout);
				//}
				z++;
			});
			/*$('#InstallationMap a.showDeployment').live('click',function(e) {
				e.preventDefault();
				selected_deployment = $(this).metadata().point;
				$('#InstallationMap > li.active').removeClass('active').css('z-index','30');
				$('#InstallationMap li.active').removeClass('active');
				$('#InstallationMap .allLocations').hide();
				$('ul.deployments li.deployment').hide().find('li.location').hide();
				$(this).parent().addClass('active').css('z-index','40');
				$(selected_deployment).show().parent().parent().show();
				$(selected_deployment).find('.carousel ul.images').jcarousel({
					scroll: 1,
					wrap: 'both'
				});
				$(selected_deployment).find('.carousel div[disabled=true]').attr('disabled', 'false');
			});*/
			$('#InstallationMap a.showDeploymentFromGroup').live('click',function(e) {
				e.preventDefault();
				selected_deployment = $(this).metadata().point;
				$('ul.deployments li.deployment').hide().find('li.location').hide();
				$(this).parent().parent().find('> li.active').removeClass('active');
				$(this).parent().addClass('active');
				$(selected_deployment).show().parent().parent().show();
				$(selected_deployment).find('.carousel ul.images').jcarousel({
					scroll: 1,
					wrap: 'both'
				});
				$(selected_deployment).find('.carousel div[disabled=true]').attr('disabled', 'false');
			});
			$('#InstallationMap > li').hover(this.mouseenter, this.mouseleave);
			$('#InstallationMap a.close').live('click',function(e) {
				e.preventDefault();
				$('#InstallationMap li.active').removeClass('active').css('z-index','30');
				$(this).parent().parent().hide();
			});
			$('#InstallationMap a.dot').live('click',function(e) {
				e.preventDefault();
			});
			if(showInstallation != "") { 
				$('ul.deployments li.deployment').hide().find('li.location').hide();
				$(showInstallation).addClass('test');
				$(showInstallation).show().parent('ul').parent('li.deployment').show().siblings().hide();
				$(showInstallation).find('.carousel ul.images').jcarousel({
					scroll: 1,
					wrap: 'both'
				});
				$(showInstallation).find('.carousel div[disabled=true]').attr('disabled', 'false');
			}
		}
	},
	mouseenter: function() { 
		$('#InstallationMap li.active').removeClass('active').css('z-index','30');
		$(this).addClass('active').css('z-index','40');
		$(this).children().next().show();
	},
	mouseleave: function() { 
		$('#InstallationMap li.active').removeClass('active').css('z-index','30');
		$(this).children().next().hide();
	}
};
$(function() { InstallationMap.init(); });

$(document).ready(function() {
	$('.latestTweets ul').jcarousel({
		vertical: true,
		scroll: 1,
		wrap: 'both',
		width: 545
	});
});

var ExpandableContent = {
  init: function(e) {
		$('ul.expandable a.collapse, ul.expandable .viewMore').hide();
		$('ul.expandable a.expand').click(ExpandableContent.expandHandler);
		$('ul.expandable a.collapse').click(ExpandableContent.collapseHandler);
	},
	expand: function($expandable_div, $clicked_link) {
		ExpandableContent.reset($expandable_div);
		$clicked_link.hide().next().show().next().slideDown(500);
	},
	collapse: function($expandable_div) {
		ExpandableContent.reset($expandable_div);
	},
	reset: function($expandable_div) {
		$expandable_div.find('a.collapse').hide();
		$expandable_div.find('.viewMore').slideUp(500);
		$expandable_div.find('a.expand').show();
	},
	collapseHandler: function(e) {
		e.preventDefault();
		ExpandableContent.collapse($(this).parents('ul.expandable'));
	},
	expandHandler: function(e) {
		e.preventDefault();
		ExpandableContent.expand($(this).parents('ul.expandable'), $(this));
	}
};
$(function() { ExpandableContent.init(); });

