$().ready(function(){
  
  //$("article").fadeTo(0, 0).fadeTo(1500, 0).fadeTo(500, 1);
  
  $(".category_form").each(function(){
    $(this).find("option").eq(0).html('Select category');
  });
  
  $("article.twocol iframe, article.twocol object, article.twocol embed").each(function(){
    var width = $(this).attr('width'),
        height = $(this).attr('height'),
        relation = height/width;
    $(this).attr({
      width:400,
      height:400*relation
    })
    console.log(relation);
  });
  
  $("article.threecol iframe, article.threecol object, article.threecol embed").each(function(){
    var width = $(this).attr('width'),
        height = $(this).attr('height'),
        relation = height/width;
    $(this).attr({
      width:530,
      height:530*relation
    })
    console.log(relation);
  });
  
  if($("#page_menu").length > 0) {
    
    $("#page_menu div").css({
      'display': 'none'
    });
    
    if(window.location.hash) {
      $(window.location.hash+" div").slideDown(500);
      setTimeout(function(){
        $("html, body").animate({scrollTop:$(window.location.hash+" div").offset().top-150}, 500);
      }, 501);
    }
    
    $("#page_menu h1").click(function(){
      var el = $(this),
          show = el.parent().find('div');
          
      if(show.css('display') == 'none') {
        $("#page_menu div").slideUp(250);
        el.parent().find('div').slideDown(500);
        setTimeout(function(){
          $("html, body").animate({scrollTop:el.offset().top-50}, 500);
        }, 501);

        return false;
      }
      
    });
    
  }
  
  $(".postform").change(function(){
    setTimeout(function(){
      $(".category_form").submit();
    }, 150);
  })
  
  $(".statements").cycle({
    fx: 'fade'
  });
  
  $('.sorting').masonry({
    itemSelector: '.mm'
  });
  
  setTimeout(function(){
    $('.sorting').masonry({
      itemSelector: '.mm'
    });
  }, 2500);
  
  
  $("#cat_1").each(function(){
    $(this).attr({
      'onchange': "return change_from_select('cat_1')"
    });
  });
  
  var links = document.getElementsByTagName('img');
  for (var i = 0; i < links.length; i++) {
    links[i]._title = links[i].title;
    links[i].onmouseover = function() {
      this.title = '';
    }
    links[i].onmouseout = function() {
      this.title = this._title;
    }
  }
  
});

function change_from_select(el) {
  var url = $("#"+el).val();
  window.location = url;
  return false;
}
