﻿
/*
    *
    * FLOWEN 2010
    *
    *
    *
*/


/*
  *  CXPosition
  *  ..
  *  Consente in caricamento di mmagini in modalità dinamica
  *  ed incorpora alcune funzioni per la visulaizzazione delle
  *  miniature
*/

        var CXAlign = new Class({
            Implements: Events,
            initialize: function(inst) {
                
                // System Properties            
                this._id = inst;                
                //
                this.SetPosition = function(c, d1, d2, p)
                {
                    //
                    var _global_var = this._id;
                    var _container = $(c);
                    //_container.setStyle("position", "relative");
//                    //
                    var _d1 = $(d1);
                    _d1.setStyle("position", "absolute");
                    var _d1_x = _d1.getCoordinates(c).left.toInt();
                    var _d1_y = _d1.getCoordinates(c).top.toInt();
                    var _d1_w = _d1.getCoordinates(c).width.toInt();
                    var _d1_h = _d1.getCoordinates(c).height.toInt();
                    
                    var _d2 = $(d2);
                    _d2.setStyle("position", "absolute");
                    //
                    var max_height = _d1_h
                    if(_d2.getCoordinates(c).height.toInt() > _d1_h) max_height = _d2.getCoordinates(c).height.toInt();
                    _container.setStyle('height', max_height + "px");
                     switch(p)
                    {
                        case 'outerright':
                            _d2.setStyle("left", (_d1_x + _d1_w) + "px");
                            _d2.setStyle("top", _d1_y + "px");
                            break;
                    }
                }                                
            }
        });

