﻿
var PopupBoxManager =Class.create();
PopupBoxManager.prototype={
    initialize: function()
    {
        this.Collection =new Array();
        this.TimerCollection = new Array();
    },
    RegistryPopupBox:function(popupbox)
    {  
        popupbox.Init();
        this.Collection[popupbox.ID]= popupbox;
        setInterval("PBManager.CheckVisible('"+popupbox.ID+"')",200);
       
    },
    RegPBForElement:function(idElementForBox,popupbox, fn_exec_text)
    {        
        if (this.TimerCollection[popupbox.ID])
            clearInterval(this.TimerCollection[popupbox.ID]);
        this.TimerCollection[popupbox.ID] = setTimeout(fn_exec_text, 850);
        
        $(idElementForBox).onmouseout=function(event)
        {
            PBManager.stopTimer(popupbox.ID);
        }
        $(idElementForBox).onclick = function(event)
        {
            PBManager.stopTimer(popupbox.ID);
        }
    },
    SetVisible:function(id,state)
    {        
        var _popupbox = this.Collection[id];
        if($(_popupbox.ID).visible())
             _popupbox.IsVisible=state;
    },
    GetPopupBoxByID:function(id)
    {
        return this.Collection[id];
    },
    CheckVisible:function(id)
    {
        var _popupbox = this.Collection[id];
        if($(_popupbox.ID).visible()
            && _popupbox.IsVisible==false)
        {
            //$(_popupbox.ID).hide();
             jq("#"+_popupbox.ID).fadeOut("slow"); 
        }
    },
    VisualShow:function(id)
    {
        var _popupbox = this.Collection[id];
        
        if (_popupbox.VisibleWidth < _popupbox.Width+1 ||
            _popupbox.VisibleHeight< _popupbox.Height+1
            )
        {   
            _popupbox.VisibleWidth += 75; 		    		
            _popupbox.VisibleHeight += 75;
            var rect = 'rect(auto, '+  _popupbox.VisibleWidth +'px, '+_popupbox.VisibleHeight+'px, auto)';
            $(_popupbox.ID).style.clip = rect;
            setTimeout("PBManager.VisualShow('"+_popupbox.ID+"')",30);
        }
    },
    stopTimer:function(id)
    {
        try
        {
            var _popupbox = this.Collection[id];
            _popupbox.IsVisible=false;
            
            clearInterval(this.TimerCollection[id]);
            this.TimerCollection[id]=null;
        }
        catch(e){};
    }
}
var PBManager = new PopupBoxManager();

