var ELEMENTS_CACHE = {};

function $() {
    var elements = new Array();
    for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];
        if (ELEMENTS_CACHE[element]){element = ELEMENTS_CACHE[element]}
        else if (typeof element == 'string'){element = document.getElementById(element)}

        if (arguments.length == 1) return element;
        elements.push(element);
    }
    return elements;
}




/*
    JumpBox control
*/
function JumpBox(){
    this._isIE = ((navigator.userAgent.toLowerCase().indexOf("msie") != -1) && (!(window.opera)));
    this._JumpBoxListDelayTimer = null;
    this._ListBox = null;

    this.Toggle = function(ListId){
        if(this._ListBox != null){
            this._HideList();
        }
        this._ListBox = $(ListId);
        if(this._ListBox.offsetWidth == 0 && this._ListBox.innerHTML != ''){
            if(this._isIE){
                this._ListBox.filters[0].Apply();
                this._ListBox.filters[0].Play();
            }
            this._ListBox.style.display = 'block';
        }else{
            this._HideList();
        }
    }

    this.onListMouseOut = function(){
        if(this._ListBox != null){
            this._JumpBoxListDelayTimer = setTimeout("JumpBox._HideList()", 150);
        }
    }

    this.onListMouseOver = function(){
        if(this._JumpBoxListDelayTimer){
            clearTimeout(this._JumpBoxListDelayTimer);
        }
    }

    this._HideList = function(){
        this._ListBox.style.display = 'none';
    }

    this.onItemMouseOver = function(ItemObject){
        ItemObject.id = 'jump-box-current-element';
    }

    this.onItemMouseOut = function(ItemObject){
        ItemObject.id = '';
    }

    this.onSelect = function(UrlToGo, JumpBoxId, SelectedValue){
        this._HideList();
        this._ChangeValue(JumpBoxId, SelectedValue);
        if(UrlToGo != ''){
            document.location.href = UrlToGo;
        }
    }

    this._ChangeValue = function(JumpBoxId, SelectedValue){
        $(JumpBoxId).innerHTML = SelectedValue;
    }

    this.FillChildControl = function(ChildControlId, List){
        $(ChildControlId).innerHTML = List;
    }
}

var JumpBox = new JumpBox();




/*
    PreviewPopup control
*/
function PreviewPopup(){
    this._Popup = null;
    this._PreviewPopupDelayTimer = null;

    this.Show = function(PopupId, event){
        if(this._Popup == null){
            clearTimeout(this._PreviewPopupDelayTimer);
            if(!event) event=window.event;
            this._Popup = $(PopupId);
            this._Popup.style.marginTop =  '20px';
            this._Popup.style.marginLeft =  '15px';
            this._Popup.style.display = 'block';
        }
    }

    this.Hide = function(){
         this._PreviewPopupDelayTimer = setTimeout("PreviewPopup._Hide()", 50);
    }

    this._Hide = function(){
        this._Popup.style.display = 'none';
        this._Popup = null;

    }
}

var PreviewPopup = new PreviewPopup();




/*
    Map control
*/
function Map(){

    this.ShowHint = function(coords, title, types){
        if(this.Hint == null){
            coords = coords.split('|');
            $('map-hint').style.display = 'block';
            $('map-hint').style.marginTop = (coords[0] - 25) + 'px';
            $('map-hint').style.marginLeft = (coords[1]) + 'px';
               imgs = '';
               _types = types.split('|');
            for(i = 0; i < _types.length; i++){
                imgs += '<img src="' + _types[i] + '" />';
            }
            $('map-hint').innerHTML = '<span> ' + title + ' </span>' + imgs;
        }
    }

    this.HideHint = function(){
        $('map-hint').style.display = 'none';
    }
}

var Map = new Map();




