/***********************************
File:    sgcms.js
Author  : filippo pacini <filippo.pacini@gmail.com>
License :
The contents of this file are subject to the Mozilla Public
License Version 1.1 (the "License"); you may not use this file
except in compliance with the License. You may obtain a copy of
the License at http://www.mozilla.org/MPL/

Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
the License for the specific language governing rights and
limitations under the License.
The Initial Developer of the Original Code is S.G. Consulting
srl. Portions created by S.G. Consulting s.r.l. are Copyright (C)
2008 S.G. Consulting srl. All Rights Reserved.

Dependencies:
- sgjs
- jquery

************************************/
function SGItem(iditem, title, start_date, end_date) {
  this.id = iditem;
  this.title = title;
  this.date = start_date;
  this.end_date = end_date;
  return this;
};

/* sgCal is an object that wraps different calendar implementation
 * Right now the only supported calendar is ui.jquery.datepicker
 */
var sgCal = {
  highlightDates: function(calObj, selDates, calType) {
    if (calType == 'ui.jquery.datepicker') {
      var selEls = calObj.find('table > tbody > tr > td > a');
      selEls.each(
	function(){if (selDates.indexOf(jQuery(this).html())>=0) {
		     jQuery(this).addClass('ui-state-myhighlight');
		   } else {
		     jQuery(this).parent().addClass('ui-datepicker-unselectable');
		   }
		  });
    } else {
      throw {name: 'sgCalException', message: 'Unsupported Calendar Type'};
    }
  },

  getJSDate: function(calObj, calType) {
    // returns a js Date object
    if (calType == 'ui.jquery.datepicker') {
      return calObj.datepicker('getDate');
    } else {
      throw {name: 'sgCalException', message: 'Unsupported Calendar Type'};
    }
  },

  getDate: function(calObj, calType) {
    // returns the selected date (integer between 1 and 31)
    if (calType == 'ui.jquery.datepicker') {
      return calObj.datepicker('getDate').getDate();
    } else {
      throw {name: 'sgCalException', message: 'Unsupported Calendar Type'};
    }
  },

  getMonth: function(calObj, calType) {
    // returns the selected month (integer between 1 and 12)
    if (calType == 'ui.jquery.datepicker') {
      return calObj.datepicker('getDate').getMonth()+1;
    } else {
      throw {name: 'sgCalException', message: 'Unsupported Calendar Type'};
    }
  },

  getYear: function(calObj, calType) {
    // returns the selected year (integer e.g. 2009)
    if (calType == 'ui.jquery.datepicker') {
      return calObj.datepicker('getDate').getFullYear();
    } else {
      throw {name: 'sgCalException', message: 'Unsupported Calendar Type'};
    }
  },

  changeMonthYear: function(calObj, calType, cbFun) {
    //apply a the cbFun callback to the next button in calendar
    if (calType == 'ui.jquery.datepicker') {
      jQuery('.ui-datepicker-prev').attr(
	'href',
	cbFun(sgCal.getYear(calObj, calType), sgCal.getMonth(calObj, calType)-1)
      );
      jQuery('.ui-datepicker-next').attr(
	'href',
	cbFun(sgCal.getYear(calObj, calType), sgCal.getMonth(calObj, calType)+1)
      );
    } else {
      throw {name: 'sgCalException', message: 'Unsupported Calendar Type'};
    }

  }
};


var sgItems = {
  repr: function(list) {
    for (var i=0, l=list.length; i<l; i++) {
      console.log(list[i]);
    }
  },

  selectedDates: function (itemList, currYear, currMonth) {
    var selDates = sgFun.map(function(item) {return sgItems._extractDay(item.date, item.end_date, currYear, currMonth);}, itemList);
    selDates = selDates.flatten();
    //console.log(selectedDates);
    return selDates;
  },

  toCal: function(idCal, itemList, baseUrl, calType) {
    // highlights dates on calendar
    var t = typeof idCal;
    var cal;
    var newUrl = baseUrl || '';
    if (t === 'string') {
      cal = jQuery('#'+idCal);
    } else {
      cal = idCal;
    }
    var selectedDates = sgItems.selectedDates(itemList, sgCal.getYear(cal, calType), sgCal.getMonth(cal, calType));
    // highlight dates and add click callback
    sgCal.highlightDates(cal, selectedDates, 'ui.jquery.datepicker');
    // set next and previous calendar button:
    var clickCbMonth = function(year, month) {
      return newUrl+'eventi.html?year='+year+'&month='+month;
    };
    sgCal.changeMonthYear(cal, 'ui.jquery.datepicker', clickCbMonth);
  },

  _extractDay: function(startDate, endDate, currY, currM) {
    var startY = startDate.toString().substr(0,4);
    var startM = startDate.toString().substr(4,2);
    var startD = parseInt(startDate.toString().substr(6,2));
    var endY = startDate.toString().substr(0,4);
    var endM = endDate.toString().substr(4,2);
    var endD = parseInt(endDate.toString().substr(6,2));
    var res = [];
    if (endY>currY) {
      // la data fine e' in un anno successivo quindi vado fino al 31
      endD = 31;
    } else if (endM>currM) {
      // la data fine e' in un mese successivo quindi vado fino al 31
      endD = 31;
    }
    if (startY<currY) {
      // la data inizio e' nell'anno precedente quindi parto da 1
      startD = 1;
    } else if (startM<currM) {
      // la data inizio e' in un mese precedente quindi parto dal 1
      startD = 1;
    }
    for (var i=startD; i<=endD; i++) {
      res[res.length] = i.toString();
    }
    return res;
  }
};

