var imageTypes = 'gif,jpg,jpeg,png';
var imageExt = /\.(gif|jpg|jpeg|png)/;

function checkImages(input, form)
{
    if(imageExt.test($F(input))) {
		$(form).submit();
    } else {
        alert('Este arquivo nao e permitido. Somente ('+imageTypes+')');
        $(input).value='';
    }
}

function galleryEditImage(id)
{
	var legend = prompt('Digite a nova lengenda');
	if(legend == null) return false;
	showSpinner();
	new Ajax.Request("index.php?q=gallery/editLegend", {
   		parameters : 'legend=' + legend + '&id=' + id,
   		onSuccess : function(result) {
			hideSpinner();
			$('legend_'+id).innerHTML = result.responseText;
		}
	});



}

var Application =  {
	showCode: function(a, id) {
    	a.innerHTML = $(id + "_codediv").visible() ? "View source" : "Hide source"
    	$(id + "_codediv").toggle();
  	},

	pager : function(url, target) {

		/*new Ajax.Updater(target, '/default.php/events/pager/'+url, {
			evalScripts : true
		});*/

		new Ajax.Updater(target, '/'+url, {
			evalScripts : true
		});
	},

	loadEventPage : function(page) {
		new Ajax.Updater('events-content', '/'+page, {
			evalScripts : true
		});
	},

	loadPage : function(page, target) {

		if(target == undefined) {
			target = 'panel_content';
			new Ajax.Request(page, {
				onSuccess : function(transport) {
					$(target).innerHTML = '';
					var d = document.createElement('div');
					d.innerHTML  = '<a href="javascript:void(0);" onclick="$(\''+target+'\').hide();">Fechar</a>';
					d.innerHTML += transport.responseText;
					$(target).appendChild(d);// = transport.responseText;
					Effect.Appear(target, {duration: 1.0});
				}
			});
		} else {
			new Ajax.Updater(target, page, {
				evalScripts : true
			});
			Effect.Appear(target, {duration: 1.0});
		}
	},

	loadContactForm : function(page, target) {
		if(target == openedForm) return;
		else {
			new Ajax.Updater(target, '/'+page, {
				evalScripts : true
			});
			Effect.Appear(target, {duration: 1.0});
			if(openedForm != null)
                $(openedForm).innerHTML = '';
			openedForm = target;
		}
	}
}

function toggleStatus(id, uri)
{
	new Ajax.Updater(id, '/' + uri + '/' + id.replace('status_',''), {});
}

function closeContentPanel()
{
	Effect.Fade('panel_content', {
		duration : 1.0,
		afterFinish : function() {
			$('panel_content').innerHTML = '';
		}
	});
}
function showSpinner()
{
	$('spinner').style.display = 'block';
}

function hideSpinner()
{
	$('spinner').style.display = 'none';
}

function getStates(sel, names)
{
	$('frm_cidades').innerHTML = '<option>Aguarde...</option>';
	new Ajax.Request('/contact/getStates/'+sel.value, {
		onSuccess : function(transport) {
			var result = transport.responseText.evalJSON();
			$('frm_cidades').innerHTML = '<option>Selecione</option>';
			for(var i = 0; i < result.length; i++)
			{
				var c = document.createElement('option');
				if(names == true) {
					c.value = result[i].name;
				} else c.value = result[i].id;
				c.innerHTML = result[i].name;
				$('frm_cidades').appendChild(c);
			}

			$('state_name').value = sel[sel.selectedIndex].innerHTML;
		}
	});
}

function getGalleries(sel, names)
{
    $('event').innerHTML = '<option>Aguarde...</option>';
    new Ajax.Request('/default.php/events/getEventsWithGallery/'+sel.value, {
        onSuccess : function(transport) {
            var result = transport.responseText.evalJSON();
            $('event').innerHTML = '<option value="0">Selecione um evento</option>';
            for(var i = 0; i < result.length; i++)
            {
                var c = document.createElement('option');
                if(names == true) {
                    c.value = result[i].name;
                } else c.value = result[i].id;
                c.innerHTML = result[i].name;
                $('event').appendChild(c);
            }
        }
    });
}

var nodeFocus = null;
function makeFocus(id)
{
	if(nodeFocus == null || id == nodeFocus)
	{
		nodeFocus = id;
		return;
	} else {
		$(nodeFocus).hide();
		nodeFocus = id;
	}
}

SlideShow = Class.create();

SlideShow.prototype = {
	currentSection : '',
	sections : '',
	viewPort : '',
	initialize: function(e) {
		this.viewPort = $('slide-show');
		this.sections = this.viewPort.getElementsByClassName('section');
		this.currentSection = 0;
		this.play();
	},

	play : function() {
		Effect.Appear(this.sections[this.currentSection].id);
		new PeriodicalExecuter(this.swapImages, 5);
	},

	swapImages : function(pe)
	{
		Effect.Fade(sldShow.sections[sldShow.currentSection].id, {
			afterFinish : function() { Effect.Appear(sldShow.sections[sldShow.currentSection].id); }
		});

		if(sldShow.currentSection == (sldShow.sections.length - 1)) sldShow.currentSection = 0;
		else sldShow.currentSection += 1;

		//if(!confirm('continue?')) pe.stop();
	}
};

function hideFlash() { $('topo').setStyle({top:'-1000px'}); }
function showFlash() { $('topo').setStyle({top:'0px'}); }

