/*
	Metro Group Services	
*/


$(function() {

	$('a, input[type=password]').click(function() {
		if(!$(this).attr('href') || $(this).attr('href') == "" || $(this).attr('href') == "#")
		{
			alert("Under construction: Page coming soon!");
      return false;
		}
	});

  var MAX_VISIBLE_ROWS = 1;

  if($('.row-border').length == 1)
  {
    MAX_VISIBLE_ROWS = 18; //if scrolling the row-border table (rec cap projects)
  }

  $('.scrolling-table tbody tr').each(function(ix, el)
  {
    if(ix>=MAX_VISIBLE_ROWS) $(el).hide();
  });

  var prnt = $('.row-border, .scrolling-table').parent();
    prnt.append("<div id='table-scroll'>"+
      "<a id='scroll-down' href='#'>" +
        "<img src='/wordpress/wp-content/themes/metrogroupservices/custom/img/down-arrow.png' /></a>"+
      "<a id='scroll-up' style='display:none' href='#'>" +
        "<img src='/wordpress/wp-content/themes/metrogroupservices/custom/img/up-arrow.png' /></a>"+
      "</div>");


  var cur_scroll_start = 0;
  $("#scroll-down").click(function()
  {
    $($('.scrolling-table tbody tr')[cur_scroll_start]).hide();
    $($('.scrolling-table tbody tr')[cur_scroll_start+MAX_VISIBLE_ROWS]).show();
    

    cur_scroll_start += 1;

    $("#scroll-up").show();
  
    if($('.scrolling-table tbody tr').length <= cur_scroll_start+MAX_VISIBLE_ROWS)
    {
      $("#scroll-down").hide();
    }
    return false;
  });

  $("#scroll-up").click(function()
  {
    cur_scroll_start -= 1;
    $($('.scrolling-table tbody tr')[cur_scroll_start+MAX_VISIBLE_ROWS]).hide();
    $($('.scrolling-table tbody tr')[cur_scroll_start]).show();
    
    

    $("#scroll-down").show();
  
    if(cur_scroll_start == 0)
    {
      $("#scroll-up").hide();
    }
    return false;
  });

  //$(".row-border").load(function()
  //{

    //$(".row-border thead tr").css({ 'position': 'absolute', 'left': 0, 'top': 0 })
    //    .after("<tr id=\"inserted\"><td style=\"height: 20px; background: #fff;\" colspan=\"5\">&nbsp;</td></tr>");
    
    //$("div.inner-rb").css({ 'height': '200px' });
  //});

  //CONSTRUCTION: alert for all subpages
  /*$('#subpages a').click(function() {
    
      //check title
      if(document.location.href.indexOf("maintenance-") < 0) {
        alert("Under construction: Page coming soon!");
        return false;
      }
    
  });*/

  $('#info_and_search #search').attr("placeholder_txt", "SEARCH");
  //$('#info_and_search input:text').addPlaceholder();

	//justify/hyphenate
	$("#content .content-thin p, .content-wide p").addClass("sweet-justice");

  //cache the ticker
  var ticker = $("#service-list #inner ul");
  
  //wrap dt:dd pairs in divs
  ticker.children().filter("li").each(function() {
    
    var dt = $(this),
      container = $("<div>");
    
    dt.next().appendTo(container);
    dt.prependTo(container);
    
    container.appendTo(ticker);
  });
  
  //hide the scrollbar
  ticker.css("overflow", "hidden");
  
  //animator function
  function animator(currentItem) {
      
    //work out new anim duration
    var distance = currentItem.height();
    
    if(isNaN(distance)){ distance = 10; } 

    var mrgTop = parseInt(currentItem.css("marginTop"));
    if(isNaN(mrgTop)){ mrgTop = 0; } 

   	duration = (distance + mrgTop) / 0.025;



    //animate the first child of the ticker
    currentItem.animate({ marginTop: -distance }, duration, "linear", function() {
      
   //move current item to the bottom
   currentItem.appendTo(currentItem.parent()).css("marginTop", 0);

   //recurse
   animator(currentItem.parent().children(":first"));
    }); 
  };
  
  //start the ticker
  animator(ticker.children(":first"));
    
  /*//set mouseenter
  ticker.mouseenter(function() {
    
    //stop current animation
    ticker.children().stop();
    
  });
  
  //set mouseleave
  ticker.mouseleave(function() {
            
          //resume animation
    animator(ticker.children(":first"));
    
  });*/
});
