/* from our buddy, Dean Edwards via John Resig */

// written by Dean Edwards, 2005
// with input from Tino Zijdel, Matthias Miller, Diego Perini

// http://dean.edwards.name/weblog/2005/10/add-event/

function addEvent(element, type, handler) {
	if (element.addEventListener) {
		element.addEventListener(type, handler, false);
	} else {
		// assign each event handler a unique ID
		if (!handler.$$guid) handler.$$guid = addEvent.guid++;
		// create a hash table of event types for the element
		if (!element.events) element.events = {};
		// create a hash table of event handlers for each element/event pair
		var handlers = element.events[type];
		if (!handlers) {
			handlers = element.events[type] = {};
			// store the existing event handler (if there is one)
			if (element["on" + type]) {
				handlers[0] = element["on" + type];
			}
		}
		// store the event handler in the hash table
		handlers[handler.$$guid] = handler;
		// assign a global event handler to do all the work
		element["on" + type] = handleEvent;
	}
};
// a counter used to create unique IDs
addEvent.guid = 1;

function removeEvent(element, type, handler) {
	if (element.removeEventListener) {
		element.removeEventListener(type, handler, false);
	} else {
		// delete the event handler from the hash table
		if (element.events && element.events[type]) {
			delete element.events[type][handler.$$guid];
		}
	}
};

function handleEvent(event) {
	var returnValue = true;
	// grab the event object (IE uses a global event object)
	event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
	// get a reference to the hash table of event handlers
	var handlers = this.events[event.type];
	// execute each event handler
	for (var i in handlers) {
		this.$$handleEvent = handlers[i];
		if (this.$$handleEvent(event) === false) {
			returnValue = false;
		}
	}
	return returnValue;
};

function fixEvent(event) {
	// add W3C standard event methods
	event.preventDefault = fixEvent.preventDefault;
	event.stopPropagation = fixEvent.stopPropagation;
	return event;
};
fixEvent.preventDefault = function() {
	this.returnValue = false;
};
fixEvent.stopPropagation = function() {
	this.cancelBubble = true;
};



addEvent(window,"load",init);

var close_menu_timer;  //used to hold the timer to delay close_menu
var close_menu = false; //used to decide whether the document.click should close menus or not

function init(){
	var unframed = (top == self);	
	if (!unframed) top.location.href = '/';

	var rows = $('tbody.highlight tr');
	for (var i=0; i<rows.length; i++) {
	    rows[i].onmouseover = function() { $(this).addClass('highlight');}
	    rows[i].onmouseout = function() { $(this).removeClass('highlight');}
	}
	
}
function spiffyBox(element){
	//thanks to spiffycorners
	var parentDiv = document.createElement("div");
	$(parentDiv).addClassName('round_box');
	$(element).up().appendChild(parentDiv);
	$(parentDiv).appendChild(element);
	new Insertion.Before(element,'<b class="round"><b class="round1"><b></b></b><b class="round2"><b></b></b><b class="round3"></b><b class="round4"></b><b class="round5"></b></b>');
	new Insertion.After(element, '<b class="round"><b class="round5"></b><b class="round4"></b><b class="round3"></b><b class="round2"><b></b></b><b class="round1"><b></b></b></b>');
}
function spiffyLayout(element){
	//thanks to spiffycorners
	var parentDiv = document.createElement("div");
	$(parentDiv).addClassName('round_layout_box');
	$(element).up().appendChild(parentDiv);
	$(parentDiv).appendChild(element);
	new Insertion.Before(element,'<b class="round_layout"><b class="round_layout1"><b></b></b><b class="round_layout2"><b></b></b><b class="round_layout3"></b><b class="round_layout4"></b><b class="round_layout5"></b></b>');
	new Insertion.After(element, '<b class="round_layout"><b class="round_layout5"></b><b class="round_layout4"></b><b class="round_layout3"></b><b class="round_layout2"><b></b></b><b class="round_layout1"><b></b></b></b>');
}
function toggleProfileList(element){
	//if visible, hide
	if($(element).visible()){
		Effect.toggle(element,'blind',{duration:0.1, afterFinish: checkVisibility});
		($(element).previous()).removeClassName('pressed');		
	}else{
		Effect.toggle(element,'blind',{duration:0.1, afterFinish: checkVisibility});		
		($(element).previous()).addClassName('pressed');
		window.clearTimeout(close_menu_timer);
		close_menu=false; //set this to false so we don't trigger hideAllLists before the animation completes
	}
}
function checkVisibility(obj){
	if($(obj.element).visible()){
		close_menu_timer = window.setTimeout("close_menu=true",300); //after a short time, set close_menu to be true, then a click on the document will close the menus
	}	
}
function hideAllPopups(){
	document.getElementsByClassName('popup').each(function(z){if(z.visible()){togglePopup(z);}});
}
function closeAllLists(){
	document.getElementsByClassName('profile_list_links').each(function(z){if(z.visible()){toggleProfileList(z);}});
}

function updateInviteCount() {
   var url = "";
   var pars = "action=invite-count";
   var myAjax = new Ajax.Request( url, 
                    { method: 'get',
                      parameters: pars, 
                      onComplete: function(res) {
                          if(res.responseText.length) {
                             $('invite_count').innerHTML = res.responseText;
                           } else {
                             $('invite_count').innerHTML = '0';
                           }
                     }
                     });
}
function sendInvite(user) {
  email = $F('friend');
  var url = '';
  var pars = 'action=invite&email=' + email;
  var myAjax = new Ajax.Request( url, { method: 'get', parameters: pars, onComplete: inviteStatus });
}
function inviteStatus(ajax) {
    if(!$('invite_status').visible()) {
        Element.toggle('invite_status');
    }
    if(ajax.responseText == "true") {
        $('invite_status').style.color = 'green';
        $('invite_status').innerHTML = "Invitation sent.";
    } else if(ajax.responseText == "invalid") {
        $('invite_status').style.color = 'red';
        $('invite_status').innerHTML = "Invalid email address.";
    } else {
        $('invite_status').style.color = 'red';
        $('invite_status').innerHTML = "Failed to send invitation.";
    }
    window.setTimeout("new Effect.Fade('invite_status')",5500);
    updateInviteCount();
}


function togglePopup(element){
	el = $(element);
	if(el.visible()){
		el.toggle()
	}	
}
document.onclick = function(e) {
		if(close_menu == true){
			close_menu = false;
			closeAllLists()			
		}
}
//document.onkeypress = function(e){if(e.keyCode == 27){hideAllPopups(); return false;}}
