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

// IE6 alpha PNG support
// http://www.dillerdesign.com/experiment/DD_belatedPNG/
var DD_belatedPNG;
if (DD_belatedPNG) {
  DD_belatedPNG.fix('#SiteTools .content,#SiteTools fieldset');
  DD_belatedPNG.fix('#Content ul.homeColumns li a.more, #AtreNetSpotlight a.more, #Content ul.clientList li.caseStudy a, #ClientListLegend img');
  DD_belatedPNG.fix('#Content ul.rapidRelaunchSteps li a, #Content ul.rapidRelaunchSteps li.first a, #Content ul.rapidRelaunchSteps li a.ui-state-active, #Content ul.rapidRelaunchSteps li.first a.ui-state-active');
  DD_belatedPNG.fix('#Content dl.featuredClients ul.actions li a, ul#FeaturedClientNavigation li a.back, ul#FeaturedClientNavigation li a.next, #Content dl.featuredClients ul.actions li a');
  DD_belatedPNG.fix('.services #Content ul.columns li.column .footer a.more, #Sidebar #SecondaryNavigation li a.active, #Sidebar #SecondaryNavigation li.over a, #Content .featuredProject .footer a.more, #Content .featuredWhitePaper .footer a.download');
  DD_belatedPNG.fix('.ui-dialog, .ui-dialog  .channel, .ui-dialog-titlebar-close');
  DD_belatedPNG.fix('.cmsDiagram img');
}

/* Special search behavior */
var Search = {
  prompt: "Search", // default value - should get real value from markup
  init: function() { this.prompt = $('#q').focus(this.focus).blur(this.blur).val(); },
  focus: function() { if (this.value == Search.prompt) { this.value = ''; } },
  blur: function() { if (this.value.length == 0) { this.value = Search.prompt; } }
};

var NewsletterSignup = {
	prompt: "Submit",
	lastId: null,
	init: function() {
		$('#NewsletterSignup, #SubscribeModal').each(function() {
			var $$ = $(this);
			if (this.id == 'SubscribeModal') {
				$$.find('form').submit(NewsletterSignup.submitModal);
			} else {
	 			$$.find('form').submit(NewsletterSignup.submit);
				var $s = $$.find('input.submit');
				NewsletterSignup.prompt = $$.find('input.text')
											.focus(NewsletterSignup.focus)
											.blur(NewsletterSignup.blur)
											.val();
				var $a = $('<a href="#" class="submit">' + $s.val() + '</a>');
				$a.click(function(e) { e.preventDefault(); $(this).parents('form:first').submit(); });
				$s.replaceWith($a);
			}
		});
	},
	submitModal: function(e) {
		e.preventDefault();
		NewsletterSignup.lastId = 'SubscribeModal';
		var $$ = $(this);
		$.post($$.attr('action'), $$.serialize(), NewsletterSignup.completed, 'json');
		return false;		
	},
	submit: function(e) {
		e.preventDefault();
		NewsletterSignup.lastId = 'NewsletterSignup';
		var $$ = $(this);
		$.post($$.attr('action'), $$.serialize(), NewsletterSignup.completed, 'json');
		return false;
	},
	completed: function(data) {
		var $$ = $('#' + NewsletterSignup.lastId);
		if (data['status'] == 'error') {
			var $msg_area = $$.find('#MessageArea');
			if ($msg_area.length == 0) {
				$msg_area = $('<p class="error" id="MessageArea"></p>');
				$$.prepend($msg_area);
			}
			$msg_area.html(data['message']).show().fadeTo(2500, 1, function(e) {
				$(this).fadeOut('normal');
			});
		} else {
			$$.html(data['message']);
		}
	},
	focus: function() { if (this.value == NewsletterSignup.prompt) { this.value = ''; } },
	blur: function() { if (this.value.length == 0) { this.value = NewsletterSignup.prompt; } }
};

var WhitePaperSignup = {
	prompt: "Submit",
	init: function() {
		var $$ = $('#SubscribeWhitePaperForm');
		this.prompt = $$.find('input.emailField').focus(this.focus).blur(this.blur).val();
	},
	focus: function() { if (this.value == WhitePaperSignup.prompt) { this.value = ''; } },
	blur: function() { if (this.value.length == 0) { this.value = WhitePaperSignup.prompt; } }
};

$(document).ready(function() {
	Search.init();
	NewsletterSignup.init();
	WhitePaperSignup.init();
});

// Convert un-tabbed display into tabbed display and activate tabs
$(document).ready(function() {
	var $tabs = $('#Content div.tabsPreSetup');
	var $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.tab');
					var title = $h2.html();
					$tabnav.append('<li id="' + id + 'Tab"><a href="#' + id + '" class="noPopup"><span>' + title + '</span></a></li>');
					$h2.remove();
				});
	// Need delay for browsers to get their DOM in order
	setTimeout(function() { $tabs.tabs(); }, 500);

	// Only move to the top of the page when the tabs are present so not to affect real anchor links.
	var tabsPresent = $('div.tabsPreSetup').size(this);
	if (tabsPresent > 0)
	{
		// Move to the top of the page
		window.scrollTo(0,0);
	}

	$tabs.removeClass('tabsPreSetup');
});


