var helperTitle; //need to create this variable initially so the various functions can use it
var helperPosOffsetX = 15;  //set the horizontal offset of the title helper popup
var helperPosOffsetY = 0; //set the vertical offset of the title helper popup

Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
Prototype.Browser.IE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 7;
Prototype.Browser.IE8 = Prototype.Browser.IE && !Prototype.Browser.IE6 && !Prototype.Browser.IE7;


Event.observe(window, 'load', function() {
									   
	taggaFileStyle();
	
	//taggaSelectStyle();
	
	$$('.success-message', '.error-message', '.warning-message').each(function(elm) {
										 
		var elmt = elm.firstDescendant();
										 
		if(!elmt) {
			elm.insert('<a class="error-close"></a>');
		}
									
	});
	
	$$('a.error-close').invoke('observe', 'click', closeError);
	
	
	$$('.helper').invoke('observe', 'mouseover', titleHelperShow);
	$$('.helper').invoke('observe', 'mouseout', titleHelperHide);
	
	
});

function closeError() {
	
	$(this).up(0).fade({duration: 0.2});
	
}

function titleHelperShow() {
	
	Event.observe(document, 'mousemove', setHelperPos);
	$$('.helper').invoke('observe', 'mouseout', titleHelperHide);
	
	helperTitle = $(this).readAttribute('title'); //grab the title of the element we're hovering over and store it in a variable
	
	$(this).writeAttribute('title', ''); //remove the title of the element we're hovering over to stop the browser popup from occuring
	
	if ($('helperWindow') == null) { //check to see if the helper popup has already been created, if not, create it
	
		var div = new Element('div', {'id': 'helperWindow'});
		$(document.body).insert(div);
		$(div).update(helperTitle).addClassName('helper-box').setStyle({display: 'none'});
		
	} else {  //if the helper popup already exists, then we just need to update the innerHTML with the title variable
		
		$('helperWindow').update(helperTitle);
		
	}

	$('helperWindow').show();
}

function titleHelperHide() {
	
	if ($('helperWindow')) {
		$('helperWindow').remove();	
	}
	
	$(this).writeAttribute('title', helperTitle);  //put the title that we stored on hover back into the element
	
}

function setHelperPos(event) {
	
		var mouseX = (Event.pointerX(event));	//grab the cursors current X position and store it
		var mouseY = (Event.pointerY(event));
		
		if ($('helperWindow') != null) {
			//alert('Viewport: '+(document.viewport.getWidth())+', '+'MouseX: '+(mouseX)+', '+'Calculation:'+(document.viewport.getWidth()-mouseX));
			if (((document.viewport.getHeight()-mouseY) < 200) || ((document.viewport.getWidth()-mouseX) < 200)) {
				$('helperWindow').setStyle({
					right: document.viewport.getWidth()-mouseX-helperPosOffsetX+'px',
					bottom: document.viewport.getHeight()-mouseY-helperPosOffsetY+'px'
				});
			} else {
				$('helperWindow').setStyle({
					left: mouseX+helperPosOffsetX+'px',
					top: mouseY+helperPosOffsetY+'px'
				});
			}
		}
		
}

