function resetSizes(){
  window.oldw=0
  window.oldh=0
}
resetSizes()
function megaReset(){
  resetSizes()
  setTimeout(resetSizes,1)
  setTimeout(resetSizes,100)
  setTimeout(resetSizes,500)
}
$(megaReset)
$('#background img').live('load',megaReset)

function resize(){
  var w=$(window)
  if(window.oldw==w.width() && window.oldh==w.height())return
  window.oldw=w.width()
  window.oldh=w.height()

  $('#background img').each(function(){
    if(!this.loaded)return resetSizes()
    var i=$(this)
    if(!i.data.width){
        if(!(i.width()*i.height()))return
        i.data.width=i.width()
        i.data.height=i.height()
    }
    var wprop=i.data.width/w.width()
    var hprop=i.data.height/w.height()
    prop=wprop<hprop?wprop:hprop

    var width=i.data.width/prop
    var height=i.data.height/prop
    i.width(width)
    i.height(height)
    s={position:'absolute',top:0,left:0}
    if(width>w.width()){
      s.left='-'+(width-w.width())/2+'px'
    }
    if(height>w.height()){
      s.top='-'+(height-w.height())/2+'px'
    }
    i.css(s)
  })

  $('#maincontent').css('min-height',w.height()-300+'px')

}

$(document).ready(function(){
    //esconde o botão de "carregar mais posts" se não houver próxima página
    if($('nav.nextprev span.next a').length==0) $('#nextbut').hide();

	$('.subItem').hide();
    $('nav li a').click(function(){
		
		$('.subItem').hide();
		
		//remove o current-item-page de todos os objetos
		$('nav li').removeClass('current-item-page');
		
		// abre o subItem do respectivo objeto clicado
		$(this).parent().find('.subItem').show('fast').end().addClass('current-item-page');

    });

						$('#background').cycle({
							next: 'a.next-photo',
							prev: 'a.prev-photo',
							pager: 'div.nav',
              timeout: 0,
              fx: 'none'
						});

	// Simula o uso de 'placeholder' em browsers sem suporte
	if (!elementSupportsAttribute('input', 'placeholder')) {
		$("textarea, input")
		.each(function() {
		if($(this).val().replace(/\s/gi,'')=="" && $(this).attr("placeholder") )
			$(this).val($(this).attr("placeholder")).addClass("placeholder")
		})
	.focus(function() {
		if($(this).val()==$(this).attr("placeholder"))
			$(this).val("").removeClass("placeholder")
		})
	.blur(function() {
		if($(this).val().replace(/\s/gi,'')=="" && $(this).attr("placeholder") )
			$(this).val($(this).attr("placeholder")).addClass("placeholder")
		});
	}
	// Limpa os campos com placeholder simulado no envio dos formularios
	$('form').submit(function() {
		$(this).find('textarea,input').each(function() {
			if($(this).val()==$(this).attr('placeholder')) $(this).val('');
		});
	});

  setInterval(resize,10)

  $('.post').addClass('processed')

});

// Testa o suporte de um atributo em um elemento
function elementSupportsAttribute(element, attribute){
	var test = document.createElement(element);
	if (attribute in test) {
		return true;
	}
	else {
		return false;
	}
}

function tobot(){
  $('div.post:last').after($('#nextbut'))
  colorsetup()
  $('div.post:not(.processed)').each(processPost)
  ss.fixAllLinks()
}

function processPost(){
  var post=$(this)
  post.find('div.script').each(function(){
    eval($(this).html())
  })
  post.find('.hackadelic-sliderPanel').each(function(){
    source=post.find('#'+$(this).attr('id').replace(/Panel/g,'Note'))
    myInitSlider($(this),source)
  })
  post.addClass('processed')
}

function colorsetup(){
  $('div.gallery').each(function(){
    $(this).find('a').attr('rel',$(this).attr('id'))
  })
  if($('.post').fancybox) $('.post a[href $= ".jpg"],.post a[href $= ".png"],.post a[href $= ".gif"],.post a[href $= ".flv"]').fancybox({
    overlayColor:'#000',
    overlayOpacity:0.7
  })
}

$(colorsetup)

function toggleSlider(target) {
	jQuery(target).slideToggle('fast');
}
function toggleSliderOfGroup(group, target) {
	var t = jQuery(target);
	if (t.css('display') == 'none') {
		var g = jQuery(group);
		g.slideUp('fast');
	}
	t.slideToggle('fast');
}
function myInitSlider(target, source) {
	var t = jQuery(target);
	if ( t.length && !t.data('hackadelized') ) {
		var s = jQuery(source);
		t.html( s.html() ); t.data('hackadelized', true);
		s.replaceWith('');
	}
  if(t.hasClass('auto-expand')){
    t.addClass('block').removeClass('concealed')
  }else{
    t.addClass('block').hide().removeClass('concealed')
  }
	return t;
}

