$(function() {
    for(i=0;i<7;i++){
        $('#DraggableItem' + i).draggable({ containment: '#Content', scroll: false, stack: '.Draggable' });
        if($.cookie('#DraggableItem' + i) != 'hide') {
            OpenClose(i);
        }
    }
});
function OpenClose(p_intId) {
    if($('#DraggableItem' + p_intId).is(':visible')) {
        $('#DraggableItem' + p_intId).hide();
        $('#DraggableCheck' + p_intId).attr('class', 'NotChecked');
        $.cookie('#DraggableItem' + p_intId, 'hide', { expires: 365 });
    } else {
        $('#DraggableItem' + p_intId).show();
        $('#DraggableCheck' + p_intId).attr('class', 'Checked');
        $.cookie('#DraggableItem' + p_intId, 'show', { expires: 365 });
    }
}

function Time(p_blnAdd, p_strBackgroundClientId, p_strTimeClientId) {
    var l_strTime = $('#' + p_strTimeClientId).html();
    var l_intHour = parseInt(l_strTime.substring(0,2), 10);
    var l_intMinutes = parseInt(l_strTime.substring(3,5), 10);
    var l_strHour = l_intHour.toString();
    var l_strMinutes;
    if(p_blnAdd) {
        if(l_intMinutes==45) {
            if(l_intHour==23) {
                l_strHour = '00';
            } else {
                l_intHour++;
                l_strHour = l_intHour.toString();
            }
            l_strMinutes = '00';
        } else {
            l_intMinutes+=15;
            l_strMinutes = l_intMinutes.toString();
        }
    } else {
        if(l_intMinutes==00) {
            if(l_intHour==0) {
                l_strHour = '23';
            } else {
                l_intHour--;
                l_strHour = l_intHour.toString();
            }
            l_strMinutes = '45';
        } else {
            l_intMinutes-=15;
            l_strMinutes = l_intMinutes.toString();
        }
    }
    if(l_strHour.length==1) {
        l_strHour = '0' + l_strHour;
    }
    if(l_strMinutes.length==1) {
        l_strMinutes = '0' + l_strMinutes;
    }
    $('#' + p_strTimeClientId).html(l_strHour + ":" + l_strMinutes)
    $.get('/ajax/Home/BackgroundImage.aspx?Hour=' + l_strHour +'&Minutes=' + l_strMinutes + '&Season=' + m_strSeasonId, function(p_objData) {
        $('<img />', {'src': p_objData, 'class': 'BackGround' }).prependTo(
            $('#BackGroundContainer').children('img').fadeOut(1000, function() {
                $(this).remove();
            }
        ).end());
    });
    $.get('/ajax/Home/BackgroundMusic.aspx?Hour=' + l_strHour +'&Minutes=' + l_strMinutes + '&Season=' + m_strSeasonId, function(p_objData) {
        m_strMusic = p_objData;
    });
}

var m_strSeasonId;
var m_strMusic='';

function Season(p_strChosen, p_strWinter, p_strSpring, p_strSummer, p_strAutumn, p_strDbId, p_strBackgroundClientId, p_strTimeClientId) {
    m_strSeasonId = p_strDbId;
    $('#' + p_strWinter).css('color','#fff');
    $('#' + p_strSpring).css('color','#fff');
    $('#' + p_strSummer).css('color','#fff');
    $('#' + p_strAutumn).css('color','#fff');
    $('#' + p_strChosen).css('color','#2a3102');
    
    var l_strTime = $('#' + p_strTimeClientId).html();
    var l_intHour = parseInt(l_strTime.substring(0,2), 10);
    var l_intMinutes = parseInt(l_strTime.substring(3,5), 10);
    var l_strHour = l_intHour.toString();
    var l_strMinutes = l_intMinutes.toString();
    if(l_strHour.length==1) {
        l_strHour = '0' + l_strHour;
    }
    if(l_strMinutes.length==1) {
        l_strMinutes = '0' + l_strMinutes;
    }
    $.get('/ajax/Home/BackgroundImage.aspx?Hour=' + l_strHour +'&Minutes=' + l_strMinutes + '&Season=' + m_strSeasonId, function(p_objData) {
        $('<img />', {'src': p_objData, 'class': 'BackGround' }).prependTo(
            $('#BackGroundContainer').children('img').fadeOut(1000, function() {
                $(this).remove();
            }
        ).end());
    });
    $.get('/ajax/Home/BackgroundMusic.aspx?Hour=' + l_strHour +'&Minutes=' + l_strMinutes + '&Season=' + m_strSeasonId, function(p_objData) {
        m_strMusic = p_objData;
    });
}

function SetFireHazard(p_objThis) {
    $.get('/ajax/Home/FireHazard.aspx', function(p_objData) {
        $(p_objThis).css('left', p_objData);
     });
}

var m_objListener = new Object();
m_objListener.onInit = function() {
    this.position = 0;
};
m_objListener.onUpdate = function() {
    var isPlaying = this.isPlaying;
    var url = this.url;
    var volume = this.volume;
    var position = this.position;
    var duration = this.duration;
            	
    var id3_artist = this.id3_artist;
    var id3_album = this.id3_album;
    var id3_songname = this.id3_songname;
    var id3_genre = this.id3_genre;
    var id3_year = this.id3_year;
    var id3_track = this.id3_track;
    var id3_comment = this.id3_comment;
};


function getFlashObject() {
    return document.getElementById("Mp3Player");
}

function PlayMusic(p_strUrlMp3, p_objThis) {
    var l_strUrlMp3 = p_strUrlMp3;
    if(m_strMusic!='') {
        l_strUrlMp3 = m_strMusic;
    }
    if (m_objListener.position == 0) {
        getFlashObject().SetVariable("method:setUrl", l_strUrlMp3);
        getFlashObject().SetVariable("method:play", "");
        p_objThis.src='/images/home/MusicOn.png';
    } else {
        getFlashObject().SetVariable("method:stop", "");
        p_objThis.src='/images/home/MusicOff.png';
    }
    getFlashObject().SetVariable("enabled", "true");
}


