var msgs = {
	normal: 'Click here to enter your message.',
	generic: 'Season\'s greetings from home.',
	christmas: 'Blessings to you at Christmas.',
	hanukkah: 'Best wishes for a happy Hanukkah.',
	kwanzaa: 'Celebrating the spirit of Kwanzaa.',
	newyear: 'Wishing you a safe, peaceful New Year.'
}
var baseurl = 'http://lpe.daytondailynews.com/share/bridgethegap/';

/* Layout style switcher */
function setLayout(str) {
	switch (str) {
		case 'horizontal':
			$('photo').className = 'photo_h';
			$('msg').className = 'msg_h';
			$('h_layout').checked = true;
			btg.replaceEditor({cols: 58, rows: 3, highlightcolor: '#e9e9ff'});
			break;
		case 'vertical':
			$('photo').className = 'photo_v';
			$('msg').className = 'msg_v';
			$('v_layout').checked = true;
			btg.replaceEditor({cols: 20, rows: 12, highlightcolor: '#e9e9ff'});
			break;
		default: break;
	}
}

/* Function to handle changes to photoselect */
function photoselectHandler() {
	var s = $('photoselect');
	switch (s.value) {
		case 'new':
			$('photo').innerHTML = 'Use the dropdown menu above to select a photo.';
			btg.addPhotoWin();
			break;
		case '': 
			s.selectedIndex=0;
			$('photo').innerHTML = 'Use the dropdown menu above to select a photo.';
			break;
		default:
			$('photo').innerHTML = '<img id="photoimg" src="'+s.value+'" style="display: none" />';
			setLayout(s.options[s.selectedIndex].getAttribute('layout'));
			new Effect.Appear('photoimg',{duration: .25, from: 0, to: 1})
			break;
	}
}

/* Function to handle changes to messageselect */
function messageselectHandler() {
	var s = $('messageselect');
	switch (s.value) {
		case 'custom': 
			$('msg').innerHTML = prompt('Enter your custom greeting: '); 
			$('msg').innerHTML = ($('msg').innerHTML != '') ? $('msg').innerHTML : msgs.normal;  
			new Effect.Highlight('msg',{startcolor: '#E9E9FF'}); 
			break;
		case '': 
			s.selectedIndex = 0; $('msg').innerHTML = msgs.normal; 
			new Effect.Highlight('msg',{startcolor: '#E9E9FF'}); 
			break;
		default: 
			$('msg').innerHTML = eval('msgs.'+s.value); 
			new Effect.Highlight('msg',{startcolor: '#E9E9FF'}); 
			break;
	}
}

/* Form submission */
function sendCard() {
	var data = Form.serialize('cardform');
	data = data.replace(/layout.+/,'');
	if ($('photoimg'))
		data += '&photourl='+$('photoimg').getAttribute('src');
	else {
		$('messages').innerHTML = 'You must select a photo to continue.';
		new Effect.Appear('messages',{duration: .25});
		return;
	}
	if ($('photo').className == 'photo_h')
		data += '&layout=horizontal';
	else
		data += '&layout=vertical';
	data += '&message='+$('msg').innerHTML;
	var ajax = new Ajax.Request(baseurl+'ajax/send',{
		method: 'post',
		postBody: data,
		onSuccess: function(rs) {
			if (rs.responseText == 1)
				location.href = baseurl+'home/thanks';
			else
			{
				$('messages').innerHTML = rs.responseText;
				new Effect.Appear('valueError',{from:0,to:1,duration:.25});
			}
		},
		onFailure: function(rs) {
			alert('There was an error sending your card.');
		},
		on404: function(rs) {
			alert('There was an error sending your card.');
		},
		onException: function(rs,err) {
			alert('There was an error sending your card.');
		}
	});
}

/* Btgform Class */
var Btgform = Class.create();
Btgform.prototype = {
	initialize: function(opts) {
		this.editor = new Ajax.InPlaceEditor('msg', baseurl+'ajax/repeat', opts);
	},
	replaceEditor: function(opts) {
		this.editor.dispose();
		this.editor = new Ajax.InPlaceEditor('msg', baseurl+'ajax/repeat', opts);
	},
	addPhotoWin: function() {
		var win = Builder.node('div',{
			id: 'addPhotoWin',
			style: 'background: #E9E9E9; position: absolute; top: 315px; width: 600px; left: 602px; margin-left: -301px; border: 1px solid; z-index: 10000;'
		},[
			Builder.node('iframe',{
				frameborder: 0,
				width: '600px',
				height: '250px',
				src: baseurl+'upload',
				id: 'uploadFrame'
			})
		]);
		toggleSelects();
		document.getElementsByTagName('body')[0].appendChild(win);
		Element.scrollTo($('oh_title'));
	}
}

/* Toggles visibility of selects */
function toggleSelects() {
	$A(document.getElementsByTagName('select')).each(function(s) {
		Element.toggle(s);
	});
}

/* Random name/address pair from server */
function genericAddr() {
	/*var current_name = $F('to_name');
	var current_email = $F('to_email');
	$('to_name').value = '';
	$('to_email').value = '';
	var a = new Ajax.Request(baseurl+'ajax/random_address',{
		method: 'post',
		postBody: 'name='+current_name+'&email='+current_email,
		onFailure: function(rs) {
			alert('There was an error getting a random name from the server.');
		},
		on404: function() {
			alert('The server could not be found.');
		},
		onException: function(rs,err) {
			alert('There was an error processing the results of the query.');
		},
		onSuccess: function(rs) {
			try {
				eval(rs.responseText);
				$('to_name').value = address.name;
				$('to_email').value = address.email;
			}
			catch (e) {
				alert('There was an error processing the results of the query: '+e+' at line '+e.lineNumber);
			}
		}
	});*/
	$('to_name').value = 'Service Members';
	$('to_email').value = 'servicemideast@coxohio.com';
}