$(document).ready(function() {
	$(".popupwindow").popupwindow();
	$('a[href$=.pdf],a[href^=http://],a[href^=https://]')
		.not($('a.noPopup'))
		.popupwindow();
});

/* begin - Rapid Relaunch Steps */
var  $tabs = '';

var RapidRelaunchSteps = {

  init: function() {
	$tabs = $('#Content div.stepsPreSetup');
	var $tabnav = $tabs.prepend('<ul class="rapidRelaunchSteps selfClear"></li>').find('> ul');
	$tabs	.find('> div')
				.each( function(i) {
					var id = this.id;
					var $h3 = $(this).find('h3');
					var title = $h3.html();
					$tabnav.append('<li id="' + id + 'Tab"><a href="#' + id + '" class="noPopup"><span>' + title + '</span></a></li>');
					$h3.remove();
				});
	// Need delay for browsers to get their DOM in order
	setTimeout(function() { $tabs.tabs(); }, 500);

	// Only move to the top of the page when the tabs are present so not to affect real anchor links.
	var tabsPresent = $('div.stepsPreSetup').size(this);
	if (tabsPresent > 0)
	{
		// Move to the top of the page
		window.scrollTo(0,0);
	}

	$tabs.removeClass('stepsPreSetup');
	
		var c = this;
		var n = $('ul.rapidRelaunchSteps');
		
		// 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); });
  	
  	  $('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'); 
		$show_content_id = $(this).addClass('currentActive').attr('id').replace('Tab', '');
		$current_content_id = $('.rapidRelaunchSteps li.ui-tabs-selected').removeClass('ui-tabs-selected');
		$tabs.tabs('select', $show_content_id);
	},
	mouseleave: function() { 
		$(this).removeClass('over').children('a').removeClass('over'); 
		$('.rapidRelaunchSteps li.currentActive').removeClass('currentActive');
		//$current_content_id = $('.rapidRelaunchSteps li.currentActive').attr('id').replace('Tab', '');
		//$tabs.tabs('select', $current_content_id);
	}
};

$(function() { 
	RapidRelaunchSteps.init(); 
});
/* end - Rapid Relaunch Steps */

/* Simple show/hide content on the Featured Projects tab on the Clients page. */
$(function() { 
	$("ul#FeaturedClientNavigation li a.back").addClass("disabled").css('background-image','url(../i/common/back-disabled.png)');
	$("#FeaturedClientTwo").hide();
	$("ul#FeaturedClientNavigation li a.back").bind("click", function(e){
		$("#FeaturedClientTwo").hide();
		$("#FeaturedClientOne").show();
		$("ul#FeaturedClientNavigation li a.next").removeClass("disabled").css('background-image','url(../i/common/next.png)');
		$("ul#FeaturedClientNavigation li a.back").addClass("disabled").css('background-image','url(../i/common/back-disabled.png)');
		return false;
    });
	$("ul#FeaturedClientNavigation li a.next").bind("click", function(e){
		$("#FeaturedClientOne").hide();
		$("#FeaturedClientTwo").show();
		$("ul#FeaturedClientNavigation li a.back").removeClass("disabled").css('background-image','url(../i/common/back.png)');
		$("ul#FeaturedClientNavigation li a.next").addClass("disabled").css('background-image','url(../i/common/next-disabled.png)');
		return false;
    });
});

$(document).ready(function() {
	$("#SubscribeModal").dialog({
		bgiframe: true,
		closeOnEscape: true,
		autoOpen: false,
		modal: true,
		width: 528
	});

	$('#SubscribeNow').click(function() {
		$('#SubscribeModal').dialog('open');
		return false;
	});
	$('#SubscribeModal a.submit').click(function() {
		$('#SubscribeModal').dialog('close');
	});
});

$(document).ready(function() {
	$("a[href*='#todo']").addClass('todoLink');
});


// Mini Gallery
$(document).ready(function() {
	var $div = $('div.galleryWidget');
	$div.each(function() {
		var $$ = $(this);
		var id = $$.attr('id');
		var flashvars = {
			xml: $$.metadata().xml,
			bannersFolder: $$.metadata().bannersFolder
		};
		var params = {
			allowFullScreen: "false",
			scale: "noscale",
			menu: "false",
			wmode: "transparent"
		};
		swfobject.embedSWF("/mini-gallery/PowerRotator.swf", id, 640, 420, '9.9.0', '/minigallery/expressInstall.swf', flashvars, params);
	});
});

