// JavaScript Document
var Pms = {};
Pms.sitePath = '/';

Pms.initApproveStatusWidgets = function(selector, divselector) {
	
	if(!selector) {
		selector = ".approvewidgetlink";	
	}
	if(!divselector) {
		divselector = ".approvewidgetdiv";
	}
	jQuery.each($(selector), function(i, val) {
		var id = $(this).metadata().id;
		var divid = "approvewidgetdiv" + id;
		$(val).click(function(event) {
			$("#" + divid).toggle();
		});
	});
	
	jQuery.each($(selector), function(i, val) {
		var id = $(this).metadata().id;
		var divid = "approvewidget" + id;
		$(val).click(function(event) {
			$("#" + divid).toggle();
		});
	});
	
	jQuery.each($(divselector + " select"), function(i, val) {
		$(val).change(function(event) {
			var id = $(this).metadata().id;
			var url = Pms.sitePath + "request/setapprovestatus.php?id=" + id + "&ts=" + Math.random() + "&approved=" + $(this).val();
			GabeUtil.screenDimLoading(true);
			// alert(url);
			// window.location.href = url;
			
			GabeUtil.ajaxRequest('get', url, {
				onLoad : function(response) {
					GabeUtil.screenDimLoading(false);
				},
				onOK : function(response) {
					var cid = response.id;
					var newhtml = response.data;
					$("#approvewidgetlink" + cid).html(newhtml);
					$("#approvewidgetdiv" + cid).hide();
				}					 
			}, 'json');
			
		});
	});
}

Pms.initCategoryWidgets = function(selector) {
	if(!selector) {
		selector = ".categorywidget";
	}
	$(selector).html("Loading categories...");
	jQuery.each($(selector), function(i, val) {
		var parentid = $(this).metadata().parentid;
		var ctype = $(this).metadata().ctype;
		var tags = $(this).metadata().tags;
		var url = "request/ccategories.php?parent_id=" + parentid + "&currtags=" + tags;
		GabeUtil.ajaxRequest('get', url, {
			onLoad : function(response) {
				$(val).html(response);
				$(".newcategorybtn", $(val)).click(function(event) {
					var newcat = $("input[name='newcategory']:first", $(val)).val();
					if(newcat.length < 3) {
						alert("Please enter the label of the new category.");
						return;
					}
					var url = "request/addpost.php?name=" + encodeURIComponent(newcat) + "&parent_id=" + parentid + "&ctype=category&ts=" + Math.random();
					// alert(url);
					
					GabeUtil.ajaxRequest('get', url, {
						onOK : function(response) {
							Pms.initCategoryWidgets(selector);	
						}		 
					}, 'json');
					
				});
			}					 
		});
	});
}

Pms.initStoreWidgets = function(selector, fname, requrl) {
	if(!requrl) {
		requrl = '';
	}
	if(!selector) {
		selector = ".storeswidget";
	}
	if(!fname) {
		fname = "tags";
	}
	$(selector).html("Loading stores...");
	jQuery.each($(selector), function(i, val) {
		var parentid = $(this).metadata().parentid;
		var ctype = $(this).metadata().ctype;
		var stores = $(this).metadata().currstores;
		var url = requrl + "request/cstores.php?parent_id=" + parentid + "&currstores=" + stores + "&fieldname=" + fname;
		// alert(url);
		GabeUtil.ajaxRequest('get', url, {
			onLoad : function(response) {
				$(val).html(response);
				$(".newstorebtn", $(val)).click(function(event) {
					var newcat = $("input[name='newstore']:first", $(val)).val();
					if(newcat.length < 3) {
						alert("Please enter the name of the new category.");
						return;
					}
					var url = "request/addmember.php?companyname=" + encodeURIComponent(newcat) + "&parent_id=" + parentid + "&category=store&ts=" + Math.random();
					// alert(url);
					
					GabeUtil.ajaxRequest('get', url, {
						onOK : function(response) {
							Pms.initStoreWidgets(selector);	
						}		 
					}, 'json');
					
				});
			}					 
		});
	});
}

Pms.initContentComments = function(selector, opts) {
	if(!selector) {
		selector = ".commentswrapper";
	}
	if(!opts) {
		opts = {};
	}
	
	
	$(".addcommentbtn").click(function(event) {
		var id = $(this).metadata().id;
		$("#commentdiv" + id).toggle();
	});
	
	var lastForm;
	
	$(".commentform").ajaxForm({
		beforeSubmit : function(odata, oform, opt) {
			lastForm = oform;
			$("input[name='submitbtn']", $(oform)).hide();
			$(".loadingbtn", $(oform)).show();
			return true;
		},
		success : function(response) {
			alert(response.message);
			if(response.status == 'OK') {
				// alert("#commentdiv" + response.blogid);
				$("#commentdiv" + response.blogid).hide();
				$("#commentsdiv" + response.blogid).show();
				Pms.blogComments(response.blogid);
			}
			$("input[name='submitbtn']", $(lastForm)).show();
			$(".loadingbtn", $(lastForm)).hide();
		},
		dataType : 'json'	
	});
	
	$(".getcommentsbtn").click(function(event) {
		var blogid = $(this).metadata().id;
		if($("#commentsdiv" + blogid).is(":visible")) {
			$("#commentsdiv" + blogid).toggle();
		} else {
			$("#commentsdiv" + blogid).toggle();
			Pms.blogComments(blogid);
		}
		
	});
			
}

Pms.blogComments = function(blogid) {
	$("#commentsdiv" + blogid).html("<p align='center'>Loading Comments...<br /><img src='" + Pms.sitePath + "image/loading.gif' class='loadingbtn' /></p>");
	window.location.href = '#' + "commentsdiv" + blogid;
	var url = Pms.sitePath + "request/blogcomments.php?parent_id=" + blogid + "&order_by=" + encodeURIComponent('c.id desc') + "&ts=" + Math.random();
	GabeUtil.ajaxRequest('get', url, {
		onLoad : function(response) {
			$("#commentsdiv" + blogid).html(response);
			
			$(".deletecomment").click(function(event) {
				var commentid = $(this).metadata().id;
				var conf = confirm("Delete this comment?");
				if(!conf) {
					return;
				}
				var url = Pms.sitePath + "request/deletecomment.php?comment_id=" + commentid + "&ts=" + Math.random();
				// alert(url);
				GabeUtil.ajaxRequest('get', url, {
					onOK : function(response) {
						blogComments(blogid);
					}
				}, 'json');
			});
		}
	});
}
