var Discussions = new function DSCS(){
	this.MakePost = function DSCS_MakePost(id, status, type, DiscussionCategories, errors, post, topic, dscType, dscCatId, entityId){
		if(typeof DiscussionHome != "undefined"){
			var btnId = id + "_" + status + "_" + type;
			if(DiscussionHome.ChildDiscussionID > 0
				&& id == "lnkPost") {
				btnId += "_" + DiscussionHome.ChildDiscussionID;
			}
			return DiscussionHome.MakePost(btnId, DiscussionCategories, errors, post, topic);
		}
		var link = document.getElementById(id);
		var preLoadedDiscussionCategoryID = 0;
		
		if ($(link).is('.discussionFilterCategory')){
			preLoadedDiscussionCategoryID= EF.Utils.JSData.get("jsdDiscussionCategoryID")
		}
		
		var newDiv = document.createElement("div");
		addClass(newDiv,"reply_cont");
		newDiv.id = "Reply" + id;
		link.style.display = "none";
		link.parentNode.appendChild(newDiv);

		var header;
		if (type == 1 || type == 6 || type == 9){
			header = '<span class="start_discussion_cont" onclick="Discussions.CancelReply(\''+ id +'\')">Start a discussion</span>';
		} else if (type == 2){
			header = '<span class="make_posting" onclick="Discussions.CancelReply(\''+ id +'\')">Make a post</span>';
		} else if (type == 3){
			header = '<div class="y_message" onclick="Discussions.CancelReply(\''+ id +'\')">Your reply</div>';
		}

		var err = errors ? decodeURIComponent(errors) : '';
		if(status == 0){
			newDiv.innerHTML = header
					+ '<div class="not_possible_reply">'
					+ err
					+ 'To make a post or reply please <a href="/Log-in.aspx?referer='
					+ encodeURIComponent(location.href)
					+ '" >Sign in</a>, <a href="/Register.aspx?referer='
					+ encodeURIComponent(location.href)
					+ '" >Register</a> or <span onclick="UniversalLogin.fbLogin(true, UniversalLogin.RefreshPage);" class="fb_connect_btn">&nbsp;</span></div>'
					+ '<div class="reply_btn_cont">';

		} else if (status == 1){
			newDiv.innerHTML = header 
					+ '<div class="not_possible_reply">'
					+ 'This feature is not active for your account. To activate please complete your '
					+ '<a href="#" onclick="return UniversalLogin.efCompleteProfile(function(isOk){Discussions.Reload(\''+ id +'\');});">profile</a></div>'
					+ err
					+ '<div class="reply_btn_cont">';
										
		} else if(status == 2){
			if(type == 1 || type == 6 || type == 9)
				header += '<div class="new_discussion_cont">'
						+ (DiscussionCategories ? '<div id="DiscussionCategories3">' + document.getElementById("DiscussionCategories").innerHTML + '</div>' : '')
						+ '<div class="dis_tit">Discussion Topic</div>'
						+ '<input type="text" id="tTopic'+ id +'" class="new_dis_tit_cont" value="'+ (topic ? topic : '') +'" />'
						+ '<div class="dis_tit">Post</div>';
			var txt = '<div class="reply_btn_cont">'
					+ '<input type="hidden" name="type" value="'+ type +'" />'
					+ '<input type="hidden" name="id" value="'+ id	+'" />'
					+ '<input type="image" name="btnPost" src="/Images/Editorial/Contributors/postBtn.gif" onclick="return Discussions.DoPost(\''+ id +'\',' + preLoadedDiscussionCategoryID + ')" />'
					+ (dscType != null ? '<input type="hidden" name="dscType" value="'+dscType+'">' : '')
					+ (dscCatId != null ? '<input type="hidden" name="DiscussionCategoryID" id="DiscussionCategoryID" value="'+dscCatId+'">' : '')
					+ (entityId != null ? '<input type="hidden" name="entityId" value="'+entityId+'">' : '')
					+ '<input type="hidden" name="Post" id="Post'+ id +'" />'
					+ '<input type="hidden" name="Topic" id="Topic'+ id +'" />'
					+ '</div>'
					+ (DiscussionCategories || preLoadedDiscussionCategoryID ? '<div id="DiscussionCategories2" style="display:none"></div>' : '');
			newDiv.innerHTML = header 
					+ '<textarea id="tPost'+ id +'" rows="5" cols="5">'
					+ (post ? post.replace(new RegExp('&lt;br */?&gt;', 'g'), '\n') : '')
					+ '</textarea>'
					+ err;
			try{
				newDiv.innerHTML = header 
					+ '<textarea id="tPost'+ id +'" rows="5" cols="5">'
					+ (post ? post.replace(new RegExp('&lt;br */?&gt;', 'g'), '\n') : '')
					+ '</textarea>'
					+ err
					+ '<form method="post">'
					+ txt
					+ '</form>';
			} catch(e) {
				newDiv.innerHTML = header 
					+ '<textarea id="tPost'+ id +'" rows="5" cols="5">'
					+ (post ? post.replace(new RegExp('&lt;br */?&gt;', 'g'), '\n') : '')
					+ '</textarea>'
					+ err
					+ txt;
			}
		} else if(status == 3) {
			newDiv.innerHTML = header
					+'<div>This feature was disabled for your account by administrator</div>'
					+ err;
		} else if(status == 4) {
			newDiv.innerHTML = header
					+'<div>To start a discussion please select product category</div>';
		}
		var wrapper = $('<div class="reply_cont_wrap"/>');
		$(newDiv).wrap(wrapper);
		return false;
	};

	this.CancelReply = function DSCS_CancelReply(id){
		var node = document.getElementById("Reply" + id);
		node.parentNode.removeChild(node);
		document.getElementById(id).style.display = "inline"; //show a post button
		var data = id.split("_");
		var edit_btn = document.getElementById(data[0] + '_' + data[1] + '_' + data[2] + '_edit');
		if(edit_btn !=null){
		    edit_btn.style.display = "inline"; //show a edit post button
		}
		return false;
	};
	
	this.CancelEdit = function DSCS_CancelEdit(id, replyBtn){
		var node = document.getElementById("Edit" + id);
		node.parentNode.removeChild(node);
		$("#"+id).show();
		$("#"+replyBtn).show();
		return false;
	};

	var Posted = 0;
	this.DoPost = function DSCS_DoPost(id, preLoadedDiscussionCategoryID){
		if(Discussions.Posted == 1) return false;
		
		var topic = document.getElementById('tTopic' + id);
        var post = document.getElementById('tPost' + id);
        $(post).siblings('.dis_err_text').remove();
        
		if (topic 
			&& ! $(topic).val() ) {
		    $(post).after("<p class='dis_err_text'>Discussion Topic is mandatory field</p>");
		    return false;
		}
		if(topic) document.getElementById('Topic'+ id).value = topic.value;

		if (! $(post).val()) {
		    $(post).after("<p class='dis_err_text'>Post is required</p>");
		    return false;
		}
		document.getElementById('Post'+ id).value = post.value;
		
		Discussions.Posted = 1;
		
		var el = document.getElementById("DiscussionCategories2");
		if(el){
			var html;
			if (!preLoadedDiscussionCategoryID){
				$('#DiscussionCategories3 .post').each(function(){
					var name=$(this).attr('name');
					if(name){
						var value = $(this).attr('value');
						html += '<input type="hidden" name="' + name + '" value="' + value + '"/>';
					}
				});
			}else{
				html = '<input type="hidden" name="DiscussionCategoryID" value="' + preLoadedDiscussionCategoryID + '"/>';
			}
			document.getElementById("DiscussionCategories2").innerHTML = html;
		}
		
	};

	this.InitSlide = function DSCS_InitSlide(slides){
		//$(".ExtLink").live("click", Discussions.Goto);
		$('ul.product_disscussions_cont li').find('div.prod_discussion_title').click(
		function DSCS_InitSlide_product_disscussions_cont_li_click(){
			$(this).parent().toggleClass('extended_discussion');
		});
	};
    this.OpenedTab = false;
    
	this.InitDiscussionsSlide = function DSCS_InitDiscussionsSlide(){
		var disBtn = $('#discussion_tab_btn');
		var disCont = $('#discussion_tab_cont');
		disBtn.mouseover(function DSCS_InitDiscussionsSlide_disBtn_mouseover(){
			if (!Discussions.OpenedTab){
				$(this).css({backgroundPosition: "0 -26", border: "1px solid #f0c"});
			}
		}).mouseout(function DSCS_InitDiscussionsSlide_disBtn_mouseout(){
			if (!Discussions.OpenedTab){
				$(this).css({backgroundPosition: "", border: "1px solid #fff"});
			}
		}).click(function DSCS_InitDiscussionsSlide_disBtn_click(){
			
			$.efTemplates.popUps.closeSelectorPopUps();

			if (Discussions.OpenedTab){
				disCont.css({display:"none"});
				disBtn.css({backgroundPosition: "", border: "1px solid #fff"});
				Discussions.OpenedTab = !Discussions.OpenedTab;
			}else{
				$(this).css({backgroundPosition: "0 -52px", border: "1px solid #f0c"});
				Discussions.OpenedTab = !Discussions.OpenedTab;
				disCont.css({display: "block"});
			}
		});
		$("#discussion_tab_cont .lgrad_shad .rgrad_shad .btn_close_cont").click(function DSCS_InitDiscussionsSlide_btn_close_cont_click(){
			$.efTemplates.popUps.closeSelectorPopUps();
			disCont.css({display:"none"});
			disBtn.css({backgroundPosition: "", border: "1px solid #fff"});
			Discussions.OpenedTab = !Discussions.OpenedTab;
		});
		//$("#discussion_tab_cont .ExtLink").bind("click", Discussions.Goto);
	};
	
	this.Goto = function DSCS_Goto(){
		var s = this.firstChild.firstChild.nodeValue;
		var url = "";
		for(var i = 0; i < s.length; i++){
			url += String.fromCharCode(s.charCodeAt(i) - 1);
		}
		location.href = url;
	};
	
	this.SetType;
	this.SetParam;

	var setLoaded = false;
	var lastSetType;
	var lastSetParam;
	var countAlreadyLoaded;
	var postsAlreadyLoaded;

	this.LoadSet = function DSCS_LoadSet(options){
	    var defaultOptions = {
	        callback: null,
	        countOnly: false,
	        isProductListTemplate: false,
	        isCategoryProductListTemplate: false,
	        force: false
	    };
	    options = $.extend({}, defaultOptions, options || {});
	    if (Discussions.OpenedTab) {
	        options.countOnly = false;
	    }
	    if (!options.force) {
		    if (!Discussions.SetType
		        || options.isProductListTemplate && !options.isCategoryProductListTemplate && options.countOnly
		        || (
		            Discussions.SetType == lastSetType
		            && Discussions.SetParam == lastSetParam
		            && (options.countOnly && countAlreadyLoaded
		                || !options.countOnly && countAlreadyLoaded && postsAlreadyLoaded)
		            && setLoaded
		            )
		        ) {
		        return;
            }
        }
        setLoaded = false;
		lastSetType = Discussions.SetType;
		lastSetParam = Discussions.SetParam;
		countAlreadyLoaded = true;
		postsAlreadyLoaded = !options.countOnly;
		$.ajax({type: "POST",
			url: "/Discussions/DiscussionsDataSet.aspx",
			dataType: "json",
			data: "type=" + Discussions.SetType
			    + "&" + Discussions.SetParam
			    + "&countOnly=" + (options.countOnly ? 1 : 0),
			success: function(data){
				for(var key in data){
					var el = document.getElementById(key);
					if(el) el.innerHTML = data[key];
				}
				$(".javascript").each(function(){
					eval($(this).text());
				});
				if(options.callback) options.callback();
				LoginButton.RegisterEvents();
				setLoaded = true;
			}
		});
	};

    this.hidePrevious = function(){
        $(".reply_cont_wrap").each(function(){
            var cur = $(this);
            cur.prev().css("display","inline");
            cur.remove();
        });
    };
    
	this.Reload = function(actionID){
		if(!Discussions.SetType){
			window.location.reload();
		}
		Discussions.LoadSet({
		    force: true,
		    callback: function(){ $("#"+ actionID).click(); }
		});
		UniversalLogin.Wizard.Hide();
	};
};

