/************************************************\
' Qbrix software picture shifter object          '
'************************************************'
' All code is made and owned by Qbrix software.  '
' You are not allowed to change the code or to   '
' copy it to your own server.                    '
'                                                '
' All rights reserved by Qbrix software 2007     '
\************************************************/

// Declare a single namespace in the global scope
var dk;
if (!dk) dk = {};
else if (typeof(dk) != 'object')
	throw new Error('Der findes allerede en global variabel ved navn \u0027dk\u0027, som ikke er et object');
if (!dk.solbjergkirke) dk.solbjergkirke = { };
else if (typeof(dk.solbjergkirke) != 'object')
	throw new Error('Der findes allerede en global variabel ved navn \u0027dk.solbjergkirke\u0027, som ikke er et object');

dk.solbjergkirke.Picture = 
{
    Picture : null,
    TempPicture : null,
    OpacityTimer : null,
    HeaderNum: -1,
    LeftSideNum: [-1, -1],
    
    
    GetHeaderNum: function()
    {
        var that = dk.solbjergkirke.Picture;
        var d = new Date();
        var m = d.getMonth();
        
        var r = that.HeaderNum;
        while (r == that.HeaderNum)
        {
            switch (m)
            {
                case 11:
                    r = (Math.floor(Math.random() * 12) + 1);
                    break;
                case 0:
                case 1:
                case 10:
                    r = (Math.floor(Math.random() * 11) + 1);
                    break;
                default:
                    r = (Math.floor(Math.random() * 10) + 1);
                    break;
            }
        }
        that.HeaderNum = r;
        r = r.toString();
        while (r.length < 2) r = '0' + r;
        return r;
    },
    GetLeftSideNum: function()
    {
        var that = top.dk.solbjergkirke.Picture;
        var r = that.LeftSideNum[0];

        while (r == that.LeftSideNum[0] || r == that.LeftSideNum[1])
            r = (Math.floor(Math.random() * 12) + 1);
        that.LeftSideNum.push(r);
        that.LeftSideNum.shift();

        r = r.toString();
        while (r.length < 2) r = '0' + r;
        
        return r;
    },
    
    WriteHeaderImage: function()
    {
        var r = this.GetHeaderNum();
        var s = '<img src=\"Images/Banners/imgBanner' + r + '.jpg\" id=\"picBanner\" alt=\"Solbjerg kirke\" />';
        document.write(s);
    },
    WriteLeftSideImage: function(index)
    {
        var r = this.GetLeftSideNum();
        var s = '<img src=\"Images/LeftsidePics/imgLeft' + r + '.jpg\" id=\"picLeftside' + (index+1) + '\" alt=\"Solbjerg kirke\" />';
        document.write(s);
    },
    
    ShiftPicture: function(picture_id)
    {
        var that = dk.solbjergkirke.Picture;
        
        // Release memory
        if (that.Picture) delete that.Picture;
        if (that.TempPicture) delete that.TempPicture;
        if (that.OpacityTimer) delete that.OpacityTimer;
        that.Picture = null;
        that.TempPicture = null;
        that.OpacityTimer = null;

        that.Picture = document.getElementById(picture_id);
        if (that.Picture == null)
            return;

        var num, s;
        if (picture_id == 'picBanner')
        {
            num = that.GetHeaderNum();
            s = 'Images/Banners/imgBanner';
        }
        else
        {
            num = that.GetLeftSideNum();
            s = 'Images/LeftsidePics/imgLeft';
        }

        // Create new temporary picture
        that.TempPicture = new Image();
        QB.Object.AttachEvent(that.TempPicture, 'load', that.ReplacePicture);
        that.TempPicture.src = s + num + '.jpg';
        that.TempPicture.alt = 'Solbjerg kirke';
        //that.TempPicture.onload = that.ReplacePicture;
    },
    AppendError : function(error_msg)
    {
        var da = new Date();
        var elmx = document.getElementById('main-page');
        elmx.appendChild(document.createTextNode(error_msg + ' - ' + da.toLocaleString()));
        elmx.appendChild(document.createElement('br'));
    },
    ReplacePicture : function()     // Called from event, so 'this' refers to 'window'
    {
        var that = dk.solbjergkirke.Picture;
        var pic = that.Picture;
        var temp = that.TempPicture;
        if (!pic || !temp)
            return;

        // Set tempimage properties. This function is called from more than one
        // place, so don't move these lines to 'UpdateImage()' function.
        temp.id = pic.id + 'temp';
        temp.style['position'] = 'absolute';
        temp.style['left'] = QB.Object.GetPosition(pic)[0].toString() + 'px';
        temp.style['top']  = QB.Object.GetPosition(pic)[1].toString() + 'px';

        // Append new image right after old image
        pic.parentNode.appendChild(temp);
        
        // Initially set full opacity on new image
	    if (typeof(temp.style['opacity']) == 'string')
	        temp.style['opacity'] = '0.0';
	    else if (typeof(temp.style['filter']) == 'string')
	    	temp.style['filter'] = 'alpha(opacity=0)'; 

        // Start image switch with opacitytimer
        if (that.OpacityTimer) 
        {
            delete that.OpacityTimer;
            that.OpacityTimer = null;
        }
        that.OpacityTimer = new dk.qbrix.Timer(1000, 50, dk.solbjergkirke.Picture.SetOpacity, dk.solbjergkirke.Picture.CleanupOpacity);
        that.OpacityTimer.start();
    },
    SetOpacity : function(counter)     // Called from timer, so 'this' refers to 'window'
    {
        var that = dk.solbjergkirke.Picture;
        
        if (that.OpacityTimer == null || that.TempPicture == null)
            return;
        if (that.Picture == null)
            return;
            
	    // Set opacity
	    if (typeof(that.Picture.style['opacity']) == 'string')
	    {
	        // FireFox, Opera specific
	        that.Picture.style['opacity'] = (1 - 0.02*counter).toString();
	        that.TempPicture.style['opacity'] = (0.02*counter).toString();
	    }
	    else //if (typeof(that.Picture.style['filter']) == 'string')
	    {
	        // IE specific
	    	that.Picture.style['filter'] = 'alpha(opacity=' + (100 - 2*counter).toString() + ')'; 
	    	that.TempPicture.style['filter'] = 'alpha(opacity=' + (2*counter).toString() + ')'; 
	    }
    },
    CleanupOpacity : function()
    {
        delete dk.solbjergkirke.Picture.OpacityTimer;
        dk.solbjergkirke.Picture.OpacityTimer = null;
    
        // Remember id            
        var s = dk.solbjergkirke.Picture.Picture.id; 
        
        // Remove original image and references
        dk.solbjergkirke.Picture.Picture.parentNode.removeChild(dk.solbjergkirke.Picture.Picture);
        delete dk.solbjergkirke.Picture.Picture;
        dk.solbjergkirke.Picture.Picture = null;
        
        // Set zero opacity on new image
        if (typeof(dk.solbjergkirke.Picture.TempPicture.style['filter']) == 'string')
    	    dk.solbjergkirke.Picture.TempPicture.style['filter'] = 'alpha(opacity=100)'; 
        else if (typeof(dk.solbjergkirke.Picture.TempPicture.style['opacity']) == 'string')
            dk.solbjergkirke.Picture.TempPicture.style['opacity'] = '1.0';

        // Reset id and style before cleaning up
        dk.solbjergkirke.Picture.TempPicture.id = s;        
        dk.solbjergkirke.Picture.TempPicture.style['position'] = 'static';
        delete dk.solbjergkirke.Picture.TempPicture;
        dk.solbjergkirke.Picture.TempPicture = null;
    }
}

