var ccmDiscussion = {  

	//canSubmit: true,
	totalAttachments: 0,
	cantSubmit:false, 
	submit: function(form) {  
		if (this.cantSubmit) { 
			return false;
		} 
		this.cantSubmit=true;
		this.submittedForm=form;
		ccmDiscussion.showLoading();
		$("div#ccm-discussion-post-errors").html("");
		return true;
	},
	
	response: function(data) {	
		resp = eval("(" + data + ")");
		if (resp.errors) {
			ccmDiscussion.hideLoading();
			for(i = 0; i < resp.errors.length; i++) {
				$("div#ccm-discussion-post-errors").append(resp.errors[i] + '<br>');
			}
			
			if(resp.newCaptcha) {
				$('.ccm-captcha-image').attr('src',resp.newCaptcha);
				$('.ccm-input-captcha').val('');
			}
			
			this.cantSubmit=false;			
		} else if (resp.redirect) {
			ccmDiscussion.redirect(resp.redirect);
		} else if (resp.pending) {
			$('form#discussion-post-form-form').hide();
			$('div#ccm-discussion-post-pending').show();
		}
	},
	
	redirect: function(redir) {
		window.location.href = redir;
	},
	
	postForm: function(cID) {
		
		this.cantSubmit=false;
		if (parseInt(cID) > 0) {
			$("input[name=cDiscussionPostParentID]").val(cID);		
		}
		
		$('#ccm-discussion-post-form-wrapper').show();
		
		setTimeout(function() {
			window.location.hash = '#ccm-discussion-post-anchor';
		}, 100);	

		/* ccmDiscussion.clearPostForm(); */
	},
	
	postOverlay: function(cID,uID) {
		this.cantSubmit=false;
		if (parseInt(cID) > 0) {
			$("input[name=cDiscussionPostParentID]").val(cID);		
		}
		
		//display login popup if not logged in
		/*
		if(uID==0){
			this.parentPostCID=parseInt(cID);
			ccmPopupLogin.show("",function(){ ccmDiscussion.post( ccmDiscussion.parentPostCID , parseInt(ccmPopupLogin.uID) ); });
			return false;
		}
		*/
		$.fn.dialog.open({
			title: "Add Discussion",
			element: '#ccm-discussion-post-form',
			width: 550,
			modal: false,
			onClose: function() {
				// we have to retrieve the add event element and insert it back into the dom where it used to be
				// trust me, this is necessary due to the javascript we're loading into the popup
				$("#ccm-discussion-post-form").prependTo("#ccm-discussion-post-form-wrapper");	
				ccmDiscussion.clearPostForm();
				
			},
			height: 470
		});
		
	},

	clearPostForm: function() {	
		$("div#ccm-discussion-post-form input#subject").val("");
		$("div#ccm-discussion-post-form textarea#message").val("");
		//$("div#ccm-discussion-post-form input#subject").get(0).focus();	
		$("div#ccm-discussion-post-form div.ccm-discussion-attachments-wrapper").html('');
		$("div#ccm-discussion-post-form input[name=track]").attr('checked', true);
		$("div#ccm-discussion-post-form input[name=cDiscussionPostID]").val(0);
	},
	
	showLoading: function() {
		ccmDiscussion.cantSubmit = true;
		$("#ccm-discussion-post-form .ccm-input-submit").get(0).disabled = true;
		$("div.ccm-discussion-post-loader").show();
	},

	hideLoading: function() {
		ccmDiscussion.canSubmit = true;
		$("#ccm-discussion-post-form .ccm-input-submit").get(0).disabled = false;
		$("div.ccm-discussion-post-loader").hide();
	},

	
	addAttachment: function() {
		ccmDiscussion.totalAttachments++;
		$(".ccm-discussion-add-attachment").html("Attach another file");
		var html = $(".ccm-discussion-attachments-selector").html();
		$(".ccm-discussion-attachments-wrapper").append(html);
	},
	
	removeAttachment: function(link) {
		ccmDiscussion.totalAttachments--;
		var p = $(link).parent();
		p.remove();	
		
		if (ccmDiscussion.totalAttachments == 0) {
			$(".ccm-discussion-add-attachment").html("Attach a file");
		}
	},
	
	downloadAttachments: function(action) {
		$.fn.dialog.open({
			title: "Attachments",
			href: action,
			width: 400,
			modal: false,
			onClose: function() {
				$('#ccm-discussion-files-content').html('');
			},
			height: 300
		});
	}, 
	
	editReply: function(url, useOverlay) {
		this.cantSubmit=false;
		$.get(url, false, function(r) {
			var html = $(r).find("#ccm-discussion-post-form").html();
			$(document.body).find("#ccm-discussion-post-form").html(html);
			if (useOverlay) {
				$.fn.dialog.open({
					title: "Edit",
					element: '#ccm-discussion-post-form',
					width: 550,
					modal: false,
					onClose: function() {
						// we have to retrieve the add event element and insert it back into the dom where it used to be
						// trust me, this is necessary due to the javascript we're loading into the popup
						$("#ccm-discussion-post-form").prependTo("#ccm-discussion-post-form-wrapper");
						ccmDiscussion.clearPostForm();
					},
					height: 470
				});
			} else {
				$('#ccm-discussion-post-form-wrapper').show();
				setTimeout(function() {
					window.location.hash = '#ccm-discussion-post-anchor';
				}, 100);	
			}			
		});
	},
	
	changePostToPage: function(url) {
		$.fn.dialog.open({
			title: "Change Post to Page",
			replace: true,
			href: url,
			width: 500,
			modal: false,
			height: 400
		});	
	
	},
	
	deletePost: function(url) {
		$.fn.dialog.open({
			title: "Delete",
			href: url,
			width: 300,
			modal: false,
			height: 200
		});	
	}
}

ccmDiscussionTrack = {

	viewTrackOverlay: function(url, dotrackurl, removetrackurl) {
		if (jQuery.fn.dialog.getTotalOpen() > 0) {
			$("#ccm-dialog-content0").load(url);
			/* Barefoot Modification barefootmodifcation BFMOD: iwr: start/stop monitoring label without page refresh */
			if ( $("#monitor-btn").hasClass("tracking") ) {
				$("#monitor-btn").removeClass("tracking");
				$("#monitor-btn").addClass("not-tracking");				
			} else {
				$("#monitor-btn").removeClass("not-tracking");
				$("#monitor-btn").addClass("tracking");
			}
			
		} else {
			var h = 'trackaction=' + encodeURIComponent(dotrackurl) + '&removetrackaction=' + encodeURIComponent(removetrackurl);
			var url = (url.indexOf('?') > -1) ? url + '&' + h : url + '?' + h;

			$.fn.dialog.open({
				title: "Monitor",
				replace: true,
				href: url,
				width: 300,
				modal: false,
				height: 260
			});
		}
	}
}

function showNextProfileOverlay(elem) {
	elem.next('.profile-preview-overlay').show();
}




ccmDiscussionBadges = {  
	
	init : function () {		
		$('a.ccm-show-badges-trigger').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() {
													 
			ccmDiscussionBadges.clearHideTmr(); 
													 
			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(url){ 
		$.fn.dialog.open({
			title: '',
			href: url,
			width: 550,
			modal: false,
			height: 400
		});		
	},
	
	saveBadges : function(f){ 
		var url=f.action;
		var data=$(f).serialize();;
		$.post(url, data, function(r){ 
			$('#ccm-dialog-content0').html(r);
			window.location.reload(true);
		}, "HTML");
		return false;
	}
}
$(document).ready(function () { ccmDiscussionBadges.init() } );

