jQuery(document).ready(function() {

  if (jQuery('#ls-share').length>=1) {
    if (typeof SHARETHIS !='undefined') {
      var lsShare = SHARETHIS.addEntry(
          {title:'SHARE'},
          {button:false}
        );
      var shareBtn = jQuery('#ls-share');
      lsShare.attachButton(shareBtn[0]);
    }
  }
  jQuery('a#start-discussion-link, a.post-comment').click(function(){
    jQuery.scrollTo( '#start-discussion', 800 );
    return false;
  });

  //slider
  if (jQuery('.photogallery .gal-img').length>=1) {
    var mainSlider = jQuery('.photogallery .gal-img').slider({
      callback : mainSlider_cb,
      scroll: 1,
      visible:1,
      li:605
    });
  }

  if (jQuery('#featured-img li').length>=1) {
    var rrSlider = jQuery('#featured-img').slider({
      callback : rrSlider_cb,
      scroll: 1,
      visible:1,
      li:336
    });
  }

  jQuery('.go-to-comments').click(function(){
    if (jQuery('#late-new').length<=0) {
      commentsDiv = '#start-discussion';
    }else {
      commentsDiv = '#late-new';
    }
    jQuery.scrollTo( commentsDiv, 800 );
  });

  var action = jQuery.query.get('action');
  if (action!='') {
    if (jQuery('#'+action).length <=0) {
      scrollDiv = '#start-discussion';
    }else {
      scrollDiv = '#'+action;
    }
    jQuery.scrollTo(scrollDiv, 800 );
  }

  jQuery('.goTo-single').click(function(){
    goTourl = jQuery(this).attr('single_href');
    window.top.location=goTourl;
    return false;
  });
  jQuery('#browse-posts-content li a').click(function(){
    jQuery('#browse-posts-content li a').removeClass('active');
    jQuery(this).addClass('active');
    jQuery('#browse-content div').hide();

    //show content
    contentLi = jQuery(this).parent('li');
    contentID = jQuery(contentLi).attr('class');
    jQuery('#browse-content div.'+contentID).show();
    return false;
  });
  jQuery('#most-pop-content li a').click(function(){
    jQuery('#most-pop-content li a').removeClass('active');
    jQuery(this).addClass('active');
    jQuery('#pop-content ul').hide();

    //show content
    contentLi = jQuery(this).parent('li');
    contentID = jQuery(contentLi).attr('class')+'-content';
    jQuery('#pop-content ul#'+contentID).show();
    return false;
  });

  jQuery('ul#what-im-reading li a.pane').click(function(){
    jQuery('ul#what-im-reading li a.pane').removeClass('active');
    jQuery(this).addClass('active');
    jQuery('#read-content ul').hide();
    jQuery('#read-content ul').parent('.jScrollPaneContainer').hide();

    //show content
    contentLi = jQuery(this).parent('li');
    contentID = jQuery(contentLi).attr('class');
    jQuery('#read-content ul.'+contentID).show();
    jQuery('#read-content ul.'+contentID).parent('.jScrollPaneContainer').show();
    return false;
  });

  if (jQuery('#sendnewstips span').length>=1) {
  jQuery('#sendnewstips span').click(function(){
    jQuery('#sendnewstips span').removeClass();
      if (jQuery('#sendnewstips dl').css('display') == "none") {
        jQuery('#sendnewstips dl').show();
        jQuery('#sendnewstips span').addClass('toggleup');
      } else {
        jQuery('#sendnewstips dl').hide();
        jQuery('#sendnewstips span').addClass('toggledown');
      }
      return false;
    });
  }

  if (jQuery('#tweets').length > 0 && jQuery('#twitter_id').val() != '') {
  var url = 'http://twitter.com/status/user_timeline/'+jQuery('#twitter_id').val()+'.json?count=3&callback=?';
  jQuery.getJSON(url,
   function(data){
    var weekday = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
    var monthname = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
    var html = '';
    jQuery.each(data, function(i, item) {
    var values = item.created_at.split(" ");
    time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
    var post_date = new Date(Date.parse(time_value));
    post_time = (post_date.getHours() > 12) ? (post_date.getHours()%12)+'PM' : post_date.getHours()+'AM';
      html += '<div class="tweet"><div class="arrow"></div><div class="container"><p class="name">'+ item.user["name"]+'<br/><span class="time-date">'
          +post_time+' '+weekday[post_date.getDay()]+', '+monthname[post_date.getMonth()]+' '+post_date.getDate()+', '+post_date.getFullYear()+
          '</span></p><p>'
          +item.text+
          '</p></div></div>';
       });
    jQuery("#tweets ul li").html(html);
    jQuery('#twitter_module').show();
   });
  }

  //if (jQuery('#contributors-list').length>=1) {
    jQuery('#contributors-list li a').hover(
      //onMouseover:
      function(e){
        var nameText = jQuery(this).html();
        var imageLink = jQuery(this).attr("alt");
        jQuery('#contributor-popup > p.name').html('<a href="'+this.href+'">'+nameText+'</a>');
        jQuery('#contributor-popup > a').attr('href',this.href);
        jQuery('#contributor-popup .viewposts a').attr('href',this.href);
        //forces null source when alt attribute
        if(imageLink != ""){
          jQuery('#popup-image').attr("src",jQuery(this).attr("alt"));
        }
        else{
            jQuery('#popup-image').parent('a').remove();
        }
        // image not shown  when no source.
        if(jQuery('#popup-image').attr("src") == ""){
           jQuery('#popup-image').css("display","none");
        }
        else{
          jQuery('#popup-image').css("display","block");
        }
        jQuery("#contributor-popup").appendTo(jQuery(this).parent());
        jQuery('#contributor-popup').css({
          'display' : 'block',
          'position' : 'absolute',
          'left' : '-156px',
          'top' : '-106px',
          'visibility': 'visible'
        });
        nameText = jQuery(this).html().toString();
      },
      //onMouseOut:
      function() {
        //slide = jQuery(this).attr('slideId');
      }
    );
    jQuery('#contributor-popup , .contributors-1 , .contributors-2').bind('mouseleave', function(){
      jQuery('#contributor-popup').css('display','none');
      jQuery('#popup-image').attr("src",jQuery(this).attr("src",""));
    });
  //} //end if(jQuery('#contributors-list').length>=1)

  fontResizer('.articles .the_content p','11px','12px','14px');
  jQuery('#read-content ul').jScrollPane({showArrows:true, scrollbarWidth: 23, arrowSize: 23, dragMaxHeight:0});
  jQuery('#read-content ul.read-papers').parents('.jScrollPaneContainer').show();
  jQuery('#read-content ul.hide').parents('.jScrollPaneContainer').hide();

  if (jQuery('form#discussion-form').length==1) {
    limitChars('textarea.leave-com-mid');
  }

});

