/*
 * jQuery facebook module v1.0.0 - http://www.onlinetvcast.com/
 *
 *
 */

// FIXME: глобальный рефакторинг
 
 (function($) {
 	function Module_Facebook(options) {
 		var defaults = {
 			_oauth_link : 'users_oauth_facebook',
 			_window_oauth_title : 'Connect_with_Facebook'
 		};
        this.options = $.extend(defaults, options);
 	}
 	 	 	 	
 	Module_Facebook.prototype = {
 		init: function() {
 			this.activateLogin();
 		},
 				
		activateLogin: function () {
			var self 				= this;
			var $box 	 			= $('.box');
			var $loading 			= $('.loading', $box);
		
			$('.' + self.options._oauth_link).bind('click', function() {
				// Hiding central div to work in all browsers with all player
				$('#center_big .center').hide();
				if ($.browser.msie && $.browser.version == '6.0') {
					// for ie6 hiding selects
					$('select').css('visibility', 'hidden');
				}		
				$box.height($(document).height());
				$loading.show();
				// http://stackoverflow.com/questions/1444677/javascript-window-open-not-working-in-ie8
				var _window_oauth = window.open($(this).attr('rel'), self.options._window_oauth_title, 
				"width=850,height=700,menubar=no,location=yes,resizable=yes,scrollbars=yes,status=yes,screenX=20,screenY=30");
				self.centerWindow(_window_oauth, 850, 700);
							
				// If window has been closed, unblock main window
				var timer = window.setInterval(function(){
					if (_window_oauth.closed) {
						$('.customzone_close').click();
						window.clearInterval(timer);
					}
				}, 500);
				
				// If main window has been reload or closed, close oAuth window
				$(window).bind('unload', function(){
					_window_oauth.close();
				});
								
				return false;
			});
		},
		
		closeWindowOauth: function(){
			if (window.opener) {
				window.opener.location.reload();
				// When you restart the parent window, the current will be closed (Since we desired delay and without unblock)
				// self.close();
			}
			self.close();
		},
		
		centerWindow: function(_window, width, height) {
			var canCool;
			canCool = navigator.appVersion.charAt(0);
			var $_window = $(_window); 
			var top; var left; 
			if (canCool >= 4) { 
				left = (screen.width - width)/2;  
				top  = (screen.height - height)/2;
				// _window but not, $_window
				_window.moveTo(left,top); 
				_window.resizeTo(width, height); 
				_window.focus(); 
			}
		},
		
		centerCurrentWindow: function() {
			var canCool;
			canCool = navigator.appVersion.charAt(0); 
			var top; var left; 
			if (canCool >= 4) { 
				left = (screen.width - 800)/2; 
				top  = (screen.height - 600)/2; 
				window.moveTo(left,top); 
				window.resizeTo(800, 600); window.focus(); 
			}
		}
	}
 	
 	$.Module_Facebook = function(options) {
 		return new Module_Facebook(options);
 	};
 	
 	$.Module_Facebook_prototype = function() {
 		return Module_Facebook.prototype;
 	};
 
 })(jQuery);
