﻿


jQuery.fn.initCarousel = function () {
  return $(this).each(function () {
    var currentPosition = 0;
    var slideWidth = 700;
    var slides = $(this).find('.slide');
    var nSlides = slides.length;

    slides.wrapAll('<div class="slideInner"></div>');
    $(this).find('.slideInner').css({ 'width': nSlides * slideWidth });

    $(this).prepend('<span class="control leftControl" rel="leftControl" style="display:none">Slide to the left</span>').append('<span class="control rightControl" rel="rightControl">Slide to the right</span>');

    $(this).append('<ul />');
    for (var i = 0; i < nSlides; i++) {
      $(this).find('ul').append('<li><a href="#">' + (i + 1) + '</a></li>');
    }
    $('#carousel>ul li:first a').addClass('active');

    $(this).find('ul a').bind('click', function () {
      var current = $(this).parent('li');
      var nr = $('#carousel ul li').index(current);
      currentPosition = nr;
      $('ul a').removeClass('active');
      $(this).addClass('active');

      if (currentPosition == 0) {
        $(this).parents('#carousel').find('.leftControl').hide()
      } else {
        $(this).parents('#carousel').find('.leftControl').show()
      }
      if (currentPosition == nSlides - 1) {
        $(this).parents('#carousel').find('.rightControl').hide()
      } else {
        $(this).parents('#carousel').find('.rightControl').show()
      }

      $(this).parents('#carousel').find('.slideInner').animate({
        'marginLeft': slideWidth * (-nr)
      });
    });

    $(this).find('.control').bind('click', function () {
      currentPosition = ($(this).attr('rel') == 'rightControl') ? currentPosition + 1 : currentPosition - 1;

      if (currentPosition == 0) {
        $(this).parents('#carousel').find('.leftControl').hide()
      } else {
        $(this).parents('#carousel').find('.leftControl').show()
      }
      if (currentPosition == nSlides - 1) {
        $(this).parents('#carousel').find('.rightControl').hide()
      } else {
        $(this).parents('#carousel').find('.rightControl').show()
      }
      $('ul a').removeClass('active');
      $('ul li:nth-child(' + (currentPosition + 1) + ') a').addClass('active');

      $(this).parents('#carousel').find('.slideInner').animate({
        'marginLeft': slideWidth * (-currentPosition)
      });
    });
  });
};



$(document).ready(function () {

  $('#carousel').initCarousel();
  $('.grid_6 .newsitem:last').addClass('last-child');
  

  $("a[href$='.pdf']").addClass("pdf");
  $("a[href$='.xls'],a[href$='.xlsx']").addClass("xls");
  $("a[href$='.doc'],a[href$='.docx']").addClass("doc");
  $("a[href$='.zip'],a[href$='.rar'],a[href$='.exe']").addClass("file");
});
