// ==========================================================================
// Creates a method similar to insertBefore, but instead it inserts after.
// ==========================================================================

function insertAfter(newElement,targetElement) {
	var parent = targetElement.parentNode
	if (parent.lastChild == targetElement) {
		parent.appendChild(newElement)
	} else {
		parent.insertBefore(newElement, targetElement.nextSibling)
	}
}


// ==========================================================================
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
// ==========================================================================

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


// ==========================================================================
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
// ==========================================================================

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}


// ==========================================================================
// Adds events to be executed after the document is loaded.
// By Simon Willison (See http://simonwillison.net)
// ==========================================================================

function addLoadEvent(func) {
  var oldonload = window.onload
  if (typeof window.onload != 'function') {
    window.onload = func
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload()
      }
      func()
    }
  }
}


// ==========================================================================
// Sets a negative left margin to center the Rep Photo container on the setup page.
// ==========================================================================

function centerRepPhoto() {
	var containers = $$('#content div.profile-image')
	containers.each(function(container) {
		var containerWidth = container.clientWidth
		var margin = "-" + containerWidth/2 + "px"
		container.setStyle({marginLeft: margin})
	})
}

function loadCenterRepPhoto() {
	document.body.onload += centerRepPhoto();
}


// ==========================================================================
// Vertically centers the Rep photo on his/her page.
// ==========================================================================

function verticalCenterRepPhoto() {
	var images = $$('#content div.rep-header p.photo img')
	images.each(function(image) {
		var imageHeight = image.clientHeight
		var margin = "-" + imageHeight/2 + "px"
		image.setStyle({marginTop: margin})
	})
}

function loadVerticalCenterRepPhoto() {
	document.body.onload += verticalCenterRepPhoto();
}


// ==========================================================================
// Adds the login page to the user's bookmarks.
// See http://labnol.blogspot.com/2006/01/add-to-favorites-ie-bookmark-firefox.html
// ==========================================================================

function bookmarkLogin() {

	var title = "DAH Personal Web Pages : Login"; 
	var url = "http://mypage.demarleathome.com/login";

	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title); }
	else if(window.opera && window.print) { // Opera Hotlist
		return true; }
	else alert("We're sorry, your browser doesn't support this function.")
 }

// ==========================================================================
// Show preview of current page
// ==========================================================================

function savePage(form_id, path) {
  form = $(form_id);
  form.action = path;
  form.target = '_self';
  form.getElements().each(function(formField) {
    if (formField.name == '_method') {
      formField.value = 'put'
    }
  }); 
}

// ==========================================================================
// Save page form, required after preview
// ==========================================================================

function showPreview(form_id, previewPath) {
  form = $(form_id);
  form.action = previewPath;
  form.target = 'preview';
  form.getElements().each(function(formField) {
    if (formField.name == '_method') {
      formField.value = 'post'
    }
  });  
  form.submit();  
}

// ==========================================================================
// Cookie functions
// ==========================================================================

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

// ==========================================================================
// Print Rep Co-ordinates
// ==========================================================================

function doesRepExist() {
  var repInfo = readCookie('rep_info');
  return repInfo != undefined && !repInfo.blank();
}

function repData() {
  return unescape(readCookie('rep_info')).replace(/\+/g, ' ').evalJSON();
}

function printRepInfo() {
    if (doesRepExist()) {
      var output = new EJS({url: '/javascripts/views/representatives/header.ejs'}).render(repData());
      document.write(output);
    }
}

function printContactInfo() {  
  if (doesRepExist()) {
    var output = new EJS({url: '/javascripts/views/representatives/contact.ejs'}).render(repData());
    document.write(output);
  } else {
    var output = new EJS({url: '/javascripts/views/representatives/contact_default.ejs'}).render({});
    document.write(output);  
  }
}

function printContactLink() {  
  if (doesRepExist()) {
    var output = new EJS({url: '/javascripts/views/representatives/contact_link.ejs'}).render(repData());
    document.write(output);
  } else {
    var output = new EJS({url: '/javascripts/views/representatives/contact_link_default.ejs'}).render({});
    document.write(output);  
  }
}