/*
 * ICESOFT COMMERCIAL SOURCE CODE LICENSE V 1.1
 *
 * The contents of this file are subject to the ICEsoft Commercial Source
 * Code License Agreement V1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of the
 * License at
 * http://www.icesoft.com/license/commercial-source-v1.1.html
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * Copyright 2009-2015 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 */


if (!window.ice) {
    window.ice = new Object();
}
if (!window.ice.ee) {
    window.ice.ee = new Object();
}
if (!window.ice.ee.compat) {
    window.ice.ee.compat = new Object();
}
if (!window.ice.ee.compat.loaded) {
    (function(namespace) {
        namespace.loaded = true;
        namespace.version = 2001;

        //include prototype.js

        namespace.slideshowInstances = {};
        namespace.slideshow = (function(id, slideDur, transDur) {
            var _slideDuration = slideDur;
            var _tranDuration = transDur;
            var _id = id || ice.ee.compat.slideshowInstances.length;
            var _numSlides = document.getElementById(_id + '_root').children.length;
            var _currentSlide = 0;
            var _timer = 0;
            var instances = ice.ee.compat.slideshowInstances;
            var _instance = {
                start: function() {
                    for (var i = 0; i < _numSlides; i++) {
                        Element.hide(document.getElementById(_id + ':' + i), {duration:_tranDuration, from:1.0, to:0});
                    }
                    Element.appear(document.getElementById(_id + ':0'), {duration:_tranDuration, from:0, to:1});
                    _timer = setInterval(function() {
                        if (!document.getElementById(_id + '_root')) {
                            clearInterval(_timer);
                            _timer = 0;
                            instances[_id] = null;
                        }
                        else {
                            try {
                                Element.fade(document.getElementById(_id + ':' + _currentSlide), _tranDuration);
                                _currentSlide = (_currentSlide + 1) % _numSlides;
                                Element.appear(document.getElementById(_id + ':' + _currentSlide), _tranDuration);
                            }
                            catch(err) {
                                clearInterval(_timer);
                                _timer = 0;
                                instances[_id] = null;
                            }
                        }
                    }, _slideDuration);
                },
                stop: function() {
                    clearInterval(_timer);
                    _timer = 0;
                },
                changeDuration: function(duration) {
                    duration = Number(duration);
                    if (duration != NaN) {
                        instances[_id].stop();
                        _slideDuration = duration;
                        instances[_id].start();
                    }
                },
                getTimer: function() {
                    return _timer;
                }
            }
            if (instances[_id]) {
                clearInterval(instances[_id].getTimer());
            }
            instances[_id] = _instance;
            return _instance;
        });

        namespace.statusbarInstances = {};
        namespace.statusbar = (function(id, speed, gap) {
            var _id = id || '';
            var _txt1Id = id + '_txt1';
            var _txt2Id = id + '_txt2';
            var _speed = speed || 2;
            var _cspeed = speed || 2;
            var _gap = gap || 0;
            var _timer = 0;
            var instances = ice.ee.compat.statusbarInstances;
            var _instance = {
                start: function() {
                    var txt1 = document.getElementById(_txt1Id);
                    var txt2 = document.getElementById(_txt2Id);
                    if (txt1 && txt2) {
                        txt2.style.left = txt1.offsetWidth - _gap + 'px';
                        txt2.style.left = txt1.offsetWidth - _gap + 'px';
                        _timer = setInterval(function() {
                            var txt1 = document.getElementById(_txt1Id);
                            var txt2 = document.getElementById(_txt2Id);
                            if (txt1 && txt2) {
                                try {
                                    if (parseInt(txt1.style.left) > (txt1.offsetWidth * (-1) + 8)) {
                                        txt1.style.left = parseInt(txt1.style.left) - _cspeed + 'px';
                                    } else {
                                        txt1.style.left = parseInt(txt2.style.left) + txt1.offsetWidth + _gap + 'px';
                                    }
                                    if (parseInt(txt2.style.left) > (txt1.offsetWidth * (-1) + 8)) {
                                        txt2.style.left = parseInt(txt2.style.left) - _cspeed + 'px';
                                    }
                                    else {
                                        txt2.style.left = parseInt(txt1.style.left) + txt1.offsetWidth + _gap + 'px';
                                    }
                                }
                                catch(err) {
                                    clearInterval(_timer);
                                    _timer = 0;
                                    instances[_id] = null;
                                }
                            } else {
                                clearInterval(_timer);
                                _timer = 0;
                                instances[_id] = null;
                            }
                        }, 50);
                    }
                },
                pause: function() {
                    _cspeed = 0;
                },
                resume: function() {
                    _cspeed = _speed
                },
                getTimer: function() {
                    return _timer;
                }

            }
            if (instances[_id]) {
                clearInterval(instances[_id].getTimer());
            }
            instances[_id] = _instance;
            return _instance;
        });

        namespace.pollingInstances = {};
        namespace.polling = (function(id, interval) {
            var _timer = 0;
            var _id = id;
            var _interval = interval || 10000;
            var instances = ice.ee.compat.pollingInstances
            var _instance = {
                start: function() {
                    _timer = setInterval(function() {
                        try {
                            var elem = document.getElementById(_id);
                            if (!elem) {
                                clearInterval(_timer);
                                instances[_id] = null;
                            }
                            iceSubmit(formOf(elem), elem, new Object());
                        }
                        catch(err) {
                            clearInterval(_timer);
                            instances[_id] = null;
                        }
                    }, _interval);
                },
                getTimer: function() {
                    return _timer;
                }

            }
            if (instances[_id]) {
                clearInterval(instances[_id].getTimer());
            }
            instances[_id] = _instance;
            return _instance;
        });

        namespace.richdatagrid = {
            onmouseout:  function(element, id) {
                element = $(element);
                element.removeClassName('rdgColOn');
                element.up().up().removeClassName('rdgColOn');
                $$('.' + id + '_rdgml')[0].removeClassName('richDatGrdMoveLeftOn');
                $$('.' + id + '_rdgmr')[0].removeClassName('richDatGrdMoveRightOn');
                $$('.' + id + '_rdgh')[0].removeClassName('richDatGrdHideOn');
            },
            onmouseover: function(element, id) {
                element = $(element);
                element.addClassName('rdgColOn');
                element.up().up().addClassName('rdgColOn');
                $$('.' + id + '_rdgml')[0].addClassName('richDatGrdMoveLeftOn');
                $$('.' + id + '_rdgmr')[0].addClassName('richDatGrdMoveRightOn');
                $$('.' + id + '_rdgh')[0].addClassName('richDatGrdHideOn');
            }
        };

        namespace.spinner = {
            onmouseout: function(element, dir) {
                Element.removeClassName(element, 'spinner' + dir + '_on');
                Element.addClassName(element, 'spinner' + dir);
            },
            onmouseover: function(element, dir) {
                Element.removeClassName(element, 'spinner' + dir);
                Element.addClassName(element, 'spinner' + dir + '_on');
            }

        };

        namespace.schedule = {
            changeCalView: function(element, view, year, month, day) {
                var form = $(formOf(element));
                if (form) {
                    element = $(element);
                    var changeCalViewCommandLink;
                    var possibleLinks = $$('.changeCalView');
                    for (var l = 0; l < possibleLinks.length; l++) {
                        if (element.descendantOf(possibleLinks[l].up().up().up())) {
                            changeCalViewCommandLink = possibleLinks[l];
                            break;
                        }
                    }
                    if (changeCalViewCommandLink) {
                        var formHiddenInputSpanChildren;
                        var formHiddenInput;
                        var formId = form.id + 'hdnFldsDiv';
                        for( var i = 0 ; i < form.children.length ; i++ ){
                            if( form.children[i].id == formId) {
                                formHiddenInputSpanChildren = form.children[i].children;
                                break;
                            }
                        }
                        for (var i = 0; i < formHiddenInputSpanChildren.length; i++) {
                            if (formHiddenInputSpanChildren[i].name.indexOf(form.id) === 0) {
                                formHiddenInput = formHiddenInputSpanChildren[i];
                                break;
                            }
                        }
                        if (formHiddenInput) {
                            form[formHiddenInput.name].value = changeCalViewCommandLink.id;
                            form['calendarViewType'].value = view;
                            form['monthCursor'].value = month;
                            form['dayCursor'].value = day;
                            form['yearCursor'].value = year;
                            iceSubmit(form, changeCalViewCommandLink, new Object());
                            form[formHiddenInput.name].value = '';
                            form['calendarViewType'].value = '';
                            form['monthCursor'].value = '';
                            form['dayCursor'].value = '';
                            form['yearCursor'].value = '';
                        }
                    }

                }
                return false;
            }
        }

    })(window.ice.ee.compat);

}