// Set up cross-browser event handling (IE5+, NS6+ and Mozilla/Gecko)
// Useage: addEvent(window, 'load', addListeners, false);
function addEvent(elm, evType, fn, useCapture)
{
	if (elm.addEventListener) {
		elm.addEventListener(evType, fn, useCapture);
		return true;
	} else if (elm.attachEvent) {
		var r = elm.attachEvent('on' + evType, fn);
		return r;
	} else {
		elm['on' + evType] = fn;
		return true;
	}
}

// Add url to bookmarks
function addBookmark(title, url)
{
	//var url = top.document.location; // current page
	if (document.all) {
		window.external.AddFavorite(url, title);
	/*} else if (window.sidebar) { // Opens in sidebar
		window.sidebar.addPanel(title, url, '');*/
	} else {
		alert("Your browser does not support automatic site bookmarking.\n"
			+ "Please bookmark Savvy Mommy manually:\n\n"
			+ "Click OK and then press Ctrl + D");
	}			  
}

function addToFavorites(title, url)
{
	// set defaults if no arguments passed
	var title = title ? title : document.titles;
	var url = url ? url : location.href;
	var site_name = 'Savvy Mommy';
	var allowFF = false;
	
	// IE Compatible
	if (document.all && window.external) { 
		window.external.AddFavorite(url, title) 
	// Firefox side panel
	} else if (allowFF && window.sidebar) { 
		window.sidebar.addPanel(title, url, '');
	// Bookmarking not supported
	} else { 
		alert("Your browser does not support automatic site bookmarking.\n"
			+ "Please bookmark " + site_name + " manually:\n\n"
			+ "Click OK and then press Ctrl + D"); 
	}
}

// ||||||||||||||||||||||||||||||||||||||||||||||||||
// Open external links in a new window
function handleExternalLinks()
{
	var anchors = document.getElementsByTagName("a");
	var i, href;
	for (i = 0; i < anchors.length; i++) {
		if (!anchors[i].href) continue;
		href = anchors[i].href;
		if (href.indexOf("savvymommy") == -1 &&	href.indexOf("savvy.local") == -1 && href.indexOf("veepveep") == -1) {
			if (href.indexOf("javascript:") == -1 ){ // Href is not a javascript call
				if (!anchors[i].onclick){ // Href does not have an onclick event
					if (href.indexOf("mailto:") == -1) { // Href is not a mailto:
						if (href.indexOf("http://") != -1) { // Href is not relative (for Safari)
							anchors[i].setAttribute("target", "_blank");
						}
					}
				}
			}
		}
	}
}

/**
* Photo Corners
* Add photo corners to all images
* @author Brad Benninger of Studio814
* @link http://www.studio814.com
* @updated February, 2006
* @note IE6 1px alignment bug on right and bottom sides of images with odd widths and heights
* @note DOES NOT support Internet Explorer (because of PNG alpha)
* @note image will be floated by the following script
* @usage <img src="patemm.jpg" width="103" height="65" class="cornered" />
*/

// Configuration
// -------------------------------------------------------------------------------------------------------
var defaultFloat = 'left';		// sets the default side to float the image to
var pngAlpha = true;			// PNG corner has an alpha channel? (disables script for IE if true)

function photoCorners()
{
	if (!document.getElementById) return; 
	if (!document.getElementsByTagName) return;
	
	var imgs = document.getElementsByTagName('img');
	
	for (var i = 0; i < imgs.length; i++) {
		if (imgs[i].className && (' ' + imgs[i].className + ' ').indexOf(' cornered ') != -1) {
			// the wrapper element holds all of the corners in place
			var wrapper = document.createElement('div');
			var original = imgs[i];
			
			wrapper.className = 'wrapper';

			// copy margin and float from image to wrapper
			if (original.currentStyle) {
				// for IE Browsers (see link below for more info)
				// http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/currentstyle.asp
				if (!pngAlpha) {
					if (original.currentStyle.styleFloat == 'none') {
						wrapper.style.styleFloat = defaultFloat;
					} else {
						wrapper.style.styleFloat = original.currentStyle.styleFloat;
					}
					wrapper.style.margin = original.currentStyle.margin;
					original.style.styleFloat = 'none';
				} else {
					// no alpha channels for IE
					return false;
				}
			} else {
				// standards compliant browsers
				// wrapper MUST be floated
				// todo: check for align attribut if float doesn't exist
				if (getComputedStyle(original, '').getPropertyValue('float') == 'none') {
					wrapper.style.cssFloat = defaultFloat;				
				} else {
					wrapper.style.cssFloat = getComputedStyle(original, '').getPropertyValue('float');
				}
				wrapper.style.marginTop = getComputedStyle(original, '').getPropertyValue('margin-top');
				wrapper.style.marginRight = getComputedStyle(original, '').getPropertyValue('margin-right');
				wrapper.style.marginBottom = getComputedStyle(original, '').getPropertyValue('margin-bottom');
				wrapper.style.marginLeft = getComputedStyle(original, '').getPropertyValue('margin-left');
				original.style.cssFloat = 'none';
			}
			
			// remove margin from image
			original.style.margin = 0;
			
			original.parentNode.replaceChild(wrapper, original);
			
			// fabricate our four corner DIVs
			// attach the appropriate classes to each
			var tl = document.createElement('div');
			var br = document.createElement('div');
			
			// assign class names to divs
			tl.className = 'tl';
			br.className = 'br';
			
			// Actually add all of the tags to the document
			wrapper.appendChild(tl); 
			wrapper.appendChild(br); 
			wrapper.appendChild(original);
		}
	}
}

// remove IE image background style flicker
// http://evil.che.lu/2006/9/25/no-more-ie6-background-flicker
try { document.execCommand('BackgroundImageCache', false, true); } catch(e) {}

// |||||||||||||||||||||||||||||||||||||||||||
// Inititalize listeners and functions
addEvent(window, 'load', photoCorners, false);
addEvent(window, 'load', handleExternalLinks, false);
