var customColor = "F6921E";

var imgAnimation;
var firstImage = true;
var currentImage;
var nextImage;

var galleryAnimation;
var firstGallery = true;
var currentGallery;
var nextGallery;

function reloadAll() {
    var frm1 = getFrameWindow('frmMenu1');
    var frm2 = getFrameWindow('frmMenu2');
    var frm3 = getFrameWindow('frmMenu3');
    var frmText = getFrameWindow('frmText');

    frm1.location.reload();
    frm2.location.reload();
    frm3.location.reload();
    frmText.location.reload();
}

function setSelectedMenu(frameId, menuId) {
    var frm = getFrameWindow(frameId);
    var links = frm.document.getElementsByTagName("A");

    for (var i = 0; i < links.length; i++) {
        if (links[i].id == "menu" + menuId) {
            links[i].className = 'customColor';
        } else {
            links[i].className = '';
        }
    }
}

function changeCustomColor(color) {
    colorFade(self, customColor, color, 25, 30, changeCustomColorUpdate);
}

function changeCustomColorUpdate(color) {
    customColor = color;

    changecss(".customColor", "color", color);

    var frm1 = getFrameWindow('frmMenu1');
    var frm2 = getFrameWindow('frmMenu2');
    var frm3 = getFrameWindow('frmMenu3');
    var frmGallery = getFrameWindow('frmGallery');

    frm1.changecss(".customColor", "color", color);
    frm2.changecss(".customColor", "color", color);
    frm3.changecss(".customColor", "color", color);
    //frmGallery.changecss(".customColor", "color", color);
}

function setOpacity(obj, opacity) {
    if (opacity == 100) {
        obj.style.filter = null;

        // Safari<1.2, Konqueror
        obj.style.KHTMLOpacity = null;

        // Older Mozilla and Firefox
        obj.style.MozOpacity = null;

        // Safari 1.2, newer Firefox and Mozilla, CSS3
        obj.style.opacity = null;
    } else {
        opacity = (opacity == 100) ? 99.999 : opacity;

        // IE/Win
        obj.style.filter = "alpha(opacity:" + opacity + ")";

        // Safari<1.2, Konqueror
        obj.style.KHTMLOpacity = opacity / 100;

        // Older Mozilla and Firefox
        obj.style.MozOpacity = opacity / 100;

        // Safari 1.2, newer Firefox and Mozilla, CSS3
        obj.style.opacity = opacity / 100;
    }
}

function hideText() {
    var frmText = document.getElementById('frmText');

    frmText.src = "text.aspx";
}

function changeTextToPage(page) {
    var frmText = document.getElementById('frmText');

    frmText.src = "text.aspx?page=" + page;
}

function changeTextToPageImage(pageimage) {
    var frmText = document.getElementById('frmText');

    frmText.src = "text.aspx?pageimage=" + pageimage;
}

function changeImageTo(url) {
    if (currentImage != url) {
        nextImage = url;

        fadeImage();
    }
}

function fadeImage() {
    if (firstImage) {
        firstImage = false;
        changeImage();
    } else {
        fadeImageOut();
    }
}

function changeImage() {
    var img = document.getElementById("imgImage");
    img.onload = fadeImageIn;
    img.src = nextImage;
    currentImage = nextImage;
    nextImage = null;
}

function fadeImageIn() {
    if (imgAnimation) imgAnimation.stop();
    imgAnimation = new SmoothMovement(0, 100);
    imgAnimation.animate(20, fadeImageUpdate, fadeImageComplete);
}

function fadeImageOut() {
    if (imgAnimation) imgAnimation.stop();
    imgAnimation = new SmoothMovement(100, 0);
    imgAnimation.animate(20, fadeImageUpdate, fadeImageComplete);
}

function fadeImageUpdate(position, animation) {
    var img = document.getElementById("imgImage");

    setOpacity(img, position);
}

function fadeImageComplete(animation) {
    if (animation.position == 0) {
        changeImage();        
    }
}

function fadeGallery() {
    if (firstGallery) {
        firstGallery = false;
        changeGallery();
    } else {
        fadeGalleryOut();
    }
}

function changeGallery() {
    var frmGallery = document.getElementById('frmGallery');
    frmGallery.src = "gallery.aspx?page=" + nextGallery;
    currentGallery = nextGallery;
    nextGallery = null;
}

function fadeGalleryIn() {
    var position = 0;

    if (galleryAnimation) {
        galleryAnimation.stop();
        position = galleryAnimation.position;
    }

    if (position != 100) {
        galleryAnimation = new SmoothMovement(0, 100);
        galleryAnimation.animate(20, fadeGalleryUpdate, fadeGalleryComplete);
    } else {
        fadeGalleryComplete(galleryAnimation);
    }
}

function fadeGalleryOut() {
    var position = 100;

    if (galleryAnimation) {
        galleryAnimation.stop();
        position = galleryAnimation.position;
    }

    if (position != 0) {
        galleryAnimation = new SmoothMovement(100, 0);
        galleryAnimation.animate(20, fadeGalleryUpdate, fadeGalleryComplete);
    } else {
        fadeGalleryComplete(galleryAnimation);
    }
}

function fadeGalleryUpdate(position, animation) {
    var frmGallery = document.getElementById('frmGallery');

    setOpacity(frmGallery, position);
}

