// @author Soldatoff Alexey <info@debug.org.ua>
window.UserProfile = {

	timer: null,
	timerBeforeOpen: null,

	boxWidth: 308,

	delayOpen: 500,
	delayClose: 300,
	delayBeforeClose: 200,

	elementWidth: 300,

	getUserProfile: function(userID, event) {

		var div = $('jsup_' + userID);
		userID = Number(parseInt(userID));

		if(userID) {

			var url = '/js/user_profile/' + userID;

			var ajax_options = {
				method: 'get',
				onComplete: function(response) {

					if ( '' === response.trim() ) {
						this.cancel();
					} else {

						//div.setHTML.delay(100, window, response);
						div.setHTML(response);
						//this.evalScripts();

						div.setStyles({'display' : 'block', 'opacity' : 0});
						var myFx = new Fx.Style(div, 'opacity').start(0,1);

						UserProfile.position(event);
					}
				},
				onRequest: function() {
					div.setHTML('<img src="http://i.bigmir.net/hp/img/progress_bar.gif" />');
				},
				data: Object.toQueryString( {DEBUG_MODE: 0, _rnd: Math.random()} )
			};

			(new Ajax(url, ajax_options)).request();
		}
	},
	

	openBox : function(userID, event) {

		var popup_profile = $('jsup_' + userID);

		if ( __Relation ) __Relation.userID = userID;

		if( !popup_profile ) {

			popup_profile = new Element('div', {
				'class' : 'popup popup_profile_box',
				'id' : 'jsup_' + userID,
				'width' : UserProfile.elementWidth,
				styles : {
					'backgroundImage' : 'url(http://i.bigmir.net/spacer.gif)'
				}
			});

			popup_profile.addEvent('mouseleave', function(){
				UserProfile.timer = UserProfile.closeBox.delay(UserProfile.delayBeforeClose, window, userID);
			});

			popup_profile.addEvent('mouseenter', function(){
				$clear(UserProfile.timer);
			});

			popup_profile.injectAfter(event.target);
			UserProfile.getUserProfile(userID, event); // get user profile using ajax;
		} else {

			popup_profile.setStyles({'display' : 'block', 'opacity' : 0});
			var myFx = new Fx.Style(popup_profile, 'opacity').start(0,1);
			UserProfile.position(event);
		}
		UserProfile.hideFlashContent();
	},
	

	closeBox: function(userID) {

		var popup_profile = $('jsup_' + userID);
		
		if(popup_profile) {
			popup_profile.setStyles({'display' : 'none'});
		}
		UserProfile.showFlashContent();
	},
	

	initBox: function(elem) {

		elem.addEvent('mouseenter', function(event){
			$$('div.popup_profile_box').each(function(el){
				el.setStyles({'display' : 'none'});
			});

			$clear(UserProfile.timer);
			UserProfile.timerBeforeOpen = UserProfile.openBox.delay(UserProfile.delayOpen, window, [this.rel, new Event(event)]);
		});

		elem.addEvent('mouseleave', function(event){
			$clear(UserProfile.timerBeforeOpen);
			UserProfile.timer = UserProfile.closeBox.delay(UserProfile.delayClose, window, this.rel);
		});

		// remove title
		elem.each(function(item){
			if(item.title) item.title = '';
		});
	},


	initBoxPattern: function(elem) {
		UserProfile.initBox(elem.getElements('a.jsup[rel]'));
	},


	hideFlashContent : function() {
		if(window.showHideFlash) {
			showHideFlash(false);
		}
	},
	showFlashContent : function() {
		if(window.showHideFlash) {
			showHideFlash(true);
		}
	},


	position: function(myEvent) {

		var elem = $(myEvent.target);

		var popup_profile = $('jsup_' + elem.rel);

		if(popup_profile) {

			var elemCoords = elem.getCoordinates();
			var coords = popup_profile.getCoordinates();

			var windowHeight = window.getHeight();
			var windowWidth = window.getWidth();

			var scrollTop = window.getScrollTop();
			var scrollLeft = window.getScrollLeft();

			var boxBottom = coords.height + myEvent.client.y;

			var boxRight = UserProfile.boxWidth + myEvent.client.x;

			popup_profile.setStyles({'top' : myEvent.client.y + scrollTop, 'left' : myEvent.client.x + scrollLeft}); // default normal position

			if (boxBottom >= windowHeight) {
				popup_profile.setStyle('top', myEvent.client.y + scrollTop-coords.height);
			}

			if (boxRight >= windowWidth) {
				popup_profile.setStyle('left', myEvent.client.x + scrollLeft-UserProfile.boxWidth);
			}
		}
	}
}
