var cstart = 1; // start with this comment
var climit = 20;  // show this many comments
var artListCntr = 0; // used in left side headlines box
var available_weeks = [];
var comments_clicked = 0;

$(document).ready(function(){
  showComments();


  /* ------------------ left headlines list ------------------ */
  $('div#headlines-nav span.nav-arrows img#down').show();
  $('div#headlines-nav span.nav-arrows img#up').click(function(){ pageHeadlines('up'); });
  $('div#headlines-nav span.nav-arrows img#down').click( function() { pageHeadlines('down'); });

  /* ------------------ right dvdclinic reviews box -------------------- */
  $('div#dvd-clinic-review span.nav-arrows img.next').show();
  $('div#dvd-clinic-review span.nav-arrows img.next').click(function(){ pageRtBox('next', $(this)); });
  $('div#dvd-clinic-review span.nav-arrows img.prev').click(function(){ pageRtBox('prev', $(this)); });

  /* ------------------ right latest reviews box -------------------- */
  $('div#latest-review span.nav-arrows img.next').show();
  $('div#latest-review span.nav-arrows img.next').click(function(){ pageRtBox('next', $(this)); });
  $('div#latest-review span.nav-arrows img.prev').click(function(){ pageRtBox('prev', $(this)); });

  /* ------------------ right latest trailer box -------------------- */
  $('div#latest-trailers-videos span.nav-arrows img.next').show();
  $('div#latest-trailers-videos span.nav-arrows img.next').click(function(){ pageRtBox('next', $(this)); });
  $('div#latest-trailers-videos span.nav-arrows img.prev').click(function(){ pageRtBox('prev', $(this)); });

  /* ------------------ right latest columns box -------------------- */
  $('div#latest-columns span.nav-arrows img.next').show();
  $('div#latest-columns span.nav-arrows img.next').click(function(){ pageRtBox('next', $(this)); });
  $('div#latest-columns span.nav-arrows img.prev').click(function(){ pageRtBox('prev', $(this)); });

  /* ------------------ right latest podcasts box -------------------- */
  $('div#latest-podcast-shows span.nav-arrows img.next').show();
  $('div#latest-podcast-shows span.nav-arrows img.next').click(function(){ pageRtBox('next', $(this)); });
  $('div#latest-podcast-shows span.nav-arrows img.prev').click(function(){ pageRtBox('prev', $(this)); });

  /* ------------------ arrow dvd reviews ---------------------------- */
  if ($('div#dvd-reviews').length == 1) { 
    $('div#dvd-reviews span.nav-arrows img.next').show();
    $('div#dvd-reviews span.nav-arrows img.next').click(function(){ pageRtBox('next', $(this)); });
    $('div#dvd-reviews span.nav-arrows img.prev').click(function(){ pageRtBox('prev', $(this)); });
  }


  /* ------------------ most popular ------------------ */
  $('div#most-popular div.category a.week').click(function() { 
    if (!$(this).hasClass('active')) { 
      $('div#most-popular div.category a').removeClass('active');
      $(this).addClass('active'); 
      $('div#most-popular table.headlines').hide();
      $('div#most-popular table.seven-day').show('fast');
    }
    return false;
  });
  $('div#most-popular div.category a.today').click(function() { 
    if (!$(this).hasClass('active')) { 
      $('div#most-popular div.category a').removeClass('active');
      $(this).addClass('active'); 
      $('div#most-popular table.headlines').hide();
      $('div#most-popular table.one-day').show('fast');
    }
    return false;
  });
  $('div#most-popular div.category a.month').click(function() { 
    if (!$(this).hasClass('active')) { 
      $('div#most-popular div.category a').removeClass('active');
      $(this).addClass('active'); 
      $('div#most-popular table.headlines').hide();
      $('div#most-popular table.thirty-day').show('fast');
    }
    return false;
  });


  /* ---------------- news archives -------------------------- */
  if ($('div.archives').length == 1) { 
    // load with first week visible, hide/show others as needed
    $('div.archives table.headlines').hide();


    var cntr = 0;
    for (i = 52; i >= 0; i--) { 
      
      var tmp = "div.archives table.week";
      if (i < 10) { tmp += "0"; } 
      tmp += i;

      if ($(tmp).length > 0) {
	available_weeks[cntr] = tmp;
	cntr++;
      }
    }
    showWeek(0,available_weeks); // show the first week of stories

  }


  
  /* ------------------ comments ----------------------------- */
  //showComments();
  $("div#latest_members_next").show();
  $("div.comment .show_full_comment").click(function() { 
    $(this).hide();
    var parent = $(this).parent().parent().parent().parent().get();
    var test = $(parent).find("div.comment_excerpt");
    if ($(test).html() == null) { parent = $(parent).parent(); }
    $(parent).find("div.comment_excerpt").hide();
    $(parent).find("div.comment_full").show();
    return false;

  });
  $("div.comment img.reply_to_post").click(function() {
    $(this).hide();
    $("div.comment div.reply_form").hide();
    var parent = $(this).parent().parent().get()
    //var parent = $(this).parent().parent().parent().parent().parent().get();
    $(parent).find("div.reply_form").show();
  });
  $("div.comment div.reply_form input.reply_submit").click(function() { 
    submitReply(this);
  });
  $("div.comment div.reply_count").click(function() { 
    var cr = $(this).parent().find("div.comment_replies");
    if ($(cr).css("display") == 'none') { 
      $(cr).slideDown('fast');
      $(this).find("img.arrow").attr({ src: "images_talkback/arrow-down.png" });
      $(this).find("img.expand_button").attr({ src: "images_talkback/contract.png" });
    } else { 
      $(cr).slideUp('fast');
      $(this).find("img.arrow").attr({ src: "images_talkback/arrow-right.png" });
      $(this).find("img.expand_button").attr({ src: "images_talkback/expand.png" });
    }
  });
  $("div#page-nav #more").click(function(){ 
    cstart = cstart + climit;
    //$("div.comment").hide();
    comments_clicked = 1;
    showComments();
  });
  $("div#page-nav #back").click(function(){ 
    cstart = cstart - climit;
    //$("div.comment").hide();
    comments_clicked = 1;
    showComments();
  });

  loadPodcasts();

});

