
//==================================================== HELPER FUNCTIONS

var validSectionNames = "info blog code lab";
var defaultSectionName = "info";
/**
 * Take a page section and subsection and determine whether user can comment.
 * A page must have its pageId noted down in <div id="mainContent" pageId="<?php echo($blogPost->id); ?>" >
 */
function pageCanAddComments() {
	//alert("CAN ADD COMMENTS? " + currentSubSection + "  " + currentSection);
	if (currentSection == "blog" && currentSubSection.indexOf("show") >= 0) {
		return true;
	} else if (currentSection == "code" || currentSection == "lab" ) {
		//-- only can comment if there is a subsection and that subsection is not e.g. /matlab
		if (currentSubSection.length > 0 && currentSubSection.lastIndexOf("/") != 0) {
			return true;
		}
	}
	return false;
}


/**
 * Create effects for all links in a parentNode
 */
function setUpLinkEffects(parentNode, mouseOverFunction, mouseOutFunction) {
	$(parentNode).hover(null, null);
	$(parentNode).hover(mouseOverFunction,mouseOutFunction );
}

/**
 * Return a section name (e.g. blog, info) from a full URL (e.g. http://lenkaspace.net/info/aboutMe
 */
function findSectionName(url) {
	//-- make URL standalone:
	url = url.toString();
	//var url2 = url + "";
	//-- first, get rid of unneccessary index.php
	url = url.replace("index.php/","");
	url.replace("index.php","");
	
	//-- remove baseUrl from url:
	url = url.substring(BASE_URL.length,url.length);
	//-- remove sub pages from url:
	var whereNextPart = url.indexOf("/");
	if (whereNextPart > 0) {
		url = url.substring(0,whereNextPart);
	}
	
	//-- make sure url is one of the defined, sections, otherwise it's error (a predefined section), return it
	if (url.length > 0 && validSectionNames.indexOf(url) >= 0) {
		return url;
	} else {
		return defaultSectionName;
	}
}


/**
 * Return a subsection name (e.g. show/4) from a full URL (e.g. http://lenkaspace.net/blog/show/4
 */
function findSubSectionName(url) {
	//-- make URL standalone:
	url = url.toString();
	//var url2 = url + "";
	//-- first, get rid of unneccessary index.php
	url = url.replace("index.php/","");
	url.replace("index.php","");
	
	//-- remove baseUrl from url:
	url = url.substring(BASE_URL.length,url.length);
	//-- remove section from url:
	var whereNextPart = url.indexOf("/");
	if (whereNextPart > 0) {
		url = url.substring(whereNextPart,url.length);
	} else {
		url = "";
	}
	//-- remove hash location
	var whereHash = url.indexOf("#");
	if (whereHash > 0) {
		url = url.substring(0,whereHash);
	}
	return url;
}

/**
 * Return a subsection name (e.g. show/4) from a full URL (e.g. http://lenkaspace.net/blog/show/4
 */
function findSubSectionMainName(subSectionName_) {
	//-- make standalone and remove first /:
	var subSectionName = subSectionName_.substring(1,subSectionName_.length);
	
	var whereNextPart = subSectionName.indexOf("/");
	if (whereNextPart > 0) {
		return subSectionName.substring(0,whereNextPart);
	} else {
		return subSectionName;
	}
}

/**
 * Return URL without hash based on hash value.
 * Return window.location if no hash present
 */
function convertWindowHashedPathToNormal() {
	var hashPath = window.location.hash + "";
	if (hashPath.length > 0) {
		hashPath = hashPath.substr(1,hashPath.length);
		return(BASE_URL + hashPath);
	} else {
		return window.location;
	}
}

/**
 * Print N tabs into a html file
 */
function printCodeTabs(numTabs) {
	for (var i=0;i<numTabs;i++) {
		document.write("&nbsp;&nbsp;&nbsp;&nbsp;");
	}
}

