var RecaptchaOptions = {
   theme : 'clean'
};

Effect.BlindRight = function(element) {
  element = $(element);
  var elementDimensions = element.getDimensions();
  return new Effect.Scale(element, 100, Object.extend({
    scaleContent: false,
    scaleY: false,
    scaleFrom: 0,
    scaleMode: { originalHeight: elementDimensions.height, originalWidth: elementDimensions.width},
    restoreAfterFinish: true,
    afterSetup: function(effect) {
      effect.element.makeClipping().setStyle({
        width: '0px',
        height: effect.dims[0] + 'px'
      }).show();
    },
    afterFinishInternal: function(effect) {
      effect.element.undoClipping();
    }
  }, arguments[1] || { }));
};

Effect.BlindLeft = function(element) {
  element = $(element);
  element.makeClipping();
  return new Effect.Scale(element, 0,
    Object.extend({ scaleContent: false,
      scaleY: false,
      scaleMode: 'box',
      scaleContent: false,
      restoreAfterFinish: true,
      afterSetup: function(effect) {
        effect.element.makeClipping().setStyle({
          height: effect.dims[0] + 'px'
        }).show();
      },
      afterFinishInternal: function(effect) {
        effect.element.hide().undoClipping();
      }
    }, arguments[1] || { })
  );
};

function menuLoad() {
	$$('#header>span.fakeLink').each(function(n) { n.observe('click', textToggle); });
}
var bodyLoaded;
function bodyLoadGeneral() {
	
}

function bodyLoadpiece() {
	if(bodyLoaded) { return true; }else{
	bodyLoaded = true;
	var re = /\#([^\-]*)-/;
	var page = re.exec(window.location)
	if(page) {
		if($(page[1])) {
			$$('.page').detect(function(n) { return n.style.display != 'none'; }).toggle();
			$$('.comments').detect(function(n) { return n.style.display != 'none'; }).toggle();
			$(page[1]).toggle();
			$(page[1]+'-comments').toggle();
			$('text_id_field').value = $(page[1]).down().innerHTML;
		}
	}
	controlReset($$('.page').detect(function(n) { return n.style.display != 'none'; }));

	if(bottom) {
		window.scrollTo(0,document.height);
	}
	
	bodyLoadGeneral();
	}
}

function bodyLoaduser() {
	bodyLoadGeneral();
}

function bodyLoadnew() {
	$('textAreas').nextArea = $('next_piece_number').innerHTML;
	bodyLoadGeneral();
}

function controlReset(currentItem)
{
	if(currentItem.next('.page')) {
		$('nextPage').setStyle({ visibility:'visible'});
		$('lastPage').setStyle({ visibility:'visible'});
	}else{
		$('nextPage').setStyle({ visibility:'hidden'});
		$('lastPage').setStyle({ visibility:'hidden'});
	}
	if(currentItem.previous('.page')) {
		$('firstPage').setStyle({ visibility:'visible'});
		$('prevPage').setStyle({ visibility:'visible'});
	}else{
		$('firstPage').setStyle({ visibility:'hidden'});
		$('prevPage').setStyle({ visibility:'hidden'});
	}
}

function firstPage() {
	window.scrollTo(0,0);
// 	$('piece').fade({ queue:'end', duration:0.25}).appear({ queue:'end', duration:0.25});
	$$('.page').detect(function(n) { return n.style.display != 'none'; }).toggle();
	var currentItem = $$('.page').first().toggle();
	$('text_id_field').value = currentItem.down().innerHTML;

	$$('.comments').detect(function(n) { return n.style.display != 'none'; }).toggle();
	$$('.comments').first().toggle();
	
	var pageTitle;
	if(pageTitle = currentItem.down().next(2).down().innerHTML){
		pageTitle = '-'+pageTitle.replace(/[^a-z0-9\-]/gi, '_');
	}
	window.location = '#'+Element.identify(currentItem)+pageTitle;
	controlReset(currentItem);
}

