 $(document).ready(function(){
  
    if( $('.post_share').length > 0 && !($.browser.msie && parseInt($.browser.version) < 7) ) {
        var descripY = parseInt($('.post.narrow').offset().top) - 20;
        var $postShare = $('.post_share');
        var pullX = $postShare.css('margin-left');
        
        $(window).data('scrollBound', false);
        
        function positionShareScroll() {
            var scrollY = $(window).scrollTop();
            var fixedShare = $postShare.css('position') == 'fixed';
            if ( scrollY > descripY && !fixedShare ) {
                $postShare.stop().css({
                    position: 'fixed',
                    left: '50%',
                    top: 20,
                    marginLeft: -566
                });
            } else if ( scrollY < descripY && fixedShare ) {
                $postShare.css({
                    position: 'relative',
                    left: 0,
                    top: 0,
                    marginLeft: pullX
                });
            }
        }
        
        
        $(window).resize(function(){
            var windowW = $(window).width();
            var pulledOutside = $postShare.css('margin-left') == pullX;
            if ( windowW >= 1140 ) {
                if ( !$(window).data('scrollBound') ) {
                    if ( !pulledOutside ) {
                        $postShare.animate({ marginLeft: pullX });
                    }
                    $(window)
                        .data('scrollBound', true)
                        .bind('scroll.positionShare', function(){
                            positionShareScroll()
                        })
                    ;
                    positionShareScroll();
                }
            } else {
                if ( pulledOutside || $(window).data('scrollBound') ) {
                    $postShare
                        .css({ position: 'relative', left: 0, top: 0 })
                        .animate({marginLeft: 0})
                    ;
                    $(window)
                        .data('scrollBound', false)
                        .unbind('scroll.positionShare')
                    ;
                }
            }
        });
        $(window).resize();
    }
    
    // Inits
    initExternalLinks();
    initTagLinks();
    initVisibleMeasures();
    initPageRefresh();
/*    initFixFlash();*/


$(window).scroll(function () { positionShareScroll();
    });

  });
  
  
  
  
(jQuery);
/* -------- Function Definitions -------- */
function initPageRefresh(){
  jQuery('body.home, body.page-template-page-channel-php, body.page-template-page-channel-sub-php').each(function(){
    window.setTimeout('window.location.reload()', 900000);
  });
}
function initExternalLinks(){
  jQuery('a[rel*=external]').each(function(){
    jQuery(this).attr('target', '_blank');
  });
}
function initTagLinks(){
  jQuery('a.nav-link').each(function(){
    jQuery(this).click(function(){LOTCC.bcpw("int",this.innerHTML);});
  });
  jQuery('.post_share a, .post_share iframe').not('#st_sharethis, #st_email').each(function(){
    var lot_cat = jQuery(this).parents('.post_share').attr('class');
    lot_cat = lot_cat.replace('post_share ', '');
    jQuery(this).click(function(){LOTCC.bcpw("int",lot_cat);})
  });
}
function dropdownNavLinks(){
  var initialp = $('#secondary_nav ul.nav li:first').position();
  $('#secondary_nav ul.nav li').each(function(initialp){
    var p = $(this).position();
    if(p.top > initialp.top){
      $(this).addClass('extra');
    }
  });
  $('#secondary_nav ul.nav li.extra:first').each(function(){
    $(this).before('<li><a href=#>More...</a><ul class="extra-menu dropdown"></ul><li>');
    $('#secondary_nav ul.nav li.extra').each(function(){
      $(this).parent().find('.extra-menu').append(this).end().end().remove();
    });
  });
}
function initFixFlash(){
  embed = '';
  holder = '';
  flash = '';
  
  jQuery('object').each(function(){
    jQuery(this).append('<param value="transparent" name="wmode"/>');
    embed = jQuery(this).find('embed');
    if (embed != undefined) {
      jQuery(this).attr('data', embed.attr('src'));
      jQuery(this).attr('type', embed.attr('src'));
      embed.remove();
      holder = jQuery(this).parent();
      flash = jQuery(this);
      jQuery(this).remove();
      holder.append(flash);
    }
  });
}
// ====================
// = Visible Measures =
// ====================
var popupStatus = 0;
//loading popup with jQuery magic!
function loadPopup(){
  //loads popup only if it is disabled
  if(popupStatus==0){
    /*jQuery("#backgroundPopup").css({
      "opacity": "0.7"
    });*/
    jQuery("#backgroundPopup").fadeIn("slow");
    jQuery("#popupBody").fadeIn("slow");
    popupStatus = 1;
  }
}
//disabling popup with jQuery magic!
function disablePopup(){
  //disables popup only if it is enabled
  if(popupStatus==1){
    jQuery("#backgroundPopup").fadeOut("slow");
    jQuery("#popupBody").fadeOut("slow");
    popupStatus = 0;
  }
}
//centering popup
function centerPopup(){
  //request data for centering
  var windowWidth = document.documentElement.clientWidth;
  var windowHeight = document.documentElement.clientHeight;
  var popupHeight = jQuery("#popupBody").height();
  var popupWidth = jQuery("#popupBody").width();
  //centering
  //only need force for IE6
  jQuery("#backgroundPopup").css({
    "height": windowHeight
  });
}
//CONTROLLING EVENTS IN jQuery
function initVisibleMeasures(){
  if (jQuery('#visible-measures') !== undefined){
    jQuery('body').append("<div id='backgroundPopup'> </div><div id='popupBody' style='display:none;'><div id='video'></div><a id='popupClose' href='#'>CLOSE</a></div>");
    //LOADING POPUP
    //Click the button event!
    jQuery(".preview a").click(function(){
      jQuery('#video').remove();
      
      jQuery('#popupBody').append('<div id="video"><iframe width="640" height="385" style="border:0;" src="' + jQuery(this).attr('href') + '"></div>');
      
      //centering with css
      centerPopup();
      //load popup
      loadPopup();
      return false;
    });
    //CLOSING POPUP
    //Click the x event!
    jQuery("#popupClose").click(function(){
      disablePopup();
      return false;
    });
    //Click out event!
    jQuery("#backgroundPopup").click(function(){
      disablePopup();
    });
    //Press Escape event!
    jQuery(document).keypress(function(e){
      if(e.keyCode==27 && popupStatus==1){
        disablePopup();
      }
    });
  }
};
