var gulp = require('gulp');

var browserSync = require('browser-sync');
var cleancss = require('gulp-clean-css');
var fs = require('fs');
var header = require('gulp-header');
var include = require('gulp-include');
var jshint = require('gulp-jshint');
var rename = require('gulp-rename');
var runSequence = require('run-sequence');
var sass = require('gulp-sass');
var uglify = require('gulp-uglify');
var util = require('gulp-util');

var headerFilePath = 'src/js/header.js';

gulp.task('browser-sync', function () {
  browserSync.init({
    startPath: 'run',
    server: {
      baseDir: './'
    },
    port: 3000,
    ghostMode: false
  });

  gulp.watch(['dist/*.*', 'test/*.html']).on('change', browserSync.reload);
});

gulp.task('bundlejs', function () {
  return gulp.src('src/js/_main.js')
    .pipe(include())
      .on('error', console.log)
    .pipe(header(fs.readFileSync(headerFilePath, 'utf8')))
    .pipe(rename('jquery.scrolling-tabs.js'))
    .pipe(gulp.dest('dist'));
});

gulp.task('lintdist', function () {
  return gulp.src('dist/jquery.scrolling-tabs.js')
    .pipe(jshint())
    .pipe(jshint.reporter('jshint-stylish'));
});

gulp.task('lintsrc', function () {
  return gulp.src('src/js/*.js')
    .pipe(jshint())
    .pipe(jshint.reporter('jshint-stylish'));
});

gulp.task('minjs', function () {
  return gulp.src('dist/jquery.scrolling-tabs.js')
    .pipe(rename('jquery.scrolling-tabs.min.js'))
    .pipe(uglify())
    .pipe(header(fs.readFileSync(headerFilePath, 'utf8')))
    .pipe(gulp.dest('dist'));
});

gulp.task('mincss', function () {
  return gulp.src('dist/jquery.scrolling-tabs.css')
    .pipe(rename('jquery.scrolling-tabs.min.css'))
    .pipe(cleancss())
    .pipe(header(fs.readFileSync(headerFilePath, 'utf8')))
    .pipe(gulp.dest('dist'));
});

gulp.task('sass', function () {
  return gulp.src('src/scss/*.scss')
    .pipe(sass().on('error', sass.logError))
    .pipe(header(fs.readFileSync(headerFilePath, 'utf8')))
    .pipe(gulp.dest('dist'));
});


gulp.task('watch', function () {
  gulp.watch('src/scss/*.scss', ['sass']);
  gulp.watch('src/js/*.js', ['bundlejs', 'minjs']);
});

gulp.task('build', function (cb) {
  runSequence('lintsrc',
              'bundlejs',
              'lintdist',
              'minjs',
              'sass',
              'mincss',
              cb);
});

gulp.task('default', function () {
  runSequence('build',
              'browser-sync',
              'watch');
});

@@ -640,85 +640,8 @@
  %7D);
-%0A%0A  gulp.watch(%5B'dist/*.*', 'test/*.html'%5D).on('change', browserSync.reload);
 %0A%7D);
@@ -1982,18 +1982,22 @@
 css', %5B'
-sa
+buildc
 ss'%5D);%0A 
@@ -2031,25 +2031,123 @@
 %5B'bu
-ndlejs', 'minjs'%5D
+ildjs'%5D);%0A%7D);%0A%0Agulp.task('buildcss', function (cb) %7B%0A  runSequence('sass',%0A              'mincss',%0A              cb
 );%0A%7D
@@ -2166,16 +2166,18 @@
 k('build
+js
 ', funct
@@ -2301,19 +2301,82 @@
         
-'sa
+cb);%0A%7D);%0A%0Agulp.task('build', function (cb) %7B%0A  runSequence('buildc
 ss',%0A   
@@ -2379,37 +2379,38 @@
 %0A              '
-mincs
+buildj
 s',%0A            

Stop browserSync auto-reload on file change