var filters = function() {
  
  this.triggers = $('#project-filters .filter');
  this.all = $('#project-filters .all');
  this.counter = $('#project-filters strong span');
  this.list = $('#projects');
  this.selected_filters = [];
  this.timer = null;
  
  var that = this;
  
  this.selectedFilters = function() {
    var selected = $.map(that.triggers.filter('.selected'), function(el, i) {
      return $(el).attr('title');
    });
    return selected.join(',');
  }
  
  this.loadProjects = function() {
    $.ajax({
      url: 'projects/' + that.selectedFilters(),
      method: 'get',
      beforeSend: function() {
        that.list.empty().html('<div class="loading"></div>');
      },
      success: function(response) {
        that.list.html(response);
        that.counter.text(that.list.children().length);
      }
    });
  }
  
  this.triggers.click(function() {
    clearTimeout(that.timer);
    $(this).toggleClass('selected');
    that.all.removeClass('selected');
    that.timer = setTimeout(that.loadProjects, 800);
    return false;
  });
  
  this.all.click(function() {
    clearTimeout(that.timer);
    that.triggers.removeClass('selected');
    $(this).addClass('selected');
    that.timer = setTimeout(that.loadProjects, 800);
    return false;
  });
  
}

$(document).ready(function() {
  
  $('a[rel=external]').attr('target', '_blank');
  
  var slides = $('#slides .slide');
  var widget_items = $('#services a');
  var logo = $('#logo');
  var timer = null;
  var slide_index = 0;
  
  var nextSlide = function() {
    var next_index = (slide_index == slides.length - 1) ? 0 : slide_index+1;
    slides.eq(slide_index).fadeOut('slow');
    slides.eq(next_index).fadeIn('slow');
    slide_index = next_index;
  }
  
  if(slides.length) {
    var preload = new Image();
    preload.src = 'images/service-artwork.png';
    timer = setInterval(nextSlide, 4000);
  }
  
  slides.hover(
    function() { clearInterval(timer) },
    function() { timer = setInterval(nextSlide, 4000); }
  );
  
  $('#thumbs a').click(function(e) {
    $('#main-image').attr('src', $(this).attr('href'));
    console.log($(this).attr('href'));
    return false;
  });
  
  $('form .calendar a').click(function() {
    $(this).siblings('input').focus();
    return false;
  });
  
  var val = 0;

  $('#EvaluationDepartament').change(function() {
	
  	val = $(this).val();

    $.ajax({
      url: 'evaluation/departament/' + val,
      method: 'get',
      success: function(response) {
        $(".dep_container").html(response);
      }
    });
  	
  });

  $('#evaluation-form input[type=submit]').bind("click", function() {

	var $client = $('#EvaluationClient').val();
	var $project = $('#EvaluationProject').val();
	var $location = $('#EvaluationLocation').val();

	if ( ($client == "") || ($project == "") || ($location == "") ) {
		alert("Completati campurile 'Client, Proiect si Locatie'!");
		return false;
	} 

  });
  
  if($.browser.msie == false) filters();
  
});
