
/* ShareDialog Class using jQuery.js and oop.js */

var ShareDialog = Class.extend(
{
	dialog       : null,
	container    : null,
	backCover    : null,
	dialogTitle  : null,
	dialogBorder : null,
	dialogCloser : null,
	

	init : function()
	{
		this.dialog       = jQuery('#shareDialog'         );
		this.container    = jQuery('#shareDialogContainer');
		this.backCover    = jQuery('#shareDialogBackCover');
		this.dialogTitle  = jQuery('#shareDialogTitle'    );
		this.dialogBorder = jQuery('#shareDialogBorder'   );
		this.dialogCloser = jQuery('#closeShareDialogLink');

		//----- Reference to 'this' object needed inside the following events
		var thisObj = this;

		//----- Events for hiding the share dialog
		this.backCover   .click(function(event) { event.preventDefault(); thisObj.hide(); });
		this.dialogCloser.click(function(event) { event.preventDefault(); thisObj.hide(); });

		//----- Events for resizing the share dialog
		jQuery(window).resize(function() { thisObj.updateSize(); });
		jQuery(window).scroll(function() { thisObj.updateSize(); });
	},

	
	show : function(shareUrl, shareTitle, title, category, emailSharePage)
	{
		if (!title) { title = 'Share'; } // default dialog title
		
		this.updateLinks(shareUrl, shareTitle, category, emailSharePage);
		this.dialogTitle.text(title);		
		this.container.show();
		this.updateSize();

		pageTracker._trackEvent(category, 'Share');
	},

	
	hide : function()
	{
		this.container.hide();
	},


	updateLinks : function(shareUrl, shareTitle, category, emailSharePage)
	{
		var escapeUrl   = escape(shareUrl  );
		var escapeTitle = escape(shareTitle);

		if (!emailSharePage) { emailSharePage = "../color/send-to-friend.do"; } // default email share page

		var shareDialogLink_email    = jQuery( '#shareDialogLink_email'    );
		var shareDialogLink_facebook = jQuery( '#shareDialogLink_facebook' );
		var shareDialogLink_twitter  = jQuery( '#shareDialogLink_twitter'  );

		var twitterText = '' + escapeTitle;
		if ( twitterText.indexOf( 'http' ) < 0 ) twitterText += '%20' + escapeUrl;

		shareDialogLink_email   .attr( 'href', emailSharePage + '?url='               + escapeUrl );
		shareDialogLink_facebook.attr( 'href', 'http://www.facebook.com/share.php?u=' + escapeUrl );
		shareDialogLink_twitter .attr( 'href', 'http://twitter.com/home?status='      + twitterText );

		shareDialogLink_email   .click( function( e ) { pageTracker._trackEvent(category, 'ShareEmail'    ); });
		shareDialogLink_facebook.click( function( e ) { pageTracker._trackEvent(category, 'ShareFacebook' ); });
		shareDialogLink_twitter .click( function( e ) { pageTracker._trackEvent(category, 'ShareTwitter'  ); });
	},


	updateSize : function()
	{
		var w = (document.documentElement && document.documentElement.clientWidth ) || window.innerWidth  || self.innerWidth  || document.body.clientWidth;
		var h = (document.documentElement && document.documentElement.clientHeight) || window.innerHeight || self.innerHeight || document.body.clientHeight;
		var x = (document.documentElement && document.documentElement.scrollLeft  ) || window.pageXOffset || self.pageXOffset || document.body.scrollLeft;
		var y = (document.documentElement && document.documentElement.scrollTop   ) || window.pageYOffset || self.pageYOffset || document.body.scrollTop;

		var dialogLeft = ( ( w - this.dialog.width()  ) / 2 ) + x;
		var dialogTop  = ( ( h - this.dialog.height() ) / 2 ) + y;

		var dialogBorderLeft = dialogLeft - 10;
		var dialogBorderTop  = dialogTop  - 10;

		this.dialog.css('left', dialogLeft + 'px');
		this.dialog.css('top' , dialogTop  + 'px');

		this.dialogBorder.css('left', dialogBorderLeft + 'px');
		this.dialogBorder.css('top' , dialogBorderTop  + 'px');
	}	
	
}); // End class ShareDialog.


var shareDialog = new ShareDialog();


