/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

var ProgramGlobal;

function setHooverTips()
{
    //$('a[title!=""],input[title!=""],select[title!=""],img[title!=""],.ico-help[title!=""],span.tooltip').tooltip({
    console.log('setHooverTips') ;

    //return;

    console.debug(jQuery(".Action"));

    /*

   jQuery(".Action").tooltip({
		track: false,
		delay: 0,
		showURL: true,
		//showBody: " aa - ",
		//extraClass: "pretty",
		fixPNG: true,
		opacity: 0.95,
		//left: -120
		top: 5,
		left: 5
	});

    */

    //simpletipApi = jQuery(".Action").simpletip();
    /*
   jQuery(".Action a,.item_name,a").simpletip(
    {
       // content: 'tooltop text'
        
        //fixed: false
        
        position: 'top'
        ,relative: [10,10]
        
    }
    );
*/
    // Access the API of a previously initatied simpletip var api = $('JQUERY SELECTOR').eq(0).simpletip();
    // Perform a simple API call to update tooltip contents api.update('New tooltip content!');

    jQuery('#datepicker td.Action').qtip(
    {
        //tip: true
        start : true

        ,
        prerender : true

        ,
        content: {
        //text: 'I really like owls!',
        //title: { text: 'Program:' }
        }
        ,
        hide: {
            fixed: true // Make it fixed so it can be hovered over !!
            ,
            delay: 1000
        //,effect: 'slide'
        }
        ,
        show: {
            //effect: 'slide'
            solo: true
        }

        ,
        position: {
            corner: {
                //target: 'topRight',
                //tooltip: 'bottomLeft'
                target: 'rightMiddle',
                tooltip: 'leftMiddle'
            }
        }

        ,
        style: {
            width:
            {
                min: 75
                ,
                max: 1000
            }
        }

    }

    );

/*
   jQuery('.Action a,img').each(function()
   {
      jQuery(this).qtip(
      {
         content: '<a href="#">Edit</a> | <a href="#">Delete</a>', // Give it some content
         position: 'topRight', // Set its position
         hide: {
            fixed: true // Make it fixed so it can be hovered over !!
         },
         style: {
            padding: '5px 15px', // Give it some extra padding
            name: 'dark' // And style it with the preset dark theme
         }
      });
   });
    */

}

//loadCalendarData = function ()  {};
loadCalendarData = function (month,year,inst)
{
     console.log('instance datepickeru');
     console.debug(inst);


     //var datePicker = jQuery("#datepicker").datepicker;

     jQuery("#datepicker .ui-datepicker-calendar").hide();
     jQuery("#datepicker").datepicker( 'disable' ) ;
     
     //glob_datepicker.datepicker( 'disable' ) ;
     /*
     if (typeof datePicker != 'undefined')
         {
        // if (glob_datepicker != undefined)
            datePicker( 'disable' );
         }
    */
   
     if (true)
            {

                //var xmlHttpR = jQuery.getJSON('/js/getCalendarToday.php/?date_search_m=' + month + '&date_search_y=' + year + '', null,
                var xmlHttpR = jQuery.getScript('/js/getCalendarToday.php/?date_search_m=' + month + '&date_search_y=' + year + '',
                    function (jsonData, textStatus) {

                        console.log('global data Program changed:');
                        console.debug(Program);
                        //console.debug(jsonData);
                        //console.debug(jsonData);

                        //ProgramGlobal = jsonData;
                        ProgramGlobal = Program;

                        //inst._notifyChange($.datepicker._curInst);

                        //zavolat datepicker update udalost
                        console.log('updating calendar:');

                        //glob_datepicker.datepicker.onChangeMonthYear();

                      
                        //glob_datepicker._updateDatepicker();
                        //if (typeof glob_datedatePickerpicker != 'undefined')
                        {
                            console.log('INST updating calendar:');
                            jQuery.datepicker._updateDatepicker(inst);
                        }

                          
                        jQuery("#datepicker").datepicker( 'enable' ) ;
                        jQuery("#datepicker .ui-datepicker-calendar").show();



                    }

                    );
            }


};


//jQuery.datepicker._setHoovers = function () {}; // setHooverTips;
jQuery.datepicker._setHoovers = setHooverTips;

//jQuery.datepicker._loadData = loadCalendarData;

jQuery.datepicker.debug =  true;  