function showComments() { 
  $('div.comment').hide();
  var cfilter = "";
  for (i=cstart;i<cstart+climit;i++) { cfilter = cfilter + ".commentnum" + i + ", "; }
  var totalComments = $("div.comment").length;
  $("div.comment").filter(cfilter).show();

  if (cstart >= climit) { $("div#page-nav #back").show(); }
  if (cstart < climit) { $("div#page-nav #back").hide(); }
  if (cstart + climit <= $("div.comment").length) {
    $("div#page-nav #more").show();
  }

  if (cstart + climit > $("div.comment").length) { 
    tmp = cstart + '-' + $("div.comment").length;
    $("div#page-nav #more").hide();
  }

  // bottom "page" numbers
  var tmp = "";
  for (i = 0; i <= totalComments; i++) { 
    if (i % climit == 0) { 
      tmp += "<a href=\"javascript:cstart=" + i + "; showComments();\"";
      if ((cstart == i + 1) || (cstart == i)) { tmp += "class=\"sel\" "; }
      tmp += ">" + (i / climit + 1) + "</a> ";
    }
  }
  $('div#page-nav span.pages').html(tmp);

  if (comments_clicked == 1) { 
    tmp = "div.commentnum" + cstart;
    y = $(tmp).offset();
    window.scrollTo(0,y.top);
  }
}

function voteComment(obj,id,dir,site) {
  var parent = $(obj).parent().get();

  if ($(parent).find("img.voteup").attr("src") == "/images_talkback/thumb-up-pressed.png") { 
    alert('You\'ve already voted on this comment!');
  } else { 
    //parent = $(parent).parent();
    $(parent).find(".comment-num-box").html('<img src="/images_talkback/waiting.gif" alt="" border="0" />');
    $.get("http://www.joblo.com/tb_vote.php", { talk_id: id, type: dir, site: site }, 
      function(data) { 
	if (data >= 0) { data = "+" + data; }
	$(parent).find(".comment-num-box").html(data);
      });
    $(parent).find("img.voteup").attr({ src: "/images_talkback/thumb-up-pressed.png"});
    $(parent).find("img.votedown").attr({ src: "/images_talkback/thumb-down-pressed.png"});
  }
}

