function incJs(filename)
{
var body = document.getElementsByTagName('body').item(0);
script = document.createElement('script');
script.src = filename;
script.type = 'text/javascript';
body.appendChild(script)
}

function incCss(filename)
{
var body = document.getElementsByTagName('body').item(0);
script = document.createElement('link');
script.src = filename;
script.type = 'text/css';
script.rel = 'stylesheet';
body.appendChild(script)
}


//incJs("../../yui/build/calendar/calendar.js");
//incCss("../../yui/build/calendar/assets/calendar.css");


function DateChooser(buttonId, dateChoosed, data, calContainerId, calContentId) {
  this.cal = null;
  this.dlg = null;
  this.buttonId = buttonId;
  this.dateChoosed = dateChoosed;
  this.visible = false;
  this.data = data;
  this.calContainerId = (calContainerId || 'calContainer');
  this.calContentId = (calContentId || 'calContent');

  this.calCfg = {DATE_FIELD_DELIMITER:'.',MDY_DAY_POSITION:1,MDY_MONTH_POSITION:2,MDY_YEAR_POSITION:3,
                  MD_DAY_POSITION:1,MD_MONTH_POSITION:2, START_WEEKDAY: 1, 
    MONTHS_SHORT:["Яну", "Фев", "Мар", "Апр", "Май", "Юни", "Юли", "Авг", "Сеп", "Окт", "Ное", "Дек"],
  	YMONTHS_LONG:["Януари", "Февруари", "Март", "Април", "Май", "Юни", "Юли", "Август", "Септември", "Октомври", "Ноември", "Декември"], 
  	YWEEKDAYS_1CHAR:["Н", "П", "В", "С", "Ч", "П", "С"],
  	YWEEKDAYS_SHORT:["Нд", "Пн", "Вт", "Ср", "Чт", "Пт", "Сб"],
  	YWEEKDAYS_MEDIUM:["Нед", "Пон", "Вто", "Сря", "Чет", "Пет", "Съб"],
  	YWEEKDAYS_LONG:["Неделя", "Понеделник", "Вторник", "Сряда", "Четвъртък", "Петък", "Събота"]
  }
    
    
  this.createHtml = function() {
    //var body = document.getElementsByTagName('body').item(0);
    //container = document.createElement('div');container.id = 'calContainer';body.appendChild(container);
    //bdCont = document.createElement('div');bdCont.className='bd';container.appendChild(bdCont);
    //calcDiv = document.createElement('div');calcDiv.id='calContent';bdCont.appendChild(calcDiv);
  }
                                                    
  	
  this.createCalendar = function() {
    this.cal = new YAHOO.widget.Calendar(this.calContentId,this.calContainerId, 
    {close: false, LOCALE_WEEKDAYS: "short", START_WEEKDAY: 1}
     //   this.calCfg
      );
    //for( var prop in this.calCfg ) { 
    //  this.cal.cfg.setProperty(prop ,this.calCfg[prop]);
   //} 
    
    this.cal.cfg.setProperty("DATE_FIELD_DELIMITER", "."); 
	 
  	this.cal.cfg.setProperty("MDY_DAY_POSITION", 1); 
  	this.cal.cfg.setProperty("MDY_MONTH_POSITION", 2); 
  	this.cal.cfg.setProperty("MDY_YEAR_POSITION", 3); 
  	 
  	this.cal.cfg.setProperty("MD_DAY_POSITION", 1); 
  	this.cal.cfg.setProperty("MD_MONTH_POSITION", 2); 

    this.cal.cfg.setProperty("MONTHS_SHORT",   ["Яну", "Фев", "Мар", "Апр", "Май", "Юни", "Юли", "Авг", "Сеп", "Окт", "Ное", "Дек"]); 
  	this.cal.cfg.setProperty("MONTHS_LONG",    ["Януари", "Февруари", "Март", "Април", "Май", "Юни", "Юли", "Август", "Септември", "Октомври", "Ноември", "Декември"]); 
  	this.cal.cfg.setProperty("WEEKDAYS_1CHAR", ["Н", "П", "В", "С", "Ч", "П", "С"]); 
  	this.cal.cfg.setProperty("WEEKDAYS_SHORT", ["Нд", "Пн", "Вт", "Ср", "Чт", "Пт", "Сб"]); 
  	this.cal.cfg.setProperty("WEEKDAYS_MEDIUM",["Нед", "Пон", "Вто", "Сря", "Чет", "Пет", "Съб"]); 
  	this.cal.cfg.setProperty("WEEKDAYS_LONG",  ["Неделя", "Понеделник", "Вторник", "Сряда", "Четвъртък", "Петък", "Събота"]); 
    

    this.cal.render();
    YAHOO.util.Event.addListener(this.buttonId, "click", this.clickHandler, (!this.data ? this : this.data), true);
    this.cal.selectEvent.subscribe(this.showHanler, (!this.data ? this : this.data), true); 
  }
  
  this.createDialog = function() {
    this.dlg = new YAHOO.widget.Overlay(this.calContainerId, { 
															 // width:'159px',
															  //height:'200px',
															  visible : false, 
															  iframe: true,
															  zIndex: 10000, 
                                context: [this.buttonId, "tr", "tl"]
															 } );


    this.dlg.render();   
  }
  
  this.isDialogEl = function(el) {
    return (el == this.dlg.oDomContainer || YAHOO.util.Dom.isAncestor(this.dlg.oDomContainer, el) || 
            el == this.cal.oDomContainer || YAHOO.util.Dom.isAncestor(this.cal.oDomContainer, el)
    );
  }  
  
  this.documentClickHandler = function(e) {
    if (this.firstClick) {
      this.firstClick = false;
      return;
    }
    var el = YAHOO.util.Event.getTarget(e);
  
    if (!this.isDialogEl(el)) {
      this.hide();
    }
  }
  
  this.clickHandler = function(type,obj) {
      obj.show();
  }
  
  this.showHanler = function(type,args,obj) {
    
    if (obj.dateChoosed) {
			var dates = args[0];
			var date = dates[0];
			var year = date[0], month = date[1], day = date[2];
                  
      obj.dateChoosed(day + "." + month + "." + year, {type:type,args:args,obj:obj});
    }
    obj.hide();
  }
  
  this.init = function() {
     this.createHtml();
     this.createCalendar();
     this.createDialog();
  }
  
  this.hide = function() {
    this.dlg.hide();
    this.visible = false;
    YAHOO.util.Event.removeListener(document, "click", this.documentClickHandler);
    YAHOO.util.Event.addListener(this.buttonId, "click", this.clickHandler, (!this.data ? this : this.data), true);
  }

  this.show = function() {
    this.dlg.align("tl", "tr");  
    this.dlg.show(); 
    this.visible = true;
    
    YAHOO.util.Event.removeListener(this.buttonId, "click", this.clickHandler);
    this.firstClick = true;
    YAHOO.util.Event.addListener(document, "click", this.documentClickHandler, this, true);  
    
  }
  
  this.init();
}



function DateChooserTextBox(buttonId, textBoxId, dateChoosed, data, calContainerId, calContentId) {
  this.buttonId = buttonId;
  this.dateChoosedUserHandler = dateChoosed;
  this.textBoxId = textBoxId;
  this.visible = false;
  this.calContainerId = (calContainerId || 'calContainer');
  this.calContentId = (calContentId || 'calContent');
  
  this.dateChoosed = function (choosedDate, obj) {
    var fillTextBox = true;
    if (obj.obj.dateChoosedUserHandler) fillTextBox = obj.obj.dateChoosedUserHandler(choosedDate, obj);
    if (fillTextBox) {
       var txtBox = document.getElementById(obj.obj.textBoxId);
       if (txtBox) {
         txtBox.value = choosedDate;
       }
    } 
    
  }
  
  
  this.dateChooser = new DateChooser(this.buttonId, this.dateChoosed, this, this.calContainerId, this.calContentId);
  
  this.hide = function() {this.dateChooser.hide();}
  this.show = function() {this.dateChooser.show();}  
}



