var locked = 0;
var sendContactForm = 0;
var $i=2;
var $stopSlideShow = 0;

$.fn.preload = function() {
    this.each(function(){
        $('<img/>')[0].src = this;
    });
}

$(['http://avinger.com/images/site_imgs/select_back.png','http://avinger.com/images/site_imgs/select_bottom.png','http://avinger.com/images/site_imgs/select_back_158.png','http://avinger.com/images/site_imgs/select_bottom_158.png','http://avinger.com/images/site_imgs/popup_x_rollover.png','http://avinger.com/images/site_imgs/popup_top.png','http://avinger.com/images/site_imgs/popup_bottom.png','http://avinger.com/images/site_imgs/popup_background.png']).preload();

//content block variables
var SlideWidth = 951;
var SlideSpeed = 500;

  scrollers=[];

$(document).ready(function(){

  $('#searcharea').mouseenter(function(){
    $(this).find('div').css('background-position','bottom left');
    $(this).find('input').css('background-color','#9fc5db').css('color','#414c5d');
  }).mouseleave(function(){
    $(this).find('div').css('background-position','top left');
    $(this).find('input').css('background-color','#cfcfcf').css('color','#8c8c8c');
  });

  $('#searchbtn').click(function(){$('#searchform').submit();});

  $('body').click(function(){
    $('.selbtn').each(function(){
      if($(this).hasClass('up')){$(this).trigger('click');}
    });
  });


  $('.olfdistarea').bind('click', function(){
    $(this).find('.selbtn50').click();
    return false;
  });
  $('.olfdistarea').mouseenter(function(){$(this).css('cursor','pointer');}).mouseleave(function(){$(this).css('cursor','normal');});

  $('.distarea').bind('click', function(){
    $(this).find('.selbtn').click();
    return false;
  });
  $('.distarea').mouseenter(function(){$(this).css('cursor','pointer');}).mouseleave(function(){$(this).css('cursor','normal');});

  $('.subtypearea').bind('click', function(){
    $(this).find('.selbtn').click();
    return false;
  });
  $('.subtypearea').mouseenter(function(){$(this).css('cursor','pointer');}).mouseleave(function(){$(this).css('cursor','normal');});
 
  //BEGIN CONTACT FORM FUNCTIONALITY
  //////////////////////////////////
  $('.data').each(function(){
    if($(this).children('.datas').children('.mover').height()>260){$(this).children('.scrollareart').addClass('show');}
    else{$(this).children('.scrollareart').removeClass('show');} 
  });
  
  $('.diarea').bind('click', function(){
    $(this).find('.selbtn50').click();
    return false;
  });
  $('.pfarea').bind('click', function(){
    $(this).find('.selbtn50').click();
    return false;
  });
  $('.contactsubtypearea').bind('click', function(){
    if($(this).css("opacity") != 1.0)
      return false;
    else
      $(this).find('.selbtn50').click();
    
    return false;
  });


  $('#contactbutton').click(function() { 
  if(locked==1) return;
  locked = 1;
  $(this).css({ opacity: 0.5 });
  
  var inquiry = $("#inquiry").val();  
  var prefix = $("#prefix").val();  
  var first_name = $("#fname").val();
  var last_name = $("#lname").val();  
  var email = $("#cemail").val();  
  var phone = $("#cphone").val(); 
  var organization = $("#org").val();  
  var message = $(".messagearea").html();

  var dataString = 'inquiry='+ inquiry + '&prefix=' + prefix + '&first_name=' + first_name + '&last_name=' + last_name + '&email=' + email + '&phone=' + phone + '&organization=' + organization + '&message=' + message;  
    if(inquiry != ""){makeGrey('.diarea');}
    if (prefix != ""){makeGrey('.pfarea');}
    if (first_name != ""){makeGrey('.fnamearea');}
    if (last_name != ""){makeGrey('.lnamearea');}
    if (email != ""){makeGrey('.cemailarea');}
    if (message != ""){makeGrey('.messagearea');}
    makeGrey('.captchaarea');
    makeGrey('.contactsubtypearea');


    if(inquiry == ""){
        makeRed('.diarea');
	//$('#contact-error').show();
        $('#contact-error').html("Please select your inquiry type.");
        locked = 0;
        $(this).css({ opacity: 1.0 });
        return;
    }
    else if(prefix == ""){
        makeRed('.pfarea');
        //$('#contact-error').show();
        $('#contact-error').html("Please select your prefix.");
        locked = 0;
        $(this).css({ opacity: 1.0 });
        return;
    }
    else if(first_name == "First Name *"){
        makeRed('.fnamearea');
        //$('#contact-error').show();
        $('#contact-error').html("Please enter your first name.");
        locked = 0;
        $(this).css({ opacity: 1.0 });
        return;
    }
    else if(last_name == "Last Name *"){
        makeRed('.lnamearea');
        //$('#contact-error').show();
        $('#contact-error').html("Please enter your last name.");
        locked = 0;
        $(this).css({ opacity: 1.0 });
        return;
    }
    else if(email == "Email *"){
        makeRed('.cemailarea');
        //$('#contact-error').show();
        $('#contact-error').html("Please enter your email address.");
        locked = 0;
        $(this).css({ opacity: 1.0 });
        return;
    }
    else if(message == "Your message *"){
        makeRed('.messagearea');
        //$('#contact-error').show();
        $('#contact-error').html("Please enter your message.");
        locked = 0;
        $(this).css({ opacity: 1.0 });
        return;
    }

    //check if captcha is correct
    get(document.getElementById('myform'));
    var CAPTCHA = http_request.responseText;

    if($('.sub_checkbox:checkbox').is(':checked')){
      if($('#contactsubtype').val() == ""){
        makeRed('.contactsubtypearea');
        //$('#contact-error').show();
        $('#contact-error').html("Please select a subscriber type.");
        locked = 0;
        $(this).css({ opacity: 1.0 });
      }
      else{
        //submit the newsletter signup form which then sends the contact form upon success
        //if incorrect captcha, display message and return
        if(CAPTCHA == 0){
	    makeRed('.captchaarea');
            //$('#contact-error').show();
            $('#contact-error').html("Incorrect CAPTCHA.");
            locked = 0;
            $(this).css({ opacity: 1.0 });
            return false;
        }
        else{
            sendContactForm = 1;
            $('#email').val( $('#cemail').val());
            $('#subtype').val($('#contactsubtype').val());
            $('#mc-embedded-subscribe-form').submit();
            return false;
        }
      }
    }
    else{
        //if incorrect captcha, display message and return
        if(CAPTCHA == 0){
	    makeRed('.captchaarea');
            //$('#contact-error').show();
            $('#contact-error').html("Incorrect CAPTCHA.");
            locked = 0;
            $(this).css({ opacity: 1.0 });
            return;
        }
        else{
            //submit just the contact form
            $.ajax({  
                type: "POST",  
                url: "incl/php_mail_contact",  
                data: dataString,  
                success: function() {  
                 $('#contact-error').html("");
                 $('#contactbutton').hide();
                 $('#contact-success').show();
                 $('#contact-success').html("Thank you for your submission");
                }  
            });  
        return false;
        } 
    }
   });


  $('.sub_checkbox:checkbox')
    .click( function()
    {
      if ($(this).is(':checked'))
        $('.contactsubtypearea').fadeTo('slow', 1.0);
      else $('.contactsubtypearea').fadeTo('slow', 0.5);
  } );
  //END CONTACT FORM FUNCTIONALITY
  //////////////////////////////////

  $('.selbtn').click(function(event){
    if($(this).hasClass('down')){
      $(this).parent().children('.valarea').children('.values').css('display','block');
      $(this).addClass('up').removeClass('down');
    }
    else{
      $(this).parent().children('.valarea').children('.values').css('display','none');
      $(this).addClass('down').removeClass('up');
    }
    event.stopPropagation();
  });

  $('body').click(function(){
    $('.selbtn50').each(function(){
      if($(this).hasClass('up')){$(this).trigger('click');}
    });
  });

  $('.selbtn50').bind('click', function(){
    if($(this).parent().css("opacity") != 1.0){
      return false;
    }

    //up and down functions
    if($(this).hasClass('down')){
      $('.selbtn50').each(function(){
        if($(this).hasClass('up')){
          $(this).parent().children('.valarea').children('.values').css('display','none');
          $(this).parent().children('.valarea').children('.values_286').css('display','none');
          $(this).parent().children('.valarea').children('.values_890').css('display','none');
          $(this).addClass('down').removeClass('up');
        }
      });

      $(this).parent().children('.valarea').children('.values').css('display','block');
      $(this).parent().children('.valarea').children('.values_286').css('display','block');
      $(this).parent().children('.valarea').children('.values_890').css('display','block');
      //$(this).parent().children('.valarea').children('.values').css('z-index','999');
      $(this).addClass('up').removeClass('down');
    }
    else{
      $(this).parent().children('.valarea').children('.values').css('display','none');
      $(this).parent().children('.valarea').children('.values_286').css('display','none');
      $(this).parent().children('.valarea').children('.values_890').css('display','none');
      //$(this).parent().children('.valarea').children('.values').css('z-index','0');
      $(this).addClass('down').removeClass('up');
    }
    return false;
  });  
  $('.values_286 ul li a').click(function(e){
    $(this).parents('.valarea').children('input:eq(0)').attr('value',$(this).attr('rel'));
    $(this).parents('.valarea').find('.value').html($(this).html());
  });

  $('.values_890 ul li a').click(function(e){
    $(this).parents('.valarea').children('input:eq(0)').attr('value',$(this).attr('rel'));
    $(this).parents('.valarea').find('.value').html($(this).html());
  });
  $('.values ul li a').click(function(e){
    $(this).parents('.valarea').children('input:eq(0)').attr('value',$(this).attr('rel'));
    $(this).parents('.valarea').find('.value').html($(this).html());
  });

  $('.threecol .btnleft, .threecol .btnright').mouseenter(function(){
    $(this).css('backgroundPosition','0 -81px');
  }).mouseleave(function(){
    $(this).css('backgroundPosition','0 0');
  });

  $('.threecol .img287 img').mouseenter(function(){$(this).addClass('hover');}).mouseleave(function(){$(this).removeClass('hover');});

  $('.tout .img287 img').mouseenter(function(){$(this).addClass('hover');}).mouseleave(function(){$(this).removeClass('hover');});

  $('.scroller').each(function(){
    var id = $(this).attr('id');
    var gridsm = Math.max(0, ($(this).find('.scrollarea .scrollable').height() - $(this).find('.scrollarea .scrollable .scrollbar').height()) / ($(this).find('.leftcol ul li').size() - 6));
    var gridsr = Math.round(($(this).find('.scrollarea .scrollable').height() - $(this).find('.scrollarea .scrollable .scrollbar').height()) / ($(this).find('.leftcol ul li').size() - 6));
    scrollers[id + 'gridsm'] = gridsm;
    scrollers[id + 'gridsr'] = gridsr;
  });

  $('.scroller ul li').mouseenter(function(){
    var $this = $(this);
    if($this.hasClass('first')){$this.addClass('activetop');}
    else if($this.hasClass('sixth')){$this.addClass('activebottom');}
    else{$this.addClass('active');}
  }).mouseleave(function(){
    var $this = $(this);
    if(!$this.hasClass('click')){
      if($this.hasClass('first')){$this.removeClass('activetop');}
      else if($this.hasClass('sixth')){$this.removeClass('activebottom');}
      else{$this.removeClass('active');}
    }
  }).click(function(){
    var $this = $(this);
    if($this.hasClass('first')){
      $this.parent('ul').find('.click').removeClass('activetop active activebottom click');
      $this.addClass('activetop click');
    }
    else if($this.hasClass('sixth')){
      $this.parent('ul').find('.click').removeClass('activetop active activebottom click');
      $this.addClass('activebottom click');
    }
    else{
      $this.parent('ul').find('.click').removeClass('activetop active activebottom click');
      $this.addClass('active click');
    }
    var btnid = $this.attr('id')+"";
    var data = btnid.replace('btn','data');
    //alert(data);
    $(this).parents('.scroller').find('.showdata').animate({opacity : 0},function(){
      $(this).removeClass('showdata').css('display' , 'none');
      $(this).parents('.scroller').find('#'+data).css('display' , 'block').animate({opacity : 1},function(){
        $(this).addClass('showdata');
        if($(this).children('.datas').children('.mover').height()>260){$(this).children('.scrollareart').addClass('show');}
        else{$(this).children('.scrollareart').removeClass('show');};
      });
    });
  });

  $('.scrollarea .scrollarrowup').click(function(){
    var id = $(this).parents('.scroller').attr('id');
    if($(this).parents('.scroller').find('.leftcol ul li.first').html() != $(this).parents('.scroller').find('.leftcol ul li').first().html()){
      var newh = parseInt($(this).parents('.scroller').find('.leftcol ul li.first').parent().css('top')) + 70;
      $(this).parents('.scroller').find('.leftcol ul li.sixth').removeClass('sixth').prev().addClass('sixth');
      $(this).parents('.scroller').find('.leftcol ul li.first').removeClass('first').prev().addClass('first').parent().css('top',newh+'px');
      $(this).parents('.scroller').find('.scrollarea .scrollable .scrollbar').css('top', Math.max(0,$(this).parents('.scroller').find('.scrollarea .scrollable .scrollbar').position().top - scrollers[id + 'gridsm']));
      updateActiveScrollItem($(this).parents('.scroller'));
    }
  }).mouseenter(function(){
    $(this).css('backgroundPosition', 'bottom left');
  }).mouseleave(function(){
    $(this).css('backgroundPosition', 'top left');
  });

  $('.scrollarea .scrollarrowdown').click(function(){
    var id = $(this).parents('.scroller').attr('id');
    if($(this).parents('.scroller').find('.leftcol ul li.sixth').html() != $(this).parents('.scroller').find('.leftcol ul li').last().html()){
      var newh = parseInt($(this).parents('.scroller').find('.leftcol ul li.sixth').parent().css('top')) - 70;
      $(this).parents('.scroller').find('.leftcol ul li.first').removeClass('first').next().addClass('first');
      $(this).parents('.scroller').find('.leftcol ul li.sixth').removeClass('sixth').next().addClass('sixth').parent().css('top',newh+'px');
      $(this).parents('.scroller').find('.scrollarea .scrollable .scrollbar').css('top', Math.max(0,$(this).parents('.scroller').find('.scrollarea .scrollable .scrollbar').position().top + scrollers[id + 'gridsm']));
      updateActiveScrollItem($(this).parents('.scroller'));
    }
  }).mouseenter(function(){
    $(this).css('backgroundPosition', 'bottom left');
  }).mouseleave(function(){
    $(this).css('backgroundPosition', 'top left');
  });

  $('.scroller .scrollarea .scrollable .scrollbar').mouseenter(function(e) {
    var id = $(this).parents('.scroller').attr('id');
    $(this).css('backgroundPosition', 'bottom left');
    var moves = 0;
    if(Math.max(0, $(this).parents('.scroller').find('.leftcol ul').height() - $(this).parents('.scroller').find('.leftcol').height()) == 0)
      return;
    $(this).draggable({
      grid : [scrollers[id + 'gridsm'],scrollers[id + 'gridsm']],
      cursor : 'default',
      containment : 'parent',
      distance : scrollers[id + 'gridsm'],
      drag : function(e, ui) {
        $(this).css('left', 0);
      },
      stop : function(e, ui) {
        var steps = Math.round($(this).position().top / scrollers[id + 'gridsr']);
        var first = steps;
        var sixth = first + 5;
        $(this).parents('.scroller').find('.leftcol ul').css('top','-'+Math.max(0, ($(this).position().top / scrollers[id + 'gridsm']) * 70)+'px');
        $(this).parents('.scroller').find('.leftcol ul li.sixth').removeClass('sixth');
        $(this).parents('.scroller').find('.leftcol ul li.first').removeClass('first');
        $(this).parents('.scroller').find('.leftcol ul li').eq(first).addClass('first');
        $(this).parents('.scroller').find('.leftcol ul li').eq(sixth).addClass('sixth');
        updateActiveScrollItem($(this).parents('.scroller'));
      }
    }).mousedown(function(e) { return false; });
  }).mouseleave(function(){
    $(this).css('backgroundPosition', 'top left');
  });

  $('.scrollareart .scrollable .scrollbar').mouseenter(function(e) {
    $(this).css('backgroundPosition', 'bottom left');
    if(Math.max(0, $(this).parents('.data').find('.datas').find('.mover').height() - $(this).parents('.data').find('.datas').height()) == 0)
      return;
    $(this).draggable({
      cursor : 'default',
      containment : 'parent',
      drag : function(e, ui) {
        var maxtop = Math.max(0, $(this).parents('.data').find('.datas').find('.mover').height() - $(this).parents('.data').find('.datas').height());
        $(this).parents('.data').find('.datas').find('.mover').css('top', -maxtop * 2 * $(this).position().top / $(this).parent().height())
        $(this).css('left', 0);
      },
      stop : function(e, ui) {}}).mousedown(function(e) {
      return false;
    });
  }).mouseleave(function(){
    $(this).css('backgroundPosition', 'top left');
  });

  $('.scrollareart .scrollarrowup').click(function(){
    var id = $(this).parents('.data').attr('id');
    if(Math.max(0, $(this).parents('.data').find('.datas').find('.mover').height() - $(this).parents('.data').find('.datas').height()) == 0)
      return;
        var maxtop = Math.max(0, $(this).parents('.data').find('.datas').find('.mover').height() - $(this).parents('.data').find('.datas').height());
        var newtop = parseInt($(this).parents('.data').find('.datas').find('.mover').css('top')) + 25;
        if(newtop < 25){$(this).parents('.data').find('.datas').find('.mover').css('top',newtop);}
  }).mouseenter(function(){
    $(this).css('backgroundPosition', 'bottom left');
  }).mouseleave(function(){
    $(this).css('backgroundPosition', 'top left');
  });

  $('.scrollareart .scrollarrowdown').click(function(){
    var id = $(this).parents('.data').attr('id');
    if(Math.max(0, $(this).parents('.data').find('.datas').find('.mover').height() - $(this).parents('.data').find('.datas').height()) == 0)
      return;
        var maxtop = Math.max(0, $(this).parents('.data').find('.datas').find('.mover').height() - $(this).parents('.data').find('.datas').height());
        var newtop = parseInt($(this).parents('.data').find('.datas').find('.mover').css('top')) - 25;
        if(newtop > -(maxtop + 25)){$(this).parents('.data').find('.datas').find('.mover').css('top',newtop);}
  }).mouseenter(function(){
    $(this).css('backgroundPosition', 'bottom left');
  }).mouseleave(function(){
    $(this).css('backgroundPosition', 'top left');
  });

  function updateActiveScrollItem(fromhere){
    var active = fromhere.find('.click');
    if(active.hasClass('first')){
      active.removeClass('activetop active activebottom click');
      active.addClass('activetop click');
    }
    else if(active.hasClass('sixth')){
      active.removeClass('activetop active activebottom click');
      active.addClass('activebottom click');
    }
    else{
      active.removeClass('activetop active activebottom click');
      active.addClass('active click');
    }
  }


  $('#docbtn').click(function(e){
   if($('#distance').val() == 0){
      $('#phys-error-response').show();
      $('#phys-error-response').html("Please select a distance.");
      return false;
    }
    else if($('#zipcode').val() == "Zip Code"){
      $('#phys-error-response').show();
      $('#phys-error-response').html("Please enter your zip code.");
      return false;
    }
    var newlocation = '/phys_search/'+$('#zipcode').attr('value')+'/'+$('#distance').attr('value');
    window.location = newlocation;
    e.stopPropagation();
    return false;
  });
  
  $('#salesbtn').click(function(e){
   if($('#sales_distance').val() == 0){
      $('#sales-error-response').show();
      $('#sales-error-response').html("Please select a distance.");
      return false;
    }
    else if($('#sales_zipcode').val() == "Zip Code"){
      $('#sales-error-response').show();
      $('#sales-error-response').html("Please enter your zip code.");
      return false;
    }
    var newlocation = '/sales_professional_search/'+$('#sales_zipcode').attr('value')+'/'+$('#sales_distance').attr('value');
    window.location = newlocation;
    e.stopPropagation();
    return false;
  });

  $('#olfdocbtn50').click(function(e){
   if($('#olfdistance').val() == 0){
      $('.phys-inline-error-top').show();
      return false;
    }
    else if($('#olfzipcode').val() == "New zip code"){
      $('.phys-inline-error-top').hide();
      $('#phys-error-response-top').show();
      $('#phys-error-response-top').html("Please enter your zip code.");
      return false;
    }
    var newlocation = '/phys_search/'+$('#olfzipcode').attr('value')+'/'+$('#olfdistance').attr('value');
    window.location = newlocation;
    e.stopPropagation();
    return false;
  });
  
  $('#olfsalesbtn50').click(function(e){
   if($('#olfdistance').val() == 0){
      $('.phys-inline-error-top').show();
      return false;
    }
    else if($('#olfzipcode').val() == "New zip code"){
      $('.phys-inline-error-top').hide();
      $('#phys-error-response-top').show();
      $('#phys-error-response-top').html("Please enter your zip code.");
      return false;
    }
    var newlocation = '/sales_professional_search/'+$('#olfzipcode').attr('value')+'/'+$('#olfdistance').attr('value');
    window.location = newlocation;
    e.stopPropagation();
    return false;
  });

  //begin content block initialization
  $('.contentslider .btnleft').click(PreviousSlide);
  $('.contentslider .btnright').click(NextSlide);

  //set width of each slider wrapper
  $(this).find('.slider-wrapper').each(function(){
      var width=0;
      var numTouts = $(this).find('.tout').length;
      $(this).find('.tout').each(function() {
         width += $(this).outerWidth( true );
      });
      $(this).width(width);
      if(numTouts > 3){SetNavigationDisplay($(this));}
      else{HideNavigationDisplay($(this));}
  });
  //end content block initialization

  $('.twocol .twothrids .yearheading .showhide').mouseenter(function(){
    var $next = null; 
    $next = $(this).next(); 

    if($(this).hasClass('open')){
      $(this).css('backgroundPosition','-22px 0');
    }
    else{
      $(this).css('backgroundPosition','-22px -22px');
    }
    $next.css("color", "#bf2e1a");
    $next.css('cursor','pointer');
  }).mouseleave(function(){
    var $next = null; 
    $next = $(this).next(); 
    if($(this).hasClass('open')){
      $(this).css('backgroundPosition','0 0');
    }
    else{
      $(this).css('backgroundPosition','0 -22px');
    }
    $next.css("color", "#84A1B3");
    $next.css('cursor','normal');
  }).click(Toggle);

  $('.twocol .twothrids .yearheading .showhide_title').mouseenter(function(){
      var $prev = null; 
      $prev = $(this).prev(); 

      $(this).css("color", "#bf2e1a");
      $(this).css('cursor','pointer');
      //$(this).prev().mouseover();
      if($prev.hasClass('open')){
        $prev.css('backgroundPosition','-22px 0');
      }
      else{
        $prev.css('backgroundPosition','-22px -22px');
      }
  }).mouseleave(function(){
      var $prev = null; 
      $prev = $(this).prev(); 

      $(this).css("color", "#84A1B3");
      $(this).css('cursor','normal');
      if($prev.hasClass('open')){
        $prev.css('backgroundPosition','0 0');
      }
      else{
        $prev.css('backgroundPosition','0 -22px');
      }
      //$(this).prev().mouseleave();
  }).click(function(){$(this).prev().trigger('click');});


  $('#heading .btn').click(function(){
    $('#heading .sharebtns').css('display','block');
  });

  $('#heading .sharebtns .minus').click(function(){
    $('#heading .sharebtns').css('display','none');
  });

});