function taggaAlert(type,confirmMsg,execFunc,pos,neg) {
	//create the alert markup
	var masterDiv = new Element('div', {'id':'alertWarning', 'style':'display: none;'});
	var alertWrapper = new Element('div', { 'class': 'alert'});
	var alertContainer = new Element('div', {'class':'alert-warning'});
	var alertMsg = new Element('p');
	var alertInputSpan = new Element('span', {'id':'alertInputs'});
	var alertInput = new Element('input',{'value':'Ok','type':'button','id':'alertTrue'});
	var confirmInputSpan = new Element('span', {'id':'confirmInputs', 'style':'display: none;'});
	var confirmtInput1 = new Element('input',{'value':'Ok','type':'button','id':'confirmTrue'});
	var confirmtInput2 = new Element('input',{'value':'Cancel','type':'button','id':'confirmFalse'});
	var alertBlocker = new Element('div',{'id':'alertBlocker', 'style':'display:none;'});
	
	var alertDiv = masterDiv;
	var alertBlock = alertBlocker;
	
	$$('body').min().insert(masterDiv.insert(alertWrapper.insert(alertContainer.insert(alertMsg).insert(alertInputSpan.insert(alertInput)).insert(confirmInputSpan.insert(confirmtInput1).insert(confirmtInput2))))).insert(alertBlocker);
	
	
	
	//type needs to be a string, either 'confirm' or 'alert'
	//confirmMsg needs to be a string
	//execFunc needs to be a function stored in a variable
	//pos & neg need to be strings
	
	var msgPara = $('alertWarning').down('p');
	msgPara.update(confirmMsg);
	$('alertBlocker').show();
	
	if (type == 'alert') {
		
		$('alertInputs').show();
		$('confirmInputs').hide();
		
		Effect.SlideDown('alertWarning', { duration: 0.3 });
		
		$('alertTrue').observe('click', function(){
			Effect.SlideUp('alertWarning', { duration: 0.3 });
			//$('alertWarning').remove().delay(0.5);
			//$('alertBlocker').remove().delay(0.5);
			Element.remove.delay(0.5, 'alertWarning');
			Element.remove.delay(0.5, 'alertBlocker');
		});
	
	} else if (type == 'confirm') {
		if (pos) {
			$('confirmTrue').writeAttribute('value', pos);
		}
		
		if (neg) {
			$('confirmFalse').writeAttribute('value', neg);
		}
		
		$('alertInputs').hide();
		$('confirmInputs').show();
		
		Effect.SlideDown('alertWarning', { duration: 0.3 });
		
		$('confirmFalse', 'confirmTrue').invoke('observe','click', function(){
			var optionID = this.id;
			if (optionID == 'confirmTrue') {
				execFunc();
			}
			Effect.SlideUp('alertWarning', { duration: 0.3 });
			//$('alertWarning').remove();
			//$('alertBlocker').remove();
			
			Element.remove.delay(0.5, 'alertWarning');
			Element.remove.delay(0.5, 'alertBlocker');
		});
		
	}
	
	
}

function taggaFileStyle() {
	$$('input[type="file"]').each(function(s) {
		
		if (!Prototype.Browser.IE8 && !Prototype.Browser.IE7) {
			var taggaFileWrapper = new Element('span', { 'class': 'taggaFile'});
			var taggaFileButton = new Element('input', {'type':'button', 'class':'taggaFileButton', 'value':'Browse...'});
			var taggaFileInput = new Element('input', {'type':'text', 'class':'taggaFileInput custom initState', 'value':'5MB max file size...'});
			s.addClassName('taggaFileHide custom').wrap('span', { 'class': 'taggaCustomFile' }).insert(taggaFileWrapper);
			//s.wrap('span', { 'class': 'taggaCustomFile' }).insert(taggaFileWrapper);
			taggaFileWrapper.insert(taggaFileButton).insert(taggaFileInput);
		
			s.observe('change', function() {
				taggaFileInput.value = this.value;
				taggaFileInput.removeClassName('initState');
			});
		} else if (Prototype.Browser.IE8) {
			s.removeClassName('taggaFileHide');
			s.addClassName('taggaFileIE8 custom');
		} else if  (Prototype.Browser.IE7) {
			s.removeClassName('taggaFileHide');
			s.addClassName('taggaFileIE7 custom');
		}
		
	});
}

function taggaSelectStyle() {
	$$('select').each(function(s,i) {
		// Grab and set the proper width of the fake select.  We could do more dynamic sizing/styling, but this suits our purposes for now
		var selectWidth = s.getWidth();
		var selectRuseWidth = selectWidth-8;
		
		// Create the markup needed for the container and the fake select element, and put it in the DOM
		var selectHolder = new Element('div', {'class':'select-holder'});
		var selectRuse = new Element('div', {'class':'select-ruse'});
		s.wrap(selectHolder.insert(selectRuse));
		
		// Set the value of the fake select element, as well as its width
		selectRuse.innerHTML = $F(s);
		selectRuse.setStyle({width:selectRuseWidth+'px'});
		
		// Set the event observations on the select to manipulate the fake select element
		s.observe('mouseover', function() { selectRuse.addClassName('hover'); });
		s.observe('mouseout', function() { selectRuse.removeClassName('hover'); });
		s.observe('click', function() { selectRuse.innerHTML = $F(s); });
		
	});
}