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
                            +'<span class="twitter-time">'+Date.parse(item.created_at).toString('MMMM d, yyyy h:mm tt')+'</span>'+'</a>'
                            +'</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'});
  });
}

function doClearfix() {
//  $('.widget .item-list li').addClass('clearfix');
}

function doOWTTabs() {
  $('.owttablink').click(function() {
    if (!$(this).parent().hasClass('selected')) {
      $('.owttabcontent').hide();
      $('.owttablink').parent().toggleClass('selected');
      $($(this).attr('href')).show();
    }
    
    return false;
  });
}

function onRightAfter(curr, next, opts, fwd) { 
  var $ht = $(this).height(); //set the container's height to that of the current slide

  if ($.philipbloom.rightadsheight < $ht) {
    $(this).parent().animate({height: $ht});
    $.philipbloom.rightadsheight = $ht;
  }
}

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

  fixIE();

  doMenu();

  var innerw=0;
  
  innerw=parseInt($('div#blogarchives ul').innerWidth()/2,10);
  $('div#blogarchives ul').makeacolumnlists({cols:2,colWidth:innerw,equalHeight:false});

  innerw=parseInt($('div#blogcategories ul').innerWidth()/2,10);
  $('div#blogcategories ul').makeacolumnlists({cols:2,colWidth:innerw,equalHeight:false});

  innerw=parseInt($('div#links ul').innerWidth()/2,10);
  $('div#links ul').makeacolumnlists({cols:2,colWidth:innerw,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
  });

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

  $('#owtcycle').cycle({ 
    fx:      'turnDown', 
    speed:    800, 
    timeout:  4000
  });
/*
  var rcheight=0;
  $('#rightcycle > div').each(function() {
    if ($(this).height()>rcheight) {
      rcheight=$(this);
    }
  });
  
  $('#rightcycle').height(rcheight);
*/

  $('#rightcycle').cycle({ 
    fx:      'scrollLeft', 
    after: onRightAfter,
    speed:    800, 
    timeout:  6000
  });

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

  doOWTTabs();
  
  doClearfix();

  doDrawers();
  getTweets();  
});