/*
    Patchwork control
*/
function Patchwork(){
    this._CurrentItem = '';

    this.Execute = function(menu_item_id, frame1, frame2, description){
        this._HighlightMenuItem(menu_item_id);
        $('patchwork').style.backgroundImage = "none";
        $('patchwork-frame-1').style.backgroundImage = "url('" + frame1 + "')";
        $('patchwork-frame-2').style.backgroundImage = "url('" + frame2 + "')";
        $('patchwork-frame-1-text').innerHTML = description;
    }

    this.Rollback = function(menu_item_id, default_path){
        $('patchwork').style.backgroundImage = "url('" + default_path + "')"
        $('patchwork-frame-1').style.backgroundImage = "none";
        $('patchwork-frame-2').style.backgroundImage = "none";
        $('patchwork-frame-1-text').innerHTML = '';
        this._CurrentItem = $(menu_item_id);
        this._CurrentItem.className = '';
    }

    this._HighlightMenuItem = function(menu_item_id){
        if(this._CurrentItem != ''){
            this._CurrentItem.className = '';
        }
        this._CurrentItem = $(menu_item_id);
        if(this._CurrentItem.className != 'catalogue-menu-item-active'){
            this._CurrentItem.className = 'catalogue-menu-item-active';
        }else{
            this._CurrentItem.className = '';
        }
    }
}

var Patchwork = new Patchwork();




/*
    Gallery control
*/
function Gallery(){
    //Ribbon scroll speed
    this.ScrollSpeed = 10;

    this._Frame =  null;
    this._Ribbon = null;
    this._FrameWidth = 0;
    this._CursorWidth = 20;
    this._RibbonWidth = 0;
    this._widthK = 0;
    this._ElementsCount = 0;
    this._CursorStep = 0;
    this._isInitialized = false;
    this._isLoop = false;
    this._PosX = 0;
    this._ScrollTimer = new Array();
    this._StepsCounter = 0;

    this.Init = function(RibbonWidth, ElementsCount){
        this._Frame =  $('scroll-frame');
        this._Ribbon = $('gallery-ribbon');
        this._FrameWidth = parseInt(this._Frame.style.width.replace("px",""));
        this._RibbonWidth = RibbonWidth;
        this._ElementsCount = ElementsCount;

        var ldiff = this._RibbonWidth - this._FrameWidth;

        this._CursorWidth = ((ldiff) <= 0) ? this._FrameWidth : ((this._FrameWidth - ldiff) >= 20 ? (this._FrameWidth - ldiff) : 20);
        $('cursor').style.width = this._CursorWidth + 'px';
        $('cursor-in').style.width = (this._CursorWidth - 2) + 'px';
        if (this._CursorWidth == this._FrameWidth){
            $('cursor').style.display = 'none';
        }

        this._widthK = (this._FrameWidth - this._CursorWidth) / (this._RibbonWidth - this._FrameWidth);
        this._isInitialized = true;
    }

    this.ScrollLeft = function(){
        if(this._isInitialized){
            this._isLoop = true;
            if(this._PosX < 0){
                this._Move(this._PosX + this.ScrollSpeed, this._widthK);
                if(this._isLoop){
                    this._ScrollTimer = setTimeout("Gallery.ScrollLeft()", 10);

                }
                this._ButtonChangeState('right-scroll-button', 'enable');
            }else{
                this._ButtonChangeState('left-scroll-button', 'disable');
            }
        }
    }

    this.ScrollRight = function(){
        if(this._isInitialized){
            this._isLoop = true;
            if( this._RibbonWidth + this._PosX > this._FrameWidth){
                this._Move(this._PosX - this.ScrollSpeed, this._widthK);
                if(this._isLoop){
                    this._ScrollTimer = setTimeout("Gallery.ScrollRight()", 10);
                }
                this._ButtonChangeState('left-scroll-button', 'enable');
            }else{
                this._ButtonChangeState('right-scroll-button', 'disable');
            }
        }
    }

    this.ScrollStop = function(){
        this._isLoop = false;
        if(this._ScrollTimer){
            clearTimeout(this._ScrollTimer);
        }
    }

    this._Move = function(PosX, t){
        this._PosX = PosX;
        this._Ribbon.style.left = this._PosX + 'px';
        $('cursor').style.left = -(t * PosX) <= (this._FrameWidth - this._CursorWidth)?(-t * PosX + 'px'):this._FrameWidth - this._CursorWidth + 'px';
    }

    this._ButtonChangeState = function(ButtonID, State){
        Button = $(ButtonID);
        if(State == 'enable'){
            if(Button.src.indexOf('_disabled') != -1){
                Button.src = Button.src.replace('_disabled.gif', '.gif');
            }
        }else{
            if(Button.src.indexOf('_disabled') == -1){
                Button.src = Button.src.replace('.gif', '_disabled.gif');
            }
        }

    }
}

var Gallery = new Gallery();


function showPassword(){
    $('Password-text').style.display='none';
    $('Password').style.display='block';
}