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>');
			}
			this.cantSubmit=false;			
		} else if (resp.redirect) {
			ccmDiscussion.redirect(resp.redirect);
		}
	},
	
	redirect: function(redir) {
		window.location.href = redir;
	},
	
	post: 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");						
			},
			height: 470
		});
		
		$("div.ccm-discussion-field input#subject").get(0).focus();
	},

	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
		});
	}, 
	
	viewCode: function(element) {
		
		$('#discussion-code-content').html(element.parent().parent().next().html());
		jQuery.facebox($('#discussion-code').html());
	},
	
	selectSource: function() {
		$('#discussion-code-content').html('<textarea>'+$('#discussion-code-content').html()+'</textarea>');
		//$('#discussion-code-content')
	},
	
	toggleDowloadImage: function(aTag) {
		// show the image
		if(aTag.text() == '(view image)') {
			aTag.text('(hide image)');
		} else {
			aTag.text('(view image)');
		}
		aTag.next('.imageDiv').slideToggle('normal', function() {												  
			// reposition
			$('#facebox').css('left', $(window).width() / 2 - ($('#facebox table').width() / 2));														  
		});
		
	},
	
	editReply: function(replyCID){
		this.cantSubmit=false;
		this.loadReplyIntoForm(replyCID);		
	},
	
	loadReplyIntoForm: function(replyCID,parentCID){ 
		jQuery.facebox($('#discussion-post-reply-form').html());
		$("div#facebox input[@name=subject]").get(0).focus();	
		var url=CCM_REL+'/tools/discussion_reply_form/?replyCID='+parseInt(replyCID)+'&pCID='+parseInt(parentCID);
		$("#facebox form#discussion_reply_form").load( url, function(){
			
		});	
	}
}

ccmDiscussionTrack = {
	/*
	track: function(url) {
		$.get(url,function (res) {
			jQuery.facebox(res);					
		});
	},

	unTrack: function(url) {
		$.get(url,function (res) {
			jQuery.facebox(res);					
		});
	},
	*/
	
	viewTrackOverlay: function(url) {
		if (jQuery.fn.dialog.getTotalOpen() > 0) {
			$("#ccm-dialog-content0").load(url);
		} else {
			$.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() } );
