var PrimatAction = {
    GetQueryStringValue: function(name)
    {
        name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
        var regexS = "[\\?&]"+name+"=([^&#]*)";
        var regex = new RegExp( regexS );
        var results = regex.exec( window.location.href );
        
        if( results == null )
            return "";
        else
            return results[1];
    },
    LoadContent: function(url, bubble){
        new Ajax.Request(url,
        {
            method:'get',
            onSuccess: function(transport){
              var response = transport.responseText || "no response text";  
              
              try
              {
                var json = transport.responseText.evalJSON(true);
                if (!json.Success)
                {
                    if (!Object.isUndefined(json.CallFunctionOnResponse) && json.CallFunctionOnResponse != null)
                    {
                        eval(json.CallFunctionOnResponse)(json.ObjectToSerialize);
                    } 
                    
                    PrimatAction.PopProcess();
                }
              }
              catch(e)
              {
                  bubble.setContent(response);
                  bubble.show();
                  PrimatAction.PopProcess();
              }                 
            },
            onFailure: function(){ alert('Something went wrong...') }
        });
    },
    DestroyBox: function(id, stackIndex)
    {        
        if (Object.isUndefined(stackIndex) || stackIndex == null)
        {
            stackIndex = null;
            page.boxStack.each(function(n,index){
                if (n.id == id)
                {
                    stackIndex = index;
                    throw $break;
                }
            });
        }
        
        if (stackIndex == null)
        {
            return;
        }        
        
        page.boxStack[stackIndex] = null;            
        $(id).remove();
    },        
    ClearBoxStack: function()
    {        
        page.boxStack.each(function(n,index){                            
            if(!Object.isUndefined(n) && n != null && !n.immune)
            {                   
                PrimatAction.DestroyBox(n.id, index);                    
            }
        });
        
        page.boxStack = page.boxStack.compact();
    },
    CallClose: function(obj)
    {
        PrimatAction.DestroyBox($(obj).up('.box').id);
    },
    SendForm: function(formID){
        PrimatAction.PushProcess(new Process("SendFormProcess-"+formID));
        
        $(formID).select('div.form-error').each(function(n){
            n.remove();
        });
        
        var action = $(formID).action;
        var postParams = Form.serialize(formID);
        new Ajax.Request(action,
        {
            method:'post',
            parameters:postParams,
            onSuccess: function(transport){
              PrimatAction.HandleJSONResponse(transport.responseText.evalJSON(),formID);
            },
            onFailure: function(){ alert('Something went wrong...') }
        });
    },
    HandleJSONResponse: function(json,formID)
    {   
        if(json.Message != null)
        {
            alert(json.Message);
        }
                  
        if (!Object.isUndefined(json.CallFunctionOnResponse) && json.CallFunctionOnResponse != null)
        {
            eval(json.CallFunctionOnResponse)(json.ObjectToSerialize,formID);
        }        
        
        if (json.BoxModel && json.CloseBoxOnResponse)
        {            
            PrimatAction.DestroyBox($(formID).up('.box').id);
        }
        
        PrimatAction.PopProcess();
    },
    ShowFormErrors: function(obj,formID)
    {
        var fieldID = null;
        var form = $(formID);
        var foundFormField = null;           
        var errorNode = new Template('<div class="form-error">#{ErrorMessage}</div>');
        
        $A(obj).each(function(n){
            fieldID = (n.Prefix != null ? n.Prefix + "." + n.FieldID : n.FieldID).toLowerCase();                
            foundFormField = form.select('[name="' + fieldID + '"]')[0];
            foundFormField.insert({after:errorNode.evaluate(n)});                
        });
    },
    PushProcess: function(process){            
        if (!Object.isUndefined(process) && process != null)
        {                
            page.processStack.push(process);
            var processDiv = $$('div.process')[0];
            if (Object.isUndefined(processDiv) || processDiv == null)
            {
            
                processDiv = Builder.node('div',{className:'process'});
                document.body.appendChild(processDiv);
            }
        }            
    },
    PopProcess: function(){            
        page.processStack.pop();
        var processDiv = $$('div.process')[0];
        if (page.processStack.length == 0 && !Object.isUndefined(processDiv) && processDiv != null)
        {
            processDiv.remove();
        }
    },
    Redirect: function(url){
        location.href = page.siteRoot + url;
    } 
};

var Box = Class.create({
    initialize: function(immune){
        this.immune = immune;
        
        if (Object.isUndefined(this.immune) || this.immune == null)
        {
            this.immune = false;
        }
    }
});

var Bubble = Class.create(Box,{
    initialize: function($super, id, width, height, posX, posY, immune){
        this.id = id;
        this.width = width + "px";
        this.citeWidth = (width-2) + "px";
        this.height = height + "px";
        this.posX = posX + "px";
        this.posY = posY + "px";
        this.contentObj = Builder.node('blockquote');
        this.stackIndex = null;
        $super(immune);
    },
    show: function()
    {   
        PrimatAction.ClearBoxStack();
        this.stackIndex = page.boxStack.push(this) - 1;
        
        var close = Builder.node('div',{className:'close',onclick:'PrimatAction.DestroyBox(\'' + this.id + '\',' + this.stackIndex + ')'});
        var domObj = Builder.node('div',{id:this.id,className:'bubble',style:'display:none;width:'+this.width+';height:'+this.height+';left:'+this.posX+';top:'+this.posY+';'});
        domObj.appendChild(Builder.node('cite', {style:'width:'+this.citeWidth+';'}, close));
        domObj.appendChild(this.contentObj);
              
        document.body.appendChild(domObj);   
        new Effect.Appear(this.id,{duration:0.3});
    },
    setContent: function(content)
    {
        this.contentObj.innerHTML = content;
    }
});   

var Process = Class.create({
    initialize: function(id){
        this.id = id;            
    }
});

var Page = Class.create({
    initialize: function(){
        this.siteRoot = "$siteroot";
        this.boxStack = new Array();
        this.processStack = new Array();
        this.idCounter = 0;
        Action.SpecInit();
    },
    getNewID: function(id)
    {
        return id + "_" + this.idCounter++;            
    }
});

var Action = {
    SpecInit: function(){
        return false;
    },
    ShowAbstract: function(domObj, contentID)
    {
        PrimatAction.PushProcess(new Process("ShowAbstractProcess"));            
        var position = $(domObj).cumulativeOffset();
        var bubble = new Bubble(page.getNewID("bubble"), 400, 250, position.left-200, position.top-125);                        
        bubble.setContent($(contentID).innerHTML);
        bubble.show();
        PrimatAction.PopProcess();
    }
};

var page = new Page();
