var $jQ = jQuery.noConflict();

function printthispage(){
	// load the CSS
	var fileref=document.createElement("link");
	fileref.setAttribute("rel", "stylesheet");
	fileref.setAttribute("type", "text/css");
	fileref.setAttribute("href", "http://dev.cepf.net/Style Library/print.css");
	document.getElementsByTagName("head")[0].appendChild(fileref);
	
	// remove some of the div's
	jQuery('#divbodyright').hide();
	jQuery('#hairLineTop27').hide();
	
	// print the page
	window.print();
	return true;
};


function isEmpty($input) {
	$input = $jQ.trim($input);
	if ($input == null || $input == undefined || $input == '' || $input == 0) 
		{ return true; } else { return false;}
};


// read cookies	
function readCookie(name) {
  var prefix = name + "="
  var cookieStartIndex = document.cookie.indexOf(prefix)
  if (cookieStartIndex == -1)
     return null
  var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length)
  if (cookieEndIndex == -1)
     cookieEndIndex = document.cookie.length
  return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex))
};

// remove all  the div where the text is empty
function emptyMe($checkIfEmpty,$deleteIfEmpty){			
	if (isEmpty($jQ($checkIfEmpty).text())) { 
		$jQ($deleteIfEmpty).remove() 
	};
};

// loop all images to see if has a text caption
function addCaptionsToImages(){
	// if the alt attr is not available in the image tag, it will not run the script
	// see http://docs.jquery.com/Selectors/attributeHas#attribute
	$jQ('#divBody').find('img[longdesc]').each(function() {
		var $caption = $jQ(this).attr("longdesc");
		var $width = $jQ(this).attr("width")+"px";
		var $height= $jQ(this).attr("height")+"px";
		var $align = $jQ.trim($jQ(this).attr("align"));
		
		$caption = $caption.replace(/--/gi, "<br />");
		
		// in case the attr align is not present set the default
		if (isEmpty($align)) { $align='left' };
		
		// ie7 hack : for some reason the attr align break the div's
		$jQ(this).removeAttr("align")
		
		// create a div around the image and removed --width:"+ $width +";heigh:"+ $height +"; 
		$jQ(this).wrap("<div class='photoWrapper' style='float:"+ $align +";clear:"+ $align +";'></div>");
		
		// create a div after the image to add the caption text and removed --width:"+ $width +";
		$jQ(this).after("<div class='photoWrapperCaption' style='padding:6px;' >"+ $caption +"</div>");
	});	
};

function CIreplaceTitle(divToBeReplaced, div1, div2){
	// replace the divToBeReplaced, with div1, if div1 is empty replace with div2
	// notation '#div1'
	if (isEmpty(jQuery(div1).text())){
		jQuery(divToBeReplaced).text(jQuery(div2).text()); 
	} else {
		jQuery(divToBeReplaced).text(jQuery(div1).text());	
	};
};
