var pixOfTheDayTimer = 5000; // time in milliseconds for the cycling of the pixOfTheDay module
var pixOfTheDayCntr = 0;

// preload
pic1= new Image(100,25); 
pic1.src="/graphics/red-arrow-right2.png";

$(document).ready(function(){
  if ($('#pixOfTheDay').html() != '') { 
    $('#pixOfTheDay .item:first').show();
    setInterval('cyclePixOfTheDay()', pixOfTheDayTimer);
  }

  $('.searchForm strong.sub').click(function(){
    if ($('#search').val() != '') {
      $('.searchTop').submit();
    }
  });


  $('#posts .seeMore .contain a').hover(
    function(){ 
      $(this).prev().attr('src', '/graphics/arrow-right.png');
    },
    function(){ 
      $(this).prev().attr('src', '/graphics/black-arrow.png');
    }
  );

  $('#posts .entry_gallery .previewImage').hover(
    function(){ 
      $(this).parent().parent().parent().parent().find('p strong a').addClass('hover');
    },
    function(){ 
      $(this).parent().parent().parent().parent().find('p strong a').removeClass('hover');
    }
  );


  $('.searchForm strong.sub2').click(function(){
    if ($('#search2').val() != '') {
      $('.searchBottom').submit();
    }
  });

  $('.item .seeMore').hover(
    function(){ 
      $(this).find('img').attr('src','/graphics/red-arrow-right2.png');
    },
    function(){ 
      $(this).find('img').attr('src','/graphics/white-arrow-right.png');
    }
  );

  $('#pixOfTheDay .seeMore').hover(
    function(){ 
      $(this).find('img').attr('src','/graphics/red-arrow-right2.png');
    },
    function(){ 
      $(this).find('img').attr('src','/graphics/white-arrow-right.png');
    }
  );

  if (typeof navSelect != 'undefined') {
    $(navSelect).addClass('selected');

    if (navSelect == '#nav_contact') { 
      $('#contactForm p.website').hide();
      var tmp = 'Click "Yes" below if you\'re human <span class="required">(required)</span><br /><input type="radio" name="human" value="0" CHECKED> No   <input type="radio" name="human" value="1" > YES';
      $('p.human').html(tmp);
    }
  }


  if (typeof $('#commentsArea').html() != 'undefined') {
    var entryId = $('#posts .post .entry').attr('id');
    $("#commentsArea form input#entry_id").val(entryId);
    $('#commentsArea form #comment-url').parent().hide();
    var tmp = 'Click "Yes" below if you\'re human <span class="required">(required)</span><br /><input type="radio" name="human" value="0" CHECKED> No   <input type="radio" name="human" value="1" > YES';
    $('#commentsArea .human').html(tmp);
  }

  $('#videoPage .search img').click(function(){ 
    if ($('#videoPage .qry').val() != '') { 
      $('#videoPage .search #vidSearch').submit();
    } else { 
      alert("Please enter a search term!");
    }
  });

  //showFeed('/cache/x17video.xml?jdfjkghjdhf', 'latestVideos', 8);
  //showFeed('/news/index.xml', 'breakingNews .contain', 5);
  showBreakingNews();
  //showFeed('/cache/celeb-rss.xml', 'celebTweets .contain', 2);
  //showFeed('/cache/x17-twitter.xml', 'twitter .contain', 2);


  // top/bottom search form stuff  
  $('.searchForm li a').click(function(){ 
    $('.searchForm li a').removeClass('selected');
    var searchItem = $(this).html();

    // mark appropriate type
    $('.searchForm li a').each(function(){
      if ($(this).html() == searchItem) { $(this).addClass('selected'); }
    });

    // make changes to search form as needed
    if (searchItem == 'ENTIRE SITE') {
      $('.searchForm').attr('action', '/mt/search-jglkkem.cgi');
      $('.searchForm').find('input').each(function(){ 
	if ($(this).attr('name') == 'IncludeBlogs') { $(this).val('1,8'); }
	if ($(this).attr('name') == 'Template') { $(this).val('new'); }
      });
    }
    else if (searchItem == 'NEWS') {
      $('.searchForm').attr('action', '/mt/search-jglkkem.cgi');
      $('.searchForm').find('input').each(function(){ 
	if ($(this).attr('name') == 'IncludeBlogs') { $(this).val('8'); }
	if ($(this).attr('name') == 'Template') { $(this).val('new'); }
      });
    }
    else if (searchItem == 'VIDEOS') {
      $('.searchForm').attr('action', 'http://x17video.com/mt/search-998ojfjgkjekr.cgi');
      $('.searchForm').find('input').each(function(){ 
	if ($(this).attr('name') == 'IncludeBlogs') { $(this).val('1'); }
	if ($(this).attr('name') == 'Template') { $(this).val('default'); }
      });
    }
    else if (searchItem == 'PHOTOS') {
      $('.searchForm').attr('action', '/gallery/index.php');
    }
  });
    


  // fixes for images in posts
  $('.post .entry-body img').each(function(){
    if (!$(this).hasClass('postImg')) { 
      $(this).addClass('postImg');
    }

    if ($(this).attr('align') == 'right') { 
      $(this).addClass('floatRight'); 
    }
    else if ($(this).attr('align') == 'left') { 
      $(this).addClass('floatLeft');
    } 

    if ($(this).width() == '460') { $(this).css('marginLeft', '3px'); }
  }); 


  if ($('#commentsArea .comment-content').length > 0) { 
    var c = 0;
    $('#commentsArea .comment-content').each(function(){ 
      c = c + 1;
      if (c == $('#commentsArea .comment-content').length) { $(this).addClass('last'); }
    });
  }
});


