function iepng(){
  if(jQuery.browser.msie && jQuery.browser.version=='6.0'){
    jQuery.ifixpng('/images/clear.gif');
    $('div.bannerimg').ifixpng();
  }
}

function ieWarning() {
  if(jQuery.browser.msie && 
      (jQuery.browser.version=='5.0' ||
       jQuery.browser.version=='5.5' ||
       jQuery.browser.version=='6.0')){
    $("body").append('<p id="attention_ie6"><a href="http://www.mozilla.com/firefox/" title="Download Firefox now!">Warning: your browser is outdated and may not display this page correctly. Click here to resolve this issue...</a></p>');
    $("p#attention_ie6").css({zIndex:'9000'});
    $("p#attention_ie6").show();
  }
}

function shorthead(sh,headlen) {
  $(sh).each(function(j,head){
    var t=$(head).text();
    var fulll=t.length;
    var a=t.split(' ');
    var l=0;
    t='';

    $(a).each(function(i,val){
      l+=val.length+1;
//      alert(l);
      if (l<=headlen) {
        t+=val+' ';
      }
    });

    t2=t.substring(0,t.length-1);

    if (t.length<fulll) {
      t2+='...';
    }

//    alert(t.length+' '+fulll+' '+headlen);
    $(head).text(t2);
//    alert(t2);
  });
}

function hideDescription(e) {
  $(e).slideDown(500); 
  $(e).queue(function(){ 
    setTimeout(function(){ 
      $(e).dequeue(); 
    }, 4000);
  }); 
  $(e).slideUp(500); 
}

function animateBanners() {
  $('div#bannerbag1').queue(function(){ 
    setTimeout(function(){ 
      $('div#bannerbag1').dequeue(); 
//      $('div#bannerbag2').fadeIn();
//      $('div#bannerbag1').fadeOut();

      $("div#bannerbag1").animate({ 
        backgroundImage: "http://localhost/owt/wp-content/themes/owt/images/rect16571-2.jpg"
      }, 1500 );

    }, 2000);
  }); 
}

function animateMainMenu() {
  $('ul#mainmenu a').each(function() {
    var cl=$(this).attr("class");
    var clhover='menu_1_'+cl[cl.length-1];
//    alert(clhover);
    $(this).hover(function() {
        $(this).css({backgroundPosition:"0px -14px"});
    }, function() {
      if ($(this).hasClass('current_page_item')) {
        $(this).css({backgroundPosition:"0px -28px"});
      } else {
        $(this).css({backgroundPosition:"0px 0px"});
      }
    });
  });
}

function showSingleParallax(pheight) {
  $('div.singleparallax').each(function() {$(this).parent().animate({height: ((pheight+40)+'px')}); });
  $('div.singleparallax').each(function() {$(this).animate({top: '0px'}); });
}

function showGalleryParallax() {
  $('div.galleryparallax').each(function() {$(this).parent().animate({height: '160px'});});
  $('div.galleryparallax').each(function() {$(this).animate({top: '0px'});});
}

function doParallax(parent) {
  var parcont=$(parent).find('div.parcontainer');
  var offset=$('a#logo').position().left;
  var contwidth=$('div#headbag div.container').width();

  $(parcont).css({width: contwidth, paddingLeft: offset, paddingRight: offset});
  $(parcont).width(contwidth);

  var pheight=0;

  $(parent).find('div.singleparallax div.layer li').each(function() {
    if ($(this).height()>pheight) {
      pheight=$(this).height();
    }
  });
    
  var w=0;
  $(parent).find('div.parallax div.layer ul li').each(function() {
    // width + margin
    w+=$(this).outerWidth()+0;
  });

  // prepend-1 in first li
  w+=0;

  $(parent).find('div.parallax div.layer').width(w);

  if (w>contwidth) {
    $(parent).find('div.parallax').each(function() {$(this).jparallax({yparallax: false})});
  }

  showGalleryParallax(parent);
  showSingleParallax(pheight);
}