function collapseReplies(obj) {
  collapseTarget = jQuery(obj).parents('.discussions').next('.children');
  parentTarget = jQuery(obj).parents('.discussions');
  siblings = jQuery(obj).parents('.discussions').parents('.comments-left').children('.discussions');
  exp_col = jQuery(obj).parent('.exp-col');
  jQuery(collapseTarget).slideUp('normal');
  jQuery(exp_col).children('a.collapse').hide();
  jQuery(exp_col).children('a.expand').show();
  if (navigator.appName == 'Microsoft Internet Explorer') {
      var ua = navigator.userAgent;
      var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
      if (re.exec(ua) != null)
          rv = parseFloat(RegExp.$1);
      if (rv >= 8) {
        for(var i = 0; i < siblings.length && jQuery(siblings[i]).attr('id') != parentTarget.attr('id'); i++)
           ;
          if (jQuery(siblings[i+1]).length>=1) {
            jQuery(siblings[i+1]).css('padding-top','20px');
          }
      }
  }
}

function displayMessage(obj,msg,is_success_msg)
{
  if (is_success_msg) {
    jQuery('#'+obj).removeClass();
    jQuery('#'+obj).addClass('successformat');
  }
  jQuery('#'+obj).css('display','block');
  jQuery('#'+obj).html(msg);
}

function mainSlider_cb(obj) {
  jQuery('.photogallery .gal-img li').css('display','block');
  //get items height and set all of the items to have same height
  //to avoid extra white spaces
  var itemsHeight = jQuery('.gal-img li:eq(0) img').height();
  jQuery('.gal-img li').height( (itemsHeight+6));

  jQuery('.gal-img-controls #prev a').click(function() {
    obj.slidePrev();
    if (obj.opts.slideLeft) {
      //make next active
      if (obj.opts.index < (obj.totalPage)-1) {
         if (jQuery('.gal-img-controls #next a.disabled-btn').length >=1) {
           jQuery('.gal-img-controls #next a.disabled-btn').removeClass('disabled-btn');
         }
      }
      //make prev inactive
      if (obj.opts.index <=0) {
        jQuery('.gal-img-controls #prev a').addClass('disabled-btn');
      }
      //update description and image index
      var imgDescr = jQuery('.gal-img li:eq('+obj.opts.index+') img').attr('alt');
      jQuery('.gal-img-controls .caption').html(imgDescr);
      jQuery('.gal-img-controls .slide-number').html('Image '+(obj.opts.index+1)+' of '+obj.totalPage);

      //get items height and set all of the items to have same height
      //to avoid extra white spaces
      var itemsHeight = jQuery('.gal-img li:eq('+obj.opts.index+') img').height();
      jQuery('.gal-img li').height( (itemsHeight+6));
    }

    return false;
  });
  jQuery('.gal-img-controls #next a').click(function() {
    obj.slideNext();
    if (obj.opts.slideRight) {
       //make prev active
      if (obj.opts.index >0) {
        if (jQuery('.gal-img-controls #prev a.disabled-btn').length >=1) {
           jQuery('.gal-img-controls #prev a.disabled-btn').removeClass('disabled-btn');
         }
      }
      //make next inactive
      if (obj.opts.index >= (obj.totalPage)-1) {
        jQuery('.gal-img-controls #next a').addClass('disabled-btn');
      }
      //update description and image index
      var imgDescr = jQuery('.gal-img li:eq('+obj.opts.index+') img').attr('alt');
      jQuery('.gal-img-controls .caption').html(imgDescr);
      jQuery('.gal-img-controls .slide-number').html('Image '+(obj.opts.index+1)+' of '+obj.totalPage);

      //get items height and set all of the items to have same height
      //to avoid extra white spaces
      var itemsHeight = jQuery('.gal-img li:eq('+obj.opts.index+') img').height();
      jQuery('.gal-img li').height( (itemsHeight+6));
    }
    return false;
  });
}

