/*
  Simple progress bar jQuery plugin for Skylintes coming soon template
  Author: Tomas Dostal
  Copyright: As per evanto (www.themeforest.net)
*/

(function($){
  $.fn.extend({
    progressBar: function(settings) {
    
      var defaults = {
        dateStart: '09/12/2009',
        dateEnd: '12/24/2009',  
        skin: 1,
        percentage: -1
      } 
      
      var settings = $.extend(defaults,settings);
      
      return this.each(function(){
        var set = settings;  // Load the settings
        // Get the width of the progress bar element (in our case -35 px for design)
        var width = $('.background').width()-40;
        // Check if we are dealing with dates of fixed value
        if (set.percentage>0){
          //Get value of 1 unit
          var unit = width/100;
          // Just to make sure there is nothing missing
          var leftOver = width-(unit*100);
          // Count new width of tile
          var newWidth = unit*set.percentage;
          // Just pass the argument for percentage (not really great solution)
          var percentage = set.percentage;    
        }else{        
          // Count the duration in days
          var duration = Math.floor(( Date.parse(set.dateEnd) - Date.parse(set.dateStart) ) / 86400000);
          // Get today's date
          var date = new Date();
          var today = (date.getMonth()+1) + "/" + date.getDate() + "/" + date.getFullYear(); 
          // Based on complete duration and duration from start to today count how many percent we went already (round it)
          var percentage = Math.round((Math.floor((Date.parse(today) - Date.parse(set.dateStart )) / 86400000))/(duration/100));
          // Count how many pixels represents 1 day
          var unit = width / duration;
          // Just to make sure there is nothing missing
          var leftOver = width-(unit*duration);
          // Count new width from left over + duration from beginning until today
          var newWidth = leftOver+((Math.floor((Date.parse(today) - Date.parse(set.dateStart )) / 86400000)*unit));
        }
        // Now we can set skin and width to the progress bar and output percentage
        $('#progress-percentage').html(percentage);
        
        $('.bar-left').css('background','url(images/bars/bar-left-'+set.skin+'.png) no-repeat');
        $('.bar-right').css('background','url(images/bars/bar-right-'+set.skin+'.png) no-repeat');
        $('.bar-tile').css('background','url(images/bars/bar-tile-'+set.skin+'.png) repeat-x');
        
        $('.bar-left').width(11);
        
        $('.bar-tile').animate({width:newWidth},newWidth*4,function(){
          $('.bar-right').animate({width:11},200);  
        });
          
      });
         
    }
  });  
}) (jQuery);
