// JavaScript Document

var ClientCurric = {};

// get client curric category panel
ClientCurric.getCategoryPanel = function(category, userid) {
	var url = '/vwz-files/webzone/clientcurric.php?getcategorypanel=1&category=' + category + '&userid=' + userid + '&ts=' + Math.random();
	var catopt = {};
	catopt.onLoad = function(response) {
		$("#clientcurric-" + category).html(response);
		ClientCurric.registerCurricCategory($("#clientcurric-" + category));
	}
	GabeUtil.ajaxRequest('get', url, catopt);
}

ClientCurric.clientCurricIntOrderlist = null;

ClientCurric.refreshMasterRows = function(targetdiv) {

	var saveMasterOrderlist = function(ids) {
		// ids is array of ids
		clearTimeout(ClientCurric.clientCurricIntOrderlist);
		ids = ids.join(',');
		var url = '/vwz-files/webzone/clientcurric.php?orderlists=1&ts=' + Math.random() + '&ids=' + ids;
		//alert(url);
		ClientCurric.clientCurricIntOrderlist = setTimeout(function() {
			GabeUtil.ajaxRequest('get', url, {}, 'json');
		}, 2000);
	}

	var ids = new Array();
	jQuery.each($(".ccmasterrow .orderlist", $(targetdiv)), function(i, val) {
		$(val).html(i + 1);
	});
	jQuery.each($(".ccmasterrow", $(targetdiv)), function(i, val) {
		ids.push($(val).metadata().listid);
	});
	saveMasterOrderlist(ids);
}

ClientCurric.getListNotes = function(listid) {
	var parent = $("#listnotes" + listid);
	var url = '/vwz-files/webzone/clientcurric.php?getlistnotes=1&listid=' + listid + '&ts=' + Math.random();
	var getopt = {};
	getopt.onLoad = function(response) {
		$(parent).html(response);
		$(".addlistnoteform").ajaxForm({
			dataType : 'json',
			success : function(response) {
				alert(response.message);
				if(response.status == 'OK') {
					$("#updatedate" + listid).html(response.updatedate);
					$("#lastnote" + listid).html(GabeUtil.htmlDecode(response.lastnote));
					$("#lastnoteinfodiv" + listid).show();
					ClientCurric.getListNotes(listid);
				}
			}
		});
	}
	GabeUtil.ajaxRequest('get', url, getopt);
}
	
ClientCurric.registerCurricCategory = function(table) {

	var category = $(table).metadata().category;
	var userid = $(table).metadata().userid;
	
	/*
	alert(category);
	alert(userid);
	*/
	
	jQuery.each($(".lastnoteinfodiv", $(table)), function(i, val) {
		var listid = $(val).metadata().listid;
		if($("#lastnote" + listid).html() == '') {
			$("#lastnoteinfodiv" + listid).hide();
		}
	});
	
	$(".deletemasterbtn", $(table)).click(function(event) {
		var conf = confirm("Would you like to DELETE this item?\nAll of notes will also be deleted!");
		if(!conf) {
			return false;
		}
		var listid = $(this).metadata().listid;
		var url = '/vwz-files/webzone/clientcurric.php?deletecurriclist=1&listid=' + listid + '&ts=' + Math.random();
		var delopt = {};
		delopt.onOK = function(response) {
			ClientCurric.getCategoryPanel(category, userid);
		}
		GabeUtil.ajaxRequest('get', url, delopt, 'json');
	});
	
	$(".addcurriclistform", $(table)).ajaxForm({
		dataType : 'json',
		beforeSubmit : function(odata, oform, opt) {
			GabeUtil.screenDimLoading(true);
		},
		success : function(response) {
			GabeUtil.screenDimLoading(false);
			alert(response.message);
			if(response.status == 'OK') {
				ClientCurric.getCategoryPanel(category, userid);
			}
		}
	});
	
	$(".getlistnotesbtn", $(table)).click(function(event) {
		var listid = $(this).metadata().listid;
		$("#listnotes" + listid).toggle();
		if($("#listnotes" + listid).html() == '') {
			$("#listnotes" + listid).html("Loading item notes...");
			ClientCurric.getListNotes(listid);
		}
	});
	
	$(".editlistbtn", $(table)).click(function(event) {
		var listid = $(this).metadata().listid;
		$(this).closest('div').hide();
		$('#editlist' + listid).show();
	});
	
	var displayControls = function(thisbutton) {
		$(thisbutton).closest('div').hide();
		$(thisbutton).closest('div').prev().show();
	}
	
	$(".cancellistedit", $(table)).click(function(event) {
		var thisbutton = this;
		displayControls(thisbutton);
	});
	
	var thisform = null;
	
	$(".editlistform", $(table)).ajaxForm({
		dataType : 'json',
		beforeSubmit : function(odata, oform, opt) {
			thisform = 	oform;
			GabeUtil.screenDimLoading(true);
		},
		success : function(response) {
			GabeUtil.screenDimLoading(false);
			alert(response.message);
			if(response.status == 'OK') {
				var newname = $("form input[name='name']:first", $(thisform).closest('tr')).val();
				$('.listpreview', $(thisform).closest('tr')).html(newname);
				displayControls(thisform);
			}
		}
	});
	
	var getChildRows = function(arow) {
		var myid = $(arow).metadata().listid;
		var afters = $("#ccmasterrow" + myid + " ~ tr.ccmasterrow");
		var children = new Array();
		jQuery.each($(afters), function(i, val) {
			var parentlistid = $(val).metadata().parentlistid;
			// alert(parentlistid);
			if(parentlistid == myid) {
				children.push(val);
			}
		});
		return children;
	}
	
	var followChildRows = function(arow, children) {
		jQuery.each($(children), function(i, val) {
			$(arow).after(val);
		});
	}
	
	var getPrevMasterRow = function(therow) {
		var prevs = $(therow).prevAll();
		for(var i = prevs.length - 1; i >= 0; i--) {
			var thiselem = $(prevs[i]);
			if(!$(thiselem).hasClass('ccbulletrow')) {
				return thiselem;
			}
		}
	}
	
	var getNextMasterRow = function(therow) {
		var prevs = $(therow).nextAll();
		for(var i = 0; i < prevs.length; i++) {
			var thiselem = $(prevs[i]);
			if(!$(thiselem).hasClass('ccbulletrow')) {
				var nextrow = prevs[i + 1];
				if(!nextrow) {
					return thiselem;
				} else if($(nextrow).hasClass('ccbulletrow')) {
					var nextrow = getNextMasterRow(thiselem);
					if(!nextrow) {
						return prevs[prevs.length - 1];
					}
					return $(nextrow).prev();
				} else {
					return nextrow;
				}
			}
		}
		return null;
	}
	
	$(".upmasterbtn", $(table)).click(function(event) {
		var thisrow = $(this).closest('tr');
		var children = getChildRows(thisrow);
		//var prevrow = getPrevMasterRow(thisrow);
		var prevrow = $(thisrow).prev();
		if($(prevrow).hasClass('ccmasterrow')) {
			$(prevrow).before(thisrow);
		}
		followChildRows(thisrow, children);
		ClientCurric.refreshMasterRows(table);
	});
	
	$(".downmasterbtn", $(table)).click(function(event) {
		var thisrow = $(this).closest('tr');
		var children = getChildRows(thisrow);
		//var prevrow = getNextMasterRow(thisrow);
		var prevrow = $(thisrow).next();
		// alert($(prevrow).metadata().listid);
		if($(prevrow).hasClass('ccmasterrow')) {
			$(prevrow).after(thisrow);
		}
		followChildRows(thisrow, children);
		ClientCurric.refreshMasterRows(table);
	});
		
}

