/*popup*/

function Popup(){
	this.text;
	this.domain;
	this.width;
	this.height;
	this.popupDiv;
	this.closeButton;
	this.callbackFunction;
	this.likebox;
	this.likePage;
	this.profileImg;
	this.likePageTitle
	this.profileID;
	this.fanWidth;
	this.fanNumber;
	this.screenHeight;
	this.like;
	this.cookie;
	this.id;
	this.gaqOpen;
	this.gaqLike;
	this.anim;
	this.shown = false;
}
Popup.prototype.run = function(data, callbackFunction){
	this.id = data.id || 'fbLikePopup';
	this.text = data.text || '';
	this.domain = data.domain || location.hostname ;
	this.width = data.width || 500;
	this.height = data.height || 450;
	this.callbackFunction = callbackFunction;
	this.likePage = data.likePage || 'http://www.facebook.com/';
	this.profileImg = data.profileImg;
	this.likePageTitle = data.likePageTitle;
	this.profileID = data.profileID;
	this.fanWidth = data.fanWidth || 450;
	this.fanNumber = data.fanNumber || 16;
	this.gaqOpen = data.gaqOpen;
	this.gaqLike = data.gaqLike;
	this.anim = data.anim;
	this.likebox =	'<div id="fbLikeBox">' +
					'<div id="fbLBhead">' +
					'<a href="' + this.likePage + '" target="_blank">' +
					'<img class="profileimage" src="' + this.profileImg + '" alt="Lingvo">' +
					'</a>' +
					'<div class="name_block">' +
					'<a href="' + this.likePage + '" target="_blank">' +
					'<span class="name">' + this.likePageTitle + '</span> on Facebook' +
					'</a>' +
					'</div>' +
					'<div id="fbLikeButton">' +
					//'<div class="fb-like" data-href="' + this.likePage + '" data-send="false" data-width="50" data-show-faces="false"></div>' +
					'<fb:like href="' + this.likePage + '" send="false" width="50" height="" show_faces="false"></fb:like>' +
					'</div>' +
					'</div>' +
					'<div id="faces">' +
					//'<div class="fb-fan" data-profile_id="' + this.profileID + '" data-width="' + this.fanWidth + '" data-show_faces="true" data-stream="false" data-header="false" data-css="http://www.lingvo.com/upload/static/fb.css?9" data-connections="' + this.fanNumber + '"></div>' +
					'<fb:fan profile_id="' + this.profileID + '" width="' + this.fanWidth + '" show_faces="true" stream="false" header="false" css="http://www.lingvo.com/upload/static/fb.css?9" connections="' + this.fanNumber + '"></fb:fan>' +
					'</div>' +
					'<div id="fbLBfooter">' +
					'<a class="uiLinkImg" target="_blank" href="http://developers.facebook.com/plugins/?footer=1"></a>' +
	    			'<a class="uiLinkSubtle" target="_blank" href="http://developers.facebook.com/plugins/?footer=1">Facebook social plugin</a>' +
	    			'</div>' +
					'</div>';

	if(this.cookieCheck() && !this.shown && typeof FB != 'undefined') this.show();
	else if (this.callbackFunction) this.callbackFunction();
}
// (1) check cookie
Popup.prototype.cookieCheck = function(){
	this.cookie = this._checkCookie(this.id);
	if (!this.cookie) return true; // cookie don't set => show popup
	if (this.cookie == 'Done') return false; // cookie set into 'Done' => don't show popup
	if (this._checkCookie(this.id + 'Temp')) return false; // cookieTemp set => don't show popup
	return true; // cookie set into none 'Done' and cookieTemp don't set => show popup
}
// (3) create popup div
Popup.prototype.show = function (){
		this.popupDiv = document.createElement('div');
		this.popupDiv.id = 'popupDiv_' + this.id;
		this.popupDiv.className = 'popupDiv';

		this.popupDiv.innerHTML = this.text + this.likebox;

		this.popupDiv.style.width = this.width + 'px';
		this.popupDiv.style.height = this.height + 'px';
		this.popupDiv.style.marginTop = this.height / -2 + 'px';
		this.popupDiv.style.marginLeft = this.width / -2 + 'px';

			this.closeButton = document.createElement('a');
			this.closeButton.id = 'popupCloseButton';
			this.popupDiv.appendChild(this.closeButton);

		document.body.appendChild(this.popupDiv);
		FB.XFBML.parse(this.popupDiv);

		if (this.anim == 'Up') this._animUp();
		else this._animFade();

		var thiss = this;
		this.closeButton.onclick = function(){thiss.close(false)};

		_gaq.push(['_trackPageview',this.gaqOpen]);

		this.shown = true;
}
// (4) set cookie
Popup.prototype.setCookie = function(){
	var path = ';path=/';
	var domain = ';domain=' + this.domain;
	var expires = ';expires=' + new Date( new Date().getTime()+ 1000*60*60*24*365).toGMTString();

	if(this.like || parseInt(this.cookie) > 1) {
		var cookie = this.id + '=Done';
	}else{
		var count = this.cookie ? parseInt(this.cookie) + 1 : 1;
		var cookie = this.id + '=' + count;
		var expiresTemp = ';expires=' + new Date( new Date().getTime()+ 1000*60*60*24*14*count).toGMTString();
		var cookieTemp = this.id + 'Temp=true';
		document.cookie = cookieTemp + expiresTemp + path + domain;
	}

	document.cookie = cookie + expires + path + domain;
}