var PopupBox = Class.create();
PopupBox.prototype={
    initialize: function(id,width,height,bg_color,border_color)
    {
        this.Height = height;
        this.Width = width;
        this.BorderColor =border_color;
        this.BgColor =bg_color;
        this.ID=id;
        this.ContentID='_'+id;
        this.IsVisible=false;
        this.PrevIdElementForBox='';
        this.VisibleWidth=-1;
        this.VisibleHeight=-1;
        
        var border_style = 'style="border-left: solid 1px ' + this.BorderColor + '; border-right: solid 1px ' + this.BorderColor + '; background:' + this.BgColor + ';"';
        var border_style_content = 'style="border-left: solid 1px ' + this.BorderColor + '; border-right: solid 1px ' + this.BorderColor + '; background:' + this.BgColor + '; height:' + this.Height + 'px; width:' + this.Width + 'px;"';        
        
        var _width='"width:'+(this.Width+3)+'px;"';
        if(Prototype.Browser.Opera || Prototype.Browser.Gecko)
            _width='"width:100%;"'
        
        this.BeginBox='<div style="z-index:2; position:absolute; display:none; width:' +  this.Whidth + 'px;" id=' + this.ID + '>';        
        this.BeginContent='<div style='+_width+'><b class=r1 style="background:' +  this.BorderColor + ';"></b><b class=r2 ' + border_style + '></b><b class=r3 ' + border_style + '></b><b class=r4 ' + border_style + '></b></div>'
                            +'<div id='+this.ContentID+' ' + border_style_content + '>';
        this.EndContent='</div>'
                         +'<div style='+_width+'><b class=r4 ' + border_style + '></b><b class=r3 ' + border_style + '></b><b class=r2 ' + border_style + '></b><b class=r1  style="background:' + this.BorderColor + '"></b></div>';
        this.EndBox='</div>';                         
        this.Content = this.BeginContent+this.EndContent;
    },    
    SetContent:function(content)
    {
       $(this.ContentID).update(content);
    }, 
    Init:function()
    {
        this.Content = this.BeginContent+this.EndContent;
        new Insertion.Bottom($('container'),this.BeginBox+this.Content+this.EndBox);
    },   
    Show:function (idElementForBox)
    {
        var TopPadding =0;
        var LeftPadding=0;
        var ScrWidth=0;
        var ScrHeight=0;
        
        if(Prototype.Browser.IE)
        {
            TopPadding = document.body.scrollTop;
            LeftPadding= document.body.scrollLeft;
            ScrWidth = document.body.clientWidth;
            ScrHeight = document.body.clientHeight;            
            
            //alert("X = "+document.body.clientWidth+"  Y = "+document.body.clientHeight+
            //"\nX = "+document.body.scrollLeft+"  Y = "+document.body.scrollTop);
        }
        else
        {
            TopPadding = pageYOffset;
            LeftPadding= pageXOffset;
            ScrWidth = innerWidth;
            ScrHeight = innerHeight;
            
            //alert("X = "+innerWidth+"  Y = "+innerHeight+
            //"\nX = "+pageXOffset+"  Y = "+pageYOffset);
        }
        
        var idBox = this.ID;
        var pos =Position.cumulativeOffset($(idElementForBox));
	    var dim =$(idElementForBox).getDimensions();    
	    
	    if( ((pos[0]+this.Width)> (LeftPadding+ScrWidth) || 
	        (pos[1]+this.Height+dim.height)> (TopPadding+ScrHeight))
	        
	        &&((ScrWidth-LeftPadding + pos[0])>=this.Width)
	        &&((ScrHeight-TopPadding + pos[1])>=this.Height)
	        
	        &&(pos[1]>=this.Height)
	        &&(pos[0]>=this.Width)
	      )
	    {
	        
	        $(idBox).setStyle({	  
	            'left':pos[0]-this.Width+'px',
	            'top':pos[1]-this.Height+'px'
	         });
	    }
	    else
	    {
	        $(idBox).setStyle({	  
	            'left':pos[0]+'px',
	            'top':pos[1]+dim.height+'px'
	         });
	    }                
        
        this.VisibleWidth=-1;
        this.VisibleHeight=-1;        
        
        if( $(idBox).visible()==false)
        {
            this.IsVisible=true;
            
             jq("#"+idBox).fadeIn("slow");      
            
//            $(idBox).show();
//            PBManager.VisualShow(this.ID);
            
            var id_pb = this.ID;            
            $(idBox).onmouseout=function(event)
            {             
                PBManager.SetVisible(id_pb,false)
            } 
            $(idBox).onmouseover=function(event)
            {                
                PBManager.SetVisible(id_pb,true)
            }            
        }    
    }   
}
var pb_UserProfileInfo = new PopupBox('pb_UserProfileInfo',320,290,'#ffffff','#dddddd');

Event.observe(window, 'load', function() 
{
    try{
        PBManager.RegistryPopupBox(pb_UserProfileInfo);
    }
    catch(e){};
});

//pb_UserProfileInfo
function pb_UserProfile(uid,idElementForBox)
{    
    PBManager.RegPBForElement(idElementForBox,pb_UserProfileInfo,"_pb_UserProfile('"+uid+"','"+idElementForBox+"')");    
}
function _pb_UserProfile(uid,idElementForBox)
{    
    if(pb_UserProfileInfo.PrevIdElementForBox==idElementForBox)
    {
        pb_UserProfileInfo.Show(idElementForBox);
        return;
    }
    AjaxPro.onLoading = function(b) 
    {        
        if(b)
        {
           pb_UserProfileInfo.SetContent('<img src="img/loader.gif" alt="">');
           pb_UserProfileInfo.Show(idElementForBox);
        }
    }   
    AjaxManager.USR_UserProfile(uid,idElementForBox,FRM_UserProfileCallback);     
}
function FRM_UserProfileCallback(result)
{ 
    var res = result.value;
    pb_UserProfileInfo.PrevIdElementForBox=res.rs2;
    pb_UserProfileInfo.SetContent(res.rs1);
}
//-------------------