function gallery() {
  $('div.galleryparallax a.thinglink').each(function(i,val) {
    $(this).hover(function(e) {
      image=$(this).find('img');
      $(this).children('span.bignumber').hide();
      $(image).css({zIndex: '10001', top: '0px', left: '-20px'});
      $(image).show();
    },function(){
      $(this).children('span.bignumber').show();
      $(this).find('img').hide();
    });

    $(this).click(function(e) {
      e.preventDefault();

    $('div.singleparallax').remove();
      $('div#thingplace').load($(val).attr('href')+' div.singleparallax',function() {
//          alert($(this).attr('id'));
//          setupProgressBar('div.singleparallax img',true);
          doParallax('div#thingplace');
//          $('a.fullimg').lightbox();
          buttons();
          Cufon.refresh();
      });
    });
  });

  gotothing=$('ul.navnumbers').attr('id');
  if (gotothing && gotothing[0]!=null) {
    thid=gotothing.replace('gotothing-','');
//    alert(gotothing.replace('gotothing-',''));
    $('a#link'+thid).each(function(i,val) {
      $('div.singleparallax').remove();
      $('div#thingplace').load($(val).attr('href')+' div.singleparallax',function() {
          doParallax('div#thingplace');
//          $('a.fullimg').lightbox();
          buttons();
          Cufon.refresh();
      });
    });
  } else {
    $('div.galleryparallax a.thinglink:first').each(function(i,val) {
      $('div.singleparallax').remove();
      $('div#thingplace').load($(val).attr('href')+' div.singleparallax',function() {
          doParallax('div#thingplace');
//          $('a.fullimg').lightbox();
          buttons();
          Cufon.refresh();
      });
    });
  }
}

function setupProgressBar(str,earlyhide) {
  $.owt.imagestring=str;
  $.owt.imgs=$($.owt.imagestring).length;
//  alert($.owt.imgs);

  $('div#progressbar').show();

  $($.owt.imagestring).load(function() {
    $('div#progressbar').width($('div#progressbar').width()+($(window).width()/$.owt.imgs));
  });
}

function hideProgressBar() {
  $('div#progressbar').width($(window).width());
  $('div#progressbar').fadeOut(500, function() {
    $(this).width(0);
  });
}

$(window).load(function() {
  hideProgressBar();

  doParallax('body');
  
  gallery();
});

function buttons() {
  $('div#moreposts a.imgbutton img, div#sidebar a.imgbutton img').hover(function() {
      $(this).animate({width: '90px', height: '90px', paddingLeft: '0px', paddingRight: '0px', paddingTop: '0px', paddingBottom: '0px'},200);
  }, function() {
      $(this).animate({width: '70px', height: '70px', paddingLeft: '10px', paddingRight: '10px', paddingTop: '10px', paddingBottom: '10px'},200);
  });

  $('div.thingtools a.imgbutton img').hover(function() {
      $(this).animate({width: '64px', height: '64px', paddingLeft: '0px', paddingRight: '0px', paddingTop: '0px', paddingBottom: '0px'},200);
  }, function() {
      $(this).animate({width: '44px', height: '44px', paddingLeft: '10px', paddingRight: '10px', paddingTop: '10px', paddingBottom: '10px'},200);
  });
}

$(document).ready(function() {
  $.owt={};

  // no earlyhide, hide on FULL load
  setupProgressBar('div#thing img, div.galleryparallax img',false);

  shorthead('h2.home a',25);
  shorthead('h2.list a',25);
  shorthead('h4.nav a',29);
  Cufon.refresh();

  animateMainMenu();

  iepng();

  hideDescription('#description');
  
//  $('div.singlepost .entry').columnize({width: 306});
  
  $('form#commentform #submit').click(function(e) {
  	e.preventDefault();
    $('#commentform').submit();
  });

  $('div#banners').css('zIndex', 10000);

  $('div#banners').cycle({ 
    fx:     'fade', 
    timeout:  5000,
    speed: 1000
  });

  $('div.bottombag').each(function(){
    if ($(this).height()<16) {
      $(this).height(16);
    }
  });

  buttons();

  ieWarning();  
});
