var PanelReSize = 
{
	/* This is used to resize panels to be the same height as their neighbours */
	height: 0,
	init: function(callback)
	{
		/* what page are we on? */
		if ($('#everything').hasClass('section')) {
			if (PanelReSize.sectionPage()) {
				callback();
			}
		} else {
			callback();
		}
	},
	sectionPage: function()
	{
		PanelReSize.setHeight($('.sectionWrapper').children());
		PanelReSize.setHeight($('#main').children('.panel:lt(3)'));
		PanelReSize.setHeight($('#main').children('.panel:gt(2)'));
		return true;
	},
	setHeight: function(matchedSet)
	{
		$(matchedSet).each(function() {
			if ($(this).outerHeight() > PanelReSize.height) {
				PanelReSize.height = $(this).outerHeight();
			}
		});
		$(matchedSet).each(function() {
			var difference = $(this).outerHeight() - $(this).height();
			var newHeight = PanelReSize.height - difference;
			$(this).height(newHeight);
		});
		//console.log(PanelReSize.height);
		PanelReSize.height = 0;
	}
}

var DropDownExtras = 
{
	/* 	Feedback page is 310
		Search page is 312 
		Feedback button id is .feedback a 
		Search button id is .searchDropDown a */
	clicked: false, 
	maxPanelHeight: 0,
	count: 0,
	init: function(callback)
	{
		var pageUrl = '/default.aspx?page=310';
		var contentPlace = $('.feedback');
		if ($('.feedbackForm').length > 0) {
			$('.feedbackForm').html('<span class="title">Feedback</span>' + $('.feedbackForm').html());
			DropDownExtras.positionContents($('.' + $(contentPlace).attr('class') + 'Form'), contentPlace);
		} else {
			if (DropDownExtras.getContents(pageUrl, contentPlace)) {
				callback();
			}
		}
		var pageUrl = '/default.aspx?page=312';
		var contentPlace = $('.searchDropDown');
		if ($('.searchDropDownForm').length > 0) {
			DropDownExtras.positionContents($('.' + $(contentPlace).attr('class') + 'Form'), contentPlace);
		} else {
			if (DropDownExtras.getContents(pageUrl, contentPlace)) {
				callback();
			}
		}
		
		$('.feedback a').click(function(event) {
			if (!(DropDownExtras.clicked)) {
				DropDownExtras.clicked = true;
				var thisClass = '.' + $(this).parent().attr('class');
				setTimeout('DropDownExtras.pushDown(\''+thisClass+'\')', 5);
			}
			return false;
		});
		$('.searchDropDown a').click(function(event) {
			if (!(DropDownExtras.clicked)) {
				DropDownExtras.clicked = true;
				var thisClass = '.' + $(this).parent().attr('class');
				setTimeout('DropDownExtras.pushDown(\''+thisClass+'\')', 5);
			}
			return false;
		});
	},
	getContents: function(pageUrl, contentPlace)
	{
		var returnVal = false;
		$.ajax(
		{ 	url: pageUrl, 
			context: document.body, 
			success: function(data){
				DropDownExtras.addContents(data, contentPlace);
			}
		});
	},
	addContents: function(data, contentPlace)
	{
		var placeClass = $(contentPlace).attr('class');
		$(contentPlace).before('<div class="'+ placeClass +'Form">'+ data + '</div>');
		placeClass = $('.' + placeClass + 'Form');
		$(placeClass).hide();
		$(placeClass).children('title').empty().remove();
		$(placeClass).children('meta').empty().remove();
		$(placeClass).children('link').empty().remove();
		DropDownExtras.positionContents(placeClass, contentPlace);
	},
	positionContents: function(placeClass, contentPlace)
	{
		var placePosition = - ($(placeClass).height() - $(contentPlace).height()) + $(placeClass).height() + 12;
		if (curvyBrowser.ieVer == 6) {
			placePosition = placePosition - 22;
		}
		$(placeClass).css({ 'bottom' : (placePosition) });
		$(placeClass).wrapInner('<div class="inner" />');
		$(placeClass).append('<div class="footerTabBtm" />');
		$(placeClass).children('.inner').append('<a href="#" class="cancelButton">Cancel</a>');
		$('.cancelButton').click(function(event) {
			//if ($('.feedback').hasClass('under')) {
				DropDownExtras.pushUp(this);
			//}
			return false;
		});
		DropDownExtras.count = DropDownExtras.count + 1;
		if ($(placeClass).height() > DropDownExtras.maxPanelHeight) {
			DropDownExtras.maxPanelHeight = $(placeClass).height();
		}
		if (DropDownExtras.count > 1) {
			if ($('#quickLinks').height() < DropDownExtras.maxPanelHeight) {
				$('#quickLinks').height(DropDownExtras.maxPanelHeight + 10);
			}
			$('.searchDropDownForm').show();
			$('.feedbackForm').show();
			
			if (typeof(curvyCorners.init) === 'function' && curvyBrowser.ieVer != 9) { curvyCorners.init(); };
		}
		return true;
	},
	pushDown: function(buttonElement)
	{
		var buttonClass = buttonElement + 'Form';
		//$('.feedback').addClass('under');
		//$('.searchDropDown').addClass('under');
		//if (!($(buttonClass).hasClass('shown'))) {
			var oldPosition = $(buttonClass).css('bottom');
			var buttonHeight = parseInt($(buttonClass).height());
			var newPosition = parseInt(oldPosition) - buttonHeight;
			//if (!($(buttonElement).hasClass('under'))) {
			//	$(buttonElement).addClass('under');
			//}
			//$(buttonClass).addClass('show');
			//if ($(buttonElement).hasClass('under')) {
				$(buttonClass).animate({
					bottom : newPosition
					}, 300, function() {
					// Animation complete.
					var newScroll = $(window).scrollTop() + $(window).height();
					$(window).scrollTop(newScroll);
				});
				

			//}
		//}
	},
	pushUp: function(buttonElement)
	{
		var buttonClass = $(buttonElement).parent('.inner').parent();
		var oldPosition = $(buttonClass).css('bottom');
		var buttonHeight = parseInt($(buttonClass).height());
		var newPosition = parseInt(oldPosition) + buttonHeight;
		
		$(buttonClass).animate({
				bottom : newPosition
				}, 500, function() {
				// Animation complete.
				//$('.under').removeClass('under');
				//$('.show').removeClass('show');
				DropDownExtras.clicked = false;
			});
	}
};

$(document).ready(function() {
	PanelReSize.init(function() {
		DropDownExtras.init();
	});
	
	/* Run the panel re-size again to resolve curvy issues */
	//PanelReSize.init();
	
});

