try { document.execCommand("BackgroundImageCache", false, true); } catch(e) {}

sIFR.repaintOnResize = false;
sIFR.fromLocal = true;

$.Field.prototype.showError = function(){
	this.$element.addClass('invalid');
	var $errors = this.$element.parents('form').eq(0).find('div.errorDiv');
	if ($errors.size() < 1) $errors = $('#commentErrors');
	$errors.append('<span class="row error">' + this.getErrorMsg() + '</span>');
}

$.Field.prototype.hideError = function(){
	this.$element.removeClass('invalid');
	var $errors = this.$element.parents('form').eq(0).find('div.errorDiv');
	if ($errors.size() < 1) $errors = $('#commentErrors');
	$errors.find('span:contains(' + this.getErrorMsg() + ')').remove();
}

function _embedVideo(id, url){
	var params = {
	  menu: "false", bgcolor: "#FFFFFF",allowFullScreen:"true", wmode: "opaque"
	 };
	 
	var videoId = url.substring(url.indexOf('?v=') + 3, url.length);
	var thisURL = 'http://www.youtube.com/v/' + videoId + '&enablejsapi=1&rel=0&fs=1';
	swfobject.embedSWF(thisURL, id, "460", "305", "9.0.115", null, {}, params, {});
};

jQuery(function($){
	$('body').addClass('hasJS');
	
	var commentAuthorTracking;
	
	$.sifr.replace($.sifr.gothamBook,{
	   selector: '#mastheadNav ul p',
	   css: {
			'.sIFR-root': { 
				'color': '#404040', 
				'font-size': 14
			},
			'a': { 'text-decoration': 'none' },
			'a:link': { color: '#404040' },
			'a:hover': { color: '#CC0000' }
	   }
	});	
	
	if (typeof $.CustomSelect != 'undefined') {
		$('select').not('.ignore').each(function(i){
			var maxHeight = 200;
			var openTop = false;
			var callback = function(){
			};
			var $this = $(this);
			
			if ($this.attr('id') == 'country') {
				maxHeight = 350;
				openTop = true;
				callback = function(){
					window.location = $('#country').val();
				}
			}
			
			if ($this.parents('#nav').size() > 0) {
				callback = function(){
					window.location = $this.val();
				}
			}
			
			new $.CustomSelect($this, {
				customIndex: i,
				maxHeight: maxHeight,
				openAtTop: openTop,
				submitFunction: callback
			});
		});
	}
	
	$.getScript('http://s7.addthis.com/js/250/addthis_widget.js?username=xa-4c44c34d1ff22dcc&domready=1', function(){
		addthis_config = {
			services_compact: 'facebook, linkedin, digg, googlebuzz, delicious, reddit, newsvine'
		};
		
		addthis.button(".addthis_button");
	});
	
	function _retweetButton(){
		$('span.retweet').each(function(){
			var $this = $(this);
			var $iframe = $('<iframe width="90" height="20" frameborder="0" />');
			var url = $this.parents('.articleActions').find('.permalink').attr('href');
			url = escape(url.replace(/((?:\?|&)?fbc_receiver=.+)?(?:#.*)?$/, ""));
			$iframe.attr('src', 'http://api.tweetmeme.com/button.js?url=' + url + '&style=compact&source=Avaya');
			$this.append($iframe);
		});
	}
	
	_retweetButton();
	
	$('#twitterFeed').tweetable({username: 'Avaya', limit: 3});
	
	// keeps the twitter module hidden if the feeds weren't successfully pulled in within 10 secs
	var twitterCounter = 0;
	var twitterTimer = setInterval(function(){
		if ($('#twitterFeed').find('ul').exists()){
			$('#twitterModule').show();
			clearInterval(twitterTimer);
		}
		
		if (twitterCounter == 20) clearInterval(twitterTimer);
		
		twitterCounter++;
	}, 500);
	
	$('div.articleContent img').downsize({maxWidth: 460 });
	
	$('input.search')
		.focus(function(){
			var $this = $(this);
			if ($this.val() == 'Search the blog') $this.val('');
		})
		.blur(function(){
			var $this = $(this);
			if ($this.val() == '') $this.val('Search the blog');
		});
	
	$('#mastheadSearch div.inputWrapper input').val('Search Avaya.com');
	
	$('.email').click(function(e){
		e.preventDefault();
		var $this = $(this);
		var url = $this.attr('href');
		var title = $this.attr('rel');
		
		var o = new $.Overlay({
			content: '<form id="emailForm"><input type="hidden" name="postURL" value="' + url + '" /><input type="hidden" name="postTitle" value="' + title + '" /><ul><li class="row"><label>Your Name</label><input type="text" class="text" name="name" /></li><li class="row"><label>Email</label><input type="text" class="text" name="email" /></li><li class="row"><label>Your colleague\'s name</label><input type="text" class="text" name="colleaguesName" /></li><li class="row"><label>Your colleague\'s email</label><input type="text" class="text" name="colleaguesEmail" /></li><li class="row"><label>Message (optional)</label><textarea name="message"></textarea></li><li class="row buttons"><input type="submit" class="submit button" value="send" /></li></ul></form>',
			headerCopy: 'Email This Post to a Colleague',
			width: 300,
			extraClass: 'shadow suggestionOverlay'
		});
		
		o.open();
		
		var emailForm = new $.Form({
			"$element": $('#emailForm')
			,"fields": {
				"name": {
					"required": true
				}
				,"email": {
					"required": true
					,"rules": {
						"format": 'email'
					}
				}
				,"colleaguesName": {
					"required": true
				}
				,"colleaguesEmail": {
					"required": true
					,"rules": {
						"format": 'email'
					}
				}
				,"message": {
					"required": false
				}
				,"postURL": {
					"required": false
				}
				,"postTitle": {
					"required": false
				}
			}
			,"ajaxOptions": {
				"dataType": 'json'
				,"url": '/blogs/cgi-bin/mail.pl'
				,"type": 'GET'
				,"success": function(response){
					if (response.success){
						var $emailForm = $('#emailForm');
						var $ty = $('<div id="thankYouEmail" />');
						o.$element.find('h6').text('Thank You');
						$ty.append('<p>A message has been sent</p><div class="buttons"><input type="button" class="button" value="close" /></div>');
						o.$element.find('div.overlayContent').append($ty);
						$emailForm.remove();
						
						$ty.find('input.button').click(function(e){
							e.preventDefault();
							o.close();
							return false;
						})
					}
				}
			}
		});
		
		return false;
	});
	
	
	
	var utilityBar = new $.UtilityBar();
	
	// highlights and disables left nav link based on the page we're on
	var sectionName = $('h1').text().toLowerCase();
	$('#nav a').each(function(){
		var $this = $(this);

		if ($this.children('span.sectionName').text().toLowerCase() == sectionName){
			$this.addClass('sectionActive');
			return false;
		}
	});
	
	$('a.sectionActive').live('click', function(e){
		e.preventDefault;
		return false;
	});
	
	$('#tellUsLink').click(function(e){
		e.preventDefault();
		var offset = $(this).offset();
		var captcha = '';
		
		$.ajax({
			url: '/blogs/_assets/ajax/success.js',
			dataType: 'json',
			success: function(response){				
				
				var o = new $.Overlay({
					content: '<form id="suggestionForm"><div id="suggestionErrors" class="errorDiv"></div><ul><li class="row">Type your suggestion into the box below.</li><li class="row"><textarea id="suggestionBox" name="suggestions"></textarea></li><li class="row buttons"><input type="submit" value="post" class="button submit" /></li></ul></form>',
					headerCopy: 'Suggestion Box',
					width: 260,
					autoCenter: false,
					blockUI: false,
					extraClass: 'shadow suggestionOverlay',
					left: offset.left - 60,
					top: offset.top - 100
				});
				
				o.open();
				
				var suggestionForm = new $.Form({
					"$element": $('#suggestionForm')
					,"fields": {
						"suggestions": {
							"required": true
							,"errorMsg": '* You must fill out the suggestion box'
						}						
					}
					,"ajaxOptions": {
						"dataType": 'json'
						,"url": '/blogs/cgi-bin/tellus.pl'
						,"type": 'GET'
						,"success": function(response){
							if (response.success == 'true'){
								o.close();
							}
							else {															
								if (response.errors != undefined){
									$.each(response.errors, function(k, error){
										$('#suggestionErrors').append('<span class="row error">' + error + '</span>');
									});
								}
							}
						}
					}
				});
			}
		})
		
		return false;
	});
	
	var mastnav = new $.DropDown({
		containerSelector : '#mastheadNav',
		mainSelector : '.mainNavs',
		subSelector : '.subNavs',
		showTimeout : 50,
		hideTimeout : 100
	});
	
	if ($.browser.safari) $('body').addClass('safari');
	
	function _checkFooter(){
		var position = ($('#container').outerHeight() >= $(window).height() - 50) ? 'relative' : 'absolute';
		$('#footer').css('position', position);
	}
	
	_checkFooter();
	
	$(window).resize(_checkFooter);
	$('body').resize(_checkFooter);
	
	$('div.jScrollPaneDrag, input.button')
		.live('mouseover', function(){
			$(this).addClass('hover');
		})
		.live('mouseout', function(){
			$(this).removeClass('hover');
		});
	
	if (typeof $.fn.jScrollPane != 'undefined'){
		$.fn.jScrollPane.defaults.scrollbarWidth = 8;
	}
	
	
	
	$('input.button').hoverIE();
	
	$('input.formToggle').not('.ignore').customFormToggle();
	
	$('a.close').live('click', function(e){
		e.preventDefault();
		$(this).parents('.closeable').remove();
	});
});


// start:sifr
$.sifr = {
	"defaults": {
		"wmode": 'transparent'
		,"forceSingleLine": true
		,"preventWrap": true
		,"selectable": false
		,"tuneWidth": 1
		,"css": {
			".sIFR-root": { 
				"color": '#666666' 
				,"font-size": 16
			}
			,"a": { "text-decoration": 'none', 'cursor': 'pointer' }
			,"a:link": { "color": '#5F798F' }
			,"a:hover": { "color": '#5F798F' }
			,"strong, em, b, i": {
				"font-weight": 'normal'
				,"font-style": 'normal'
			}
	   }
	}
	,"gothamLight": {
		"src": '/blogs/_assets/swf/gothamLight.swf'
		,"ratios" : [9, 1.27, 13, 1.18, 18, 1.14, 26, 1.13, 32, 1.11, 37, 1.1, 64, 1.09, 105, 1.08, 107, 1.07, 119, 1.08, 122, 1.07, 1.08]
	}
	,"gothamBook": {
		"src": '/blogs/_assets/swf/gothamBook.swf'
	}
	,"replace": function(swf, options){
		if ($(options.selector).exists()){
			options = $.extend(true, {}, this.defaults, options);
		
			if (!sIFR.isActive) sIFR.activate();
			
			if (typeof(sIFR)=='object'){
				sIFR.replace(swf, options);
			}
		}
	}
}


// start:extensions
jQuery.fn.customFormToggle = function(){
	return this.each(function(){
		var $this = $(this);
		var type = $this.attr('type');
		var $span = $('<span />');
		var toggleName = $this.attr('name');
		$span.addClass(type + ' customToggle toggleName-' + toggleName);

		$this.before($span).addClass('custom-toggler custom-' + type);
		if ($this.is(':checked')) $span.addClass('checked');
		
		$span.click(function(){
			var $el = $(this);
			
			if (type == 'checkbox') ($el.is('.checked')) ? $this.uncheck() : $this.check();
			else if (type == 'radio'){
				$('input.custom-radio[name=' + toggleName + ']').uncheck();
				$this.check();
			}
		});
		
		$.listen('formReset', function(){
			$span.removeClass('checked focused');
		});
		
		$this.change(function(){
			if (type == 'checkbox'){
				if ($(this).is(':checked')) $span.addClass('checked ' + type + 'Checked'); else $span.removeClass('checked ' + type + 'Checked');
			}
			else {
				var thisName = $this.attr('name');
				$('span.toggleName-' + toggleName).removeClass('checked');
				$span.addClass('checked');
			}
		})
	});
}


jQuery.fn.truncateList = function(options){
	options = $.extend({
		maxItems: 5, selector: 'li', moreText: 'more', lessText: 'less'
	}, options);
	
	return this.each(function(){
		var $this = $(this);
		var $lastItem = $this.find(options.selector).eq(options.maxItems - 1);
		var $toggledItems = $lastItem.nextAll(options.selector);
		$toggledItems.hide();
		var $more = $('<a href="javascript:void();">' + options.moreText + '</a>');
		$more.wrap('<li />').appendTo($this)
		$('<' + options.selector + ' />').append($more).appendTo($this);
		
		$more.click(function(e){
			e.preventDefault();
			$toggledItems.toggle();
			if ($toggledItems.is(':visible')) $more.text(options.lessText); else $more.text(options.moreText);
		});
	});
}

jQuery.fn.treeList = function(options){
	options = $.extend({
		maxItems: 5, selector: 'li', moreText: 'more', lessText: 'less'
	}, options);
	
	return this.each(function(){
		var $this = $(this);
		
		$this.find('li.parent a').click(function(e){
			e.preventDefault();
			$(this).toggleClass('active').next('ul').toggle();
		});
	});
}

// returns boolean
jQuery.fn.equals = function($el){
	return ($(this).get(0) === $el.get(0));
}

jQuery.fn.hoverIE = function(){
	return this.each(function(){
		if($.browser.msie){
			$(this).hover(
				function(){ $(this).addClass('hover'); },
				function(){ $(this).removeClass('hover'); }
			);
		}
	});
}

function showKeyValue(){
	return true;
}

function validateSearchBoxForm(){
	return true;
}

