/* 
* rotator
***/

var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline = 0;

jQuery(function($) {

  //  $('#searchResults img').each(function() {
  //    $(this).width(100).height('auto');
  //  });

  /*
  * move the image-rotator on projects page
  ***/
  $('#details-heading').prependTo($('.content-wrapper'));

  /*
  * Add class of DNNEmptyPane to empty containers
  ***/
  $('.content .container-padding > div').each(function() {
    if ($(this).text().match(/^\s*$/)) {
	  //Removing the element entirely also results in the admin users not being able to edit the paramaters for this widget when the results are empty.	
      //$(this).parent('.container-padding').remove();
	  $(this).parent('.container-padding').css('padding', 0);
    }
  });

  /*
  * make the availableFilters hoverable
  ***/
  $("#availableFilters li span").hover(
  function() {
    $(this).addClass("hover");
  },
  function() {
    $(this).removeClass("hover");
  }).mouseup(function() {
    $(this).removeClass("hover");
  });

  /*
  * make the currentFilters hoverable
  ***/
  $("#currentFilters li span").hover(
  function() {
    $(this).addClass("hover");
  },
  function() {
    $(this).removeClass("hover");
  }).mouseup(function() {
    $(this).removeClass("hover");
  });
  $("#availableFilters > li.availableFilterType:first").removeClass("availableFilterType").addClass("availableFilterTypeSelected");

  /*
  * homepage rotator
  ***/
  headline_interval = setInterval(headline_rotate, 5000); //time in milliseconds
  headline_count = $("#rotator-padding > .leadinItem ").size();
  $('#rotator-padding .leadinItem:first').siblings().hide();
  $('#rotator-padding').append('<div id="rotator-controls"></div>');
  $('#rotator-padding .leadinItem').each(function(index) {
    var $rotatorType = $(this).children('h2').html();
    var $rotatorTitle = $(this).children('h3').html();
    $('#rotator-controls').append('<div><h2>' + $rotatorType + '</h2><h3>' + $rotatorTitle + '</h3></div>');
  });
  $('#rotator-controls div:first').addClass('active');
  $('#rotator-controls > div:last').css('border-right-width', '0');

  /*
  * Homepage rotator mouse hover
  ***/
  $('#rotator-controls div').hover(
  function() {
    var index = $('#rotator-controls div').index(this);
    clearInterval(headline_interval);
    $('#rotator-padding .leadinItem:eq(' + index + ')').show().siblings('.leadinItem').hide();
    $(this).siblings('.active').removeClass('active');
    $(this).addClass('active');
    old_headline = index;
  },
  function() {
    var index = $('#rotator-controls div').index(this);
    headline_interval = setInterval(headline_rotate, 5000); //time in milliseconds
  });

  /*
  * automated rotator ***/
  function headline_rotate() {
    current_headline = (old_headline + 1) % headline_count;
    //alert('curr=' + current_headline + ', old=' + old_headline + ', count=' + headline_count)
    $("#rotator-padding .leadinItem:eq(" + current_headline + ")").show().addClass('active');
    $("#rotator-padding .leadinItem:eq(" + old_headline + ")").removeClass('active').hide();
    $("#rotator-controls div:eq(" + current_headline + ")").addClass('active');
    $("#rotator-controls div:eq(" + old_headline + ")").removeClass('active');
    old_headline = current_headline;
  };

  /*
  * set the rotator up
  ***/
  $("#image-rotator").after('<div id="rotatorThumbs"></div>');
  var firstImgAlt = $("#image-rotator div:first img").attr('alt');
  var firstImg = $("#image-rotator div:first img").parent().html();
  $('#image-rotator .image-item img').each(function(index) {
    var $imageSource = $(this).parent().html();
    $(this).parent().remove();
    $("#rotatorThumbs").append('<span>' + $imageSource + '</span>');
  });
  $("#image-rotator").html('<div>' + firstImg + '</div>');
  $('#image-rotator').after('<p class="author">' + firstImgAlt + '</p>');
  $("#rotatorThumbs span:eq(0)").addClass('activeControls');
  
  //Center Image Vertically
  $containerHeight = $("#image-rotator").css('height');
  $imgHeight = $("#image-rotator>div:first-child>img").height();
  if ($imgHeight && $imgHeight < parseInt($containerHeight)){
    $ch = parseInt($containerHeight)/2;
	$ih = parseInt($imgHeight)/2;
	$topPadding = Math.floor(Math.abs($ch - $ih));
	$("#image-rotator>div:first-child").css('padding-top', $topPadding);
  }
  

  /*
  * handle the clicking
  ***/
  $("#rotatorThumbs img").click(function() {
    if ($(this).parent().hasClass('activeControls')) {
    } else {
      var newImg = $(this).attr('src');
      var newImgAlt = $(this).attr('alt');
      var newImgTitle = $(this).attr('title');
      $('#rotatorThumbs .activeControls').removeClass('activeControls');
      $(this).parent().addClass('activeControls');
      $('#image-rotator img').fadeOut(350, function() {
        $('#image-rotator + p.author').text(newImgAlt);
        //        $('#image-rotator').css({ height: $("#image-rotator img").height() });
        $('#image-rotator img').attr({ src: newImg }).css({ margin: "0", visibility: "hidden" }).show();
        $('#image-rotator').animate(350, function() {
          $('#image-rotator img').css({ visibility: "visible", display: "none" }).fadeIn(350);
		  
		  //Center Image Vertically
		  $containerHeight = $("#image-rotator").css('height');
		  $imgHeight = $("#image-rotator>div:first-child>img").height();
		  if ($imgHeight && $imgHeight < parseInt($containerHeight)){
		  $ch = parseInt($containerHeight)/2;
		  $ih = parseInt($imgHeight)/2;
			$topPadding = Math.floor(Math.abs($ch - $ih));
			$("#image-rotator>div:first-child").css('padding-top', $topPadding);
		  }else{
			$("#image-rotator>div:first-child").css('padding-top', '0px');
		  }	
		  
        });		
      });
    };
    return false;
  });
  /*
     * bind sites main search box to the mains search button.  IE was not doing this by default.
     */
  	$('.solrSiteSearchInput').bind('keypress', function(e) {
		var key = e.charCode ? e.charCode : e.keyCode ;
		if (key == 13 ) {
			$('.solrSiteSearchButton').click();
			return false;
		}
	});
	
	
	/* ************************************** */
	/* Professional/Supplier Reports pages */
	/* ************************************** */
	/* Expired */
	$("#expired_count").text($(".expired_all").size());
	
	$(".expired").click(function () {
	  var x = $(this).attr('id');
	  if (x == "expired_All")
	  {
		$(".expired_all").show();
		$("#expired_count").text($(".expired_all").size());
	  }
	  else
	  {
		$(".expired_all").hide();
		$("."+x).show();
		$("#expired_count").text($("."+x).size());
	  }
    }); 
	
	/* Expiring */
	$("#expiring_count").text($(".expiring_all").size());
	
	$(".expiring").click(function () {
	  var x = $(this).attr('id');
	  if (x == "expiring_All")
	  {
		$(".expiring_all").show();
		$("#expiring_count").text($(".expiring_all").size());
	  }
	  else
	  {
		$(".expiring_all").hide();
		$("."+x).show();
		$("#expiring_count").text($("."+x).size());
	  }
    });
	
	/* UnApproved */
	$("#unapproved_count").text($(".unapproved_all").size());
	
	$(".unapproved").click(function () {
	  var x = $(this).attr('id');
	  if (x == "unapproved_All")
	  {
		$(".unapproved_all").show();
		$("#unapproved_count").text($(".unapproved_all").size());
	  }
	  else
	  {
		$(".unapproved_all").hide();
		$("."+x).show();
		$("#unapproved_count").text($("."+x).size());
	  }
    });
	/* ************************************** */
	
	/* Move Related Articles to sidebar */
	$("div#relatedContent").append($("div.relatedarticleContentWrap"));
	/* ************************************** */
});

function checkTextLimit(field, maxlength) {
	/*var myArr = field.value.split(" ");
	if (myArr.length > maxlength){
		alert("To many words. Max allowed: " + maxlength);
		field.value = myArr.slice(0,maxlength).join(" ");
	}*/

	if (field.value.length > maxlength) {
		alert('Maximum ' + maxlength + ' Characters Allowed!');
		field.value = field.value.substring(0, maxlength);
	}
	//var reg = new RegExp("((\\b[a-z0-9]+\\b.|\\n){4})(.*)", "gis");
	//var match = reg.exec(field.value);
	//if (match){
		//alert('Maximum 4 words Allowed!');
		//field.value = match[1].substring(0, match[1].length-1);
	//}
}

