var VelocityAjaxComponent = {

   /*
     *
     */
    doGet : function(url, requestParam, id)
    {          
        //alert("doGet("+ url + "," +requestParam + ",", +id +")");
        var oAjax = new Ajax.Request( 
             url, 
                {   method: 'get', 
                    parameters: requestParam, 
                    onComplete: VelocityAjaxComponent._handleAjaxRequest.bind(this,id)
                } 
            );
    },
    
    /*
     *
     */
    doPost : function(url, requestParam, id)
    {          
        var oAjax = new Ajax.Request( 
             url, 
                {   method: 'post', 
                    parameters: requestParam, 
                    onComplete: VelocityAjaxComponent._handleAjaxRequest.bind(this,id)
                } 
            );
    },

    /**
     * Handle the response by adding an extra element
     */
    _handleAjaxRequest: function(id, request)
    {
        //alert("_handleAjaxRequest(" + id + "," + request + ")");
        var response = request.responseText;
        var elm = $(id);
        elm.innerHTML = response;
    }
};

var WieBlogt = {

    /*
     */
    uploadPhoto: function() 
    {
        var url = BASE_HREF + "/template/com/quovide/photo_upload.vm";
        var pars = $H({ 
            "test": ""
        });
        this.doGet(url, pars.toQueryString(), null);      
    },

    /*
     *
     */
    confirm: function(requestString, msg)  
    {
        if(confirm(msg))
        {
            //alert("redirect to: " + requestString);
            document.location = requestString;
        }
    },
     
     /*
      *
      */
     submitForm : function(formId, url, id)
     {
        var oForm = $(formId);
        var pars = Form.serialize(oForm);
        this.doPost(url, pars, id);       
     },
     
     /*
      */
     closePopin : function()
     {
        Windows.close(this.EMERCE_NETWORK_POPIN + "_" + this._win_index);
     },
     
    /*
     *
     */
    doGet : function(url, requestParam, id)
    {          
        //alert("doGet("+ url + "," +requestParam + ",", +id +")");
        var oAjax = new Ajax.Request( 
             url, 
                {   method: 'get', 
                    parameters: requestParam, 
                    onComplete: WieBlogt._handleAjaxRequest.bind(this,id)
                } 
            );
    },
    
    /*
     *
     */
    doPost : function(url, requestParam, id)
    {          
        var oAjax = new Ajax.Request( 
             url, 
                {   method: 'post', 
                    parameters: requestParam, 
                    onComplete: WieBlogt._handleAjaxRequest.bind(this,id)
                } 
            );
         this.closePopin();
    },

    /**
     * Handle the response by adding an extra element
     */
    _handleAjaxRequest: function(id, request)
    {
        // alert("_handleAjaxRequest(" + id + "," + request + ")");
        var response = request.responseXML.documentElement;

        var title = '';
        var titles = response.getElementsByTagName('title');
        if(titles.length>0) 
        {
            title = titles[0].firstChild.data;
        }
        
        var content = null;
        var contents = response.getElementsByTagName('content');
        if(contents.length>0) 
        {
            content = contents[0].firstChild.data;
        }
        
        if(content == null)
        {
            //
        }

        var oParent = null;
        if(id != null) 
        {
            oParent = $(id);
        }
        
        if(oParent == null)
        {
            this._openPopinWindow(title, content, id);
        } 
        else
        {
            oChild = $(id + "_child");
            if(oChild == null) 
            {
                oChild = document.createElement('div');
                oChild.id = id + "_child";
            }
            Element.Methods.update(oChild, content);            
            oParent.appendChild(oChild);
            oParent.style.display = 'block';
            oParent.style.visibility = 'visible';
        }
    },
 
    /**
     * Create a cookie 
     */
    createCookie: function(name,value,days)
    {
        if (days)
        {
            var date = new Date();
            date.setTime(date.getTime()+(days*24*60*60*1000));
            var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
    },
    /** 
     * Read a cookie 
     */
    readCookie: function(name)
    {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++)
        {   
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    },
    /**
     * Erase a cookie 
     */
    eraseCookie: function(name)
    {
        this.createCookie(name,"",-1);
    },
    
    WIEBLOGT_POPIN: "wieblogt_popin",
    // New windows have to get own id, because of timing issues
    _win_index: 0,

    /*
     * Private function to open a popin window    
     */
    _openConfirmWindow: function(title, content, id)
    {
        Dialog.confirm(
            content, 
            { windowParameters: {
                className: this.WIEBLOGT_POPIN, 
                width:300, 
                height:100}, 
                okLabel: "ja", 
                title: title,
                cancelLabel: "nee",
                cancel: function(win) { return false;},
                ok: function(win) { return true;}
        });
    },
        
    /*
     * Private function to open a popin window
     */
    _openPopinWindow: function(title, content, id)
    {
      //  alert("_openPopinWindow("+ title + ","+ content + ","+ id + ")");
        // Close the current popin
        this.closePopin();
        
        if(content == null)
        {
            return;
        }
        
        this._win_index++;
        
        // Create new popin
        var oParent = $(id);
        if(oParent == null)
        {
            oParent = document.body;
        }
        var offset = Position.realOffset(oParent);
       
        win = new Window(this.WIEBLOGT_POPIN + "_" + this._win_index, {   
            className: "wieblogt_popin", 
            title: title,
            top: offset[1] + 100, 
            left: offset[0] + 200,  
            width: 400, 
            height: 250, 
            zIndex: 150, 
            resizable: true,
            closable: true,
            draggable: true,
            minimizable: true,
            maximizable:true,
            opacity: .5
           /* showEffectOptions: {duration:2}, */
            /* parent: oParent, */
            }
        )

        win.getContent().innerHTML = content;
      //  win.setDestroyOnClose();
        win.show(false); 
    },	

	/*
	 */
	onLoad: function()
	{
		var sb = $("searchbar-input");
		sb.focus();			
	},

	/*
	 */
 	onSearchButtonClick: function(clickButton)
	{
		switch(clickButton)
		{
			case "WHO": 
				this.onSearch("WHO", getQuery());
				break;
				
			case "WHAT": 
				this.onSearch("WHAT", getQuery());
				break;
				
			case "WHEN": 
				this.onSearch("WHEN", getQuery());
				break;
				
			case "WHERE": 
				this.onSearch("WHERE", getQuery());
				break;
		}
	},

	/*
	 */
	onSearchbarKeyDown: function(e)
	{
		if (e.keyCode == 13)
		{
	//		onSearch("what", getQuery());	
		}
	},
	
	/*
	 */
	getQuery: function()
	{
		var sb = $("query");
		return sb.value;
	},
	
	/*
	 */
	onSearch: function(type, query, offset, count)
	{
		$("searchType").value = type;
		$("frmZoekform").submit();
	},
	
	/* 
	 */
	switchTab: function(mode)
	{
		var tabs = document.getElementById("tabs");
		
		switch(mode)
		{
			case "WHO": 
				tabs.src = "gfx/com/quovide/tabs_who.gif";
				break;
			case "": 
			case "WHAT": 
				tabs.src = "gfx/com/quovide/tabs_what.gif";
				break;
				
			case "WHEN": 
				tabs.src = "gfx/com/quovide/tabs_when.gif";
				break;
				
			case "WHERE": 
				tabs.src = "gfx/com/quovide/tabs_where.gif";
				break;			
		}	
	}
};

var QvFlux = {
    /*
     * Handle flux click
     */
    fluxOnClick : function(type, id, action)
    {
    	//alert("fluxOnClick(" + type + "," + action + "," + id + ")");
		onFluxClick(type,action,id); 
     }
};

