var SocialShareClass = Class.create({
	initialize: function(config) {
		this.config = Object.extend({},config)
	},
	
	increaseShareStats: function(itemId, itemType, serviceId) {
		new Ajax.Request(this.config.sharingStatUrl, {
			parameters: {'i_t':itemType, 'i_i':itemId, 's_t':serviceId},
			onComplete: function(t) {}
		})
	},

	shareOrkut: function(activityUrl, text, thumbUrl, descr, callback, itemId, itemType) {
		//just open share orkut window
		if (!descr) {
			descr = text
		}
		var url = 'http://promote.orkut.com/preview?nt=orkut.com&tt=' + encodeURIComponent(text) + '&du=' + encodeURIComponent(activityUrl) + '&cn='+encodeURIComponent(descr)
		if (thumbUrl) url += '&tn=' + encodeURIComponent(thumbUrl)
		//alert(url)
		if (itemId && itemType) {
			this.increaseShareStats(itemId, itemType, 3)/*3rd parameter in called function is constant ST_TWITTER from UpSharingInfoPeer*/
		}
		window.open(url, 'Orkut', 'width=640,height=480')
		if (typeof callback == 'function') {
			callback()
		}
	},

	/*
	* notifier should be a function called upon complete operations
	*/
	shareTwitter: function(activityUrl, text, notifier, itemId, itemType) {
		if (this.config.hasTwitterAccount) {
			new Ajax.Request(this.config.twitterShareUrl, {
				parameters: 'status='+encodeURIComponent(text+' '+activityUrl),
				onComplete: function (t) {
					r = t.responseJSON
					res = false
					if (r.status == 'success') {
						res = true
						if (itemId && itemType) {
							this.increaseShareStats(itemId, itemType, 1)/*3rd parameter in called function is constant ST_TWITTER from UpSharingInfoPeer*/
						}
					}
					if (typeof notifier == 'function') {
						notifier(res)
					}
				}.bind(this)
			});
		} else {
			window.activityShareInit = true
			window.secondStageParameters = {'message':encodeURIComponent(text+' '+activityUrl), 'notifier':notifier, 'itemId':itemId, 'itemType':itemType}
			window.open(this.config.twitterAuthorizeUrl, 'Twitter', 'width=800, height=450')	
		}
		
	},
	
	twitterSecondStage: function() {
		this.config.hasTwitterAccount = true;
		new Ajax.Request(this.config.twitterShareUrl, {
			parameters: {'status':window.secondStageParameters.message},
			onComplete: function (t) {
				r = t.responseJSON
				res = false
				if (r && r.status == 'success') {
					res = true
					if (window.secondStageParameters.itemId && window.secondStageParameters.itemType) {
						this.increaseShareStats(window.secondStageParameters.itemId, window.secondStageParameters.itemType, 1)/*3rd parameter in called function is constant ST_TWITTER from UpSharingInfoPeer*/
					}
				}	
				if (typeof window.secondStageParameters.notifier == 'function') {
					window.secondStageParameters.notifier(res)
				}
				window.activityShareInit = null
				window.secondStageParameters = null
			}.bind(this)
		});
	},

	shareFacebook: function(activityUrl, text, thumbUrl, notifier, itemId, itemType, name, description) {
		var self = this
		window.universalFbInit()
		if (!self.config.hasFbAccount) {
			FB.login(function(response){
				if(response.session) {
					if (response.perms.indexOf('read_stream')>=0 &&
					 	response.perms.indexOf('publish_stream')>=0 &&
						response.perms.indexOf('offline_access')>=0) {
						new Ajax.Request(window.socialShare.config.facebookAuthorizedUrl, {
							parameters: 'uid='+response.session.uid+'&act=auth'
						})
						self.config.hasFbAccount = true
						self.postFb(text, thumbUrl, activityUrl, name, description, notifier, {'itemId':itemId, 'itemType':itemType})
					} else {
						if ('function' == typeof notifier) {
							notifier(false)
						}
					}
				} else {
					if ('function' == typeof notifier) {
						notifier(false)
					}
				}
			}, {perms:'read_stream,publish_stream,offline_access'})
		} else {
			self.postFb(text, thumbUrl, activityUrl, name, description, notifier, {'itemId':itemId, 'itemType':itemType})
		}
	},//shareFacebook

	postFb: function(message, picture, link, name, description, notifier, itemP) {
		self = this
		if (message.strip() == '') {
			dlg = DialogManager.createDialog({withTitle:false, width:400})
			co = document.createElement('div')
			notice = document.createElement('div')
			notice.innerHTML = self.config.messages.fbPrompt
			tx = document.createElement('textarea')
			tx.id='fbComment'
			bt = document.createElement('input')
			bt.type = 'button'
			bt.value= self.config.messages.fbPost
			bt.onclick = function() {
				msg = $F('fbComment')
				if (msg == '') {
					msg = name
				}
				socialShare.postFb(msg, picture, link, name, description, notifier, itemP)
				dlg.destroy()
			}
			cn = document.createElement('input')
			cn.type = 'button'
			cn.value=self.config.messages.cancel
			cn.onclick = function() {
				dlg.destroy()
			}
			co.appendChild(notice)
			co.appendChild(tx)
			co.appendChild(bt)
			co.appendChild(cn)
			dlg.setChild(co)
			dlg.show()

			$(co).observe('click', function(event) {
				var $parent = $($(co).up().up().up());
				console.log($parent);
				$parent.setStyle({'zIndex': 9999});
				console.log($parent);
			});

			return false;
		}
		
		new Ajax.Request(self.config.fbShareUrl, {
			parameters: {
				'message': message,
				'picture': picture,
				'link': link,
				'name':name,
				'description': description
			},
			onComplete: function(t) {
				if (t.responseJSON.status == 'success') {
					if (itemP && itemP.itemId && itemP.itemType) {
						self.increaseShareStats(itemP.itemId, itemP.itemType, 4)//4 == SocialServicePeer::FACEBOOK
					}
					notifierResult = true
				} else {
					if (t.responseJSON.reason == 1) { //FacebookLib::R_INVALID_SESSION
						FB.login(function(response){
							if (response.session) {
								self.postFb(message, picture, link, name, description, notifier, itemP)
							} else {
								new Ajax.Request(window.socialShare.config.facebookAuthorizedUrl, {
									parameters: 'act=deauth'
								})
								if (typeof notifier == 'function') {
									notifier(false)
								}
							}
						})
						return true
					} else {
						notifierResult = false
					}
				}
				if (typeof notifier == 'function') {
					notifier(notifierResult)
				}
			}
		})
	}
})