// --- PIX OF THE DAY module
function cyclePixOfTheDay() { 
  var animSpeed = 300;
  pixOfTheDayCntr += 1;
  if (pixOfTheDayCntr == $('#pixOfTheDay .item').size()) { 
    pixOfTheDayCntr = 0;
  }
  var tmp = '#pixOfTheDay .pic' + pixOfTheDayCntr;
  $('#pixOfTheDay .item').hide();
  $(tmp).show(animSpeed);

}


// --- BREAKING NEWS moduel
function showBreakingNews() { 
  var url = '/news/breaking-news.xml';

  url = url + "?" +  (Math.random() * 10000);


  var cntr = 0;
  var module = '#breakingNews .contain';
  var output = '<table cellspacing="0" cellpadding="0" border="0">';
  var numToShow = 5;
  $.get(url, {}, function(xml){
    $('item',xml).each(function(){
      cntr = cntr + 1;
      if (cntr <= numToShow) { 
	var desc = $(this).find('description').text();
	var fnd = desc.match(/img src="(.*?)"/i);
	var src = fnd[1];
	desc = $(this).find('summary').text();
	desc = desc.replace(/<img.*?>/i, '');
	desc = limit_words(desc, 32);

	var dt = new Date($(this).find('pubDate').text());
	dtString = '(' + (dt.getMonth() + 1) + '/' +  dt.getDate() + '/' + dt.getFullYear() + ' ';
	
	if (dt.getHours() > 12) { dtString += (	dt.getHours() - 12 ); } 
	else { dtString += dt.getHours(); } 
	dtString  += ':';
	if (dt.getMinutes() < 10) { dtString += '0'; }
	dtString += dt.getMinutes() + ' ';
	if (dt.getHours() > 12) { dtString += 'PM'; } 
	else { dtString += 'AM'; }
	dtString += ')';



	if (cntr == 1) { 
	  output += '<tr class="item1"><td colspan="2"><table class="topTable"><tr valign="center"><td colspan="2">' + '<h3><a href="' + $(this).find('link').text() + '">' + $(this).find('title').text() + '</a></h3></td></tr><tr><td><a href="' + $(this).find('link').text() + '"><img src="' + src + '" alt="" /></a></td><td><p>' + dtString + '<br />' +  desc + '</p></td></tr></table></td></tr>';
	} else { 
	  output += '<tr class="item' + cntr + '" valign="center">';
	  output += '<td><a href="' + $(this).find('link').text() + '"><img src="' + src + '" alt="" /></a></td>';
	  output += '<td width="100%"><h3><a href="' + $(this).find('link').text() + '">' + $(this).find('title').text() + '</a></h3>';
	  output += '<p class="date">' + dtString + '</p></td></tr>';

	}

      } 
      else { return false; }
    });
  output += '</table>';
  $(module).html(output);
  });
}

