$(document).ready(function() {
  $('.hide').hide();
  
  //$("a[href^='http']").attr('target','_blank');

  $(".toggler").click(function(){
    handleEl = $(this);
    els = $(this).parents('.comment').find('ul.comStats, div.actions');
    str = handleEl.text();

    if(els.eq(0).css('display') == 'none'){
      handleEl.removeClass('collapse').text(str.replace('Expand', 'Collapse'));
    }else{
      handleEl.addClass('collapse').text(str.replace('Collapse', 'Expand'));
    }

    els.each(function(){
      $(this).fadeSliderToggle({
        speed: 500
      });
    });
    
    return false;
  });

  // Fancybox
  $("a.pop").fancybox({
    'hideOnContentClick': false
  });

  // Fancybox
  $("a.iframe-pop").fancybox({
    'hideOnContentClick': false,
    type: 'iframe',
    height: 540,
    width: 440
  });
  // Fancybox
  $("a.invite-pop").fancybox({
    autoDimensions: false,
    width: 540,
    height: 500
    
  });

  ratedEls = $('ul.clickRate').children('.rated');

  if($('ul.clickRate').find('.rateTY, .rateArr').length == 0){
    $('ul.clickRate').append('<li class="rateArr">&larr;</li>');
  }

  //ClickRate Hover
  $('ul.clickRate li').hover(
    function(){
      var el = $(this);  
      el.parent().children().removeClass('rated')
      el.addClass('hover').prevAll().addClass('hover');
      el.nextAll().removeClass('hover');
    },
    function(){
    }
    );

  $('ul.clickRate').hover(
    function(){
    },
    function(){
      var el = $(this);      
      el.children().removeClass('hover');
      ratedEls.addClass('rated');
    }
    );

  $('.reviewComment').css({
    position:'relative'
  });

  reviewHeight = $('.open').height();

  $('ul.clickRate li').click(function(){
    var el = $(this);    
    el.addClass('rated').prevAll().addClass('rated');
    el.nextAll().removeClass('rated');

    ratedEls = el.parent().children('.rated');

    el.parent().find('.rateArr').remove();

    if(el.parent().find('.rateTY').length == 0){
      el.parent().append('<li class="rateTY">&#10003;</li>');
    }

    //$('html,body').animate({
    //  scrollTop: $('#content').offset().top
    //}, 0);

    $('.beFirst').hide();

    if($('.open').css('display') == 'none'){
      $('.open').css({
        display: 'block',
        height: 0
      }).animate({
        'height': reviewHeight
      }, 700);
    }    

    return false;
  });

  $('.reviewSubmit').click(function(){

    $('.reviewComment').animate({
      height:$('.openSubmit').height()
    });

    $('.openSubmit').css({
      'position':'absolute',
      'left':0,
      'top':0
    }).animate({
      height:$('.openSubmit').height(),
      opacity:1
    }, 700);
    
    $('.open').fadeOut(700)

    return false;
  });

  $('.submitClose').click(function(){
    $('.reviewComment').animate({
      height: 0
    },700, function(){
      $(this).css({
        'display':'none'
      });
    });

    return false;
  });

  $('.reviewCancel').click(function(){    
    $('.open').animate({
      'height': 0
    }, 700, function(){
      $(this).css({
        display: 'none'
      });
    })

    return false;
  });

  $('.refineOpen a').click(function(){
    $('.refineOpen a').parent().fadeOut(700);
    $('.refineby').fadeIn(700);

    return false;
  });

  $('.refineby a').click(function(){
    $('.refineOpen').fadeIn(700);
    $('.refineby').fadeOut(700);

    return false;
  });

  $('#companyData').css({
    'position': 'relative'
  });

  $('#editCompanyDataBtn').click(function(){
    $('#companyDataEdit').css({
      'position':'absolute',
      'left':0,
      'top':0
    }).fadeIn(700).parent().animate({
      height: $('#companyDataEdit').innerHeight()
    }, 700);
    $('#companyDataStatic').hide();

    $('html,body').animate({
      scrollTop: $('#content').offset().top
    }, 700);

    return false;
  });

  $('.companyDataCancel').click(function(){
    $('#companyDataStatic').css({
      'position':'absolute',
      'left':0,
      'top':0
    }).show().parent().animate({
      'height': $('#companyDataStatic').innerHeight()
    }, 1000);
    $('#companyDataEdit').hide();

    return false;
  });

  $('.tt').tooltip({
    effect:'fade',
    predelay: 500,
    relative: true
  });

  $('.tt-right').tooltip({
    effect:'fade',
    predelay: 500,
    relative: true,
    offset: [0, -380]
  });

  $('.pop-tt').tooltip({
    effect:'fade',
    predelay: 500,
    relative: true,
    position: 'top right'
  });

  // initialize scrollable
  $("div.scrollable").scrollable({
    size: 1,
    speed: 800,
    loop: true,
    onStart: function(){
      $('.scrollable').animate({
        height: this.getVisibleItems().height()+20
      }, 500);
    }
  }).autoscroll({
    autoplay: true,
    interval: 20000
  });

  $('.toggler.home').click(function(){
    $(this).next().fadeSliderToggle({
      speed: 500
    });
    return false;
  });

  $('#message').click(function(){
    if($(this).val() == 'Please leave a comment on your experience with this company.'){
      $(this).val('');
    }
    return false;
  });

  findCompanyVal = $('#findCompany').val();

  $('#findCompany').focus(function(){
    if($(this).val() == findCompanyVal){
      $(this).val('');
    }
    $('.refineOpen a').parent().fadeOut(700);
    $('.refineby').fadeIn(700);
  });

  $('#findCompany').blur(function(){
    if($(this).val() == ''){
      $(this).val(findCompanyVal);
    }

    $('.refineOpen').fadeIn(700);
    $('.refineby').fadeOut(700);
  });

  $('input.city').focus(function(){
    if($(this).val() == 'city'){
      $(this).val('');
    }

    $('.refineOpen').stop().hide();
    $('.refineby').stop().show();
  });

  $('input.city').blur(function(){
    if($(this).val() == ''){
      $(this).val('city');
    }
  });



  //$('#accountInfo, #passwordInfo').css({position: 'relative'});

  //$('form.editInfo, form.editPassword').css({position: 'absolute', top: 0, left: 0, display: 'none'});

  $('form.editInfo, form.editPassword').css({
    display: 'none'
  });

  $('#accountInfo .editA').click(function(){
    $('#accountInfo').animate({
      height: $('form.editInfo').height()
      }, 0);
    $('div.editInfo').fadeOut(0);
    $('form.editInfo').fadeIn(1000);

    return false;
  });

  $('#accountInfo .rndCancel').click(function(){
    $('div.editInfo').fadeIn(500);
    $('form.editInfo').hide();
    $('#accountInfo').animate({
      height: $('form.editPassword').height()
      }, 0);

    return false;
  });

  $('#passwordInfo .reset').click(function(){
    $('#passwordInfo').animate({
      height: $('form.editPassword').height()
      }, 0);
    $('div.editPassword').hide();
    $('form.editPassword').fadeIn(1000);

    return false;
  });

  $('#passwordInfo .rndCancel').click(function(){
    $('div.editPassword').fadeIn(500);
    $('form.editPassword').hide();
    $('#accountInfo').animate({
      height: $('form.editPassword').height()
      }, 0);

    return false;
  });

  $('#companyDataEdit .submit').click(function(){
    $('#companyDataStatic').css({
      'position':'absolute',
      'left':0,
      'top':0
    }).fadeIn(700).parent().animate({
      height: $('#companyDataStatic').innerHeight()
    }, 0);
    $('#companyDataEdit').hide();
    
    $('h4.notice').fadeIn(700);
    return false;
  });
 
});