Popup.prototype.close = function(like){
	this.like = like;
	this.setCookie();
	document.body.removeChild(this.popupDiv);
	if (this.callbackFunction) this.callbackFunction();
}
Popup.prototype._checkCookie = function(name){
	var isCookie = document.cookie.indexOf(name + '=');
	if (isCookie == -1) return false;
	var valueStart = isCookie + name.length + 1;
	var cookieStart = document.cookie.substring(valueStart);
	var valueEnd = (cookieStart.indexOf(';') == -1) ? cookieStart.length : cookieStart.indexOf(';');
	var cookieValue = cookieStart.substring(0, valueEnd);
	return cookieValue;
}
Popup.prototype._animFade = function(){
	this.popupDiv.style.opacity = 0;
	this.popupDiv.style.filter = 'alpha(opacity=0)';

	var start = new Date().getTime();
	var popupDiv = this.popupDiv; // for FF
		setTimeout(function() {
			var now = (new Date().getTime()) - start;
			var progress = now / 500;
			if (progress < 1) {
				popupDiv.style.opacity = (1 - 0) * delta(progress) + 0;
				popupDiv.style.filter = 'alpha(opacity=' + Math.round((100 - 0) * delta(progress) + 0) + ')';
				setTimeout(arguments.callee, 10);
			}else{
				popupDiv.style.opacity = 1;
				popupDiv.style.filter = 'none';
			}
		}, 10);

	var delta = function(progress) {
		return progress;
	}
}
Popup.prototype._animUp = function (){
	this.screenHeight = window.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight);
	this.popupDiv.style.top = this.screenHeight + this.height / 2 + 'px';
	var startValue = this.screenHeight + this.height / 2;
	var finalValue = this.screenHeight / 2;
	var start = new Date().getTime();
	var popupDiv = this.popupDiv; // for FF
		setTimeout(function() {
			var now = (new Date().getTime()) - start;
			var progress = now / 1000;
			if (progress < 1) {
				popupDiv.style.top = Math.round((finalValue - startValue) * delta(progress) + startValue) + 'px';
				setTimeout(arguments.callee, 10);
			}else{
				popupDiv.style.top = finalValue + 'px';
			}
		}, 10);

	var delta = function(progress) {
		function d(progress) {
			for (var a = 0, b = 1, result; 1; a += b, b /= 2) {
				if (progress >= (7 - 4 * a) / 11)
					return -Math.pow((11 - 6 * a - 11 * progress) / 4, 2)
							+ Math.pow(b, 2);
			}
		}
		return 1 - d(1 - progress);
	}
}
