var is_ie6 = (
	window.external &&
	typeof window.XMLHttpRequest == "undefined"
)

ShareAPage = new Class({

	initialize: function(shareWidgetId, isPopup, postHeight, emailHeight, bookmarkHeight, postWidth, emailWidth, bookmarkWidth) {
		this.postHeight = postHeight || 204;//119
		this.postWidth = postWidth || 396;
		this.emailHeight = emailHeight || 234;
		this.emailWidth = emailWidth || this.postWidth;
		this.bookmarkHeight = bookmarkHeight || 234;
		this.bookmarkWidth = bookmarkWidth || this.emailWidth;
		
		// copy the textarea contents into the div that is displayed
		// Note that both are the same, the text area is sent to the social network
		$('widgetid').setHTML($(shareWidgetId).value)
		this.isPopup = isPopup;
		var centerVertical = function() {
			var shareapageWrapper = $('shareapage-wrapper');
			shareapageWrapper.setStyle('marginTop', -(shareapageWrapper.getSize().size.y / 2) + 'px');
		}
		this.tabs = new Tools.Tabs('shareTabs', 'sharePanels', {
			tabElements:	'ul.tab_title li',
			panelElements:	'.tab_panel',
			mouseOverClass: 'over',
			onComplete: { 'post': centerVertical,
								'email': centerVertical,
								'bookmark': centerVertical },
			activateOnLoad : 'first'});
	},

	// this sets the Gigya flash into the div's below.  The two numbers are in the parameters are width and height of the Gigya area 
	postWildfire: function(divName) {
		if ( typeof Wildfire != 'undefined' ) {
			// if Wildfire isn't defined we have a problem loading them and the default error message in the div will persist
			// the order here is important, the tab we are going to first must be first on the list or they all won't display 
			if (divName == 'divWildfireBookmark') {
				Wildfire.initPost(gigyacid, 'divWildfireBookmark', this.bookmarkWidth, this.bookmarkHeight, bookmarkconf);
				Wildfire.initPost(gigyacid, 'divWildfirePost', this.postWidth, this.postHeight, postconf);
				Wildfire.initPost(gigyacid, 'divWildfireEmail', this.emailWidth, this.emailHeight, emailconf);
			} else if (divName == 'divWildfirePost') {
				Wildfire.initPost(gigyacid, 'divWildfirePost', this.postWidth, this.postHeight, postconf);
				Wildfire.initPost(gigyacid, 'divWildfireBookmark', this.bookmarkWidth, this.bookmarkHeight, bookmarkconf);
				Wildfire.initPost(gigyacid, 'divWildfireEmail', this.emailWidth, this.emailHeight, emailconf);
			} else {
				Wildfire.initPost(gigyacid, 'divWildfireEmail', this.emailWidth, this.emailHeight, emailconf);
				Wildfire.initPost(gigyacid, 'divWildfireBookmark', this.bookmarkWidth, this.bookmarkHeight, bookmarkconf);
				Wildfire.initPost(gigyacid, 'divWildfirePost', this.postWidth, this.postHeight, postconf);
			}
		}
	},

	openShare: function(tabName,divName) {
		this.tabs.activate(tabName);
		this.postWildfire(divName);
		var shareBackgroundDrop = $('shareBackgroundDrop');
		shareBackgroundDrop.injectInside(document.body);
		shareBackgroundDrop.setStyles({
			opacity: '.1',
			visibility: 'hidden',
			left: 0,
			top: 0,
			width: '100%',
			height: '100%'
		});
		if($defined($('gamep'))){$('gamep').setStyle('visibility', 'hidden')}
		var shareBackgroundDropFX = shareBackgroundDrop.effects({duration: 300, transition: Fx.Transitions.Quint});
		shareBackgroundDropFX.start({
			'opacity': .5
		}).chain(function() { // executes immediately after completion of above effect
			var shareapageWrapper = $('shareapage-wrapper');
			shareapageWrapper.setStyle('marginTop', -(shareapageWrapper.getSize().size.y / 2) + 'px');
			shareapageWrapper.setStyle('visibility','visible');
			//document.body.setStyle('overflow', 'hidden');
			shareapageWrapper.injectAfter('shareBackgroundDrop');
			//disable scrollbars for IE6
			if(is_ie6){
				window.scrollTo(0,0);
				document.body.parentNode.style.overflow = 'hidden'
			}
			
		});
	},
		
	openSharePost: function() {
		this.openShare('post','divWildfirePost');
	},
	
	openShareEmail: function() {
		this.openShare('email','divWildfireEmail');
	},
	
	openShareBookmark: function() {
		this.openShare('bookmark','divWildfireBookmark');
	},

	closeShare: function() {
		if (this.isPopup) {
			window.close();
		} else {
			$('shareapage-wrapper').setStyle('visibility','hidden');
			$('shareBackgroundDrop').setStyle('visibility','hidden');
			document.body.parentNode.style.overflow = ''
			// refresh the wildfire state back to its original screen
			$('shareBackgroundDrop').setStyle('opacity','0');
			if($defined($('gamep'))){$('gamep').setStyle('visibility', 'visible')}
		}
	}

});