/*

jQuery.datepicker.oldAdjustDate = jQuery.datepicker._adjustDate;

//overriding a datepicker core function
jQuery.datepicker._adjustDate = function(id, offset, period)
{
    this.oldAdjustDate(id, offset, period);
    //to ensure that the old code still runs
    this._notifyChange($.datepicker._curInst);

    ////this ensures that the onChangeMonthYear event is fired correctly }
}
*/


jQuery.datepicker.oldUpdateDatepicker = jQuery.datepicker._updateDatepicker;

jQuery.datepicker._updateDatepicker = function(inst)
{
    this.oldUpdateDatepicker(inst);
    //to ensure that the old code still runs

    console.log('new _updateDatepicker');

    
    //this._loadData();
    
    this._setHoovers();
////this ensures that the onChangeMonthYear event is fired correctly }
}


jQuery(document).ready(function()
{


    jQuery.datepicker.setDefaults(jQuery.datepicker.regional['cs']);

    //jQuery.datepicker._updateDatepicker(glob_datepicker)

    glob_datepicker = jQuery('#datepicker').datepicker({
        inline: true,
        dateFormat: 'dd.mm.yyyy',
        showButtonPanel:false,
        changeMonth: true,
        changeYear: true,
        appendText: 'aa(yyyy-mm-dd)',

        obnov : function ()
        {

        }
        ,
        onChangeMonthYear: function(year, month, inst)
        {
            //alert('menim year month.')

            console.log('menim mesic rok nacist aktualni data');

            

            //console.debug(Program);

            loadCalendarData(month,year,inst);


            //console.debug(jQuery(".Action a"));

           

        //setHooverTips();

        }
        ,
        onSelect: function(dateText, datePick)
        {
            //alert('menim datum.')

            //location.href =
            console.debug(datePick);
            console.log(datePick.selectedDay);

        //alert(datePick.selectedDay);
        //alert('tt');
        //jQuery(".ui-datepicker td a").

        //zjistim den

        //pro den najdu v poli zaznam akcí

        //vykreslim html a

        //vygeneruju dialog ? na kliknuti a obsah ?

            

        },
    
        beforeShowDay: function(date)
        {
            //ziskat globalni promenou a projit zda je den v seznamu udalosti

            var datum = new Date();

            var poleAkci = new Array();
            var dayInfo = new Array();
           
            //prijede z PHP

            //akce test
            /*
            poleAkci[1] = new Array();
            poleAkci[2] = 'Super akce pravidelne 2 den v mesici.';

            poleAkci[10] = 'Super akce pravidelne 10 den v mesici.';
            */

            poleAkci = Program;

            //console.log(date.getDate());

            var denVmesici = date.getDate();
            //ano

            //akce vsechny
            if (poleAkci[denVmesici] != undefined)
            {

                console.log ('Akce je: ' + denVmesici );

                dayInfo[0] = true;
                //class name
                dayInfo[1] = 'Action';
                //zobrazen

                //dayInfo[2] = "Program: ";
                dayInfo[2] = "";
                //najit vsechny akce pro dany den

                //console.debug(poleAkci[denVmesici]);

                //var AkceDen = poleAkci[denVmesici];

                var akce= '';

                //vice akci v jednom dnu
                for (var i = 0; i < poleAkci[denVmesici].length; i++)
                {
                    //console.log('jedu pres akce');

                    //console.debug(poleAkci[denVmesici][i]);
                    akce = poleAkci[denVmesici][i];
                    
                    //dayInfo[2] += '<a href="#"> ' + poleAkci[denVmesici][i][1]+ '</a>';
                    dayInfo[2] += '<a href=\'/program/' + akce[3] + '/' + akce[2] + '\'> ' + akce[1] + '</a>'; 

                    if (poleAkci[denVmesici].length-1 != i)
                    {
                        dayInfo[2] += '<br /> ';
                    }

                //dayInfo[2] = 'Na Programu';
                   

                //dayInfo[2] = 'dnes hrajeme si na divadlo ;) ';

                }

            }
            else
            {
                dayInfo[0] = false;
                //class name
                dayInfo[1] = 'noAction';
                //zobrazen
                dayInfo[2] = '';
            }
              
            //ne
            
            //info

            //console.log('nastavuju hovery jsou akce?  ');
            //nelze nastavit hovery protoze se generuji pozdeji nez je nyni nastavime
            //setHooverTips();
            //console.debug(jQuery(".Action"));
            
         
            //zobrazen
         
            return dayInfo;
        }





    });



//setHooverTips();




});


  


