var domready = false;
var menuSlide;
window.addEvent('domready', function() {
    domready = true;
    
    if($('menuMaxContainer')) {
        menuSlide = new Fx.Slide($('menuMaxContainer'));
        menuSlide.hide();
        
        $('btnMenu').addEvent('click', function() {
            menuSlide.toggle();
        });
    }
    
    $$('.managerTable').each(function(item, idx, arr) {
        prep_table(item);
    });
    
    $$('input, textarea').each(function(item, idx, arr) {
        item.addEvent('focus', function() { this.addClass('focus'); });
        item.addEvent('blur', function() { this.removeClass('focus'); });
    });
});


tinyMCE.init({
    height: '400',
	mode : "specific_textareas",
	editor_selector : "richText",
	theme : "advanced",
	plugins : "safari,spellchecker,style,table,advlink,inlinepopups,paste,visualchars,nonbreaking,xhtmlxtras,template",
/* Unused Plugins:
pagebreak,layer,save,advhr,advimage,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,directionality,fullscreen,noneditable,imagemanager,filemanager
*/
	// Theme options
	theme_advanced_buttons1 : "bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
	theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
	theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
	theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage",
	theme_advanced_toolbar_location : "top",
	theme_advanced_toolbar_align : "left",
	theme_advanced_statusbar_location : "bottom",
	/*theme_advanced_resizing : true,*/

	// Example content CSS (should be your site CSS)
	content_css : "/css/flagship.css",

	// Drop lists for link/image/media/template dialogs
	template_external_list_url : "js/template_list.js",
	external_link_list_url : "js/link_list.js",
	external_image_list_url : "js/image_list.js",
	media_external_list_url : "js/media_list.js",

	// Replace values for the template plugin
	template_replace_values : {
		username : "Some User",
		staffid : "991234"
	}
});


/* ============== FUNCTIONS */

function flashMessage(message) {
    if(!domready) {
        alert(message);
        return;
    }

    // <div class="flashMsg">message</div>
    var flashDiv = new Element('div', {
        'class': 'flashMsg',
        'html': message
    });
    flashDiv.inject($('flashContainer'));

    var flashTween = new Fx.Tween(flashDiv);
    
    (function() {
        flashTween.start('opacity', 0).chain(function() { flashDiv.dispose(); });
    }).delay(3500);
}

/*
 * STRIPE_TABLE
 * author: ccoffman - 3/17/08
 * updated: ccoffman - 4/24/09
 *
 * takes a table element as its argument, and loops through any tr element children.
 * if the tr has td children, it increments the counter variable.  if the counter
 * is even, it loops through td elements on this tr and adds the className to 'alt'.
 * dependent on mootools 1.2.1
 */
function stripe_table(el) {
    var trs = el.getElements('tr');

	var counter = 0;
	if(!trs.length) return;
	
	for(i = 0; i < trs.length; i++) {
		
		tds = trs[i].getElements('td');
		if(!tds.length) continue;
		counter++;
		if(counter%2) continue;
		
		for(p = 0; p < tds.length; p++) {
			tds[p].addClass('alt');
		}
	}
}

/*
 * PREP_TABLE
 * author: ccoffman - 3/17/08
 * updated: ccoffman - 4/24/09
 *
 * takes a table element as its argument. first, the table rows are alternated
 * via stripe_table(), then onmouse* events (hover_row() on and off) are added to the td elements.
 */
function prep_table(el) {
	stripe_table(el);
	
	//do hover states...
	trs = el.getElements('tr');
	if(!trs.length) {
		return;
	}
	
	for(i = 0; i < trs.length; i++) {
		tds = trs[i].getElements('td');
		if(!tds.length) {
			continue;
		}
		
		for(p = 0; p <tds.length; p++) {
			tds[p].addEvent('mouseover', function() {
				hover_row(this, true);
			});
			tds[p].addEvent('mouseout', function() {
				hover_row(this, false);
			});
		}
	}
} //prep_table()



/*
 * HOVER_ROW
 * author: ccoffman - 3/17/08
 * updated: ccoffman - 4/24/09
 *
 * takes a td element as the td_el argument, and a boolean value for the on argument.
 * finds the parent tr element, and loops through the tr's td children (if any - 'twould be quite a paradox if not).
 * if the on argument is true, the function sets these tds' oldClassName attribute to the current className, then sets the
 * className attribute to 'on' (this needs to be defined in css).
 * if the on argument is false, the tds' className attribute is set to the oldClassName attribute value.
 */
function hover_row(td_el, on) {
	tr_el = td_el.parentNode;
	while(tr_el.nodeName != 'TR') {
		tr_el = tr_el.parentNode;
	}
	
	sibs = tr_el.getElements('td');
	if(!sibs.length) {
		return;
	}
	
	for(i = 0; i < sibs.length; i++) {
		if(on) {
			sibs[i].setAttribute('oldClassName', sibs[i].className);
			sibs[i].className = 'on';
		}
		else {
			sibs[i].className = sibs[i].getAttribute('oldClassName');
		}
	}
} //hover_row()


/* newFileInput
 * author: ccoffman - 4/28/09
 *
 * hides the text input called [id]_tmp, shows the file input called [id],
 * and destroys the span called newFileLink_[id]
 */
function newFileInput(id) {
    if(!domready) return;

    $(id+'_tmp').destroy();
    $('newFileLink_'+id).destroy();
    $(id).setStyle('display','block');
}
