var ExpandingContent = 
{
	/* Initialise function in the document.ready using 	ExpandingContent.init('.mobileCompatible ul a'); */ 
	init: function(anchor) {
		/* bind the click and focus to the correct function, include an extra anchor to allow the focus to work */
		$(anchor).bind('click', ExpandingContent.clickListener);
		$(anchor).each(function() {
			$(this).parent('li').addClass('collapsed');
			$(this.parentNode).children('ul').prepend('<a href="#" class="innerExpandable" ></a>');
		});
		$('.innerExpandable').bind('focus', ExpandingContent.focusListener);
	},
	clickListener: function(event)
	{
		var fold = this.parentNode;
		if ($(fold).hasClass("collapsed"))
		{
			ExpandingContent.expand(fold);
		}
		else
		{
			ExpandingContent.collapse(fold);
		}
		event.preventDefault();
	},
	
	focusListener: function(event)
	{
		var element = this;
		while (element.parentNode)
		{
			if ($(element.parentNode).hasClass("collapsed"))
			{
				ExpandingContent.expand(element.parentNode);
				return;
			}
			element = element.parentNode;
		}
	},
	preventDefault: function(event)
	{
		event.preventDefault();
	},
	collapse: function(fold) {
		$(fold).addClass('collapsed');
	},
	expand: function(fold) {
		$(fold).parent('ul').children('li').each(function() {
			ExpandingContent.collapse(this);
		});
		$(fold).removeClass('collapsed');
	}
};

var TabbedContent = 
{
	init: function() {
		/* bind the click, not the focus though as this would be inaccessible and confusing for keyboard users */
		$('.sectionArea .tabs a').bind('click', TabbedContent.tabListener);
	}, 
	tabListener: function(event) {
		TabbedContent.changeContents(this);
		this.blur();
		event.preventDefault();
	},
	changeContents: function (link) {
		var linkHash = link.hash;
		var tabItem = link.parentNode;
		if (tabItem.className != 'selected') {
			$('.sectionArea .section.selected').removeClass('selected');
			var linkId = linkHash.substr(1);
			$('a[name=' + linkId + ']').parent('.section').addClass('selected');
			$('.sectionArea .tabs .selected').removeClass('selected');
			$(link).parent('li').addClass('selected');
			/* change the hidden input field to allow the Salesforce form to detect the current tab */
			if ($('.demoSignUp .selectedTab').length == 1) {
				$('.demoSignUp .selectedTab').val(linkId);
			}
			/* Resolve some issues with Css3Pie */
			if ($.browser.msie) {
				Pie.fix();
			}
		}
	}
};

var ClearText = 
{
	init: function(element) {
		$(element).bind('focus', ClearText.focusListener);
	}, 
	focusListener: function(event) {
		this.value="";
	}
};