ClientCurric.loadClientCurricPanel = function(userid, parent, closebutton) {

	if(!closebutton) {
		closebutton = false;	
	}
	
	if(!parent) {
		parent = 'clientcurric-panelres';
	}
	
	if(typeof parent == 'string') {
		parent = $("#" + parent);
	}
	
	$(parent).html("<center><img src='/templates/vwztemplates/images/loadingAnimation.gif' /><br />Loading Client Curriculum...</center>");
	var url = '/vwz-files/webzone/clientcurric.php?getclientpanel=1&userid=' + userid + '&ts=' + Math.random();
	var curricopt = {};
	curricopt.onLoad = function(response) {
	
		if(closebutton) {
			// if closebutton is present
			response += "<p align='center'><input type='button' class='closewindow' value='Close' /></p>";	
		}
		$(parent).html(response);
		if(closebutton) {
			// if closebutton is present
			Webzone.registerCloseButton(response, {});
		}
		$clientcurrictabs = $("#clientcurricwrapper").tabs();
		
		ClientCurric.registerCurricCategory($("#clientcurric-websitelist"));
		ClientCurric.registerCurricCategory($("#clientcurric-orientation"));
		
		// click list item to load category's panel
		$(".categorylistitem").click(function(event) {
			var category = $(this).metadata().category;
			// load category panel only if empty :)
			if($("#clientcurric-" + category).html() == '') {
				ClientCurric.getCategoryPanel(category, userid);
			}
		});
		
		if(document.getElementById('addclientcategoryform')) {
			// check if the form exists. May be hidden if not logged-in as a sales user
			
			jQuery.each($(".categorylistitem"), function(i, val) {
				var thiscat = $(this).metadata().category;
				$("#addclientcategoryform select option[value='" + thiscat + "']").remove();
			});
			
			if($("#addclientcategoryform select option").length == 0) {
				$("#addclientcategoryform").hide();
			}
			
			$("#addclientcategoryform").ajaxForm({
				dataType : 'json',
				beforeSubmit : function(oform, odata, opt) {
					GabeUtil.screenDimLoading(true);
				},
				success : function(response) {
					GabeUtil.screenDimLoading(false);
					alert(response.message);
					if(response.status == 'OK') {
						ClientCurric.loadClientCurricPanel(userid, parent, closebutton);
					}
				}
			});
		}
		
	}
	
	GabeUtil.ajaxRequest('get', url, curricopt);
}