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, '/default.php/'+url, {
			evalScripts : true
		});

	},

	loadEventPage : function(page) {
		new Ajax.Updater('events-content', '/default.php/'+page, {
			evalScripts : true
		});
	},

	loadPage : function(page, target) {

		if(target == undefined) {
			target = 'panel_content';
			new Ajax.Request('/default.php/'+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});
                    new PeriodicalExecuter(function(pe) {
                        Application.regularizeFooter();
                        pe.stop();
                    }, 1);
				}
			});
		} else {
			new Ajax.Updater(target, '/default.php/'+page, {
				evalScripts : true
			});
			Effect.Appear(target, {duration: 1.0});
		    sFS.flush();
            new PeriodicalExecuter(function(pe) {
                Application.regularizeFooter();
                pe.stop();
            }, 1);

		}
	},

	loadContactForm : function(page, target) {
		if(target == openedForm) return;
		else {
			new Ajax.Updater(target, '/default.php/'+page, {
				evalScripts : true
			});
			Effect.Appear(target, {duration: 1.0});
			if(openedForm != null)
                $(openedForm).innerHTML = '';
			openedForm = target;
		}
	},

	tHelp : function() {
	    $('help').toggle();
	},

	regularizeFooter : function() {
        var content_height = $('content').getStyle('height');
    	content_height = parseInt(content_height.replace('px', '')) + 300;
    	//alert(content_height);
	    $('globalw').setStyle({height: content_height+'px'});
	}
}

function toggleStatus(id, uri)
{
	new Ajax.Updater(id, '/default.php/' + uri + '/' + id.replace('status_',''), {});
}

var field_number = 4;
function addNewFileField()
{
	$('file_fields').innerHTML += '<tr>\n'
	+ '<td><input class="input" type="file" name="photos_'+field_number+'" id="photo_'+field_number+'" /><br />'
	+ '<label>Legenda</label><br />'
	+ '<input class="input" type="text" name="legend_'+field_number+'" id="legend_'+field_number+'" /></td>\n'
	+ '</tr>\n';
	field_number++;
}

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 changeJournalType(el)
{
	if($('journal_type').value == 'pdf')
	{
		$('journal_file').show();
	} else {
		$('journal_file').hide();
	}
}
function getStates(sel, names)
{
    $('frm_cidades').innerHTML = '';
    var wait = document.createElement('option');
    wait.innerHTML = 'Aguarde...';
	$('frm_cidades').appendChild(wait);

	new Ajax.Request('/default.php/contact/getStates/'+sel.value, {
		onSuccess : function(transport) {
		    $('frm_cidades').innerHTML = '';
			var result = transport.responseText.evalJSON();
			var sel = document.createElement('option');
			sel.innerHTML = 'Selecione uma cidade';

			$('frm_cidades').appendChild(sel);

			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();
	}
};

switchFontSize = Class.create();
switchFontSize.prototype = {

    _current : 1,
    _manager : '',

    initialize : function(e) {
        this._current = 1;

        this.manager = new CookieManager({shelfLife:30,userData:false});

        var cookieValue = this.manager.getCookie("VU_switchFontSize");

        if(cookieValue == null)
            this.manager.setCookie("VU_switchFontSize", this._current);
        else {
            this._current = cookieValue;
            try {
                $('body_content').removeClassName('font1');
                $('body_content').addClassName('font'+(this._current));
            } catch(e) { }
        }
        //manager.clearCookie("VU_swtchFontSize");
    },

    more : function() {
        if(this._current == 4) return;
        else {
            $('body_content').removeClassName('font'+this._current);
            $('body_content').addClassName('font'+(this._current + 1));
            this._current++;
            this.manager.setCookie("VU_switchFontSize", this._current);
        }
    },

    less : function() {
        if(this._current == 1) return;
        else {
            $('body_content').removeClassName('font'+this._current);
            $('body_content').addClassName('font'+(this._current - 1));
            this._current--;
            this.manager.setCookie("VU_switchFontSize", this._current);
        }
    },

    flush: function() {
        new PeriodicalExecuter(function(pe) {
            if($('body_content').className != '') {
                $('body_content').removeClassName('font1');
                $('body_content').addClassName('font'+(sFS._current));
                pe.stop();
            } else {
                $('body_content').addClassName('font'+(sFS._current));
                pe.stop();
            }
        }, 1);
    }
}

var current_adv = null;
function toggle_adv(id)
{
    if(current_adv == id) return;
    if(current_adv == null)
    {
        $(id).show();
    } else {
        $(current_adv).hide();
        $(id).show();
    }
    current_adv = id;
}


/*CORRECT TRANSPARENT PNG IMAGES
// Correctly handle PNG transparency in Win IE 5.5 or higher.
// http://homepage.ntlworld.com/bobosola. Updated 02-March-2004

function correctPNG()
{
	for(var i=0; i)
	{
		var img = document.images[i]
		var imgName = img.src.toUpperCase()
		if (imgName.substring(imgName.length-3, imgName.length) == 'PNG')
		{
			var imgID = (img.id) ? 'id="' + img.id + '" ' : ''
			var imgClass = (img.className) ? 'class="' + img.className + '" ' : ''
			var imgTitle = (img.title) ? 'title="' + img.title + '" ' : 'title="' + img.alt + '" '
			var imgStyle = 'display:inline-block;' + img.style.cssText
			if (img.align == 'left') imgStyle = 'float:left;' + imgStyle
			if (img.align == 'right') imgStyle = 'float:right;' + imgStyle
			if (img.parentElement.href) imgStyle = 'cursor:hand;' + imgStyle
			var strNewHTML = '
				+ ' style=\"' + 'width:' + img.width + 'px; height:' + img.height + 'px;' + imgStyle + ';'
				+ 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader'
				+ '(src=\"' + img.src + '\', sizingMethod=\'scale\');\'>'
			img.outerHTML = strNewHTML
			i = i-1
		}
	}
}*/

