

$(document).ready(function () { ccmDiscussionBadges.init() } );

ccmDiscussionBadges = {  
	
	init : function () {
		$('a.discussion-profile-link').hover(
				function () { ccmDiscussionBadges.show($(this).attr('rel'),$(this))},
				function () { ccmDiscussionBadges.startHideTmr() }
		);
	}, 
	
	userInfoData:[{url:'',html:''}],
	show : function (url,linkElem) {
		ccmDiscussionBadges.clearHideTmr();
		linkElem.animate({opacity: 1.0}, 10,function() {	
			var overLayWidth = 268;
			var de = document.documentElement;
			var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
			var hasArea = w - (ccmDiscussionBadges.getAbsoluteLeft(linkElem) + ccmDiscussionBadges.getElementWidth(linkElem) + 30);
			var clickElementy = ccmDiscussionBadges.getAbsoluteTop(linkElem) - 50; //set y position
			
			if(hasArea > overLayWidth) {
				var clickElementx = ccmDiscussionBadges.getAbsoluteLeft(linkElem) + (ccmDiscussionBadges.getElementWidth(linkElem) + 5);
			} else {
				var clickElementx = ccmDiscussionBadges.getAbsoluteLeft(linkElem) - (overLayWidth + 5);
			}			
			
			if(!$('#profile-preview-overlay').length) {
			
				$("body").append('<div class="profile-preview-overlay" id="profile-preview-overlay">'+
					'<div class="profile-preview-overlay-top"></div>'+
					'<div class="profile-preview-overlay-center" id="profile-preview-overlay-center-content">'+
						'<div id="profile-overlay-loader"></div>'+
					'</div>'+
					'<div class="profile-preview-overlay-bottom"></div>'+
					'</div>');
								
				$('#profile-preview-overlay').css({left: clickElementx+"px", top: clickElementy+"px"});
				//$('#profile-preview-overlay').show();
				$('#profile-preview-overlay-center-content').html('Loading');//load(url);	
				var userHTML; 
				for(var i=0;i<ccmDiscussionBadges.userInfoData.length;i++){ 
					if(ccmDiscussionBadges.userInfoData[i].url==url){
						userHTML=ccmDiscussionBadges.userInfoData[i].userHTML;	
						break;
					}
				}
				if(userHTML){ 
					$('#profile-preview-overlay-center-content').html(userHTML);
				}else{
					ccmDiscussionBadges.lastUserBadgeURL=url;
					$.get(url, '', function(r){ 
						$('#profile-preview-overlay-center-content').html(r);
						ccmDiscussionBadges.userInfoData.push({url:ccmDiscussionBadges.lastUserBadgeURL,userHTML:r})
					}, "HTML");
				}
			}
			$('#profile-preview-overlay').mouseover(function(){ccmDiscussionBadges.clearHideTmr();});
			$('#profile-preview-overlay').mouseout(function(){ccmDiscussionBadges.startHideTmr();});			
		});
								
	},
	
	hide : function () {
		if($('#profile-preview-overlay').length) {
			//$('div#profile-preview-overlay').animate({opacity: 1.0}, 2500,function() { // hack for setTimeout
				$('div#profile-preview-overlay').remove();
			//});	
		}
	},
	
	clearHideTmr:function(){
		clearTimeout(ccmDiscussionBadges.popupTmr);
	},
	
	startHideTmr:function(){
		if($('#profile-preview-overlay-center-content').html()=='Loading')
			 setTimeout('ccmDiscussionBadges.startHideTmr()',3000);
		else ccmDiscussionBadges.popupTmr=setTimeout('ccmDiscussionBadges.hide()',1000);
	},
	
	getElementWidth : function(elem) {
		x = elem.get(0);
		return x.offsetWidth;
	},

	
	getAbsoluteLeft : function(linkElem) {
		// Get an object left position from the upper left viewport corner
		o = linkElem.get(0);
		oLeft = o.offsetLeft            // Get left position from the parent object
		 while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
			oParent = o.offsetParent    // Get parent object reference
			oLeft += oParent.offsetLeft // Add parent left position
			o = oParent
		} 
		return oLeft
	},
	
	getAbsoluteTop : function(linkElem) {
		// Get an object top position from the upper left viewport corner
		//o = document.getElementById(objectId)
		o = linkElem.get(0);
		oTop = o.offsetTop           // Get top position from the parent object
		while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
			oParent = o.offsetParent  // Get parent object reference
			oTop += oParent.offsetTop // Add parent top position
			o = oParent
		}
		return oTop
	},

	manage : function(uID){
		var url=CCM_REL+'/tools/manage_badges/?uID='+parseInt(uID);
		jQuery.facebox.loading();
		$.get(url,function (res){
			jQuery.facebox(res);					
		});	 
	},
	
	saveBadges : function(f){ 
		var url=CCM_REL+'/tools/manage_badges/';
		var data=$(f).serialize();;
		$.post(url, data, function(r){ 
			$('#facebox .popup .content').html(r);
			window.location.reload(true);
		}, "HTML");
		return false;
	}
}