//begin newsroom +/- button function
function Toggle() {
    if($(this).data('dont')==1) return;
    var $thisToggleButton = $(this);
    var $thisEventWrapper = $(this).parents("div:first").parents("div:first").find('#eventwrapper');
    $thisEventWrapper.slideToggle();
        
     if($(this).hasClass('open')){
       $(this).removeClass('open');
       $(this).css('backgroundPosition','-22px -22px');
     }
     else{
       $(this).addClass('open');
       $(this).css('backgroundPosition','-22px 0');
     }
}
//end newsroom +/- button functions

//begin content block functions

function MouseEnter(){
  $(this).css('backgroundPosition','0 -81px');
};

function MouseLeave(){
  $(this).css('backgroundPosition','0 0');
};

function CurrentWrapperMargin(element) {
    // get current margin of slider
    var currentMargin = $(element).css("margin-left");

    // first page load, margin will be auto, we need to change this to 0
    if (currentMargin == "auto") {
        currentMargin = 0;
    }

    // return the current margin to the function as an integer
    return parseInt(currentMargin);
}

function HideNavigationDisplay(element){
	var thisContentBlock = element.parents("div:first").parents("div:first").parents("div:first");
	var thisNextButton = element.parents("div:first").parents("div:first").parents("div:first").find("div.btnright");
	var thisPrevButton = element.parents("div:first").parents("div:first").parents("div:first").find("div.btnleft");
	$(thisNextButton).hide();
	$(thisPrevButton).hide();	
	$(thisContentBlock).css('margin-top', '0px');	
}