function prevPage() {
	window.scrollTo(0,0);
// 	$('piece').fade({ queue:'end', duration:0.25}).appear({ queue:'end', duration:0.25});
	var currentItem = $$('.page').detect(function(n) { return n.style.display != 'none'; }).toggle().previous('.page').toggle();
	$('text_id_field').value = currentItem.down().innerHTML;

	$$('.comments').detect(function(n) { return n.style.display != 'none'; }).toggle().previous('.comments').toggle();
	
	var pageTitle;
	if(pageTitle = currentItem.down().next(2).down().innerHTML){
		pageTitle = '-'+pageTitle.replace(/[^a-z0-9\-]/ig, '_');
	}
	window.location = '#'+Element.identify(currentItem)+pageTitle;
	controlReset(currentItem);
}

function nextPage() {
	window.scrollTo(0,0);
// 	$('piece').fade({ queue:'end', duration:0.25}).appear({ queue:'end', duration:0.25});
	var currentItem = $$('.page').detect(function(n) { return n.style.display != 'none'; }).toggle().next('.page').toggle();
	$('text_id_field').value = currentItem.down().innerHTML;

	$$('.comments').detect(function(n) { return n.style.display != 'none'; }).toggle().next('.comments').toggle();
	
	var pageTitle;
	if(pageTitle = currentItem.down().next(2).down().innerHTML){
		pageTitle = '-'+pageTitle.replace(/[^a-z0-9\-]/gi, '_');
	}
	window.location = '#'+Element.identify(currentItem)+pageTitle;
	controlReset(currentItem);
}

function lastPage() {
	window.scrollTo(0,0);
// 	$('piece').fade({ queue:'end', duration:0.25}).appear({ queue:'end', duration:0.25});
	$$('.page').detect(function(n) { return n.style.display != 'none'; }).toggle();
	var currentItem = $$('.page').last().toggle();
	$('text_id_field').value = currentItem.down().innerHTML;

	$$('.comments').detect(function(n) { return n.style.display != 'none'; }).toggle();
	$$('.comments').last().toggle();
	
	var pageTitle;
	if(pageTitle = currentItem.down().next(2).down().innerHTML){
		pageTitle = '-'+pageTitle.replace(/[^a-z0-9\-]/ig, '_');
	}
	window.location = '#'+Element.identify(currentItem)+pageTitle;
	controlReset(currentItem);
}

function pulsed() {
	 $('ratingsPanel').down().fade({ to:0.1, from:0.3, duration:0.5, queue:'end'}).appear({ to:0.3, from:0.1, duration:0.5, queue:'end'});
}

