var gPager = null;
function send_photo(valid) 
{
    if(valid.validate()) {
		Element.toggle($('send_photo'));
        //var w = window.open('?q=newsletter/send_image','upload_image', "top=400, left=300, width=300, height=200, scrollbars=no, resizable=no, status=no");
    } else alert("Por favor, preencha os dados acima."); 
    
    return false;
}

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;
		}
	});
}

function photo_return(id)
{
    $('photo').value = id;
}

var Application =  {
  	lastId : 0,
  	currentSampleNb : 0,
  	currentMainPage : null,
	currentSubPage : null,
	historyHash : new Array(),

  	getNewId : function() {
    	Application.lastId++;
    	return "window_id_" + Application.lastId;
  	},
  	
	loadMainPage : function(page, elTarget, link, controls) {

		Application.currentSubPage = null;
		if(Application.currentMainPage != null) {
			$(Application.currentMainPage).removeClassName('active');
			$(Application.currentMainPage).src = $(Application.currentMainPage).src.replace('_up', '');  
		}

		$(link).addClassName('active');
		
		$(link).src = $(link).src.replace('.gif', '_up.gif');
		
		Application.currentMainPage = link;		
		new Ajax.Updater(elTarget, 'index.php?q='+page, {
			evalScripts: true,
			parameters : 'controls='+controls
		});
	},
	
	loadSubPage : function(page, elTarget, link, controls, noHash) {
		if(Application.currentSubPage != null) {
			$(Application.currentSubPage).removeClassName('active');
			$(Application.currentSubPage).src = $(Application.currentSubPage).src.replace('_up', '');  
		}

		//$(link).addClassName('active');

		$(link).src = $(link).src.replace('.gif', '_up.gif');
		
		if(noHash == undefined) {
			Application.historyHash.push(new Array(Application.currentSubPage, elTarget));
		}
		
		Application.currentSubPage = link;		
		if(controls == undefined) controls = true;
		new Ajax.Updater(elTarget, 'index.php?q=app/loadPage/'+page, {
			evalScripts: true,
			parameters : 'return=true&controls='+true
		});
	},
	
	back : function() {
		if(Application.historyHash.length > 0) {
			var newLocation = Application.historyHash.pop();
			Application.loadSubPage(newLocation[0]+'.html', newLocation[1], newLocation[0].replace('\.html',''), false);
		} else {
			history.back();
		}
	},
	
	showImage : function(image, img) {
		//img.setAttribute('onmouseout', 'Application.hideImage(' + img + ')');
		$('image').innerHTML = '<img src="public/images/' + image + '" alt="" onmouseout="Application.hideImage();" />';
	},
	
	hideImage : function(img) {
		//img.removeAttribute('onmouseout');
		$('image').innerHTML = '';
	}
}

function checkEmail(email) 
{
	var posicao = email.indexOf("@");
  	if (posicao == -1) {
    	return false;
  	}
  	var posicao1 = email.indexOf(".", posicao);
  	if (posicao1 == -1) {
    	return false;
  	}
  	return true;	
}

function showSpinner()
{
	$('spinner').style.display = 'block';
}

function hideSpinner()
{
	$('spinner').style.display = 'none';
}

var Pager = Class.create();

Pager.prototype = {
	initialize : function(rows, perPage, link) {
		this.fPage = 0;     
		this.actPage = 0;
		this.pageLimit = perPage;
		this.rows = rows;
		this.totalPages = Math.ceil(rows/perPage);
		this.lPage=this.totalPages;
		this.mainURL = 'index.php?q='+link;
		this.renderPager();
	},
	renderPager: function() {
		var output = '';
		var ini, end;
		
		$('pager').innerHTML = '';
		if(this.actPage > 5 && (this.actPage+5)<=this.totalPages) ini = this.actPage-5;
    	else if(this.actPage > 5 &&(this.actPage+5)>this.pageLimit && (this.totalPages-10)>0) ini = this.totalPages-10;
    	else ini = 0;

    	if(ini+10 > this.totalPages) end = this.totalPages-ini;
    	else end = ini+10;

    	if(ini > 0) output += "<b><a href='#' onclick='gPager.firstPage();'>&laquo;1</a></b>&nbsp;...";
    	for (var i = ini; i < end; i++){
      		if(this.actPage == i) output += "<b>&nbsp;"+(i+1)+"</b>";
      		else output += "<a href='#' onclick='gPager.gotoPage("+i+");'>&nbsp;"+(i+1)+"</a>";
    	}
    	if(end < this.totalPages) output += "...<b><a href='#' onclick='gPager.lastPage();'>&nbsp;"+this.totalPages+"&raquo;</a><b>";
		
		var el = document.createElement('div');
		el.innerHTML = output;
		
		$('pager').appendChild(el);
		
	},
	
	firstPage: function() {
		this.gotoPage(this.fPage);	
	},
	
	lastPage: function() {
		this.gotoPage(this.lPage);
	},
	
	nextPage: function() {},
	prevPage: function() {},
	
	gotoPage: function(page) {
		this.actPage = page;

		new Ajax.Request(this.mainURL, {
			method: 'get',
			parameters: 'page='+page,
			onSuccess : this.flushData
		});
	},
	
	flushData : function (resp) {
		var obj = resp.responseText.evalJSON();
		var d = document.createElement('div');
		d.innerHTML = obj.content;
		$('table_content').innerHTML = '';		
		$('table_content').appendChild(d);
		gPager.renderPager();
	}
};