function SetNavigationDisplay(element) {
    var thisSliderWrapper = element;
    var thisNextButton = element.parents("div:first").parents("div:first").parents("div:first").find("div.btnright");
    var thisPrevButton = element.parents("div:first").parents("div:first").parents("div:first").find("div.btnleft");
    
    // get current margin
    var currentMargin = CurrentWrapperMargin(thisSliderWrapper);

    // if current margin is at 0, then we are at the beginning, hide previous
    if (currentMargin == 0) {
        $(thisPrevButton).css({ opacity: 0.5 });
        $(thisPrevButton).css('cursor','default');
        $(thisPrevButton).css('backgroundPosition','0 0');
        $(thisPrevButton).data('dont',1);
        $(thisPrevButton).unbind('mouseleave', MouseLeave);
        $(thisPrevButton).unbind('mouseenter', MouseEnter);
    }
    else {
        $(thisPrevButton).css({ opacity: 1.0 });
        $(thisPrevButton).css('cursor','pointer');
	$(thisPrevButton).data('dont',0);
        $(thisPrevButton).bind('mouseleave', MouseLeave);
        $(thisPrevButton).bind('mouseenter', MouseEnter);
    }

    // get wrapper width
    var wrapperWidth = $(element).width();

    // turn current margin into postive number and calculate if we are at last slide, if so, hide next button
    if ((currentMargin * -1) >= (wrapperWidth - SlideWidth)) {
        $(thisNextButton).css({ opacity: 0.5 });
        $(thisNextButton).css('cursor','default');
        $(thisNextButton).css('backgroundPosition','0 0');
        $(thisNextButton).data('dont',1);
        $(thisNextButton).unbind('mouseenter', MouseEnter);
        $(thisNextButton).unbind('mouseleave', MouseLeave);
    }
    else {
        $(thisNextButton).css({ opacity: 1.0 });
        $(thisNextButton).css('cursor','pointer');
        $(thisNextButton).data('dont',0);
        $(thisNextButton).bind('mouseenter', MouseEnter);
        $(thisNextButton).bind('mouseleave', MouseLeave);
    }
}