function ifThisPageReload(anchor) {
	if(anchor.replace(/#.*$/ ,'') == String(window.location).replace(/#.*$/ ,'')) {
		window.location = anchor;
		window.location.reload();
		return false;
	}else{
		return true;
	}
}

function like(that) {
	that.fade({ to:0.1, from:1, duration:0.5, queue:'end'}).appear({ to:0.3, from:0.1, duration:0.5, queue:'end'});
	var pulse = new PeriodicalExecuter( pulsed , 1);
	var like = new Ajax.Request('/like', { 'parameters': { 'postType':'like', 'text_id':that.up('.page').down().innerHTML },
		onSuccess: function(n) {
			if(n.responseText == '') {
				pulse.stop();
				that.fade({ from:0.3, queue:'end'});
			}else{
				alert(n.responseText);
				pulse.stop();
				that.appear({ from:0.1, queue:'end'});
			}
		}, onFailure: function(n) {
			pulse.stop();
			that.appear({ from:0.1, queue:'end'});
	} });
}

var setFormatChanges = $H();
var setFormatDelay;
function setFormat(fparameters) {
	setFormatChanges.update(fparameters);
	if(setFormatDelay) {
		window.clearTimeout(setFormatDelay);
	}
	setFormatDelay = function(fparameters) { new Ajax.Request('/format', { parameters: setFormatChanges
	}); setFormatChanges = $H(); }.delay(2);
}

var sizes = Array('xx-small', 'x-small', 'small', 'medium', 'large', 'x-large','xx-large');
function fontSize(by) {
	if(((currentFontSize + by) < 0) || ((currentFontSize + by) > 6)) { return 0; }
	$('mainContainer').setStyle({ fontSize: sizes[currentFontSize += by]});
	setFormat({ fontSize:sizes[currentFontSize]});
}

function fontStyle(font) {
	$('mainContainer').setStyle({ fontFamily: font });
	setFormat({ fontFamily:font});
}

function invert() {
	if(document.body.getStyle('backgroundColor') == 'rgb(17, 17, 17)') {
		document.body.setStyle({ 'backgroundColor': '#eee'});
		document.body.setStyle({ 'color': '#222'});
		$$('h1.header')[0].setStyle({ 'backgroundImage': 'url(/transpar2.png)'});
		$$('.comment').each(function(element) { element.setStyle({ 'backgroundColor': '#fff'})});
		$$('.commentMeta').each(function(element) { element.setStyle({ 'backgroundColor': '#eee'})});
		$$('.commentArea').each(function(element) { element.setStyle({ 'backgroundColor': '#fff', 'border': '1px outset #666'})});
		$$('div.rightBox')[0].setStyle({ 'backgroundColor': '#fff' });
		
		setFormat({ invert:1});
	}else{
		document.body.setStyle({ 'backgroundColor': '#111'});
		document.body.setStyle({ 'color': '#888'});
		$$('h1.header')[0].setStyle({ 'backgroundImage': 'url(/transpar.png)'});
		$$('.comment').each(function(element) { element.setStyle({ 'backgroundColor': '#222'})});
		$$('.commentMeta').each(function(element) { element.setStyle({ 'backgroundColor': '#111'})});
		$$('.commentArea').each(function(element) { element.setStyle({ 'backgroundColor': '#222', 'border': '1px outset white'})});
		$$('div.rightBox')[0].setStyle({ 'backgroundColor': '#222' });
		
		setFormat({ invert:0});
	}
}

function textToggle(event) {
	if(this.progress) { return false; }else{
		this.progress = true;
		if($$('#menu-'+this.innerHTML+' div span').detect(function(n) { return n.style.display != 'none'; })) {
			textIn(this);
		}else{
			$$('#header>span').each(textIn);
			textOut(this);
		}
	}
}

function textIn(that)
{
	try { $$('#menu-'+that.innerHTML+' div span').detect(function(n) { return n.style.display != 'none'; }).hide(); }catch(e) { return true; }
	if($$('#menu-'+that.innerHTML+' div span').detect(function(n) { return n.style.display != 'none'; })) { textIn.delay(0.01,that); }else{ that.progress = false; }
}
function textOut(that)
{
	$$('#menu-'+that.innerHTML+' div span').detect(function(n) { return n.style.display == 'none'; }).show();
	if($$('#menu-'+that.innerHTML+' div span').detect(function(n) { return n.style.display == 'none'; })) { textOut.delay(0.01,that); }else{ that.progress = false; }
}

function newTextarea(that) {
	$('textAreas').insert('<div><input type="text" name="text['+($('textAreas').nextArea++)+'][title]" value="text title no.'+($('textAreas').nextArea)+'" onfocus="this.value=\'\'" /><textarea name=\'text['+($('textAreas').nextArea-1)+'][text]\' class=\'textBody\' onfocus="this.style.textAlign=\'justify\';this.innerHTML=\'\'" onkeyup="limitSize(this, 250000);">TEXT NO.'+($('textAreas').nextArea)+'</textarea><button onclick="if(this.innerHTML == \'sure?\'){ this.up().remove(); }else{ this.innerHTML = \'sure?\'; }; return false;">remove text</button></div>');
	return false;
}

function limitSize(that,size) {
	if(that.origColour == undefined) {
		that.origColour = that.style.backgroundColor;
	}
	if(that.value.length > size) {
		that.style.backgroundColor='#faa';
	}else{
		that.style.backgroundColor=that.origColour;
	}
}

function hideNotificationUL() {
	$$('#notifications ul')[0].toggle();
}

function clearNotifications() {
	new Effect.toggle('notifications', 'slide', { duration:0.5, queue:'end'});
	
	new Ajax.Request('/clearNotifications', {
		onSuccess: function(n) {
			hideNotificationUL.delay(1);
			$('notiE').style.color='#563b9b';
		}, onFailure: function(n) {
			new Effect.toggle.delay(1,'notifications', 'slide', { duration:0.25});
	} });
}
