﻿// Radio
function launchRadio()
{
    _popup = window.open("/Radio/Radio.aspx","WebRadio","dependent=yes,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no,titlebar=no,width=260,height=220");
    //_popup.blur();
    //window.focus();
}
function closeRadio()
{
    _popup = window.open("","WebRadio","width=0,height=0");
    if(_popup != null)
    {
        _popup.close();
        window.focus();    
    }
}
// Initialization
_currentYear = 0;
_currentmonth = 0;
_timer = null;
_root_vignette = "";

$(document).ready(function() {
    initCalendar();
    initSlider();
    initMenu();
});
function initCalendar() {
    _Now = new Date();
    _DateSelected = _Now;
    _D = $.QueryString["D"];
    _M = $.QueryString["M"];
    _Y = $.QueryString["Y"];
    $("#divEventCalendar").datepicker({
        changeMonth: false,
	    changeYear: false,
	    dateFormat: "dd/mm/yyyy",
	    dayNames: ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"],
	    dayNamesMin: ["Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa"],
	    monthNames: ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"],
	    monthNamesShort: ["Jan", "Fév", "Mars", "Avr", "Mai", "Juin", "Juil", "Août", "Sept", "Oct", "Nov", "Déc"],
	    nextText: "Suivant",
	    prevText: "Précédent",
	    gotoCurrent: true,
	    firstDay: 1,
	    minDate: _Now,
	    maxDate: new Date(_Now.getFullYear() + 1, _Now.getMonth(), _Now.getDate()),
	    onChangeMonthYear: function (year, month, inst)
	    {
	        _currentYear = year;
            _currentmonth = month;
            WriteBuffer("year", year);
            WriteBuffer("month", month);
	        _timer = setInterval("disableDays()", 200);
	    },
	    onSelect: function(dateText, inst) {
	        try
	        {
	            _location = document.location.pathname;
	            _index = _location.indexOf("/", 1);
	            _location = _location.substring(0, _index + 1) + _root_vignette;
	            _D = dateText.substring(0, 2)
	            _M = dateText.substring(3, 5)
	            _Y = dateText.substring(6, 10)
	            _location = _location + "?D=" + _D + "&M=" + _M + "&Y=" + _Y;
	            document.location = _location;	        
	        }
	        catch(e)
	        {
	            alert(e.message);
	        }
	    }
    });
    _currentYear = _Now.getFullYear();
    _currentmonth = _Now.getMonth() + 1;
    if(ReadBuffer("year") != undefined && ReadBuffer("month")  != undefined) {
        _DateSelected = new Date(ReadBuffer("year"), ReadBuffer("month") - 1, 1);
        $("#divEventCalendar").datepicker("setDate", _DateSelected);
    }
    _timer = setInterval("disableDays()", 200);
}
function disableDays()
{
    clearInterval(_timer);
    $.ajax({
        type: "POST",
        url: "/Ashx/Calendar.ashx",
        data: "y=" + _currentYear + "&m=" + _currentmonth,
        async: false,
        success: function(data) {
            _Array = data.split(";");
            $(".ui-datepicker-calendar TD").each(function() {
                $(this).removeClass("ui-state-disabled");
                $(this).removeClass("ui-datepicker-unselectable");
                _i = $("A", this).html();
                if(jQuery.inArray(_i, _Array) == -1) {
                    $(this).addClass("ui-state-disabled");
                    $(this).addClass("ui-datepicker-unselectable");
                    if($("A", this).length > 0) {
                        _html = $("A", this).html();
                        $(this).html("<span class=\"ui-state-default\">" + _html + "</span>");
                    }
                }
            });
        }
    });
}
function initSlider() {
    $(".slidetabs").tabs(".images > div", {
	    effect: 'fade',
	    fadeOutSpeed: 'slow',
	    rotate: true
    }).slideshow({
        autoplay: true,
        interval: 7000
    });
}
function initMenu() {
    _LI = $(".selected_node");
    while(_LI[0] != null && _LI[0] != undefined && _LI[0].tagName == "LI") {
         $("> UL > LI", _LI).show();
        _LI = _LI.parent().parent();
    }
}
/* Buffer */
var Buffer = new Array();

function SetBuffer(name, value) {
    Buffer[name] = value;
}

function WriteBuffer(name, value) {
    SetBuffer(name, value);
    $.ajax({
        type: "POST",
        url: "/Ashx/Cookie.ashx",
        data: "n=" + name + "&v=" + value,
        async: true  
    });
}

function ReadBuffer(name) {
    if(Buffer[name] == null) {
        $.ajax({
            type: "POST",
            url: "/Ashx/Cookie.ashx",
            data: "n=" + name,
            async: false,
            success: function(data) {
                SetBuffer(name, data);   
            }
        });    
    }
    return Buffer[name]; 
}