// Thumbnail Gallery
$(document).ready(function() {
	var $div = $('div.thumbnailGallery');
	$div.each(function() {
		var $$ = $(this);
		var id = $$.attr('id');
		var flashvars = {
			xml: $$.metadata().xml,
			bannersFolder: $$.metadata().bannersFolder
		};
		var params = {
			allowFullScreen: "false",
			scale: "noscale",
			menu: "false",
			wmode: "transparent"
		};
		swfobject.embedSWF("/mini-gallery/thumbs.swf", id, 320, 240, '9.9.0', '/minigallery/expressInstall.swf', flashvars, params);
	});
});

// Home Gallery
$(document).ready(function() {
	$('#HomeMast').each(function() {
		var $$ = $(this);
		var id = $$.attr('id');
		var flashvars = {
			XMLpath: $$.metadata().xml
		};
		var params = {
			wmode: "transparent" 
		};
		swfobject.embedSWF("/media/home/swf/home-banner.swf", id, 940, 425, '8', '/minigallery/expressInstall.swf', flashvars, params);
	});
});

// Company Intro
$(document).ready(function() {
	$('#CompanyIntro').each(function() {
		var $$ = $(this);
		var id = $$.attr('id');
		var flashVars = {};
		var flashParams = {
			quality: "high",
			wmode: "transparent",
			menu: "false",
			allowScriptAccess: "sameDomain"
		};
		var flashAttributes = {};
		swfobject.embedSWF("/media/company-intro/companyintro.swf", id, "710", "272", "8.0.0.0", '/media/expressInstall.swf', flashVars, flashParams, flashAttributes);
	});
});

var $dialog;

$('.ui-widget-overlay').live('click', function(event) {
	if ($dialog) {
		$dialog.dialog('close');
	}
});

var Gallery = {
	id: 'GalleryContainer',
	init: function() {
		$('a.gallery,area.gallery').click(Gallery.click);
	},
	click: function(event) {
		event.preventDefault();
		$div = $('#' + Gallery.id);
		if ($div.length == 0) {
				$div = $('<div id="' + Gallery.id + '" style="display: none;"></div>').appendTo('body');
		}
		$div.load(this.href + ' div.modalContent', Gallery.showDialog);		
	},
	showDialog: function() {
		$div = $('#' + Gallery.id);
		if ($div.length == 0)
			return;
		
		var flashVars = {
			initialURL: escape(document.location),
			xmlfile: '/gallery_v2/images.xml'
		};
		var flashParams = {
			quality: "high",
			wmode: "transparent",
			menu: "false",
			allowScriptAccess: "sameDomain",
			allowFullScreen: "true"
		};
		var flashAttributes = {};
		if (swfobject) {
			swfobject.embedSWF('/gallery_v2/slideshow.swf', 'Gallery', 960, 800, '9.0.0.0', '/gallery_v2/expressinstall.swf', flashVars, flashParams, flashAttributes);
		}
		$dialog = $div.dialog({
				dialogClass: 'gallery-ui-dialog',
				draggable: false,
				modal: true,
				bgiframe: true,
				width: 960,
				height: 830,
				closeText: 'Return to AtreNet Site',
				resizable: true,
				title: 'AtreNet Gallery',
		   	open: function(event, ui) { }
		}).dialog('open');
	}
};

var Testimonials = {
	id: 'TestimonialsContainer',
	init: function() {
		$('ul.testimonials a').click(Testimonials.click);
	},
	click: function(event) {
		event.preventDefault();
		$div = $('#' + Testimonials.id);
		if ($div.length == 0) {
				$div = $('<div id="' + Testimonials.id + '" style="display: none;"><div id="Testimonial"></div></div>').appendTo('body');
		}
		Testimonials.showDialog(this.href);
	},
	showDialog: function(swf) {
		$div = $('#' + Testimonials.id);
		if ($div.length == 0)
			return;
		
		var flashVars = {
		};
		var flashParams = {
			quality: "high",
			wmode: "transparent",
			menu: "false",
			allowScriptAccess: "sameDomain"
		};
		var flashAttributes = {};
		if (swfobject) {
			swfobject.embedSWF(swf, 'Testimonial', 461, 360, '8.0.0.0', null, flashVars, flashParams, flashAttributes);
		}
		$dialog = $div.dialog({
				// dialogClass: 'gallery-ui-dialog',
				draggable: false,
				modal: true,
				bgiframe: true,
				width: 481,
				// height: 830,
				// closeText: 'Return to AtreNet Site',
				resizable: false,
		   		open: function(event, ui) { }
		}).dialog('open');
	}
};
$(function() { 
	Gallery.init(); 
	Testimonials.init();
	// Check if 'launchGallery' is in the query string
	var launchGallery = $.query.get("launchGallery"); 
	if (launchGallery)
	{
		// Launch the gallery if 'launchGallery' is found in the query string
		$('#ToolBar a.gallery').click();
	}
	$('.cmsDiagram').hover(function() { $(this).find('img.large').show(); }, function() { $(this).find('img.large').hide(); });
});

$(document).ready(function(){
    $("#AtreNewsNav").change(function() {
		window.location = $("option:selected", this).val();
    });
});