/*
  *  CXSlider
  *  ..
  *  Consente in caricamento di mmagini in modalità dinamica
  *  ed incorpora alcune funzioni per la visulaizzazione delle
  *  miniature
*/
        var CXSlider = new Class({
            Implements: Events,
            initialize: function(id, inst) {
                
                // System Properties            
                this._id = inst;
                this.id = id;
                // Base Properties
                this.Width = "";
                this.Height = "";
                this.Index = 0;
                this.TabIndex = 0;
                this.Total = 0;
                // Behaviour Functions
                this.SlideDelay = 5000;
                // full, single, replace, fade
                this.SlideMode = "full";
                // html, ajax
                this.ContentMode = "html";
                this.FadeDuration = 2000;
                //
                this.PreviousPanel = "";
                this.NextPanel = "";
                this.ItemPanel = "";
                this.ItemPanelNotSelectedClass = "buttonnotselected";
                this.ItemPanelSelectedClass = "buttonselected";
                this.ItemSeparatorWidth = 10;
                this.ItemSeparatorHeight = 10;
                this.ItemPanelItems = new Array();
                this.PreviousPagePanel = "";
                this.NextPagePanel = "";
                //

                //
                this.AutoPlay = true;
                // stop: - restart: - invert:
                this.Rewind = "stop";
                this.Loops = 0;
                //
                this.PageIndex = 0;
                this.MaxPageLength = 5;
                
                this.timeout = null;
                
                // Options
                this.DisplayNavigator = false;
                this.PanelNavigatorX = 0;
                this.PanelNavigatorY = 0;
                this.NavigatorDirection = "horizontal";
                
                //
                this.Play = function()
                {
                    this.AutoPlay = true;
                    this.Next();
                }
                this.Stop = function()
                {
                    this.AutoPlay = false;
                }
                

                this.EnableItems = function(pi, d)
                {
                    var _global_var = this._id;
                    var i1 = 0; var _interval = 0;
                    for (i1=0;i1<this.ItemPanelItems.length;i1++)
                    {
                         window.setTimeout(_global_var + ".EnableItem(" + i1 + ")", _interval);
                        _interval += d;
                    }                
                }
                this.DisableItems = function(pi, d)
                {
                    var _global_var = this._id;
                    var i1 = 0; var _interval = 0;
                    for (i1=0;i1<this.ItemPanelItems.length;i1++)
                    {
                         window.setTimeout(_global_var + ".DisableItem(" + i1 + ")", _interval);
                        _interval += d;
                    }                
                }

                this.EnableItem = function(i)
                {
                    this.ItemPanelItems[i].fade('in');
                }

                this.DisableItem = function(i)
                {
                    this.ItemPanelItems[i].fade('out');
                }
                
                this.DisableAutoPlay = function(i)
                {
                    this.AutoPlay = false;
                    if(this.timeout != null) clearTimeout(this.timeout);
                }
                
                // Load Panel
                this.LoadPanel = function()
                {
                    var _container = $(this.id);
                    var _offset_x = _container.getCoordinates(_container.getParent()).left.toInt();
                    var _offset_y = _container.getCoordinates(_container.getParent()).top.toInt();
                    
                    var  _previouspanel_id = this.PreviousPanel;
                    var  _nextpanel_id = this.NextPanel;
                    var  _itempanel_id = this.ItemPanel;

                    var _tabindex =  this.TabIndex;
                    var _childs = _container.getChildren()[_tabindex].getChildren();
                    var _ItemSeparatorWidth = this.ItemSeparatorWidth;
                    var _ItemSeparatorHeight = this.ItemSeparatorHeight;
                    var _ItemPanelItems = this.ItemPanelItems;

                    var _ItemPanelNotSelectedClass = this.ItemPanelNotSelectedClass;
                    var _ItemPanelSelectedClass = this.ItemPanelSelectedClass;
                    
                    var _DisplayNavigator = this.DisplayNavigator;
                    var _PanelNavigatorX = this.PanelNavigatorX;
                    var _PanelNavigatorY = this.PanelNavigatorY;
                    
                    var _NavigatorDirection = this.NavigatorDirection;
                    
                    $(window).addEvent('load', function(event){
                       
                        var _pos_x = _offset_x + parseInt(_PanelNavigatorX);
                        var _pos_y = _offset_y + parseInt(_PanelNavigatorY);
                        if(_previouspanel_id != "")
                        {
                            var _previouspanel = $(_previouspanel_id);
                            if(!_DisplayNavigator) _previouspanel.setStyle("visibility", "hidden");                     
                            _previouspanel.setStyle("position", "absolute");
                            _previouspanel.setStyle("z-index", "1000");
                            _previouspanel.setStyle("left", parseInt(_pos_x) + "px");
                            _previouspanel.setStyle("top", parseInt(_pos_y) + "px");
                            if(_NavigatorDirection == "horizontal")
                                _pos_x += _previouspanel.getCoordinates().width.toInt();
                            else 
                                _pos_y += _previouspanel.getCoordinates().height.toInt();
                            
                        }
                        if(_itempanel_id != "")
                        {
                            var i = 0;
                            var _itempanel = $(_itempanel_id);
                            var _s_html = _itempanel.get("html");
                            for (i=0;i<_childs.length;i++)
                            {
                                //
                                var _new_itempanel = _itempanel.clone().inject(_itempanel,'after');
                                if(!_DisplayNavigator) 
                                    _new_itempanel.setStyle("visibility", "hidden");
                                else
                                    _new_itempanel.setStyle("visibility", "visible");
              
                                _new_itempanel.set("class", _ItemPanelNotSelectedClass)
                                _new_itempanel.set("html", _s_html.replace(/#index#/gi, i + ""));
                                //
                                _new_itempanel.setStyle("position", "absolute");
                                _new_itempanel.setStyle("display", "block");
                                _new_itempanel.setStyle("z-index", "1000");
                                _new_itempanel.setStyle("left", parseInt(_pos_x) + "px");
                                _new_itempanel.setStyle("top", parseInt(_pos_y) + "px");
                                // Position
                                if(_NavigatorDirection == "horizontal")
                                    _pos_x += _new_itempanel.getCoordinates().width.toInt() + _ItemSeparatorWidth;
                                else 
                                    _pos_y += _new_itempanel.getCoordinates().height.toInt() + _ItemSeparatorHeight;
                                
                                _ItemPanelItems[i] = _new_itempanel;
                                if(i == 0)
                                    _new_itempanel.set("class", _ItemPanelSelectedClass);
                            }
                        }
                        if(_nextpanel_id != "")
                        {
                            var _nextpanel = $(_nextpanel_id);
                            if(!_DisplayNavigator) _nextpanel.setStyle("visibility", "hidden");                     
                            _nextpanel.setStyle("position", "absolute");
                            _nextpanel.setStyle("z-index", "1000");
                            _nextpanel.setStyle("left", (parseInt(_pos_x)) + "px");
                            _nextpanel.setStyle("top", _pos_y + "px");
                            if(_NavigatorDirection == "horizontal")
                                _pos_x += _nextpanel.getCoordinates().width.toInt();
                            else 
                                _pos_y += _nextpanel.getCoordinates().height.toInt();
                        }
                     });
                }
                                
                //
                // Load
                this.Load = function()
                {
                    var _container = $(this.id);
                    _container.setStyle("position", "relative");
                    _container.setStyle("overflow", "hidden");
                    _container.setStyle("width",  this.Width);
                    _container.setStyle("height", this.Height);
                    _container.setStyle("visibility",  "hidden");

                    var _tabindex =  this.TabIndex;
                    var _childs = _container.getChildren()[_tabindex].getChildren();
                    $(_container.getChildren()[_tabindex]).setStyle("position", "absolute");
                    $(_container.getChildren()[_tabindex]).setStyle("top", "0px");
                    $(_container.getChildren()[_tabindex]).setStyle("left", "0px");
                   
                    var i = 0;
                    for (i=0;i<_childs.length;i++)
                    {
                        $(_childs[i]).setStyle("position", "absolute");
                        $(_childs[i]).setStyle("top", "0px");
                        $(_childs[i]).setStyle("left", "0px");
                    }
                    this.Total = _childs.length;
                }
                //
                // Load
                this.Start = function(_d)
                {
                    var _global_var = this._id;
                    var _container_var = this.id;
                    var _interval = this.SlideDelay;
                    var _tabindex =  this.TabIndex;
                    $(window).addEvent('load', function(event){
                        var _container = $(_container_var);
                        var _childs = _container.getChildren()[_tabindex].getChildren();
                        //
                        var i = 0;
                        var x = 0; var y = 0;
                        for (i=0;i<_childs.length;i++)
                        {
                            $(_childs[i]).setStyle("left", x + "px");
                            $(_childs[i]).setStyle("top", y + "px");
                            //
                            x += $(_childs[i]).getCoordinates().width;
                        }
                        // Show Container
                        eval("var d" + _global_var + " = " + _global_var + ".Height;");
                        _container.tween("height", eval("d" + _global_var));
                        _container.setStyle("visibility", "visible");
                        // Play
                        window.setTimeout(_global_var + ".Next()", _interval);
                    });
                }
                
                this.In = function(i)
                {
                    var _global_var = this._id;
                    var _container_var = this.id;
                    var _container = $(_container_var);
                    var _tabindex =  this.TabIndex;
                    var _childs = _container.getChildren()[_tabindex].getChildren();
                    //
                    _childs[i].tween("left", "0px");     
                }
                
                this.LoadContent = function(i, url)
                {
                    //
                    var _global_var = this._id;
                    var _container_var = this.id;
                    eval("var ind  = " + _global_var + ".Index;");
                    var _container = $(_container_var);                    
                    var _tabindex =  this.TabIndex;
                    var _childs = _container.getChildren()[_tabindex].getChildren();
                    var _selchild = _container.getChildren()[_tabindex].getChildren()[i];
                    
                    //
                    if(this.ContentMode == "ajax")
                    {
                        var myHTMLRequest = new Request({method: 'post', evalScripts:true, evalResponse:false, url: url});
                        myHTMLRequest.addEvent("onSuccess", function (responseText, responseXML) {   
                            _selchild.hide();
                            _selchild.set('html', responseText);
                            _selchild.fade('in');
                        });
                        myHTMLRequest.send();                    
                    }
                }
                
                this.Show = function(i)
                {
                    var _global_var = this._id;
                    var _container_var = this.id;
                    eval(_global_var + ".Index = " + i + ";");
                    eval("var ind  = " + _global_var + ".Index;");
                    var _container = $(_container_var);                    
                    var _tabindex =  this.TabIndex;
                    var _childs = _container.getChildren()[_tabindex].getChildren();
                    eval(_global_var + ".Index = ind;");
                    if(ind < this.Total && ind >= 0)
                    {
                        //alert(ind)
                        // Select Item 
                        if(this.ItemPanel != "")
                        {
                            var i1 = 0;
                            for (i1=0;i1<this.ItemPanelItems.length;i1++)
                            {
                                if(ind != i1)
                                    this.ItemPanelItems[i1].set("class", this.ItemPanelNotSelectedClass);
                                else                        
                                    this.ItemPanelItems[i1].set("class", this.ItemPanelSelectedClass);
                            }
                        }
                        
                    
                        if(this.SlideMode == "full")
                        {
                            var x_interv =  _childs[ind].getStyle("left").toInt();
                            var x_newpos =  _container.getChildren()[_tabindex].getCoordinates(_container).left.toInt() - _childs[ind].getCoordinates(_container).left.toInt();
                            _container.getChildren()[_tabindex].tween("left", x_newpos + "px");
                            var i = 0;
                            for (i=0;i<_childs.length;i++)
                            {
                                var c_pos =_childs[i].getStyle("left").toInt() - x_interv;
                                //_childs[i].tween("left", c_pos + "px");                        
                            }
                        }
                        if(this.SlideMode == "single")
                        {
                            for (i=0;i<_childs.length;i++)
                            {
                                var curr_indx = parseInt(_childs[i].getStyle("z-index"));
                                if(curr_indx > 1)
                                    _childs[i].setStyle("z-index", "1");
                                else
                                    _childs[i].setStyle("z-index", "0");
                            }

                            _childs[ind].setStyle("z-index", "2");
                            _childs[ind].setStyle("left", this.Width.toInt() + "px");              
                            _childs[ind].tween("left", "0px");              
                        }
                        if(this.SlideMode == "replace")
                        {
                            var i = 0;
                            for (i=0;i<_childs.length;i++)
                            {
                                if(ind != i)
                                    _childs[i].tween("left", this.Width + "px");                        
                            }
                            window.setTimeout(_global_var + ".In(" + ind + ")",  1000);
                        }
                        if(this.SlideMode == "fade")
                        {
                            for (i=0;i<_childs.length;i++)
                            {
                                var curr_indx = parseInt(_childs[i].getStyle("z-index"));
                                if(curr_indx > 1)
                                    _childs[i].setStyle("z-index", "1");
                                else
                                    _childs[i].setStyle("z-index", "0");
                            }

                            _childs[ind].setStyle("opacity", "0");                   
                            _childs[ind].setStyle("left", "0px");
                            _childs[ind].setStyle("z-index", "2");
                            _childs[ind]. set('tween', {duration: this.FadeDuration});
                            _childs[ind].tween("opacity", "1");
                                           
                        }
                        
                     }
                }

                this.Previous = function()
                {
                    var _global_var = this._id;
                    eval("var ind  = " + _global_var + ".Index;");
                    ind--;
                    this.Show(ind);
                    if(ind >=0 && this.AutoPlay)
                        this.timeout = window.setTimeout(_global_var + ".Previous()",  this.SlideDelay);
                    else
                    {
                        if(ind == -1 && this.AutoPlay)
                        {
                            if(this.Rewind == "invert")
                            {
                                this.Loops++;
                                this.Index++
                                this.Next();
                            }
                        }   
                    }
                }

                this.Next = function()
                {
                    var _global_var = this._id;
                    eval("var ind  = " + _global_var + ".Index;");
                    //alert(ind)
                    ind++;
                    this.Show(ind);
                    if(ind < this.Total && this.AutoPlay)
                        this.timeout = window.setTimeout(_global_var + ".Next()",  this.SlideDelay);
                    else
                    {
                        if(ind == this.Total && this.AutoPlay)
                        {
                            if(this.Rewind == "restart")
                            {
                                this.Index = -1;
                                this.Next();
                            }
                            if(this.Rewind == "invert")
                            {
                                this.Index--
                                this.Previous();
                            }
                        }   
                    }
                }
            }
        });


/*
  *  CXTree
  *  ..
  *  Consente in caricamento di mmagini in modalità dinamica
  *  ed incorpora alcune funzioni per la visulaizzazione delle
  *  miniature
  *
  *  Vaersion 1.0.1
*/

        var CXTree = new Class({
            Implements: Events,
            initialize: function(inst) {
                
                // System Properties            
                this._id = inst;
                this.CloseAll = true;
                
                this.Nodes = new Array();
                this.Levels = new Array();
                this.SelectedNode = null;

                this.AddNode = function(n, l)
                {
                    this.Nodes[this.Nodes.length] = n;
                    this.Levels[this.Nodes.length] = l;
                    if(this.CloseAll)
                    {
                        if(l==0)
                            n.Status = "opened";
                        else
                            n.Status = "closed";
                    }

                    // if(this.CloseAll) this.HideNode(n.CurrentNode);
                }

                this.InitNodes = function()
                {
                    for(var i = 0; i< this.Nodes.length; i++)
                    {
                        var pNode = this.Nodes[i].CurrentNode;
                        var display = false;
                        if(this.SelectedNode)
                        {
                            display = (this.SelectedNode == this.Nodes[i] ||
                                        this.Nodes[i].ParentNode == this.SelectedNode.ParentNode);
                        }
                        if(!display)
                        {
                            if(this.Nodes[i].Status == "closed")
                            {
                                $(pNode).setStyle('visibility', 'hidden');
                                $(pNode).setStyle('display', 'none');
                            } else {
                                $(pNode).setStyle('visibility', 'visible');
                                $(pNode).setStyle('display', 'block');
                            }                       
                        } else {
                            $(pNode).setStyle('visibility', 'visible');
                            $(pNode).setStyle('display', 'block');
                        }
                    }
                }
                
                this.CloseLevelNodeButThis = function(nid)
                {
                    var cNode = nid;
                    for(var i = 0; i< this.Nodes.length; i++)
                    {
                        var pNode = this.Nodes[i].ParentNode
                        // alert($(pNode))
                        if(pNode != cNode && $(pNode) != null)
                        {
                            var cCurrentNode = this.Nodes[i].CurrentNode;                                   
                            $(cCurrentNode).setStyle('visibility', 'hidden');
                            $(cCurrentNode).setStyle('display', 'none');
                        }
                    }
                }
                
                this.ToggleNode = function(nid)
                {
                    var cNode = nid;
                    for(var i = 0; i< this.Nodes.length; i++)
                    {
                        var pNode = this.Nodes[i].ParentNode
                        if(pNode == cNode)
                        {
                            var cCurrentNode = this.Nodes[i].CurrentNode;
                            if($(cCurrentNode).getStyle('visibility') == "hidden")
                            {
                                $(cCurrentNode).setStyle('visibility', 'visible');
                                $(cCurrentNode).setStyle('display', 'block');
                                //
                               
                            }
                            else
                            {
                                $(cCurrentNode).setStyle('visibility', 'hidden');
                                $(cCurrentNode).setStyle('display', 'none');
                            }
                        }
                    }
                    //
                    this.CloseLevelNodeButThis(cNode);
                }
                
            }
        });

        var CXTreeNode = new Class({
            Implements: Events,
            initialize: function(inst, cNode, pNode) {
                
                // System Properties            
                this._id = inst;
                
                this.CurrentNode = cNode;
                this.ParentNode = pNode;
                
                this.Status = "opened";

                
            }
        });