function submitReply(obj) { 
  var parent = $(obj).parent().get();
  var talk_id = $(parent).find("input.talk_id").val();
  var user_id = $(parent).find("input.user_id").val();
  var news_id = $(parent).find("input.news_id").val();
  var site = $(parent).find("input.site").val();
  var comment = $(parent).find("textarea.comment_reply").val();
  $(parent).html('<p><img src="/images_talkback/waiting.gif" alt="" border="0" /> Submitting.... </p>');
  $.post("/tb_reply.php", { talk_id: talk_id, user_id: user_id, news_id: news_id, comment: comment, site: site },
    function(data) { 
      if (data == '') { 
	alert('Comment submit failed');
      } else {
	alert(data);
	window.location.reload();
      }
  });
}

function expandComments() { 
  if ($("div.comment div.comment_full").css("display") == 'none') { 
    $("div.comment div.comment_excerpt").hide();
    $("div.comment div.comment_full").show();
    $("div.comment .show_full_comment").hide();
    $("a.expand_link").html('Contract All');
  } else { 
    $("div.comment div.comment_full").hide();
    $("div.comment div.comment_excerpt").show();
    $("div.comment .show_full_comment").show();
    $("a.expand_link").html('Expand All');
  }
}

/* ------------------------------------------------------- */
function pageHeadlines(dir) { 
  /* --- scrolls the left side headline list --- */
  var step = 800;
  var spd = 700;
  var movingObject = $('div#headlines-content table.headlines');
 
 
  if (dir == 'down')  {
    y = $(movingObject).css('marginTop').replace(/px/g, '') - step;
    $('div#headlines-nav span.nav-arrows img#up').show();
  } else { 
    y = $(movingObject).css('marginTop').replace(/px/g, '') * 1 + step;
    $('div#headlines-nav span.nav-arrows img#down').show();
  }
  $(movingObject).animate({marginTop: y}, spd);

  if (y == 0) { $('div#headlines-nav span.nav-arrows img#up').hide(); }

  var t = $(movingObject).css('height').replace(/px/g, '') * 1 + y;
  if (t <= step) { 
    $('div#headlines-nav span.nav-arrows img#down').hide();
  }

}


/* ------------------------------------------------------- */
function pageRtBox(dir, obj) { 
  var step = 269;
  var spd = 700;
  var parentDiv = $(obj).parent().parent().parent();
  var movingObject = $(parentDiv).find('ul.slideshow');
  if ($(parentDiv).attr('id') == 'latest-trailers-videos') { 
    movingObject = $(parentDiv).find('div.container');
    step = 287;
  } 
  else if  ($(parentDiv).attr('id') == 'latest-columns') { 
    step = 274;
  }
  else if  ($(parentDiv).attr('id') == 'latest-podcast-shows') { 
    step = 270;
  }

  var x = 0;

  if (dir == 'next') { 
    x = $(movingObject).css('marginLeft').replace(/px/g, '') - step;
    $(parentDiv).find('img.prev').show();
  } else { 
    x = $(movingObject).css('marginLeft').replace(/px/g, '') * 1 + step;
    $(parentDiv).find('img.next').show();
  }


  $(movingObject).animate({marginLeft: x}, spd);

  if (x == 0) { $(parentDiv).find('img.prev').hide(); }
  if (x == step * -4) { $(parentDiv).find('img.next').hide(); }

}


