function fixIE() {
  if($.browser.msie){
    var zIndexNumber = 1000;
    $('div, embed, object').each(function() {
      $(this).css('zIndex', zIndexNumber);
      zIndexNumber -= 10;
    });
  }
}

function doMenu() {
//  $('div#mainmenu').css({zindex: '1001'});

  $('div#mainmenu > ul > li').each(function(i, val){

    c=$(val).children('ul');
    if (c[0]) {
      $(c).css({paddingTop: '0', paddingBottom: '0'});
      $(c).find('li').css({width: '100%'});

      pos=parseInt($(val).children('a').position().top);
      h=parseInt($(c).height())-pos;

      $(c).css({visibility: 'hidden', position: 'absolute', left: parseInt($(this).position().left)+'px', width: 'auto'});
      $(c).css({top: ($(val).height()+pos)+'px', height: 'auto'});
    }

    $(val).hover(function(e){
      $(this).children('a').css({color: '#ffffff', backgroundColor: '#4d4d4d'});
      p=$(this).children('ul');
      
      if (p[0]) {
//        $('div.post').find('embed').css({position: 'relative', zIndex: 1010});
//        $('div.post').find('object').css({position: 'relative', zIndex: 1010});
        $(p).css({visibility: 'visible', zIndex: 1000});
        $(p).find('ul').css({visibility: 'visible'});
      }
    }, function(){
      $(this).children('a').css({backgroundColor: 'transparent', color: '#ffffff'});
      p=$(this).children('ul');
      
      if (p[0]) {
        $(p).css({visibility: 'hidden'});
        $(p).find('ul').css({visibility: 'hidden'});
      }
    });
  });

}

function getTweets() {
  $.getJSON("http://twitter.com/statuses/user_timeline.json?screen_name=philipbloom&count=5&callback=?",
            function(data) {
              if ($(data).length>0) {
                $('div.tweets').each(function(i,val) {
                  $(val).css({backgroundImage: 'none', height: 'auto'});

  //                  $(val).append("<h2>Latest tweets</h2>");

                  $.each(data, function(j,item){
                    output='<p class="twitter-message">'
                            +'<a href="http://twitter.com/PhilipBloom/status/'+item.id+'">'+item.text+'</a>'
                            +'<br /><span class="twitter-time">'+Date.parse(item.created_at).toString('MMMM d, yyyy h:mm tt')+'</span>'
                            +'</p>';
                    $(val).append(output);
                  });
                  $(val).append("<a href='http://twitter.com/PhilipBloom' class='follow'>Follow me</a>");

                  $(val).css('paddingBottom', '50px');
                });
              }
            });
}

function doDrawers() {
  $('.drawer').css('zIndex', '100000');

  $('.drawer').hover(function() {
    $(this).animate({left: '0px'});
  },function() {
    $(this).animate({left: '-270px'});
  });
}

$(document).ready(function() {
  $.philipbloom={}
  $.philipbloom.headerbgimage=$('#header').css('backgroundImage');

  fixIE();

  doMenu();
  
  $('div#blogarchives ul').makeacolumnlists({cols:2,colWidth:120,equalHeight:false});
  $('div#blogcategories ul').makeacolumnlists({cols:2,colWidth:120,equalHeight:false});
  $('div#links ul').makeacolumnlists({cols:2,colWidth:120,equalHeight:false});
  
  $('#featuredbox').cycle({ 
    fx:      'scrollLeft', 
    speed:    800, 
    timeout:  5000 
  });

  if ($('#sectionfeaturebox .strongbox').length>1) {
    $('#sectionfeaturebox').cycle({ 
      fx:      'scrollLeft', 
      speed:    800, 
      timeout:  5000 
    });
  } else {
    $('#sectionfeaturebox .strongbox').css({ display: 'block' });
  }

  $('.homepageads #adcontainer').cycle({ 
    fx:      'scrollLeft', 
    speed:    800, 
    timeout:  7000 
  });

  $('#topcycle').cycle({ 
    fx:      'turnDown', 
    speed:    800, 
    timeout:  4000
  });

  if ($('#headingbag').height()>46) {
    $('#topads').css({ marginTop: $('#headingbag').height()+24});
  }

  doDrawers();
  getTweets();
});

