﻿function TranceSoft() { }
TranceSoft.Gallery = function() { }
TranceSoft.Gallery.CurrentPhotoID = null;
TranceSoft.Gallery.CurrentImageID = null;
TranceSoft.Gallery.MainImageFirst = null;
TranceSoft.Gallery.MainImageSecond = null;
TranceSoft.Gallery.LabelTitle = null;
TranceSoft.Gallery.LabelDescription = null;
//(1 or 2) first or second image holder
TranceSoft.Gallery.CurrentImageContainer = null;
TranceSoft.Gallery.IntervalID = null;

TranceSoft.Gallery.Initialize = function(params)
{
    TranceSoft.Gallery.CurrentImageID = params.firstImage;
    TranceSoft.Gallery.MainImageFirst = params.firstImage;
    TranceSoft.Gallery.MainImageSecond = params.secondImage;
    TranceSoft.Gallery.LabelTitle = params.labelTitle;
    TranceSoft.Gallery.LabelDescription = params.labelDesc;
    TranceSoft.Gallery.IntervalID = 0;
    TranceSoft.Gallery.CurrentImageContainer = 1;
}

TranceSoft.Gallery.ChangeImage = function(image) {
	if (TranceSoft.Gallery.IntervalID > 0
        || TranceSoft.Gallery.CurrentPhotoID == image.photoID ? image.photoID : image.attributes['photoID'].Value)
		return;
	var container;
	var lblDesc = document.getElementById(TranceSoft.Gallery.LabelDescription);
	var lblTitle = document.getElementById(TranceSoft.Gallery.LabelTitle);
	if (TranceSoft.Gallery.CurrentImageContainer == 1)
		container = document.getElementById(TranceSoft.Gallery.MainImageSecond);
	else
		container = document.getElementById(TranceSoft.Gallery.MainImageFirst);
	if (container && lblDesc && lblTitle) {
		lblTitle.innerHTML = image.title;
		lblDesc.innerHTML = image.description ? image.description : image.attributes['description'].value;
		container.src = image.mainSource ? image.mainSource : image.attributes['mainSource'].value;
		container.alt = image.alt ? image.alt : image.attributes['alt'].value;
	}
	TranceSoft.Gallery.LoadAndAnimate(image, container);
};

TranceSoft.Gallery.LoadAndAnimate = function(image, container) {
	var interval = setInterval(function() {
		if (container.complete) {

			clearInterval(interval);
			TranceSoft.Gallery.Switch(image.photoID ? image.photoID : image.attributes['photoID'].value);
		}
	}, 200);
};

TranceSoft.Gallery.GetImagePack = function()
{
    return { newImage : TranceSoft.Gallery.CurrentImageContainer == 1 ?
                document.getElementById(TranceSoft.Gallery.MainImageSecond) :
                document.getElementById(TranceSoft.Gallery.MainImageFirst),
             image : document.getElementById(TranceSoft.Gallery.CurrentImageID) }
}

TranceSoft.Gallery.Switch = function(photoID)
{
    if (TranceSoft.Gallery.IntervalID > 0)
        return;
    var imagePack = TranceSoft.Gallery.GetImagePack();
    imagePack.photoID = photoID;
    if (!(imagePack.image && imagePack.newImage))
        return;
    var opacity = 100;
    TranceSoft.Gallery.IntervalID = setInterval(
                    function()
                    {
                        opacity = opacity - 5;
                        if (opacity >= 0)
                        {
                            imagePack.image.style.opacity = opacity / 100;
                            imagePack.image.style.filter = 'alpha(opacity=' + opacity + ')';
                            imagePack.newImage.style.opacity = 1 - (opacity / 100);
                            imagePack.newImage.style.filter = 'alpha(opacity=' + (100 - opacity) + ')';
                        }
                        else
                        {
                            window.clearInterval(TranceSoft.Gallery.IntervalID);
                            TranceSoft.Gallery.IntervalID = 0;
                            TranceSoft.Gallery.CurrentPhotoID = imagePack.photoID;
                            TranceSoft.Gallery.CurrentImageID = imagePack.newImage.id;
                            TranceSoft.Gallery.CurrentImageContainer = TranceSoft.Gallery.CurrentImageID == TranceSoft.Gallery.MainImageFirst ? 1 : 2;
                        }
                    }
                    , 1);
}
