jQuery(document).ready(function() {

  jQuery('.fb-header .fb-nav li').hover(function() {
    jQuery(this).addClass('active');
    jQuery(this).children('ul').show();
  }, function() {
    jQuery(this).removeClass('active');
    jQuery(this).children('ul').hide();
  });

  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;
  });

  //reply comments button
  if (jQuery('#comments-reply-box').length>=1) {
    comment_form = '<div class="comment-reply-form-box">'+jQuery('#comments-reply-box').html()+'</div>';
    jQuery('#comments-reply-box').remove();
    jQuery('.comments-left .discussions .buttons .comment-reply').click(function(e){
      showReplyForm(this);
      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('#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');
  }
  var bcSplit = pageVars.breadcrumb.split('/');
  jQuery.ad.init({channel:pageVars.breadcrumb, ptype:pageVars.page_type, ctype:pageVars.channel_type, pageid:pageVars.page_id, categories:pageVars.categories, raw:{channel: bcSplit[0], section: bcSplit[1], subsection1: bcSplit[2], subsection2: '', subsection3: '', subsection4: ''}}, 'server');

});

function showReplyForm(obj) {
  var buttonDiv = jQuery(obj).parent('div.buttons');
  var nameDiv = jQuery(buttonDiv).parent('.date-buttons').prev().prev('.name');
  var arrowDiv = jQuery(nameDiv).prev('div.arrow');
  var targetSpace = nameDiv.parent().next('br.clear');
  var linkHash = obj.hash.replace('#','');
  var values = linkHash.split('_');

  jQuery(buttonDiv).hide();
  //jQuery(nameDiv).addClass('replying');
  //jQuery(arrowDiv).addClass('arrow-replying');
  jQuery(targetSpace).after(comment_form);

  var formBox = jQuery(targetSpace).next('.comment-reply-form-box').children('.replyformTop');
  jQuery(formBox).attr('class', 'replyformTop '+values[1]+'-reply');
  var comment_parent = jQuery(targetSpace).next('.comment-reply-form-box').children('.replyformTop').children('.leave-comments-form').children('input[name="comment_parent"]');
  jQuery(comment_parent).val(values[0]);
  jQuery('.comment-reply-form-box').show();
  limitChars('textarea.leave-com-mid');
}

function hideReplyForm(t) {
  var replyDiv = jQuery(t).parent('div.submit-cancel').parent('.leave-comments-form').parent('.replyformTop').parent('.comment-reply-form-box');
  var commentReply = jQuery(replyDiv).prev('br.clear').prev('.comment-box').children('.name');
  var arrowReply = jQuery(commentReply).prev('.arrow');
  var replyBtn = jQuery(commentReply).children('.buttons').show();
  commentReply.removeClass('replying');
  arrowReply.removeClass('arrow-replying');

  replyDiv.next('br.clear').remove();
  replyDiv.remove();
}

