setCookie = function( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
		}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape(value ) + (( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + (( path ) ? ";path=" + path : "" ) + (( domain ) ? ";domain=" + domain : "" ) + (( secure ) ? ";secure" : "" );
	}

function numberFormat(nStr) {
	nStr += '';
	x = nStr.split(',');
	x1 = x[0];
	x2 = x.length > 1 ? ',' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + '.' + '$2');
		}
	return x1 + x2;
	}

function stripNonNumeric(str) {
	str += '';
	var rgx = /^\d|\.|-$/;
	var out = '';
	for (var i = 0; i < str.length; i++ ) {
		if (rgx.test( str.charAt(i))) {
			if (!((str.charAt(i) == '.' && out.indexOf( '.' ) != -1) || ( str.charAt(i) == '-' && out.length != 0 ))) {
		        out += str.charAt(i);
				}
			}
		}
	return out;
	}

/* -- Char Counter --------------------------------------------------------- */

function charCounter(id, maxlimit, limited){
	if($F(id).length > maxlimit) {
		if(limited) {
			$(id).value = $F(id).substring(0, maxlimit);
		}
		$('counter-'+id).addClassName('red');
	} else {
		$('counter-'+id).removeClassName('red');
	}

	$('counter-'+id).update( maxlimit - $F(id).length );
}

function makeItCount(id, maxsize, limited){
	if(limited == null) limited = true;
	if ($(id)){
		$(id).observe('keyup', function() {
			charCounter(id, maxsize, limited);
			}).observe('keydown', function() {
				charCounter(id, maxsize, limited);
				});
		charCounter(id,maxsize,limited);
	}
}


document.observe('dom:loaded', function() {
	makeItCount('idea-input', 140, false);

	$$('.toggle').each(function (elm) {
		elm.observe('focus', function (event) {
			if (elm.value == elm.title) {
				elm.removeClassName('faded').value = '';
				makeItCount('idea-input', 140, false);
				}
			}).observe('blur', function (event) {
				if (elm.value == '') {
					elm.addClassName('faded').value = elm.title;
					makeItCount('idea-input', 140, false);
					}
				});
		});

	/*$('open-form').observe('click', function(event) {
		event.stop();
		if(!$(this.rel).visible()) {
			$(this.rel).blindDown({duration: 0.5});
			} else {
				$(this.rel).blindUp({duration: 0.5});
				}
		});*/

	$$('.expand-form').invoke('observe', 'click', function(event) {
		event.stop();

			$('desc-text').removeClassName('text-round').addClassName('text');
			$('expanded-form').show();
            new Effect.ScrollTo($('top-title'), {duration: 0.3});
		});

	$$('#tab-ideas li a').each(function(elm) {
		elm.observe('click', function(event) {
			event.stop();

			if (!elm.hasClassName('selected')) {
				var container = $('good-ideas').down('.ideasContainer');
				if (container.next('div.pagination')) {
					container.update(); //descendants().invoke('remove');

					if (container.next('div.pagination').down('ul')) {
						container.next('div.pagination').down('ul').remove();
					}

					container.addClassName('feedback-inner').addClassName('first').insert({bottom: '<div class="inner"></div>'});

					new Ajax.Request(PATH_.root + 'ideas', {
						parameters: {mode: elm.rel},
						onSuccess: function (transport) {
							var response = transport.responseText.evalJSON();

							switch (response.type) {
								case 'success':
									var ideas = response.ideas.evalJSON();
									container.removeClassName('feedback-inner').removeClassName('first').next('.pagination').remove();
									ideasNavigator = new ListNavigator(container, ideas, {containerID: 'ideaList'});
									break;
								case 'error':
									container.descendants().invoke('remove');
									if (container.next('.pagination').down('ul')) {
										container.next('.pagination').down('ul').remove();
									}
									container.update(response.message);
									break;
							}
						}
					});
				}

				$$('#tab-ideas li a').invoke('removeClassName', 'selected');
				elm.addClassName('selected');
			}
		});
	});

	$('submit-form').observe('mouseover', function() {
		this.addClassName('hover');
		}).observe('mouseout', function() {
			this.removeClassName('hover');
			});	

	$$('.tabs').each(function (tabs) {
		var initial = tabs.select('li a').find(function (element) { return element.hasClassName('selected'); }).up('li');

		new Carousel(tabs.down('div.tabs-inner'), tabs.down('ul').select('li'), tabs.select('a.controls'), {controlClassName: 'controls', duration: 0.2, initial: initial});
		});

	$('feedback-form').observe('submit', function (event) {
		event.stop();

		if (!this.hasClassName('disabled')) {
			this.addClassName('disabled');

			var overlay = this.down('.overlay');
			overlay.setStyle({width: this.getWidth() + 'px', height: this.getHeight() + 'px'});
			overlay.appear({duration: 0.3, to: 0.7});

			var info = this.previous('.text').hide();
			info.down('.info').removeClassName('error').removeClassName('ok');

			new Ajax.Request(PATH_.root + 'add', {
				parameters: this.serialize(),
				onSuccess: (function (transport) {
					overlay.hide();
					this.removeClassName('disabled');

					var response = transport.responseText.evalJSON();
					switch (response.type) {
						case 'success':
							var idea = response.idea;

							if ($('tab-ideas').select('a').find(function (elm) {return elm.hasClassName('selected')}).rel == 'good') {
								var container = $('good-ideas').down('.ideasContainer');
								if (container.next('div.pagination')) {
									container.update('');

									if (container.next('div.pagination').down('ul')) {
										container.next('div.pagination').down('ul').remove();
									}

									container.addClassName('feedback-inner').addClassName('first').insert({bottom: '<div class="inner"></div>'});

									new Ajax.Request(PATH_.root + 'ideas', {
										parameters: {mode: 'new'},
										onSuccess: function (transport) {
											var response = transport.responseText.evalJSON();

											switch (response.type) {
												case 'success':
													var ideas = response.ideas.evalJSON();
													container.update('').removeClassName('feedback-inner').removeClassName('first').next('.pagination').remove();
													ideasNavigator = new ListNavigator(container, ideas, {containerID: 'ideaList'});

													$('ideas-title').scrollTo();
													new Effect.Highlight($('good-ideas').down('ul').down('li'));
													break;
												case 'error':
													container.descendants().invoke('remove');
													if (container.next('.pagination').down('ul')) {
														container.next('.pagination').down('ul').remove();
													}
													container.update(response.message);
													break;
											}
										}
									});
								}

								$$('#tab-ideas li a').invoke('removeClassName', 'selected');
								$('new').addClassName('selected');
							} else {
								if ($('ideaList')) {
									ideasNavigator.addNew(idea);
								} else {
									var container = $('good-ideas').down('.ideasContainer');
									container.update('').removeClassName('feedback-inner').removeClassName('first').next('.pagination').remove();
									ideasNavigator = new ListNavigator(container, new Array(idea), {containerID: 'ideaList'});
								}
								$('ideas-title').scrollTo();
								new Effect.Highlight($('good-ideas').down('ul').down('li'));
							}
							break;
						case 'error':
							info.show().down('.info').addClassName('error').update(response.message);
							break;
					}
				}).bind(this)
			});
		}
	});
});