/**
 *
 * Miscellaneous and utility scripts. Development space before moving scripts 
 * into their own files.
 *
*/




// TODO - move this out of misc
/*******************************************************************************
* Link classes		                                                          **
*******************************************************************************/

/**
 * Add class names like 'file' or 'pdf' to links pointing to these things
*/

openc.addLinkClasses = function() {

	// Add file suffix as class to anchors
	
	var linkSelector = 'a[@href$=.pdf], a[@href$=.doc], a[@href$=.xls]';

	// All links except those in a FeaturedLink div
	jq(linkSelector).not(jq('div.FeaturedLink a')).each(function(){
		var classFromFileSuffix = jq(this).attr('href').replace(/.+\.([a-zA-Z]{3}$)/, '$1').toLowerCase();
		jq(this).addClass(classFromFileSuffix);
	});
	
	// All lists in the content and in links portlet
	jq('div#portal-column-content ul, div.links-portlet ul').each(function() {
	
	
		// Loop through each li and try to find one that contains anything 
		// other than a link selection
		
		var containsOnlyLinks = true;
		
		jq(this).find('li').each(function() {
		
			
			// Clean any Kupu whitespace between tags
			openc.removeWhitespace(this);
			
			// If the number of children is not the same as the number of file 
			// links
			if(this.childNodes.length != jq(this).find(linkSelector).length) {
				containsOnlyLinks = false;
			}
		});
		// If the list contains only links
		if(containsOnlyLinks) {
			jq(this).addClass('links-list');
		}
	
	});

}

// DOM loaded
jq(function() {

	if(openc.IS_RIA_CLIENT && !openc.IS_EDIT_PAGE && openc.IS_DESKTOP) {
		openc.addLinkClasses();
	}
});

















/**
 * Equal height columns
*/

openc.equalHeightColumns = function() {

	var columns = new Array();
	
	// Get references to column divs
	// todo - all of one class?
	var c1 = jq('div#portal-column-one');
	var c2 = jq('div#portal-column-two');
	var c3 = jq('div#portal-column-content');
	
	// Push columns that exist onto array
	if(c1[0]) {
		columns.push(c1);
	}
	if(c2[0]) {
		columns.push(c2);
	}
	if(c3[0]) {
		columns.push(c3);
	}
	
		
	// Find the deepest bottom edge of all columns
	var greatestOffsetBottom = 0;
	var offsetOptions = {padding:true};
	
	for(var i = 0 ; i < columns.length ; i++) {
	
		var offsetBottom = columns[i].offset(offsetOptions).top + columns[i].height();
		greatestOffsetBottom = Math.max(greatestOffsetBottom, offsetBottom);
	}
	openc.log("*" + greatestOffsetBottom);
	
	// Adjust all columns height to reach the deepest offset bottom
	for(var i = 0 ; i < columns.length ; i++) {
		openc.log(greatestOffsetBottom + "-" + columns[i].height() + " - " + columns[i].offset(offsetOptions).top);
		openc.log(greatestOffsetBottom - columns[i].height() - columns[i].offset(offsetOptions).top);
		columns[i].css("background","red");
	}
}
/*******************************************************************************
* Admin CSS hooks added, TODO: do this with XSLT		                      **
*******************************************************************************/

jq(function(){
	jq('a:contains("Edit this Page")').parent().addClass('admin-injected');
});