function fadeGalleryComplete(animation) {
    if (animation.position == 0) {
        changeGallery();
    }
}

function hideGallery() {
    fadeGalleryOut();
}

function loadGallery(page) {
    if (currentGallery != page) {
        nextGallery = page;

        fadeGallery();
    }
}

function getFrameWindow(frameId) {
    return (document.frames ? document.frames[frameId] : document.getElementById(frameId).contentWindow);
}

function iframeResizeText(iframe, iframeHolder) {
    if (iframe.animation) {
        iframe.animation.stop();
    }

    var frm1 = document.getElementById('frmMenu1');
    var frm2 = document.getElementById('frmMenu2');
    var frm3 = document.getElementById('frmMenu3');

    var maxMenuHeight = Math.max(Math.max(frm1.height, frm2.height), frm3.height);

    var frm = getFrameWindow(iframe.id);
    var start = iframe.height;
    var height = frm.document.getElementById('tblTextHolder').scrollHeight;

    if (height < 12) height = 12;

    if (height > 400 - maxMenuHeight) {
        height = 400 - maxMenuHeight;

        frm.enableScroll(height);
    }

    iframe.animation = new SmoothMovement(eval('' + start), height);
    iframe.animation.obj = iframe;
    iframe.animation.objHolder = iframeHolder;
    iframe.animation.stop();
    if (iframeHolder.style.visibility == "hidden") {
        iframe.height = 0;
    }
    iframe.animation.animate(20, iframeUpdateSize, iframeAnimateComplete);
}

function iframeResizeContent(iframe, iframeHolder) {
    if (iframe.animation) {
        iframe.animation.stop();
    }

    var start = iframe.height;
    var height = getFrameWindow(iframe.id).document.getElementById('tblMenuHolder').scrollHeight;

    iframe.animation = new SmoothMovement(eval('' + start), height);
    iframe.animation.obj = iframe;
    iframe.animation.objHolder = iframeHolder;
    iframe.animation.stop();
    if (iframeHolder.style.visibility == "hidden") {
        iframe.height = 0;
    }
    iframe.animation.animate(20, iframeUpdateSize, iframeAnimateComplete);
}

function iframeUpdateSize(position, animation) {
    var iframe = animation.obj;
    var iframeHolder = animation.objHolder;

    iframe.height = animation.position;
    if (iframe.height < 2) {
        iframeHolder.style.visibility = "hidden";
    } else {
        iframeHolder.style.visibility = "visible";
    }
}

function iframeAnimateComplete(animation) {
    var iframe = animation.obj;
    var iframeHolder = animation.objHolder;

    if (iframe.height < 5) {
        iframeHolder.style.visibility = "hidden";
    }
}

function formFocus(obj, defaultvalue) {
    var value = obj.value;

    if (value == defaultvalue) obj.value = "";
}

function formBlur(obj, defaultvalue) {
    var value = obj.value;

    if (value == "") obj.value = defaultvalue;
}

// main function to process the fade request //
function colorFade(target, start, end, steps, speed, fadeUpdate) {
    var startrgb, endrgb, er, eg, eb, step, rint, gint, bint, step;
    steps = steps || 20;
    speed = speed || 20;
    clearInterval(target.timer);
    endrgb = colorConv(end);
    er = endrgb[0];
    eg = endrgb[1];
    eb = endrgb[2];
    if (!target.r) {
        startrgb = colorConv(start);
        r = startrgb[0];
        g = startrgb[1];
        b = startrgb[2];
        target.r = r;
        target.g = g;
        target.b = b;
    }
    rint = Math.round(Math.abs(target.r - er) / steps);
    gint = Math.round(Math.abs(target.g - eg) / steps);
    bint = Math.round(Math.abs(target.b - eb) / steps);
    if (rint == 0) { rint = 1 }
    if (gint == 0) { gint = 1 }
    if (bint == 0) { bint = 1 }
    target.step = 1;
    target.timer = setInterval(function() { animateColor(target, steps, er, eg, eb, rint, gint, bint, fadeUpdate) }, speed);
}

// incrementally close the gap between the two colors //
function animateColor(target, steps, er, eg, eb, rint, gint, bint, fadeUpdate) {
    var color;
    if (target.step <= steps) {
        var r = target.r;
        var g = target.g;
        var b = target.b;
        if (r >= er) {
            r = r - rint;
        } else {
            r = parseInt(r) + parseInt(rint);
        }
        if (g >= eg) {
            g = g - gint;
        } else {
            g = parseInt(g) + parseInt(gint);
        }
        if (b >= eb) {
            b = b - bint;
        } else {
            b = parseInt(b) + parseInt(bint);
        }
        color = 'rgb(' + r + ',' + g + ',' + b + ')';
        fadeUpdate(color);
        target.r = r;
        target.g = g;
        target.b = b;
        target.step = target.step + 1;
    } else {
        clearInterval(target.timer);
        color = 'rgb(' + er + ',' + eg + ',' + eb + ')';
        fadeUpdate(color);
    }
}

// convert the color to rgb from hex //
function colorConv(color) {
    var rgb = [parseInt(color.substring(0, 2), 16),
    parseInt(color.substring(2, 4), 16),
    parseInt(color.substring(4, 6), 16)];
    return rgb;
}