function rrSlider_cb(obj) {
  jQuery('#featured-img li').css('display','block');

  // set play-slideshow image display = "block" when hovering over slideshow image
  jQuery('li.slideshow-image').hover(
    function () {
      jQuery('a.view-slideshow').css('display','block');
    },
    function () {
      jQuery('a.view-slideshow').css('display','none');
    });
  jQuery('a.view-slideshow').hover(
    function () {
      jQuery('a.view-slideshow').css('display','block');
    },
    function () {
      jQuery('a.view-slideshow').css('display','none');
    });

  //update first rr descr
  var firstDescr = jQuery('#featured-img li:eq(0) img').attr('alt');
  var firstLink = jQuery('#featured-img li:eq('+obj.opts.index+') img').attr('link');
  jQuery('#featured-img-controls .caption').html('<a href="'+firstLink+'">'+firstDescr+'</a>');
  jQuery('#featured-img-controls .slide-number').html('Slideshow 1 of '+obj.totalPage);

  //get items height and set all of the items to have same height
  //to avoid extra white spaces
  var itemsHeight = jQuery('#featured-img li:eq(0) img').height();
  jQuery('#featured-img li').height( (itemsHeight+6));

  jQuery('#featured-img-controls #prev a').click(function() {
    obj.slidePrev();
    if (obj.opts.slideLeft) {
      //make next active
      if (obj.opts.index < (obj.totalPage)-1) {
         if (jQuery('#featured-img-controls #next a.disabled-btn').length >=1) {
           jQuery('#featured-img-controls #next a.disabled-btn').removeClass('disabled-btn');
         }
      }
      //make prev inactive
      if (obj.opts.index <=0) {
        jQuery('#featured-img-controls #prev a').addClass('disabled-btn');
      }
      //update description and image index
      var imgDescr = jQuery('#featured-img li:eq('+obj.opts.index+') img').attr('alt');
      var imgLink = jQuery('#featured-img li:eq('+obj.opts.index+') img').attr('link');
      jQuery('#featured-img-controls .caption').html('<a href="'+imgLink+'">'+imgDescr+'</a>');
      jQuery('#featured-img-controls .slide-number').html('Slideshow '+(obj.opts.index+1)+' of '+obj.totalPage);

      //get items height and set all of the items to have same height
      //to avoid extra white spaces
      var itemsHeight = jQuery('#featured-img li:eq('+obj.opts.index+') img').height();
      jQuery('#featured-img li').height( (itemsHeight+6));
    }

    return false;
  });
  jQuery('#featured-img-controls #next a').click(function() {
    obj.slideNext();
    if (obj.opts.slideRight) {
       //make prev active
      if (obj.opts.index >0) {
        if (jQuery('#featured-img-controls #prev a.disabled-btn').length >=1) {
           jQuery('#featured-img-controls #prev a.disabled-btn').removeClass('disabled-btn');
         }
      }
      //make next inactive
      if (obj.opts.index >= (obj.totalPage)-1) {
        jQuery('#featured-img-controls #next a').addClass('disabled-btn');
      }
      //update description and image index
      var imgDescr = jQuery('#featured-img li:eq('+obj.opts.index+') img').attr('alt');
      var imgLink = jQuery('#featured-img li:eq('+obj.opts.index+') img').attr('link');
      jQuery('#featured-img-controls .caption').html('<a href="'+imgLink+'">'+imgDescr+'</a>');
      jQuery('#featured-img-controls .slide-number').html('Slideshow '+(obj.opts.index+1)+' of '+obj.totalPage);

      //get items height and set all of the items to have same height
      //to avoid extra white spaces
      var itemsHeight = jQuery('#featured-img li:eq('+obj.opts.index+') img').height();
      jQuery('#featured-img li').height( (itemsHeight+6));
    }
    return false;
  });
}