/* ------------------------------------------------------- */
function loadPodcasts() {
  // process the podcast feeds to build a list of recent podcasts/shows
  var cntr =0;
  var theItems = new Array();

  // joblo podcast
  $.get('/newsfeeds/joblo-podcast.xml', {}, function(xml){
    $('item',xml).each(function(){
      cntr++;
      if (cntr <= 8) { 
	var key = Date.parse($(this).find("pubDate").text());
	if ($.browser.safari) {
	  key += "<>JOBLO PODCAST<>" + $(this).find('link').text() + "<>" + $(this).find("image").text();
	} else { 
	  key += "<>JOBLO PODCAST<>" + $(this).find('link').text() + "<>" + $(this).find("itunes\\:image").text();
	}
	theItems.push(key);
      }
    });

    // arrow podcast
    cntr = 0;
    $.get('/newsfeeds/arrow-podcast.xml', {}, function(xml){
      $('item',xml).each(function(){
	cntr++;
	if (cntr <= 8) { 
	  var key = Date.parse($(this).find("pubDate").text());
	  if ($.browser.safari) {
	    key += "<>ARROW PODCAST<>" + $(this).find('link').text() + "<>" + $(this).find('image').text();
	  } else { 
	    key += "<>ARROW PODCAST<>" + $(this).find('link').text() + "<>" + $(this).find('itunes\\:image').text();
	  }

	  theItems.push(key);
	}
      });

      // joblo movie show podcast
      cntr = 0;
      $.get('/newsfeeds/joblo_movie_show.xml', {}, function(xml){
	$('item',xml).each(function(){
	  cntr++;
	  if (cntr <= 8) { 
	    var key = Date.parse($(this).find("pubDate").text());
	    if ($.browser.safari) {
	      key += "<>JOBLO MOVIE SHOW<>" + $(this).find('link').text() + "<>" + $(this).find('image').text();
	    } else { 
	      key += "<>JOBLO MOVIE SHOW<>" + $(this).find('link').text() + "<>" + $(this).find('itunes\\:image').text();
	    }
	    theItems.push(key);
	  }
	});
	theItems.sort();
	

	// update the page
	var output = "";
	for (i=0; i <= 12; i++){ 
	  var item = theItems.pop();
	  var parts = item.split("<>");
	  output += "<li><a href=\"" + parts[2] + "\"><img src=\"" + parts[3] + "\" />" + parts[1] + "</a></li>\n";
	}
	$('div#latest-podcast-shows ul.slideshow').html(output);

      });
    });
  });

}

/* ------------------------------------------------------- */
function showWeek(week) { 
  $('div.archives table.headlines').hide();
  $(available_weeks[week]).show();
  if (week - 1 < available_weeks.length) {
    $('div.archives div.nav-links a.next').show();
  }
  if (week > 0) { 
    $('div.archives div.nav-links a.previous').show();
  }
  if (week + 1 == available_weeks.length) { 
    $('div.archives div.nav-links a.next').hide();
  }
  if (week == 0) {
    $('div.archives div.nav-links a.previous').hide();
  }


  var href = 'javascript: showWeek(' + (week + 1) + ');';
  $('div.archives div.nav-links a.next').attr('href', href);
  var href = 'javascript: showWeek(' + (week - 1) + ');';
  $('div.archives div.nav-links a.previous').attr('href', href);

  y = $('div.archives').offset();
  window.scrollTo(0,y.top);

}



/* ------------------------------------------------------- */
function newWindow(myPage,myName,w ,h) {
var winT = (screen.height-h)/ 2;
var winL = (screen.width-w)/ 2;
winProps = 'height='+h+',width='+w+',top='+winT+',left='+winL+',toolbar=no ,scrollbars=no,resizable=no';
win = window.open(myPage, myName, winProps);
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function newWindow_Scroll(myPage,myName,w,h,scroll) {
var winT = (screen.height-h)/ 2;
var winL = (screen.width-w)/ 2;
winProps = 'height='+h+',width='+w+',top='+winT+',left='+winL+',toolbar=no,scrollbars=yes,resizable=no';
win = window.open(myPage, myName, winProps);
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function PrinterFriendly(url, w, h) {
var winT = (screen.height-h)/ 2;
var winL = (screen.width-w)/ 2;
winProps = 'height='+h+',width='+w+',top='+winT+',left='+winL+',toolbar=no,scrollbars=yes,resizable=yes';
win = window.open(url, 'printerfriendly', winProps);
if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}




