﻿/*
Author/Owner: 	Daniel Peel.
Contact: 		mintpants@tiscali.co.uk.
Details: 		Opacity JavaScript With callback functionality replaces the old one created 22.02.08
Created:		16.04.09
Updated: 
*/

function ChangeOpacity(id, opacity) {
  
    var _style = GetObj(id).style;
    _style.opacity = (opacity / 100);
    _style.MozOpacity = (opacity / 100);
    _style.KhtmlOpacity = (opacity / 100);
    _style.filter = "alpha(opacity=" + opacity + ")";
}

function Opacity(el) {
    
    this.elObj = GetObj(el);    
}

Opacity.prototype.StartOpacity;

Opacity.prototype.EndOpacity;

Opacity.prototype.Time = 300;

Opacity.prototype.Run = function() {

    //speed for each frame
    var speed = Math.round((this.Time / 100));
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if (this.StartOpacity > this.EndOpacity) {
        for (i = this.StartOpacity; i >= this.EndOpacity; i--) {
            setTimeout("ChangeOpacity('" + this.elObj.id + "'," + i + ");", (timer * speed));
            timer++;            
        } 
    }
    else if (this.StartOpacity < this.EndOpacity) {
        for (i = this.StartOpacity; i <= this.EndOpacity; i++) {
            setTimeout("ChangeOpacity('" + this.elObj.id + "'," + i + ");", (timer * speed));
            timer++;           
        }
    }
    //CallBack doesnt work because the for loop just sets up a series of timeouts then exits before the timeouts 
    //have completed.
}

Opacity.prototype.Toggle = function() {
   
    if (GetOpacity(this.elObj) == 0) {
        this.StartOpacity = 0;
        this.EndOpacity = 100;
        this.Run();
    } else {
        this.StartOpacity = 100;
        this.EndOpacity = 0;
        this.Run();
    }
}

Opacity.prototype.Blend = function(newEl) { 
        
        //Run on new object
        var opacIn = new Opacity(newEl);
        opacIn.StartOpacity = 0;
        opacIn.SndOpacity = 100;
        opacIn.Time = this.Time;   
        opacIn.Run();
        
        //run current objet
        this.StartOpacity = 100;
        this.EndOpacity = 0;
        this.Run(); 
}

/*Example
    var opac = new Opacity(element);
    opac.StartOpacity = 100;
    opac.EndOpacity = 0;
    opac.Time = 500;   
    opac.Run();
*/

//---------------------------------------------------------------------------------------------------------------------------------------------------------------------
function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if (opacStart > opacEnd) {
        for (i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')", (timer * speed));
            timer++;
        }
    } else if (opacStart < opacEnd) {
        for (i = opacStart; i <= opacEnd; i++) {
            setTimeout("changeOpac(" + i + ",'" + id + "')", (timer * speed));
            timer++;
        }
    }
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
    var object = document.getElementById(id).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}


//Helpe method that toggles opactiy
function shiftOpacity(id, millisec) {
    //if an element is invisible, make it visible, else make it ivisible
    if (document.getElementById(id).style.opacity == 0) {
        opacity(id, 0, 100, millisec);
    } else {
        opacity(id, 100, 0, millisec);
    }
}


//blend images
function blendimage(divid, imageid, imagefile, millisec) {
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //set the current image as background
    document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";

    //make image transparent
    changeOpac(0, imageid);

    //make new image
    document.getElementById(imageid).src = imagefile;

    //fade in image
    for (i = 0; i <= 100; i++) {
        setTimeout("changeOpac(" + i + ",'" + imageid + "')", (timer * speed));
        timer++;
    }
}