function NextSlide() {
    if($(this).data('dont')==1) return;
    if(locked==1) return;
    locked=1;
    var thisSliderWrapper =   $(this).parent().find('.slider-wrapper');

    // get the current margin and subtract the slide width
    var newWrapperMargin = CurrentWrapperMargin(thisSliderWrapper) - SlideWidth;
    
    // slide the wrapper to the left to show the next panel at the set speed. Then set the nav display on completion of animation.
    $(thisSliderWrapper).animate({ marginLeft: newWrapperMargin }, SlideSpeed, function () {locked=0;SetNavigationDisplay(thisSliderWrapper)});
}

function PreviousSlide() {
    if($(this).data('dont')==1) return;
    if(locked==1) return;
    locked=1;
    var thisSliderWrapper =   $(this).parent().find('.slider-wrapper');
    
    // get the current margin and subtract the slide width
    var newWrapperMargin = CurrentWrapperMargin(thisSliderWrapper) + SlideWidth;

    // slide the wrapper to the right to show the previous panel at the set speed. Then set the nav display on completion of animation.
    $(thisSliderWrapper).animate({ marginLeft: newWrapperMargin }, SlideSpeed, function () {locked=0;SetNavigationDisplay(thisSliderWrapper)});
} 
//end content block functions


function makeGrey(element) {
  $(element).removeClass('error');
  $(element).addClass('grey');
  $(element).find('input').removeClass('errortext');
  $(element).find('input').addClass('greytext');
  $(element).find('textarea').removeClass('errortext');
  $(element).find('textarea').addClass('greytext');
}
function makeRed(element) {
  $(element).removeClass('grey');
  $(element).addClass('error');
  $(element).find('input').removeClass('greytext');
  $(element).find('input').addClass('errortext');
  $(element).find('textarea').removeClass('greytext');
  $(element).find('textarea').addClass('errortext');
}
function makeCGrey(element) {
  $(element).removeClass('error');
  $(element).addClass('cgrey');
  $(element).find('input').removeClass('errortext');
  $(element).find('input').addClass('cgreytext');
  $(element).find('textarea').removeClass('errortext');
  $(element).find('textarea').addClass('cgreytext');
}
function makeCRed(element) {
  $(element).removeClass('cgrey');
  $(element).addClass('error');
  $(element).find('input').removeClass('cgreytext');
  $(element).find('input').addClass('errortext');
  $(element).find('textarea').removeClass('cgreytext');
  $(element).find('textarea').addClass('errortext');
}
function makeCustomFileGrey(element) {
  $(element).css({'color': '#808084', 'border': '1px solid #FFFFFF'});
}
function makeCustomFileRed(element) {
  $(element).css({'color': '#FF0000', 'border': '1px solid #FF0000'});
}


