/* Put this somewhere better.
 */
function getElementsByClassName(strClass, strTag, objContElm) {
  strTag = strTag || "*";
  objContElm = objContElm || document;    
  var objColl = objContElm.getElementsByTagName(strTag);
  if (!objColl.length &&  strTag == "*" &&  objContElm.all) objColl = objContElm.all;
  var arr = new Array();                              
  var delim = strClass.indexOf('|') != -1  ? '|' : ' ';   
  var arrClass = strClass.split(delim);    
  for (var i = 0, j = objColl.length; i < j; i++) {                         
    var arrObjClass = objColl[i].className.split(' ');   
    if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
    var c = 0;
    comparisonLoop:
    for (var k = 0, l = arrObjClass.length; k < l; k++) {
      for (var m = 0, n = arrClass.length; m < n; m++) {
        if (arrClass[m] == arrObjClass[k]) c++;
        if ((delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) {
          arr.push(objColl[i]); 
          break comparisonLoop;
        }
      }
    }
  }
  return arr; 
}

// To cover IE 5 Mac lack of the push method
Array.prototype.push = function(value) {this[this.length] = value; };

window.onload = function() {
	draggables = getElementsByClassName("draggable");
	numDrag = draggables.length;
	
	for (i=0; i < numDrag; i++) {
		drag = new Drag.Move(draggables[i].id, {'container': document.getElementById("fridge")});
		
		/* If it's a link, we need to add some checking to make sure that
		 * the link fires only if clicked-- not if dragged.
		 */
		if (draggables[i].href) {
			draggables[i].deltaX = 0;
			draggables[i].deltaY = 0;
			drag.targetID = draggables[i].id;
			drag.addEvent('onStart', function() {
				target = document.getElementById(this.targetID);
				target.startX = parseInt(target.style.left);
				target.startY = parseInt(target.style.top);
			});
			drag.addEvent('onComplete', function() {
				target = document.getElementById(this.targetID);
				target.deltaX = parseInt(target.style.left) - target.startX;
				target.deltaY = parseInt(target.style.top) - target.startY;
			});
			draggables[i].onclick = function() {
				if (this.deltaX == 0 && this.deltaY == 0)
					return true;
				else
					return false;
			}
		}
	}
	
	headlines = getElementsByClassName("headline");
	numHeadlines = headlines.length;
	
	for (i=0; i < numHeadlines; i++) {
		headlines[i].onclick = function() {
			id = this.id.slice(9);
			if (page = document.getElementById("page_" + id)) {
				note = document.getElementById("note_" + id);
				
				deltaLeft = 60;
				deltaTop = 130;
				
				if (note.className.indexOf("white") > 0 || note.className.indexOf("pink") > 0)
					deltaTop += 25;
				
				leftValue = parseInt(note.style.left) - deltaLeft;
				topValue = parseInt(note.style.top) - deltaTop;
				/* Check to see if the note will go offscreen.  If so, move it over
				 * so that we don't create horizontal scrollbars, which mess up the
				 * fridge background.  And look ugly.
				 */
				if (leftValue > 400)
					leftValue -= 100;
				
				if (topValue + page.clientHeight > 960)
					topValue = 960 - page.clientHeight;
				
				page.style.left = leftValue + "px";
				page.style.top = topValue + "px";
				
				page.style.display = "block";
				
				return false;
			}
			return true;
		}
	}
	
	closers = getElementsByClassName("page_close");
	numClosers = closers.length;
	
	for (i=0; i < numClosers; i++) {
		closers[i].onclick = function() {
			document.getElementById("page_" + this.id.slice(6)).style.display = "none";
			return false;
		}
	}
	
	emails = getElementsByClassName("email");
	numEmails = emails.length;
	
	for (i=0; i < numEmails; i++) {
		emails[i].onclick = function() {
			if (this.deltaX == 0 && this.deltaY == 0) {
				theForm = document.getElementById("email_form_" + this.id.slice(6));
				theForm.style.left = this.style.left;
				theForm.style.top = parseInt(this.style.top) + 25 + "px";
				if (theForm.style.display == "block")
					theForm.style.display = "none";
				else
					theForm.style.display = "block";
				
			}
			return false;
		}
		document.getElementById("close_email_form_" + emails[i].id.slice(6)).onclick = function() {
			document.getElementById("email_form_" + this.id.slice(17)).style.display = "none";
			return false;
		}
	}
	
	embeds = getElementsByClassName("embed");
	numEmbeds = embeds.length;
	
	for (i=0; i < numEmbeds; i++) {
		embeds[i].onclick = function() {
			if (this.deltaX == 0 && this.deltaY == 0) {
				id = this.id.slice(6);
				
				/* The first time this pops up, we adjust the height style attribute
				 * in the iframe tag to match the page's height.
				 */
				height = document.getElementById("page_" + id).clientHeight;

				if (height && height > 0)
					document.getElementById("embed_code_" + id).value
						= document.getElementById("embed_code_" + id).value.toString().replace(/375px/, 20 + height + "px"); 

				theForm = document.getElementById("embed_form_" + id);
				theForm.style.left = this.style.left;
				theForm.style.top = parseInt(this.style.top) + 25 + "px";
				if (theForm.style.display == "block")
					theForm.style.display = "none";
				else
					theForm.style.display = "block";
				
			}
			return false;
		}
		document.getElementById("close_embed_form_" + embeds[i].id.slice(6)).onclick = function() {
			document.getElementById("embed_form_" + this.id.slice(17)).style.display = "none";
			return false;
		}
	}
}
