var window_scroll;

var rAccordion = new Class({
	
	initialize: function(container, toggleClass, elementClass, options){
		this.container = container;
		this.tClass = toggleClass;
		this.eClass = elementClass;
		this.options = options;
		this.selector = '#' + this.container + ' > .';
		this.makeAccordion();
	},
	
	makeAccordion: function(){
		new Accordion(
			$$(this.selector+this.tClass),
			$$(this.selector+this.eClass),
			this.options
		).addEvents({
			// The onActive and onComplete events added to the stack here to
			// attempt to address some of the css issues.
			'onActive': function(toggle){
				if(toggle.getParent().getStyle('height') != 0)
					toggle.getParent().setStyle('height', '');
			},
			'onComplete': function(a){
				if ($defined(a)) {
					var height = 0;
					a.getParent().getChildren().each(function(e){
						height = height + e.offsetHeight;
					});
					if(height != a.getParent().offsetHeight && a.getParent().offsetHeight != 0)
						a.getParent().setStyle('height','');
				}
			}
		});
		this.selector += this.eClass + ' > .';
		if($defined($$(this.selector)[0]))
			this.makeAccordion();
	}
	
});

var resize_body = function(){
			b = window.getSize()
			b = Math.round(b.y*1.01);
			a = $('main').getSize().y+$('footer').getSize().y+$('header').getSize().y
			if (b>a) { $('body').setStyle('height',b); };
};

var grid_size = function(element, override) {	
	var height = 0;
	if (override){
		element.getChildren().each(function(e){ height = height + e.getSize().y; });
	}
	else{
		height = element.offsetHeight;
	}
	rest = height % 4;
	if (rest != 0){
		rest = (4-rest)
	}
	new_height = height + rest;
	element.setStyle('height', new_height+'px');
};

var images_fix = function (tag) {
	$$('#'+tag+' img').each(function(image){ image_width_height(image, tag); });
};


var image_width_height = function(img, tag) {    
    if (( img.getAttribute('width') == null ) || ( img.getAttribute('height') == null )){
      img.onload = function() {
			var pic_real_width = this.width;
			var pic_real_height = this.height;
			var pic_new_width = 567;
			var pic_new_height = (pic_new_width / pic_real_width) * pic_real_height;
		    this.setAttribute('width', pic_new_width+'px');  
		    this.setAttribute('height', pic_new_height+'px');
			this.getParent().setStyle('width', pic_new_width+'px');  
			this.getParent().setStyle('height', pic_new_height+'px'); 
			grid_size($(tag),1);
		};
	};
};

var show_bottom = function (id){
	divh = $('inserir_item_'+id+'_element').offsetHeight;
	divt = $('comentarios_item_'+id+'_element').offsetHeight;	
	if (!(divt==0) && !(divh==0)) {
		window_scroll.toElement('inserir_item_'+id);
	}
};

window.addEvent('domready', function(){
	resize_body();
	window_scroll = new Fx.SmoothScroll({duration: 1000},window);
dbug.enable();/*turn debugging on if it's not already*/
/*var x = 'hi there';
dbug.log('the value of x is "%s"', x); "the value of x is "hi there"
var x = {apple: 'red', lemon: 'yellow'};
dbug.log('the value of x is %1.o', x);
 in the console:
  the value of x is
  apple: 'red'
  lemon: 'yellow
*/
});
window.addEvent('resize', function(){
	resize_body();
});