function submitComment(obj,divID) {
  var targetForm = jQuery(obj).parent().parent();
  if (divID =='#start-discussion') {
    var replyDiv = jQuery(targetForm).parent('#start-discussion');
    var thankyouDiv = '#start-discussion';
  }else {
    var replyDiv = jQuery(targetForm).parent('.replyformTop');
    var thankyouDiv = '.comment-reply-form-box';
  }

  var parentID = jQuery(targetForm).children('#comment_parent').val();
  var replyID = 'currentReply-'+parentID;
    jQuery(targetForm).addClass(replyID);
  var errorDiv = jQuery('form.'+replyID).prev().prev('div.messages');
  var options = {
    dataType: 'json',
    method: 'POST',
    success: function(data) {
      if (data.success==false) {
        //reset the colors first
        jQuery('form.'+replyID+' input, form.'+replyID+' textarea').removeClass('error');
        var errorMsg ='';
        jQuery.each(data.errors, function(i,val) {
          jQuery('form.'+replyID+' input[name="'+i+'"], form.'+replyID+' textarea[name="'+i+'"]').addClass('error');
          errorMsg += '<p>'+val+'</p>';
        });
        jQuery(errorDiv).html(errorMsg);
        jQuery(errorDiv).show();
      }else if (data.success ==true) {
        jQuery(replyDiv).removeClass('replyformTop');
        var replyClass = jQuery(replyDiv).attr('class') ? jQuery(replyDiv).attr('class') : 'first-level-reply' ;

        var successMsg = '';
          successMsg +='<div class="'+replyClass+'">';
            successMsg +='<div class="arrow"></div>	';
               successMsg +='<h3>Thank You!</h3>';
                successMsg +='<p>Your comment has been submitted and should appear shortly.</p>';
          successMsg +='</div>';

        jQuery(replyDiv).slideUp(800,function(){
          jQuery(thankyouDiv).html(successMsg);
          if (divID=='#start-discussion') {
            jQuery(thankyouDiv).addClass('comments-left');
            jQuery(thankyouDiv).css('border','none');
          }
          jQuery(thankyouDiv).show();
        });
      }
    }
  };
  jQuery('.'+replyID).ajaxSubmit(options);
}
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;
  });
}

function expandReplies(obj,cid,cssClass,comment_status) {
  var values = {
      action: 'expand-comments',
      cid: cid,
      level:cssClass,
      comment_status: comment_status
  };
  postUrl = 'http://'+window.location.host+'/wp-content/plugins/liveshot_comments/liveshot_comments.php';
  targetOutput = jQuery(obj).parents('.discussions').next('.children');
  exp_col = jQuery(obj).parent('.exp-col');
  if( jQuery(targetOutput).hasClass('hasContent')==false){
    jQuery.get(postUrl,values,
      function(data) {
        if(data.success==true){
          if (targetOutput.length==0) {
            targetOutput = jQuery(obj).parents('.discussions');
            jQuery(targetOutput).after('<div class="children hasContent">'+data.output+'</div>');
            slideDownDiv = jQuery(obj).parents('.discussions').next('.children');
            jQuery(slideDownDiv).slideDown('normal');
            jQuery(exp_col).children('a.expand').hide();
            jQuery(exp_col).append('<a class="collapse" href="#" style="display:block;" onclick="collapseReplies(this);return false;"><img src="/wp-content/themes/liveshot/images/collapse.jpg" /></a>');
          }else {
            jQuery(targetOutput).addClass('hasContent');
            jQuery(targetOutput).replaceWith('<div class="children hasContent">'+data.output+'</div>');
            jQuery(targetOutput).slideDown('normal');
            jQuery(exp_col).children('a.expand').hide();
            jQuery(exp_col).append('<a class="collapse" href="#" style="display:block;" onclick="collapseReplies(this);return false;"><img src="/wp-content/themes/liveshot/images/collapse.jpg" /></a>');
          }
        }
      }
    ,'json');
  }else{
    jQuery(targetOutput).slideDown('normal');
    jQuery(exp_col).children('a.expand').hide();
    jQuery(exp_col).children('a.collapse').show();
  }
  return false;
}

function collapseReplies(obj) {
  collapseTarget = jQuery(obj).parents('.discussions').next('.children');
  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();
}

function limitChars(element) {
  var limit = FOX.settings.comments_chars;
  jQuery(element).keyup(function(){
      // get the value of the textarea
      var strs = this.value;
      // count its length
      var count = strs.length;

      // if it is over the limit, display the message
      if(count > limit) {
        jQuery(this).val(strs.substring(0,limit));
        alert("You can only type in " + limit  +  " characters ");
        return false;
      }

      //update the display div with the amount of characters used
      var the_text = "Characters used : "+ count + "/" + limit;
      var textDiv = jQuery(this).parent().children('.characters-used');
      jQuery(textDiv).html(the_text);
  });
}