var Pie = 
{
	remove: function()
	{
		$('.css3Pie').removeClass('css3Pie'); /* CSS3PIE compatibility issue work around */
	},
	add: function()
	{
		/* CSS3PIE compatibility issue work around - all elements needed to be rounded go here */
		$('#mainArea .inner').addClass('css3Pie');
		$('#mainArea .navSecondary ul li a').addClass('css3Pie'); 
		$('#mainArea .section #mainContent h2.banner').addClass('css3Pie');
		$('#mainContent .sectionArea').addClass('css3Pie');
		$('#mainContent .sectionArea .tabs ul li').addClass('css3Pie');
		$('#mainArea .section #mainContent .sectionArea .section').addClass('css3Pie');
		$('#mainArea .content .decoration').addClass('css3Pie');
		$('#mainContent ul.buttons li').addClass('css3Pie');
		$('#mainArea .product #mainContent').addClass('css3Pie');
		$('#mainArea .product #mainContent .demoSignUp').addClass('css3Pie');
		$('#mainArea .product #mainContent .subSecCont').addClass('css3Pie');
		$('#mainArea .product #mainContent .subSection').addClass('css3Pie');
		
		if (!$.browser.msie6) {
			$('input[type="text"]').addClass('css3Pie');
			$('input[type="submit"]').addClass('css3Pie');
		
			/* Resolve issue with demo signup inputs not showing */
			$('input[type="submit"]').css('position','relative');
			$('input[type="text"]').css('position','relative');
			$('fieldset').css('position','relative');
		}
	}, 
	fix: function()
	{
		var leftPixel = 0;
		var topPixel = 0;
		$('#mainContent css3-container').each(function() {
			leftPixel = 0;
			topPixel = 0;
			leftPixel = $(this).css('left');
			leftPixel = leftPixel.slice(0, -2);
			topPixel = $(this).css('top');
			topPixel = topPixel.slice(0, -2);
			if (leftPixel > 1000) {
				leftPixel = parseInt(leftPixel/100);
				leftPixel = leftPixel + 'px';
				$(this).css('left', leftPixel);
				if (topPixel > 1000) {
					topPixel = parseInt(topPixel/100);
					topPixel = topPixel + 'px';
					$(this).css('top', topPixel);
				}
			}
		});
		$('#mainContent').next('css3-container').each(function() {
			leftPixel = 0;
			topPixel = 0;
			leftPixel = $(this).css('left');
			leftPixel = leftPixel.slice(0, -2);
			topPixel = $(this).css('top');
			topPixel = topPixel.slice(0, -2);
			if (leftPixel > 1000) {
				leftPixel = (leftPixel/100) + 'px';
				$(this).css('left', leftPixel);
			}
			if (topPixel > 1000) {
				topPixel = (topPixel/100) + 'px';
				$(this).css('top', topPixel);
			}
		});
		$('.sectionArea .tabs css3-container').each(function(){
			leftPixel = 0;
			leftPixel = $(this).css('left');
			leftPixel = leftPixel.slice(0, -2);
			if (leftPixel > 1000) {
				leftPixel = (leftPixel/100) + 'px';
				$(this).css('left', leftPixel);
			}
		});
		$('.sectionArea .demoSignUp css3-container').each(function(){
			leftPixel = 0;
			topPixel = 0;
			leftPixel = $(this).css('left');
			leftPixel = leftPixel.slice(0, -2);
			topPixel = $(this).css('top');
			topPixel = topPixel.slice(0, -2);
			if (leftPixel > 1000) {
				leftPixel = (leftPixel/100) + 'px';
				$(this).css('left', leftPixel);
			}
			if (topPixel > 1000) {
				topPixel = (topPixel/100) + 'px';
				$(this).css('top', topPixel);
			}
		});
		$('#mainArea .section #mainContent .sectionArea css3-container').each(function(){
			leftPixel = 0;
			topPixel = 0;
			leftPixel = $(this).css('left');
			leftPixel = leftPixel.slice(0, -2);
			topPixel = $(this).css('top');
			topPixel = topPixel.slice(0, -2);
			if (leftPixel == 99 && topPixel == 99) {
				leftPixel = '0px';
				topPixel = '0px';
				$(this).css('left', leftPixel);
				$(this).css('top', topPixel);
			}
		});
		
		$('.shadow > css3-container').each(function() {
			leftPixel = 0;
			topPixel = 0;
			leftPixel = $(this).css('left');
			leftPixel = leftPixel.slice(0, -2);
			topPixel = $(this).css('top');
			topPixel = topPixel.slice(0, -2);
			if (leftPixel > 1000) {
				leftPixel = parseInt(leftPixel/100);
				leftPixel = leftPixel + 'px';
				$(this).css('left', leftPixel);
			}
			if (topPixel > 1000) {
				topPixel = parseInt(topPixel/2500);
				topPixel = topPixel + 'px';
				$(this).css('top', topPixel);
			}
		});
		$('#mainContent > css3-container').each(function() {
			leftPixel = 0;
			topPixel = 0;
			leftPixel = $(this).css('left');
			leftPixel = leftPixel.slice(0, -2);
			topPixel = $(this).css('top');
			topPixel = topPixel.slice(0, -2);
			if (leftPixel < 0 || topPixel < 0) {
				leftPixel = '0px';
				topPixel = '0px';
				$(this).css('left', leftPixel);
				$(this).css('top', topPixel);
			}
		});
	}
}

var CheckZoom =
{
    init: function() {
		if ($.browser.version.substr(0, 1) == 7) {
			var b = document.body.getBoundingClientRect();
			var newZoom = (b.right - b.left) / document.body.clientWidth;
			if (newZoom != 1) {
				Pie.remove();
				$(window).unbind('resize');
			}
		}
		var nScaleFactor = screen.deviceXDPI / screen.logicalXDPI;
		if (nScaleFactor != 1) {
			Pie.remove();
            $(window).unbind('resize');
		} else {
			Pie.add();
		}
    }
}

jQuery.browser.msie6 = jQuery.browser.msie &&
    parseInt(jQuery.browser.version) == 6 &&
    !window["XMLHttpRequest"];
$(document).ready(function() {
	if ($('.termsLink').length > 0) {
		$('.termsLink').bind('click', function() {
			if ($(this).parent().find('.terms').length > 0) {
				$(this).parent().find('.terms').toggle();
			}
			return false;
		});
	}
	var sWindWidth = $(window).width();
	TabbedContent.init();
	ClearText.init('.searchText');
	if ($.browser.msie) {
		Pie.add();
		window.onbeforeprint = Pie.remove;
		window.onafterprint = Pie.add;
		
		
		/* Fix for IE && CSS3Pie zoom bug */
		CheckZoom.init();
		$(window).bind('resize', function() {
			if (sWindWidth != $(window).width()) {
				Pie.remove();
				$(window).unbind('resize');
				CheckZoom.init();
			} else {
				CheckZoom.init();
			}
		});
		
	}
	
	/* Resolve some issues with Css3Pie and IE 7 */
	if ($.browser.version.substr(0, 1) == 7) {
		fixPie = setTimeout(Pie.fix, 10);
		fixPie2 = setTimeout(Pie.fix, 100);
		
		resetPageReload();
	}
});

function resetPageReload() {
	$('div').bind('resize', pageReload);
	}
	
function pageReload() { 
	$('div').unbind('resize');
	fixPie = setTimeout(Pie.fix, 15);
	fixPie2 = setTimeout(Pie.fix, 1000);
	pageReloadTimer = setTimeout(resetPageReload, 10);
	} 




